gliffy 0.0.6 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,56 +1,73 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Gliffy::API::Response do
4
+ context "when response is not an error" do
4
5
  let(:xml) { fixture("documents") }
5
6
  let(:response) { Gliffy::API::Response.new(xml) }
6
7
 
7
8
  it "allows to search node by XPath expression" do
8
- expect(response).to respond_to :node
9
- expect(
10
- response.node("//g:documents")
11
- ).to be_instance_of Gliffy::API::Response
9
+ expect(response).to respond_to :node
10
+ expect(
11
+ response.node("//g:documents")
12
+ ).to be_instance_of Gliffy::API::Response
12
13
  end
13
14
 
14
15
  it "allows to search for a set of nodes by XPath expression" do
15
- expect(response).to respond_to :nodes
16
+ expect(response).to respond_to :nodes
16
17
 
17
- result = response.nodes("//g:document")
18
- expect(result).to be_instance_of Array
19
- expect(result.length).to eq 3
18
+ result = response.nodes("//g:document")
19
+ expect(result).to be_instance_of Array
20
+ expect(result.length).to eq 3
20
21
  end
21
22
 
22
23
  it "allows to access text node content" do
23
- node = response.node("//g:document[3]/g:name")
24
- expect(node).to respond_to :content
25
- expect(
26
- node.content
27
- ).to eq "World Domination Flow"
24
+ node = response.node("//g:document[3]/g:name")
25
+ expect(node).to respond_to :content
26
+ expect(
27
+ node.content
28
+ ).to eq "World Domination Flow"
28
29
  end
29
30
 
30
31
  it "allows to get string node content by XPath expression" do
31
- expect(response).to respond_to :string
32
- expect(
33
- response.string("//g:document[2]/g:name")
34
- ).to eq "SNPP Domain Model"
32
+ expect(response).to respond_to :string
33
+ expect(
34
+ response.string("//g:document[2]/g:name")
35
+ ).to eq "SNPP Domain Model"
35
36
  end
36
37
 
37
38
  it "allows to get integer node content by XPath expression" do
38
- expect(response).to respond_to :integer
39
- expect(
40
- response.integer("//g:document[1]/g:owner/@id")
41
- ).to eq 202
39
+ expect(response).to respond_to :integer
40
+ expect(
41
+ response.integer("//g:document[1]/g:owner/@id")
42
+ ).to eq 202
42
43
  end
43
44
 
44
45
  it "allows to get timestamps by XPath expression" do
45
- expect(response).to respond_to :timestamp
46
- expect(
47
- response.timestamp("//g:document[1]/g:create-date")
48
- ).to eq Time.at(1215538283000 / 1000).to_datetime
46
+ expect(response).to respond_to :timestamp
47
+ expect(
48
+ response.timestamp("//g:document[1]/g:create-date")
49
+ ).to eq Time.at(1215538283000 / 1000).to_datetime
49
50
  end
50
51
 
51
52
  it "allows to check for a node existence by XPath" do
52
- expect(response).to respond_to :exists
53
- expect(response.exists("//g:document[1]/@is-public")).to be_false
54
- expect(response.exists("//g:document[2]/@is-public")).to be_true
53
+ expect(response).to respond_to :exists
54
+ expect(response.exists("//g:document[1]/@is-public")).to be_false
55
+ expect(response.exists("//g:document[2]/@is-public")).to be_true
55
56
  end
56
- end
57
+
58
+ it "knows it" do
59
+ expect(response).to respond_to :error?
60
+ expect(response.error?).to be_false
61
+ end
62
+ end
63
+
64
+ context "when response is an error" do
65
+ let(:xml) { fixture("error-401") }
66
+ let(:response) { Gliffy::API::Response.new(xml) }
67
+
68
+ it "knows it" do
69
+ expect(response).to respond_to :error?
70
+ expect(response.error?).to be_true
71
+ end
72
+ end
73
+ end
@@ -1,206 +1,206 @@
1
- require 'spec_helper'
2
-
3
- describe Gliffy::API do
4
- let(:key) { "sample-key" }
5
- let(:secret) { "sample-secret" }
6
- let(:account_id) { 112233 }
7
- let(:consumer) { double(OAuth::Consumer) }
8
- let(:api) { Gliffy::API.new(account_id, key, secret) }
9
-
10
- it "has an OAuth service consumer" do
11
- expect(api).to respond_to :consumer
12
- expect(api.consumer).to be_instance_of OAuth::Consumer
13
- end
14
-
15
- context "when being initialized" do
16
- it "uses API key and shared secret to create a new OAuth consumer object" do
17
- OAuth::Consumer.should_receive(
18
- :new
19
- ).with(
20
- key,
21
- secret,
22
- hash_including(:site => Gliffy.web_root, :scheme => :query_string)
23
- ).and_return(
24
- consumer
25
- )
26
-
27
- api = Gliffy::API.new(account_id, key, secret)
28
- expect(api.consumer).to be(consumer)
29
- end
30
- end
31
-
32
- it "has a Gliffy API account id" do
33
- expect(api).to respond_to :account_id
34
- expect(api.account_id).to eq account_id
35
- end
36
-
37
- it "has a Giffy account" do
38
- response = fixture(
39
- "account",
40
- :account_id => account_id,
41
- :expiration => 1000
42
- )
43
- account = double(Gliffy::Account)
44
-
45
- Gliffy::API::Facade.any_instance.stub(:get).and_return(response)
46
- Gliffy::Account.should_receive(
47
- :load
48
- ).with(
49
- instance_of(Gliffy::API::Facade),
50
- response
51
- ).and_return(account)
52
-
53
- expect(api).to respond_to :account
54
- expect(api.account).to be account
55
- end
56
-
57
- it "has an application name" do
58
- expect(api).to respond_to :application_name
59
- end
60
-
61
- describe "application name" do
62
- it "has a default value" do
63
- expect(api.application_name).to eq Gliffy.default_application_name
64
- end
65
-
66
- it "can be overridden" do
67
- expect(api).to respond_to :application_name=
68
-
69
- test_name = "TEST APP NAME"
70
- api.application_name = test_name
71
- expect(api.application_name).to eq test_name
72
- end
73
- end
74
-
75
- it "has a plain HTTP API facade" do
76
- expect(api).to respond_to :plain
77
- expect(api.plain).to be_instance_of Gliffy::API::Facade
78
- end
79
-
80
- it "has a secure HTTPS API facade" do
81
- expect(api).to respond_to :secure
82
- expect(api.secure).to be_instance_of Gliffy::API::Facade
83
- end
84
-
85
- it "handles GET requests" do
86
- expect(api).to respond_to :get
87
- end
88
-
89
- it "handles raw GET requests" do
90
- expect(api).to respond_to :raw
91
- end
92
-
93
- it "handles POST requests" do
94
- expect(api).to respond_to :post
95
- end
96
-
97
- it "generates signed web links" do
98
- expect(api).to respond_to :web
99
- end
100
-
101
- context "when doing a request" do
102
- let(:url) { 'http://www.gliffy.com/test' }
103
- let(:params) { { :param => 'value' } }
104
- let(:xml) { fixture_xml("document", :document_id => 11, :document_name => "TEST" ) }
105
- let(:raw) { double(Object, :body => xml) }
106
-
107
- it "delegates to 'raw' call when doing GET" do
108
- api.should_receive(
109
- :raw
110
- ).with(
111
- url, params
112
- ).and_return(xml)
113
-
114
- result = api.get(url, params)
115
-
116
- expect(result.exists('//g:documents')).to be_true
117
- end
118
-
119
- it "delegates raw GET requsts to OAuth token instance" do
120
- OAuth::AccessToken.any_instance.should_receive(
121
- :get
122
- ).with(
123
- 'http://www.gliffy.com/test?param=value'
124
- ).and_return(raw)
125
-
126
- result = api.raw(url, params)
127
- expect(
128
- result
129
- ).to eq xml
130
- end
131
-
132
- it "delegates POST requests to OAuth token instance" do
133
- OAuth::AccessToken.any_instance.should_receive(
134
- :post
135
- ).with(
136
- url,
137
- params
138
- ).and_return(raw)
139
-
140
- result = api.post(url, params)
141
- expect(
142
- result
143
- ).to be_instance_of Gliffy::API::Response
144
- expect(result.exists('//g:documents')).to be_true
145
- end
146
-
147
- it "delegates signed links generation to OAuth consumer" do
148
- signed_url = "mock signed url value"
149
-
150
- OAuth::Consumer.any_instance.should_receive(
151
- :create_signed_request
152
- ).with(
153
- :get,
154
- 'http://www.gliffy.com/test?param=value',
155
- instance_of(OAuth::AccessToken)
156
- ).and_return(double(Object, :path => signed_url))
157
-
158
- expect(api.web(url, params)).to eq signed_url
159
- end
160
- end
161
-
162
- it "allows to impersonate user" do
163
- expect(api).to respond_to :impersonate
164
- end
165
-
166
- context "when impersonating user" do
167
- let(:response) { Gliffy::API::Response.new(fixture("token")) }
168
-
169
- it "sends POST to correct URL" do
170
- escaped_user = URI.escape 'test@test.com'
171
-
172
- Gliffy::API::Facade.any_instance.should_receive(
173
- :post
174
- ).with(
175
- "/accounts/#{account_id}/users/#{escaped_user}/oauth_token.xml",
176
- hash_including(:action => "create")
177
- ).and_return(
178
- response
179
- )
180
-
181
- api.impersonate("test@test.com")
182
- end
183
-
184
- it "updates OAuth token using server response" do
185
- Gliffy::API::Facade.any_instance.should_receive(
186
- :post
187
- ).and_return(
188
- response
189
- )
190
-
191
- OAuth::AccessToken.any_instance.should_receive(
192
- :token=
193
- ).with(
194
- "140a1b58c248d13872499df769606766"
195
- )
196
-
197
- OAuth::AccessToken.any_instance.should_receive(
198
- :secret=
199
- ).with(
200
- "481830f5827e35b0644a32c1caac5245"
201
- )
202
-
203
- api.impersonate("test@test.com")
204
- end
205
- end
206
- end
1
+ require 'spec_helper'
2
+
3
+ describe Gliffy::API do
4
+ let(:key) { "sample-key" }
5
+ let(:secret) { "sample-secret" }
6
+ let(:account_id) { 112233 }
7
+ let(:consumer) { double(OAuth::Consumer) }
8
+ let(:api) { Gliffy::API.new(account_id, key, secret) }
9
+
10
+ it "has an OAuth service consumer" do
11
+ expect(api).to respond_to :consumer
12
+ expect(api.consumer).to be_instance_of OAuth::Consumer
13
+ end
14
+
15
+ context "when being initialized" do
16
+ it "uses API key and shared secret to create a new OAuth consumer object" do
17
+ OAuth::Consumer.should_receive(
18
+ :new
19
+ ).with(
20
+ key,
21
+ secret,
22
+ hash_including(:site => Gliffy.web_root, :scheme => :query_string)
23
+ ).and_return(
24
+ consumer
25
+ )
26
+
27
+ api = Gliffy::API.new(account_id, key, secret)
28
+ expect(api.consumer).to be(consumer)
29
+ end
30
+ end
31
+
32
+ it "has a Gliffy API account id" do
33
+ expect(api).to respond_to :account_id
34
+ expect(api.account_id).to eq account_id
35
+ end
36
+
37
+ it "has a Giffy account" do
38
+ response = fixture(
39
+ "account",
40
+ :account_id => account_id,
41
+ :expiration => 1000
42
+ )
43
+ account = double(Gliffy::Account)
44
+
45
+ Gliffy::API::Facade.any_instance.stub(:get).and_return(response)
46
+ Gliffy::Account.should_receive(
47
+ :load
48
+ ).with(
49
+ instance_of(Gliffy::API::Facade),
50
+ response
51
+ ).and_return(account)
52
+
53
+ expect(api).to respond_to :account
54
+ expect(api.account).to be account
55
+ end
56
+
57
+ it "has an application name" do
58
+ expect(api).to respond_to :application_name
59
+ end
60
+
61
+ describe "application name" do
62
+ it "has a default value" do
63
+ expect(api.application_name).to eq Gliffy.default_application_name
64
+ end
65
+
66
+ it "can be overridden" do
67
+ expect(api).to respond_to :application_name=
68
+
69
+ test_name = "TEST APP NAME"
70
+ api.application_name = test_name
71
+ expect(api.application_name).to eq test_name
72
+ end
73
+ end
74
+
75
+ it "has a plain HTTP API facade" do
76
+ expect(api).to respond_to :plain
77
+ expect(api.plain).to be_instance_of Gliffy::API::Facade
78
+ end
79
+
80
+ it "has a secure HTTPS API facade" do
81
+ expect(api).to respond_to :secure
82
+ expect(api.secure).to be_instance_of Gliffy::API::Facade
83
+ end
84
+
85
+ it "handles GET requests" do
86
+ expect(api).to respond_to :get
87
+ end
88
+
89
+ it "handles raw GET requests" do
90
+ expect(api).to respond_to :raw
91
+ end
92
+
93
+ it "handles POST requests" do
94
+ expect(api).to respond_to :post
95
+ end
96
+
97
+ it "generates signed web links" do
98
+ expect(api).to respond_to :web
99
+ end
100
+
101
+ context "when doing a request" do
102
+ let(:url) { 'http://www.gliffy.com/test' }
103
+ let(:params) { { :param => 'value' } }
104
+ let(:xml) { fixture_xml("document", :document_id => 11, :document_name => "TEST" ) }
105
+ let(:raw) { double(Object, :body => xml) }
106
+
107
+ it "delegates to 'raw' call when doing GET" do
108
+ api.should_receive(
109
+ :raw
110
+ ).with(
111
+ url, params
112
+ ).and_return(xml)
113
+
114
+ result = api.get(url, params)
115
+
116
+ expect(result.exists('//g:documents')).to be_true
117
+ end
118
+
119
+ it "delegates raw GET requsts to OAuth token instance" do
120
+ OAuth::AccessToken.any_instance.should_receive(
121
+ :get
122
+ ).with(
123
+ 'http://www.gliffy.com/test?param=value'
124
+ ).and_return(raw)
125
+
126
+ result = api.raw(url, params)
127
+ expect(
128
+ result
129
+ ).to eq xml
130
+ end
131
+
132
+ it "delegates POST requests to OAuth token instance" do
133
+ OAuth::AccessToken.any_instance.should_receive(
134
+ :post
135
+ ).with(
136
+ url,
137
+ params
138
+ ).and_return(raw)
139
+
140
+ result = api.post(url, params)
141
+ expect(
142
+ result
143
+ ).to be_instance_of Gliffy::API::Response
144
+ expect(result.exists('//g:documents')).to be_true
145
+ end
146
+
147
+ it "delegates signed links generation to OAuth consumer" do
148
+ signed_url = "mock signed url value"
149
+
150
+ OAuth::Consumer.any_instance.should_receive(
151
+ :create_signed_request
152
+ ).with(
153
+ :get,
154
+ 'http://www.gliffy.com/test?param=value',
155
+ instance_of(OAuth::AccessToken)
156
+ ).and_return(double(Object, :path => signed_url))
157
+
158
+ expect(api.web(url, params)).to eq signed_url
159
+ end
160
+ end
161
+
162
+ it "allows to impersonate user" do
163
+ expect(api).to respond_to :impersonate
164
+ end
165
+
166
+ context "when impersonating user" do
167
+ let(:response) { Gliffy::API::Response.new(fixture("token")) }
168
+
169
+ it "sends POST to correct URL" do
170
+ escaped_user = URI.escape 'test@test.com'
171
+
172
+ Gliffy::API::Facade.any_instance.should_receive(
173
+ :post
174
+ ).with(
175
+ "/accounts/#{account_id}/users/#{escaped_user}/oauth_token.xml",
176
+ hash_including(:action => "create")
177
+ ).and_return(
178
+ response
179
+ )
180
+
181
+ api.impersonate("test@test.com")
182
+ end
183
+
184
+ it "updates OAuth token using server response" do
185
+ Gliffy::API::Facade.any_instance.should_receive(
186
+ :post
187
+ ).and_return(
188
+ response
189
+ )
190
+
191
+ OAuth::AccessToken.any_instance.should_receive(
192
+ :token=
193
+ ).with(
194
+ "140a1b58c248d13872499df769606766"
195
+ )
196
+
197
+ OAuth::AccessToken.any_instance.should_receive(
198
+ :secret=
199
+ ).with(
200
+ "481830f5827e35b0644a32c1caac5245"
201
+ )
202
+
203
+ api.impersonate("test@test.com")
204
+ end
205
+ end
206
+ end