google_contacts_api 0.7.0 → 0.7.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,33 @@
1
+ class TestClass
2
+ include GoogleContactsApi::Contacts
3
+
4
+ def initialize(api)
5
+ @api = api
6
+ end
7
+ end
8
+
9
+ describe GoogleContactsApi::Contacts do
10
+ let(:api) { double("api") }
11
+ let(:test_class) { TestClass.new(api) }
12
+ let(:server_response) do
13
+ OpenStruct.new(
14
+ code: 200,
15
+ body: contact_set_json
16
+ )
17
+ end
18
+
19
+ describe "#get_contacts" do
20
+ before do
21
+ allow(api).to receive(:get).and_return(server_response)
22
+ end
23
+
24
+ it "calls 'get' on api" do
25
+ expect(api).to receive(:get).with("contacts/default/full", kind_of(Hash)).and_return(server_response)
26
+ test_class.get_contacts
27
+ end
28
+
29
+ it "returns contacts set" do
30
+ expect(test_class.get_contacts).to be_an_kind_of GoogleContactsApi::ContactSet
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,39 @@
1
+ describe GoogleContactsApi::GroupSet do
2
+ context "when entries are present" do
3
+ before(:all) do
4
+ @group_set_json = group_set_json
5
+ @group_set = GoogleContactsApi::GroupSet.new(@group_set_json)
6
+ end
7
+
8
+ specify "the right starting index is set" do
9
+ expect(@group_set.start_index).to eq(1)
10
+ end
11
+
12
+ specify "the right number of results per page is set" do
13
+ expect(@group_set.items_per_page).to eq(25)
14
+ end
15
+
16
+ specify "the right number of total results is set" do
17
+ expect(@group_set.total_results).to eq(5)
18
+ end
19
+
20
+ specify "results are parsed into Groups" do
21
+ expect(@group_set.to_a.first).to be_instance_of(GoogleContactsApi::Group)
22
+ end
23
+ end
24
+
25
+ context "when entries are not present" do
26
+ before(:all) do
27
+ @empty_group_set_json = empty_group_set_json
28
+ @empty_group_set = GoogleContactsApi::GroupSet.new(@empty_group_set_json)
29
+ end
30
+
31
+ specify "totals_results is equal to 0" do
32
+ expect(@empty_group_set.total_results).to eq(0)
33
+ end
34
+
35
+ specify "@results variable is an empty array" do
36
+ expect(@empty_group_set.instance_variable_get("@results")).to eq([])
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,92 @@
1
+ describe GoogleContactsApi::Group do
2
+ subject { GoogleContactsApi::Group.new(group_json_hash) }
3
+
4
+ describe "#system_group?" do
5
+ context "when group has system group" do
6
+ it "returns true" do
7
+ expect(subject).to be_system_group
8
+ end
9
+ end
10
+
11
+ context "when group has no system group" do
12
+ it "returns false" do
13
+ @group = GoogleContactsApi::Group.new(group_not_system_json_hash)
14
+ expect(@group).not_to be_system_group
15
+ end
16
+ end
17
+ end
18
+
19
+ describe "#system_group_id" do
20
+ context "when system group is set" do
21
+ it "returns system group id" do
22
+ expect(subject.system_group_id).to eq('Contacts')
23
+ end
24
+ end
25
+
26
+ context "when system group is not set" do
27
+ it "returns nil" do
28
+ @group = GoogleContactsApi::Group.new(group_not_system_json_hash)
29
+ expect(@group.system_group_id).to be_nil
30
+ end
31
+ end
32
+ end
33
+
34
+ describe "#contacts" do
35
+ before do
36
+ @api = double("api")
37
+ @group = GoogleContactsApi::Group.new(contact_json_hash, nil, @api)
38
+ allow(@group).to receive(:id).and_return("group id")
39
+ end
40
+
41
+ it "should be able to get contacts" do
42
+ expect(@group).to receive(:get_contacts).with(hash_including({"group" => "group id"})).and_return("contact set")
43
+ expect(@group.contacts).to eq("contact set")
44
+ end
45
+
46
+ it "should use the contact cache for subsequent access" do
47
+ expect(@group).to receive(:get_contacts).with(hash_including({"group" => "group id"})).and_return("contact set").once
48
+ @group.contacts
49
+ contacts = @group.contacts
50
+ expect(contacts).to eq("contact set")
51
+ end
52
+ end
53
+
54
+ describe "#contacts!" do
55
+ before do
56
+ @api = double("api")
57
+ @group = GoogleContactsApi::Group.new(contact_json_hash, nil, @api)
58
+ allow(@group).to receive(:id).and_return("group id")
59
+ end
60
+
61
+ it "should be able to get contacts" do
62
+ expect(@group).to receive(:get_contacts).with(hash_including({"group" => "group id"})).and_return("contact set")
63
+ expect(@group.contacts!).to eq("contact set")
64
+ end
65
+
66
+ it "should use the contact cache for subsequent access" do
67
+ expect(@group).to receive(:get_contacts).with(hash_including({"group" => "group id"})).and_return("contact set").twice
68
+ @group.contacts
69
+ contacts = @group.contacts!
70
+ expect(contacts).to eq("contact set")
71
+ end
72
+ end
73
+
74
+ describe "#links" do
75
+ it "returns an array of hrefs" do
76
+ expect(subject.links).to eq ["https://www.google.com/m8/feeds/groups/example%40gmail.com/full/6"]
77
+ end
78
+ end
79
+
80
+ describe "#self_link" do
81
+ it "returns href of the link with rel = 'self'" do
82
+ expect(subject.self_link).to eq "https://www.google.com/m8/feeds/groups/example%40gmail.com/full/6"
83
+ end
84
+ end
85
+
86
+ describe "#edit_link" do
87
+ it "returns hre of the link with rel = 'edit'" do
88
+ group = GoogleContactsApi::Group.new(group_not_system_json_hash)
89
+ expect(group.edit_link).to eq "https://www.google.com/m8/feeds/groups/example%40gmail.com/full/270f"
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,33 @@
1
+ class TestClass
2
+ include GoogleContactsApi::Groups
3
+
4
+ def initialize(api)
5
+ @api = api
6
+ end
7
+ end
8
+
9
+ describe GoogleContactsApi::Groups do
10
+ let(:api) { double("api") }
11
+ let(:test_class) { TestClass.new(api) }
12
+ let(:server_response) do
13
+ OpenStruct.new(
14
+ code: 200,
15
+ body: group_set_json
16
+ )
17
+ end
18
+
19
+ describe "#get_groups" do
20
+ before do
21
+ allow(api).to receive(:get).and_return(server_response)
22
+ end
23
+
24
+ it "calls 'get' on api" do
25
+ expect(api).to receive(:get).with("groups/default/full", kind_of(Hash)).and_return(server_response)
26
+ test_class.get_groups
27
+ end
28
+
29
+ it "returns groups set" do
30
+ expect(test_class.get_groups).to be_an_kind_of GoogleContactsApi::GroupSet
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,35 @@
1
+ describe GoogleContactsApi::ResultSet do
2
+ subject { GoogleContactsApi::ResultSet.new(contact_set_json) }
3
+
4
+ describe "#each" do
5
+ context "when block is provided" do
6
+ it "yields to block for each result" do
7
+ expect(subject.instance_variable_get(:@results)).to receive(:each)
8
+ subject.each { |x| x }
9
+ end
10
+ end
11
+
12
+ context "when block is not provided" do
13
+ it "returns an enumerator" do
14
+ expect(subject.each).to be_a_kind_of Enumerator
15
+ end
16
+ end
17
+ end
18
+
19
+ describe "#has_more?" do
20
+ context "when there are more results" do
21
+ it "returns true" do
22
+ expect(subject.has_more?).to be true
23
+ end
24
+ end
25
+
26
+ context "when there are no results anymore" do
27
+ it "returns false" do
28
+ total_results = subject.instance_variable_get(:@total_results)
29
+ per_page = subject.instance_variable_get(:@items_per_page)
30
+ subject.instance_variable_set(:@start_index, total_results - per_page + 2)
31
+ expect(subject.has_more?).to be false
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,69 @@
1
+ describe GoogleContactsApi::Result do
2
+ subject do
3
+ GoogleContactsApi::Result.new({
4
+ "id" => {
5
+ "$t" => "http://www.google.com/m8/feeds/contacts/example%40gmail.com/base/0"
6
+ },
7
+ "updated" => {
8
+ "$t" => "2011-07-07T21:02:42.360Z"
9
+ },
10
+ "title" => {
11
+ "$t" => "Contact 1"
12
+ },
13
+ "content" => {
14
+ "$t" => "Contact 1 Content"
15
+ },
16
+ "category" => [{
17
+ "scheme" => "http =>//schemas.google.com/g/2005#kind",
18
+ "term" => "http =>//schemas.google.com/contact/2008#contact"
19
+ }],
20
+ "gd$deleted" => true
21
+ })
22
+ end
23
+
24
+ describe "#id" do
25
+ it "returns id parsed from entry" do
26
+ expect(subject.id).to eq "http://www.google.com/m8/feeds/contacts/example%40gmail.com/base/0"
27
+ end
28
+ end
29
+
30
+ describe "#title" do
31
+ it "returns title parsed from entry" do
32
+ expect(subject.title).to eq "Contact 1"
33
+ end
34
+ end
35
+
36
+ describe "#content" do
37
+ it "returns content parsed from entry" do
38
+ expect(subject.content).to eq "Contact 1 Content"
39
+ end
40
+ end
41
+
42
+ describe "#updated" do
43
+ it "returns updated parsed from entry" do
44
+ expect(subject.updated).to be_a_kind_of DateTime
45
+ expect(subject.updated.to_s).to eq "2011-07-07T21:02:42+00:00"
46
+ end
47
+ end
48
+
49
+ describe "#categories" do
50
+ it "returns category parsed from entry" do
51
+ expect(subject.categories).to eq subject.category
52
+ end
53
+ end
54
+
55
+ describe "#deleted?" do
56
+ context "when 'gd$deleted' is presented in the entry" do
57
+ it "returns true" do
58
+ expect(subject.deleted?).to eq true
59
+ end
60
+ end
61
+
62
+ context "when 'gd$deleted' is not presented in the entry" do
63
+ it "returns false" do
64
+ not_deleted_entry = GoogleContactsApi::Result.new("id" => "123")
65
+ expect(not_deleted_entry.deleted?).to eq false
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,66 @@
1
+ describe GoogleContactsApi::User do
2
+ let(:oauth) { double ("oauth") }
3
+ let(:user) { GoogleContactsApi::User.new(@oauth) }
4
+
5
+ describe "#groups" do
6
+ it "calls 'get_groups' from GoogleContactsApi::Groups" do
7
+ expect(user).to receive(:get_groups)
8
+ user.groups
9
+ end
10
+
11
+ it "passess params to 'get_groups'" do
12
+ expect(user).to receive(:get_groups).with(test: :param)
13
+ user.groups(test: :param)
14
+ end
15
+ end
16
+
17
+ describe "#groups!" do
18
+ it "calls 'get_groups' from GoogleContactsApi::Groups" do
19
+ expect(user).to receive(:get_groups)
20
+ user.groups
21
+ end
22
+
23
+ it "passess params to 'get_groups'" do
24
+ expect(user).to receive(:get_groups).with(test: :param)
25
+ user.groups(test: :param)
26
+ end
27
+
28
+ it "resets groups" do
29
+ expect(user).to receive(:get_groups).and_return("group set").twice
30
+ user.groups
31
+ groups = user.groups!
32
+ expect(groups).to eq("group set")
33
+ end
34
+ end
35
+
36
+ describe "#contacts" do
37
+ it "calls 'get_contacts' from GoogleContactsApi::Contacts" do
38
+ expect(user).to receive(:get_contacts)
39
+ user.contacts
40
+ end
41
+
42
+ it "passess params to 'get_contacts'" do
43
+ expect(user).to receive(:get_contacts).with(test: :param)
44
+ user.contacts(test: :param)
45
+ end
46
+ end
47
+
48
+ describe "#contacts!" do
49
+ it "calls 'get_contacts' from GoogleContactsApi::Contacts" do
50
+ expect(user).to receive(:get_contacts)
51
+ user.contacts
52
+ end
53
+
54
+ it "passess params to 'get_contacts'" do
55
+ expect(user).to receive(:get_contacts).with(test: :param)
56
+ user.contacts(test: :param)
57
+ end
58
+
59
+ it "resets contacts" do
60
+ expect(user).to receive(:get_contacts).and_return("contact set").twice
61
+ user.contacts
62
+ contacts = user.contacts!
63
+ expect(contacts).to eq("contact set")
64
+ end
65
+ end
66
+ end
@@ -5,6 +5,8 @@ require 'json'
5
5
  require 'hashie'
6
6
  require 'net/http'
7
7
  require 'google_contacts_api'
8
+ require 'ostruct'
9
+ Dir[File.dirname(__FILE__) + "/support/**/*.rb"].each {|f| require f }
8
10
 
9
11
  puts "Testing version #{GoogleContactsApi::Version::STRING}"
10
12
 
@@ -15,37 +17,3 @@ puts "Testing version #{GoogleContactsApi::Version::STRING}"
15
17
  RSpec.configure do |config|
16
18
  end
17
19
 
18
- def load_file(filename)
19
- f = File.open(File.join(File.dirname(__FILE__), filename))
20
- json = f.read
21
- f.close
22
- json
23
- end
24
-
25
- def contact_set_json
26
- load_file("contact_set.json")
27
- end
28
-
29
- def group_set_json
30
- load_file("group_set.json")
31
- end
32
-
33
- def empty_contact_set_json
34
- load_file("empty_contact_set.json")
35
- end
36
-
37
- def contact_json_hash
38
- Hashie::Mash.new(JSON.parse(contact_set_json)).feed.entry.first
39
- end
40
-
41
- def contact_no_emails_json_hash
42
- Hashie::Mash.new(JSON.parse(contact_set_json)).feed.entry[1]
43
- end
44
-
45
- def contact_no_primary_email_json_hash
46
- Hashie::Mash.new(JSON.parse(contact_set_json)).feed.entry[2]
47
- end
48
-
49
- def group_json_hash
50
- Hashie::Mash.new(JSON.parse(group_set_json)).feed.entry.first
51
- end
@@ -0,0 +1,48 @@
1
+ module RspecHelpers
2
+ def load_file(filename)
3
+ f = File.open(File.join(File.dirname(__FILE__), '../fixtures', filename))
4
+ json = f.read
5
+ f.close
6
+ json
7
+ end
8
+
9
+ def contact_set_json
10
+ load_file("contact_set.json")
11
+ end
12
+
13
+ def group_set_json
14
+ load_file("group_set.json")
15
+ end
16
+
17
+ def empty_contact_set_json
18
+ load_file("empty_contact_set.json")
19
+ end
20
+
21
+ def empty_group_set_json
22
+ load_file("empty_group_set.json")
23
+ end
24
+
25
+ def contact_json_hash
26
+ Hashie::Mash.new(JSON.parse(contact_set_json)).feed.entry.first
27
+ end
28
+
29
+ def contact_no_emails_json_hash
30
+ Hashie::Mash.new(JSON.parse(contact_set_json)).feed.entry[1]
31
+ end
32
+
33
+ def contact_no_primary_email_json_hash
34
+ Hashie::Mash.new(JSON.parse(contact_set_json)).feed.entry[2]
35
+ end
36
+
37
+ def group_json_hash
38
+ Hashie::Mash.new(JSON.parse(group_set_json)).feed.entry.first
39
+ end
40
+
41
+ def group_not_system_json_hash
42
+ Hashie::Mash.new(JSON.parse(group_set_json)).feed.entry.last
43
+ end
44
+ end
45
+
46
+ RSpec.configure do |config|
47
+ config.include RspecHelpers
48
+ end