mirror-api 0.0.9 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,218 @@
1
+ require_relative "spec_helper"
2
+
3
+ describe "Contacts" do
4
+
5
+ before do
6
+ @token = "my-token"
7
+ @api = Mirror::Api::Client.new(@token)
8
+ end
9
+
10
+ describe "delete" do
11
+ context "with valid params" do
12
+ before do
13
+ @id = "123123312"
14
+ stub_request(:delete, "https://www.googleapis.com/mirror/v1/contacts/#{@id}").
15
+ with(headers: json_get_request_headers(@token)).
16
+ to_return(status: 200,
17
+ body: "",
18
+ headers: {})
19
+ end
20
+ it "should return true" do
21
+ @api.contacts.delete(@id).should be_true
22
+ end
23
+ end
24
+
25
+ context "with invalid params" do
26
+ before do
27
+ @id = "blah"
28
+ stub_request(:delete, "https://www.googleapis.com/mirror/v1/contacts/#{@id}").
29
+ with(headers: json_get_request_headers(@token)).
30
+ to_return(status: 400,
31
+ body: "",
32
+ headers: {})
33
+ end
34
+ it "should return nil" do
35
+ contact = @api.contacts.delete(@id)
36
+ contact.should == nil
37
+ end
38
+ end
39
+
40
+ end
41
+
42
+ describe "get" do
43
+
44
+ context "with valid params" do
45
+ before do
46
+ @id = "0987"
47
+
48
+ stub_request(:get, "https://www.googleapis.com/mirror/v1/contacts/#{@id}").
49
+ with(headers: json_get_request_headers(@token)).
50
+ to_return(status: 200,
51
+ body: fixture("contacts_item.json", true),
52
+ headers: {})
53
+ end
54
+ it "should return a contact with .kind == 'mirror#contact'" do
55
+ contact = @api.contacts.get(@id)
56
+ contact.kind.should == 'mirror#contact'
57
+ end
58
+ end
59
+
60
+ context "with invalid params" do
61
+ before do
62
+ @id = "bad_id"
63
+
64
+ stub_request(:get, "https://www.googleapis.com/mirror/v1/contacts/#{@id}").
65
+ with(headers: json_get_request_headers(@token)).
66
+ to_return(status: 404,
67
+ body: fixture("contacts_item.json", true),
68
+ headers: {})
69
+ end
70
+ it "should return nil" do
71
+ contact = @api.contacts.get(@id)
72
+ contact.should == nil
73
+ end
74
+ end
75
+
76
+ end
77
+
78
+ describe "insert" do
79
+
80
+ context "with valid params" do
81
+ before do
82
+ @body = {id: '1234', displayName: 'Demo App', imageUrls: ["http://pixelr3ap3r.com/wp-content/uploads/2012/08/357c6328ee4b11e1bfbf22000a1c91a7_7.jpg"]}
83
+
84
+ stub_request(:post, "https://www.googleapis.com/mirror/v1/contacts/").
85
+ with(body: @body,
86
+ headers: json_post_request_headers(@token, @body.to_json)).
87
+ to_return(status: 200,
88
+ body: fixture("contacts_item.json", true),
89
+ headers: {})
90
+ end
91
+ it "should return a contact with .kind == 'mirror#contact'" do
92
+ contact = @api.contacts.insert(@body)
93
+ contact.kind.should == 'mirror#contact'
94
+ end
95
+ end
96
+
97
+ context "with invalid params" do
98
+ before do
99
+ @body = {canIHazContact: "Really you thought that was valid?!"}
100
+
101
+ stub_request(:post, "https://www.googleapis.com/mirror/v1/contacts/").
102
+ with(body: @body,
103
+ headers: json_post_request_headers(@token, @body.to_json)).
104
+ to_return(status: 404,
105
+ body: "",
106
+ headers: {})
107
+ end
108
+ it "should return nil" do
109
+ contact = @api.contacts.insert(@body)
110
+ contact.should == nil
111
+ end
112
+ end
113
+
114
+ end
115
+
116
+ describe "list" do
117
+
118
+ context "with valid params" do
119
+ before do
120
+
121
+ stub_request(:get, "https://www.googleapis.com/mirror/v1/contacts/").
122
+ with(headers: json_get_request_headers(@token)).
123
+ to_return(status: 200,
124
+ body: fixture("contacts_list.json", true),
125
+ headers: {})
126
+ end
127
+
128
+ it "should return a list of contacts" do
129
+ contacts = @api.contacts.list()
130
+ contacts.should_not be_nil
131
+ contacts.items.count.should == 2 # see fixture
132
+ end
133
+ end
134
+
135
+ end
136
+
137
+ describe "patch" do
138
+
139
+ context "with valid params" do
140
+ before do
141
+ @id = '1234'
142
+ @body = {displayName: 'Demo App'}
143
+
144
+ stub_request(:patch, "https://www.googleapis.com/mirror/v1/contacts/#{@id}").
145
+ with(body: @body,
146
+ headers: json_post_request_headers(@token, @body.to_json)).
147
+ to_return(status: 200,
148
+ body: fixture("contacts_item.json", true),
149
+ headers: {})
150
+ end
151
+ it "should return a contact with .kind == 'mirror#contact'" do
152
+ contact = @api.contacts.patch(@id, @body)
153
+ contact.kind.should == 'mirror#contact'
154
+ end
155
+ end
156
+
157
+ context "with invalid params" do
158
+ before do
159
+ @id = '1234'
160
+ @body = {derp: 'troll'}
161
+
162
+ stub_request(:patch, "https://www.googleapis.com/mirror/v1/contacts/#{@id}").
163
+ with(body: @body,
164
+ headers: json_post_request_headers(@token, @body.to_json)).
165
+ to_return(status: 400,
166
+ body: "",
167
+ headers: {})
168
+ end
169
+ it "should return nil" do
170
+ contact = @api.contacts.patch(@id, @body)
171
+ contact.should == nil
172
+ end
173
+ end
174
+
175
+ end
176
+
177
+ describe "update" do
178
+
179
+ context "with valid params" do
180
+ before do
181
+ @id = '1234'
182
+ @body = {displayName: 'Demo App'}
183
+
184
+ stub_request(:put, "https://www.googleapis.com/mirror/v1/contacts/#{@id}").
185
+ with(body: @body,
186
+ headers: json_post_request_headers(@token, @body.to_json)).
187
+ to_return(status: 200,
188
+ body: fixture("contacts_item.json", true),
189
+ headers: {})
190
+ end
191
+ it "should return a contact with .kind == 'mirror#contact'" do
192
+ contact = @api.contacts.update(@id, @body)
193
+ contact.kind.should == 'mirror#contact'
194
+ contact.phone_number.should == "1234345587"
195
+ end
196
+ end
197
+
198
+ context "with invalid params" do
199
+ before do
200
+ @id = '1234'
201
+ @body = {derp: 'troll'}
202
+
203
+ stub_request(:put, "https://www.googleapis.com/mirror/v1/contacts/#{@id}").
204
+ with(body: @body,
205
+ headers: json_post_request_headers(@token, @body.to_json)).
206
+ to_return(status: 400,
207
+ body: "",
208
+ headers: {})
209
+ end
210
+ it "should return nil" do
211
+ contact = @api.contacts.update(@id, @body)
212
+ contact.should == nil
213
+ end
214
+ end
215
+
216
+ end
217
+
218
+ end
@@ -0,0 +1,25 @@
1
+ {
2
+ "kind": "mirror#subscription",
3
+ "id": "timeline",
4
+ "updated": "2013-04-23T04:38:18.124Z",
5
+ "collection": "timeline",
6
+ "operation": [
7
+ "UPDATE"
8
+ ],
9
+ "callbackUrl": "https://yourawesomewebsite.com/callback",
10
+ "verifyToken": "longstringthatissupposedtobesecure",
11
+ "userToken": "user_1",
12
+ "notification": {
13
+ "collection": "timeline",
14
+ "itemId": "1234",
15
+ "operation": "UPDATE",
16
+ "userActions": [
17
+ {
18
+ "type": "SHARE",
19
+ "payload": "string"
20
+ }
21
+ ],
22
+ "verifyToken": "longstringthatissupposedtobesecure",
23
+ "userToken": "user_1"
24
+ }
25
+ }
@@ -0,0 +1,30 @@
1
+ {
2
+ "kind": "mirror#subscriptionsList",
3
+ "items": [
4
+ {
5
+ "kind": "mirror#subscription",
6
+ "id": "timeline",
7
+ "updated": "2013-04-23T04:38:18.124Z",
8
+ "collection": "timeline",
9
+ "operation": [
10
+ "UPDATE"
11
+ ],
12
+ "callbackUrl": "https://yourawesomewebsite.com/callback",
13
+ "verifyToken": "longstringthatissupposedtobesecure",
14
+ "userToken": "user_1",
15
+ "notification": {
16
+ "collection": "timeline",
17
+ "itemId": "1234",
18
+ "operation": "UPDATE",
19
+ "userActions": [
20
+ {
21
+ "type": "SHARE",
22
+ "payload": "string"
23
+ }
24
+ ],
25
+ "verifyToken": "longstringthatissupposedtobesecure",
26
+ "userToken": "user_1"
27
+ }
28
+ }
29
+ ]
30
+ }
@@ -1,9 +1,9 @@
1
1
  {
2
- "kind": "glass#timelineItem",
3
- "id": "1234567890",
4
- "selfLink": "https://www.googleapis.com/mirror/v1/timeline/1234567890",
5
- "created": "2012-09-25T23:28:43.192Z",
6
- "updated": "2012-09-25T23:28:43.192Z",
7
- "etag": "\"G5BI0RWvj-0jWdBrdWrPZV7xPKw/t25selcGS3uDEVT6FB09hAG-QQ\"",
8
- "text": "Hello world"
2
+ "kind": "mirror#timelineItem",
3
+ "id": "1234567890",
4
+ "selfLink": "https://www.googleapis.com/mirror/v1/timeline/1234567890",
5
+ "created": "2012-09-25T23:28:43.192Z",
6
+ "updated": "2012-09-25T23:28:43.192Z",
7
+ "etag": "\"G5BI0RWvj-0jWdBrdWrPZV7xPKw/t25selcGS3uDEVT6FB09hAG-QQ\"",
8
+ "text": "Hello world"
9
9
  }
@@ -0,0 +1,15 @@
1
+ {
2
+ "kind": "mirror#timeline",
3
+ "nextPageToken": "string",
4
+ "items": [
5
+ {
6
+ "kind": "glass#timelineItem",
7
+ "id": "1234567890",
8
+ "selfLink": "https://www.googleapis.com/mirror/v1/timeline/1234567890",
9
+ "created": "2012-09-25T23:28:43.192Z",
10
+ "updated": "2012-09-25T23:28:43.192Z",
11
+ "etag": "\"G5BI0RWvj-0jWdBrdWrPZV7xPKw/t25selcGS3uDEVT6FB09hAG-QQ\"",
12
+ "text": "Hello world"
13
+ }
14
+ ]
15
+ }
@@ -0,0 +1,68 @@
1
+ require_relative "spec_helper"
2
+
3
+ describe "Locations" do
4
+ before do
5
+ @token = "my-token"
6
+ @api = Mirror::Api::Client.new(@token)
7
+ end
8
+
9
+ describe "get" do
10
+
11
+ context "with valid params" do
12
+ before do
13
+ @id = "0987"
14
+
15
+ stub_request(:get, "https://www.googleapis.com/mirror/v1/locations/#{@id}").
16
+ with(headers: json_get_request_headers(@token)).
17
+ to_return(status: 200,
18
+ body: fixture("locations_item.json", true),
19
+ headers: {})
20
+ end
21
+
22
+ it "should get the location for @id" do
23
+ location = @api.locations.get(@id)
24
+ location.should_not be_nil
25
+ location.display_name.should == "Home" # see fixture
26
+ end
27
+ end
28
+
29
+ context "with invalid params" do
30
+ before do
31
+ @id = "0987asdasds"
32
+
33
+ stub_request(:get, "https://www.googleapis.com/mirror/v1/locations/#{@id}").
34
+ with(headers: json_get_request_headers(@token)).
35
+ to_return(status: 404, body: "",
36
+ headers: {})
37
+
38
+ end
39
+
40
+ it "should not get the item" do
41
+
42
+ item = @api.locations.get(@id)
43
+ item.should be_nil
44
+ end
45
+ end
46
+ end
47
+
48
+ describe "list" do
49
+
50
+ context "with valid params" do
51
+ before do
52
+
53
+ stub_request(:get, "https://www.googleapis.com/mirror/v1/locations/").
54
+ with(headers: json_get_request_headers(@token)).
55
+ to_return(status: 200,
56
+ body: fixture("locations_list.json", true),
57
+ headers: {})
58
+ end
59
+
60
+ it "should return a list of locations" do
61
+ locations = @api.locations.list()
62
+ locations.should_not be_nil
63
+ locations.items.count.should == 2 # see fixture
64
+ end
65
+ end
66
+ end
67
+
68
+ end
data/spec/oauth_spec.rb CHANGED
@@ -21,6 +21,23 @@ describe Mirror::Api::OAuth do
21
21
  access_token.should == "BOOYAH"
22
22
  end
23
23
  end
24
+
25
+ context "has invalid params" do
26
+ before do
27
+ @body = {client_id: @oauth.client_id, client_secret: @oauth.client_secret, refresh_token: @oauth.refresh_token, grant_type: "refresh_token"}
28
+
29
+ stub_request(:post, "https://accounts.google.com/o/oauth2/token").
30
+ with(body: @body,
31
+ headers: json_post_request_headers(@body.to_json)).
32
+ to_return(status: 200,
33
+ body: {:error => {:message => "bad request"}}.to_json,
34
+ headers: {})
35
+ end
36
+
37
+ it "should raise an error" do
38
+ expect{@oauth.get_access_token}.to raise_error
39
+ end
40
+ end
24
41
 
25
42
  end
26
43
 
@@ -0,0 +1,38 @@
1
+ require "spec_helper"
2
+
3
+ describe Mirror::Api::RequestData do
4
+
5
+ describe "initialize" do
6
+ it 'should should convert keys to camelCase' do
7
+ data = Mirror::Api::RequestData.new(:text => "abc", :menu_items => ["ABC"])
8
+ data.should_not be_nil
9
+ data.text.should == "abc"
10
+ data.menu_items.should == ["ABC"]
11
+ data.menuItems.should == ["ABC"]
12
+ end
13
+ end
14
+
15
+ describe "to_json" do
16
+ it "should only pass the camelCase" do
17
+ data = Mirror::Api::RequestData.new(:text => "abc", :menu_items => ["ABC"])
18
+ json = data.to_json
19
+ JSON.parse(json).should == {"text"=>"abc", "menuItems"=>["ABC"]}
20
+ end
21
+ end
22
+
23
+ describe "updates" do
24
+ it "should support changes" do
25
+ data = Mirror::Api::RequestData.new(:text => "abc", :menu_items => ["ABC"])
26
+ data.menu_items = ["DEF"]
27
+ json = data.to_json
28
+ JSON.parse(json).should == {"text"=>"abc", "menuItems"=>["DEF"]}
29
+ end
30
+
31
+ it "should support appending" do
32
+ data = Mirror::Api::RequestData.new(:text => "abc", :menu_items => ["ABC"])
33
+ data.menu_items << "DEF"
34
+ json = data.to_json
35
+ JSON.parse(json).should == {"text"=>"abc", "menuItems"=>["ABC", "DEF"]}
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,66 @@
1
+ require "spec_helper"
2
+
3
+ describe Mirror::Api::Request do
4
+
5
+ before do
6
+ @creds = {:token => "swewew"}
7
+ end
8
+
9
+ describe "initialize" do
10
+ it 'should create a Request' do
11
+ req = Mirror::Api::Request.new(@creds)
12
+ req.should_not be_nil
13
+ end
14
+
15
+ describe "options" do
16
+
17
+ describe "resource" do
18
+ it 'defaults to TIMELINE' do
19
+ req = Mirror::Api::Request.new(@creds)
20
+ req.resource == Mirror::Api::Request::TIMELINE
21
+ end
22
+
23
+ it 'validates resource is valid' do
24
+ req = Mirror::Api::Request.new(@creds, {:resource => Mirror::Api::Request::LOCATIONS})
25
+ req.resource == Mirror::Api::Request::LOCATIONS
26
+
27
+ expect{req.resource = "soup"}.to raise_error
28
+ end
29
+ end
30
+
31
+ describe "params" do
32
+ it "should take a RequestData" do
33
+ data = Mirror::Api::RequestData.new({params: {text: "abc"}})
34
+ req = Mirror::Api::Request.new(@creds, data)
35
+ req.params.text.should == "abc"
36
+ end
37
+
38
+ it "should take a hash" do
39
+ req = Mirror::Api::Request.new(@creds, {params: {text: "abc"}})
40
+ req.params.text.should == "abc"
41
+ end
42
+
43
+ it "should ignore empty hashes" do
44
+ req = Mirror::Api::Request.new(@creds, {})
45
+ req.params.should be_nil
46
+ end
47
+
48
+ it "should throw an error for other data types" do
49
+ expect {Mirror::Api::Request.new(@creds, {params: 45})}.to raise_error
50
+ end
51
+ end
52
+
53
+ describe "logger" do
54
+ it "should take a logger" do
55
+ logger = Logger.new(STDOUT)
56
+ req = Mirror::Api::Request.new(@creds, {logger: logger, params: {text: "abc"}})
57
+ req.logger.should == logger
58
+ end
59
+
60
+ it "should raise an error when logger is not a logger" do
61
+ expect{Mirror::Api::Request.new(@creds, {logger: "Hi", params: {text: "abc"}})}.to raise_error
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end