harpy 1.0.0 → 2.0.0.preview1
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.
- checksums.yaml +5 -5
- data/.forgejo/workflows/test.yml +33 -0
- data/.github/workflows/test.yml +20 -0
- data/.gitignore +2 -1
- data/README.md +8 -9
- data/harpy.gemspec +8 -5
- data/lib/harpy/client.rb +51 -13
- data/lib/harpy/entry_point.rb +3 -3
- data/lib/harpy/gem_version.rb +14 -0
- data/lib/harpy/resource.rb +113 -96
- data/lib/harpy/version.rb +9 -0
- data/lib/harpy.rb +3 -2
- data/spec/harpy/client_spec.rb +85 -36
- data/spec/harpy/entry_point_spec.rb +19 -19
- data/spec/harpy/resource_spec.rb +254 -255
- data/spec/harpy_spec.rb +20 -20
- data/spec/spec_helper.rb +31 -2
- metadata +63 -51
- data/.travis.yml +0 -9
- data/Gemfile.lock +0 -56
data/spec/harpy/resource_spec.rb
CHANGED
|
@@ -4,13 +4,12 @@ describe Harpy::Resource do
|
|
|
4
4
|
describe ".from_url(hash)" do
|
|
5
5
|
let(:company_url) { "http://localhost/company/1" }
|
|
6
6
|
let(:json_response) { %Q|{"name": "Harpy ltd", "link": [{"rel": "self", "href": "#{company_url}"}]}| }
|
|
7
|
-
let(:success_response) { Typhoeus::Response.new :code => 200, :body => json_response}
|
|
8
7
|
it "queries multiple resources in parallel and return instances" do
|
|
9
|
-
|
|
8
|
+
stub_request(:get, company_url).to_return(status: 200, body: json_response)
|
|
10
9
|
responses = Harpy::Resource.from_url({ Harpy::Spec::Company => [company_url] })
|
|
11
|
-
responses.
|
|
12
|
-
responses[Harpy::Spec::Company].
|
|
13
|
-
responses[Harpy::Spec::Company].first.name.
|
|
10
|
+
expect(responses.size).to eq(1)
|
|
11
|
+
expect(responses[Harpy::Spec::Company].size).to eq(1)
|
|
12
|
+
expect(responses[Harpy::Spec::Company].first.name).to eq("Harpy ltd")
|
|
14
13
|
end
|
|
15
14
|
end
|
|
16
15
|
end
|
|
@@ -35,7 +34,7 @@ module Harpy
|
|
|
35
34
|
super
|
|
36
35
|
end
|
|
37
36
|
def check_lastname
|
|
38
|
-
|
|
37
|
+
throw :abort unless lastname
|
|
39
38
|
end
|
|
40
39
|
def callback_before_save
|
|
41
40
|
@callbacks << :save
|
|
@@ -59,7 +58,7 @@ describe "class including Harpy::Resource" do
|
|
|
59
58
|
context "called with only one url" do
|
|
60
59
|
let(:url){ "http://localhost/company/1" }
|
|
61
60
|
it "is a properly filled-in instance of Harpy::Spec::Company on success" do
|
|
62
|
-
response =
|
|
61
|
+
response = httpx_response :code => 200, :body => <<-eos
|
|
63
62
|
{
|
|
64
63
|
"name": "Harpy Ltd",
|
|
65
64
|
"link": [
|
|
@@ -67,21 +66,21 @@ describe "class including Harpy::Resource" do
|
|
|
67
66
|
]
|
|
68
67
|
}
|
|
69
68
|
eos
|
|
70
|
-
Harpy.client.
|
|
69
|
+
expect(Harpy.client).to receive(:get).with(url).and_return response
|
|
71
70
|
result = Harpy::Spec::Company.from_url url
|
|
72
|
-
result.
|
|
73
|
-
result.name.
|
|
74
|
-
result.link("self").
|
|
71
|
+
expect(result).to be_kind_of Harpy::Spec::Company
|
|
72
|
+
expect(result.name).to eq("Harpy Ltd")
|
|
73
|
+
expect(result.link("self")).to eq(url)
|
|
75
74
|
end
|
|
76
75
|
it "is nil when not found" do
|
|
77
|
-
response =
|
|
78
|
-
Harpy.client.
|
|
79
|
-
Harpy::Spec::Company.from_url(url).
|
|
76
|
+
response = httpx_response :code => 404
|
|
77
|
+
expect(Harpy.client).to receive(:get).with(url).and_return response
|
|
78
|
+
expect(Harpy::Spec::Company.from_url(url)).to be_nil
|
|
80
79
|
end
|
|
81
80
|
it "delegates response code != 200 or 404 to client" do
|
|
82
|
-
response =
|
|
83
|
-
Harpy.client.
|
|
84
|
-
Harpy.client.
|
|
81
|
+
response = httpx_response :code => 500
|
|
82
|
+
expect(Harpy.client).to receive(:get).with(url).and_return response
|
|
83
|
+
expect(Harpy.client).to receive(:invalid_code).with response
|
|
85
84
|
Harpy::Spec::Company.from_url(url)
|
|
86
85
|
end
|
|
87
86
|
end
|
|
@@ -89,7 +88,7 @@ describe "class including Harpy::Resource" do
|
|
|
89
88
|
let(:url1){ "http://localhost/company/1" }
|
|
90
89
|
let(:url2){ "http://localhost/company/2" }
|
|
91
90
|
it "is a properly filled-in instance of Harpy::Spec::Company on success" do
|
|
92
|
-
response1 =
|
|
91
|
+
response1 = httpx_response :code => 200, :body => <<-eos
|
|
93
92
|
{
|
|
94
93
|
"name": "Harpy Ltd",
|
|
95
94
|
"link": [
|
|
@@ -97,7 +96,7 @@ describe "class including Harpy::Resource" do
|
|
|
97
96
|
]
|
|
98
97
|
}
|
|
99
98
|
eos
|
|
100
|
-
response2 =
|
|
99
|
+
response2 = httpx_response :code => 200, :body => <<-eos
|
|
101
100
|
{
|
|
102
101
|
"name": "Harpy Inc",
|
|
103
102
|
"link": [
|
|
@@ -105,25 +104,25 @@ describe "class including Harpy::Resource" do
|
|
|
105
104
|
]
|
|
106
105
|
}
|
|
107
106
|
eos
|
|
108
|
-
|
|
109
|
-
|
|
107
|
+
stub_request(:get, url1).to_return(status: 200, body: response1.body.to_s)
|
|
108
|
+
stub_request(:get, url2).to_return(status: 200, body: response2.body.to_s)
|
|
110
109
|
results = Harpy::Spec::Company.from_url [url1, url2]
|
|
111
|
-
results.
|
|
112
|
-
results[0].
|
|
113
|
-
results[0].name.
|
|
114
|
-
results[0].link("self").
|
|
115
|
-
results[1].
|
|
116
|
-
results[1].name.
|
|
117
|
-
results[1].link("self").
|
|
110
|
+
expect(results.size).to eq(2)
|
|
111
|
+
expect(results[0]).to be_kind_of Harpy::Spec::Company
|
|
112
|
+
expect(results[0].name).to eq("Harpy Ltd")
|
|
113
|
+
expect(results[0].link("self")).to eq(url1)
|
|
114
|
+
expect(results[1]).to be_kind_of Harpy::Spec::Company
|
|
115
|
+
expect(results[1].name).to eq("Harpy Inc")
|
|
116
|
+
expect(results[1].link("self")).to eq(url2)
|
|
118
117
|
end
|
|
119
118
|
end
|
|
120
119
|
end
|
|
121
120
|
describe ".from_urn(urn)" do
|
|
122
121
|
context "when entry point is not set" do
|
|
123
122
|
it "raises Harpy::EntryPointRequired" do
|
|
124
|
-
|
|
123
|
+
expect{
|
|
125
124
|
Harpy::Spec::Company.from_urn("urn:harpy:company:1")
|
|
126
|
-
}.
|
|
125
|
+
}.to raise_error Harpy::EntryPointRequired
|
|
127
126
|
end
|
|
128
127
|
end
|
|
129
128
|
context "when entry point is set" do
|
|
@@ -132,38 +131,38 @@ describe "class including Harpy::Resource" do
|
|
|
132
131
|
it "asks Harpy.entry_point to convert urn to url then call .from_url" do
|
|
133
132
|
# urn:harpy:company:1 -> http://localhost/company/1
|
|
134
133
|
Harpy.entry_point = double
|
|
135
|
-
Harpy.entry_point.
|
|
134
|
+
expect(Harpy.entry_point).to receive(:urn).with(urn).and_return url
|
|
136
135
|
|
|
137
136
|
# http://localhost/company/1 -> Harpy::Spec::Company instance
|
|
138
|
-
Harpy::Spec::Company.
|
|
139
|
-
Harpy::Spec::Company.from_urn(urn).
|
|
137
|
+
expect(Harpy::Spec::Company).to receive(:from_url).with(url).and_return(expected = double)
|
|
138
|
+
expect(Harpy::Spec::Company.from_urn(urn)).to be expected
|
|
140
139
|
end
|
|
141
140
|
it "is nil if urn is not found" do
|
|
142
141
|
# urn:harpy:company:1 -> nil
|
|
143
142
|
Harpy.entry_point = double
|
|
144
|
-
Harpy.entry_point.
|
|
143
|
+
expect(Harpy.entry_point).to receive(:urn).with(urn)
|
|
145
144
|
|
|
146
|
-
Harpy::Spec::Company.from_urn(urn).
|
|
145
|
+
expect(Harpy::Spec::Company.from_urn(urn)).to be_nil
|
|
147
146
|
end
|
|
148
147
|
end
|
|
149
148
|
end
|
|
150
149
|
describe ".from_id(id)" do
|
|
151
150
|
context "when urn has not been overriden" do
|
|
152
151
|
it "raises NotImplementedError" do
|
|
153
|
-
|
|
152
|
+
expect{
|
|
154
153
|
Harpy::Spec::Company.from_id(1)
|
|
155
|
-
}.
|
|
154
|
+
}.to raise_error NotImplementedError
|
|
156
155
|
end
|
|
157
156
|
end
|
|
158
157
|
context "when urn has been overriden but entry point is not set" do
|
|
159
158
|
let(:urn) { "urn:harpy:company:1" }
|
|
160
159
|
it "raises Harpy::EntryPointRequired" do
|
|
161
160
|
# 1 -> urn:harpy:company:1
|
|
162
|
-
Harpy::Spec::Company.
|
|
161
|
+
expect(Harpy::Spec::Company).to receive(:urn).with(1).and_return urn
|
|
163
162
|
|
|
164
|
-
|
|
163
|
+
expect{
|
|
165
164
|
Harpy::Spec::Company.from_id(1)
|
|
166
|
-
}.
|
|
165
|
+
}.to raise_error Harpy::EntryPointRequired
|
|
167
166
|
end
|
|
168
167
|
end
|
|
169
168
|
context "when entry point is set and urn has been overriden" do
|
|
@@ -171,25 +170,25 @@ describe "class including Harpy::Resource" do
|
|
|
171
170
|
let(:url) { "http://localhost/company/1" }
|
|
172
171
|
it "asks Harpy.entry_point to convert urn to url then call .from_url" do
|
|
173
172
|
# 1 -> urn:harpy:company:1
|
|
174
|
-
Harpy::Spec::Company.
|
|
173
|
+
expect(Harpy::Spec::Company).to receive(:urn).with(1).and_return urn
|
|
175
174
|
|
|
176
175
|
# urn:harpy:company:1 -> http://localhost/company/1
|
|
177
176
|
Harpy.entry_point = double
|
|
178
|
-
Harpy.entry_point.
|
|
177
|
+
expect(Harpy.entry_point).to receive(:urn).with(urn).and_return url
|
|
179
178
|
|
|
180
179
|
# http://localhost/company/1 -> Harpy::Spec::Company instance
|
|
181
|
-
Harpy::Spec::Company.
|
|
182
|
-
Harpy::Spec::Company.from_id(1).
|
|
180
|
+
expect(Harpy::Spec::Company).to receive(:from_url).with(url).and_return(expected = double)
|
|
181
|
+
expect(Harpy::Spec::Company.from_id(1)).to be expected
|
|
183
182
|
end
|
|
184
183
|
it "is nil if urn is not found" do
|
|
185
184
|
# 1 -> urn:harpy:company:1
|
|
186
|
-
Harpy::Spec::Company.
|
|
185
|
+
expect(Harpy::Spec::Company).to receive(:urn).with(1).and_return urn
|
|
187
186
|
|
|
188
187
|
# urn:harpy:company:1 -> nil
|
|
189
188
|
Harpy.entry_point = double
|
|
190
|
-
Harpy.entry_point.
|
|
189
|
+
expect(Harpy.entry_point).to receive(:urn).with(urn)
|
|
191
190
|
|
|
192
|
-
Harpy::Spec::Company.from_id(1).
|
|
191
|
+
expect(Harpy::Spec::Company.from_id(1)).to be_nil
|
|
193
192
|
end
|
|
194
193
|
end
|
|
195
194
|
end
|
|
@@ -197,20 +196,20 @@ describe "class including Harpy::Resource" do
|
|
|
197
196
|
context "called with only one url" do
|
|
198
197
|
let(:url){ "http://localhost/company/1" }
|
|
199
198
|
it "is true on success" do
|
|
200
|
-
response =
|
|
201
|
-
Harpy.client.
|
|
199
|
+
response = httpx_response :code => 204
|
|
200
|
+
expect(Harpy.client).to receive(:delete).with(url).and_return response
|
|
202
201
|
result = Harpy::Spec::Company.delete_from_url url
|
|
203
|
-
result.
|
|
202
|
+
expect(result).to be_truthy
|
|
204
203
|
end
|
|
205
204
|
it "is false when not found" do
|
|
206
|
-
response =
|
|
207
|
-
Harpy.client.
|
|
208
|
-
Harpy::Spec::Company.delete_from_url(url).
|
|
205
|
+
response = httpx_response :code => 404
|
|
206
|
+
expect(Harpy.client).to receive(:delete).with(url).and_return response
|
|
207
|
+
expect(Harpy::Spec::Company.delete_from_url(url)).to be_falsey
|
|
209
208
|
end
|
|
210
209
|
it "delegates response code != 204 or 404 to client" do
|
|
211
|
-
response =
|
|
212
|
-
Harpy.client.
|
|
213
|
-
Harpy.client.
|
|
210
|
+
response = httpx_response :code => 500
|
|
211
|
+
expect(Harpy.client).to receive(:delete).with(url).and_return response
|
|
212
|
+
expect(Harpy.client).to receive(:invalid_code).with response
|
|
214
213
|
Harpy::Spec::Company.delete_from_url url
|
|
215
214
|
end
|
|
216
215
|
end
|
|
@@ -218,9 +217,9 @@ describe "class including Harpy::Resource" do
|
|
|
218
217
|
describe ".delete_from_urn(urn)" do
|
|
219
218
|
context "when entry point is not set" do
|
|
220
219
|
it "raises Harpy::EntryPointRequired" do
|
|
221
|
-
|
|
220
|
+
expect{
|
|
222
221
|
Harpy::Spec::Company.delete_from_urn("urn:harpy:company:1")
|
|
223
|
-
}.
|
|
222
|
+
}.to raise_error Harpy::EntryPointRequired
|
|
224
223
|
end
|
|
225
224
|
end
|
|
226
225
|
context "when entry point is set" do
|
|
@@ -229,38 +228,38 @@ describe "class including Harpy::Resource" do
|
|
|
229
228
|
it "asks Harpy.entry_point to convert urn to url then call .from_url" do
|
|
230
229
|
# urn:harpy:company:1 -> http://localhost/company/1
|
|
231
230
|
Harpy.entry_point = double
|
|
232
|
-
Harpy.entry_point.
|
|
231
|
+
expect(Harpy.entry_point).to receive(:urn).with(urn).and_return url
|
|
233
232
|
|
|
234
233
|
# http://localhost/company/1 -> Harpy::Spec::Company instance
|
|
235
|
-
Harpy::Spec::Company.
|
|
236
|
-
Harpy::Spec::Company.delete_from_urn(urn).
|
|
234
|
+
expect(Harpy::Spec::Company).to receive(:delete_from_url).with(url).and_return(expected = double)
|
|
235
|
+
expect(Harpy::Spec::Company.delete_from_urn(urn)).to be expected
|
|
237
236
|
end
|
|
238
237
|
it "is nil if urn is not found" do
|
|
239
238
|
# urn:harpy:company:1 -> nil
|
|
240
239
|
Harpy.entry_point = double
|
|
241
|
-
Harpy.entry_point.
|
|
240
|
+
expect(Harpy.entry_point).to receive(:urn).with(urn)
|
|
242
241
|
|
|
243
|
-
Harpy::Spec::Company.delete_from_urn(urn).
|
|
242
|
+
expect(Harpy::Spec::Company.delete_from_urn(urn)).to be_falsey
|
|
244
243
|
end
|
|
245
244
|
end
|
|
246
245
|
end
|
|
247
246
|
describe ".delete_from_id(id)" do
|
|
248
247
|
context "when urn has not been overriden" do
|
|
249
248
|
it "raises NotImplementedError" do
|
|
250
|
-
|
|
249
|
+
expect{
|
|
251
250
|
Harpy::Spec::Company.delete_from_id(1)
|
|
252
|
-
}.
|
|
251
|
+
}.to raise_error NotImplementedError
|
|
253
252
|
end
|
|
254
253
|
end
|
|
255
254
|
context "when urn has been overriden but entry point is not set" do
|
|
256
255
|
let(:urn) { "urn:harpy:company:1" }
|
|
257
256
|
it "raises Harpy::EntryPointRequired" do
|
|
258
257
|
# 1 -> urn:harpy:company:1
|
|
259
|
-
Harpy::Spec::Company.
|
|
258
|
+
expect(Harpy::Spec::Company).to receive(:urn).with(1).and_return urn
|
|
260
259
|
|
|
261
|
-
|
|
260
|
+
expect{
|
|
262
261
|
Harpy::Spec::Company.delete_from_id(1)
|
|
263
|
-
}.
|
|
262
|
+
}.to raise_error Harpy::EntryPointRequired
|
|
264
263
|
end
|
|
265
264
|
end
|
|
266
265
|
context "when entry point is set and urn has been overriden" do
|
|
@@ -268,48 +267,48 @@ describe "class including Harpy::Resource" do
|
|
|
268
267
|
let(:url) { "http://localhost/company/1" }
|
|
269
268
|
it "asks Harpy.entry_point to convert urn to url then call .from_url" do
|
|
270
269
|
# 1 -> urn:harpy:company:1
|
|
271
|
-
Harpy::Spec::Company.
|
|
270
|
+
expect(Harpy::Spec::Company).to receive(:urn).with(1).and_return urn
|
|
272
271
|
|
|
273
272
|
# urn:harpy:company:1 -> http://localhost/company/1
|
|
274
273
|
Harpy.entry_point = double
|
|
275
|
-
Harpy.entry_point.
|
|
274
|
+
expect(Harpy.entry_point).to receive(:urn).with(urn).and_return url
|
|
276
275
|
|
|
277
276
|
# http://localhost/company/1 -> Harpy::Spec::Company instance
|
|
278
|
-
Harpy::Spec::Company.
|
|
279
|
-
Harpy::Spec::Company.delete_from_id(1).
|
|
277
|
+
expect(Harpy::Spec::Company).to receive(:delete_from_url).with(url).and_return(expected = double)
|
|
278
|
+
expect(Harpy::Spec::Company.delete_from_id(1)).to be expected
|
|
280
279
|
end
|
|
281
280
|
it "is nil if urn is not found" do
|
|
282
281
|
# 1 -> urn:harpy:company:1
|
|
283
|
-
Harpy::Spec::Company.
|
|
282
|
+
expect(Harpy::Spec::Company).to receive(:urn).with(1).and_return urn
|
|
284
283
|
|
|
285
284
|
# urn:harpy:company:1 -> nil
|
|
286
285
|
Harpy.entry_point = double
|
|
287
|
-
Harpy.entry_point.
|
|
286
|
+
expect(Harpy.entry_point).to receive(:urn).with(urn)
|
|
288
287
|
|
|
289
|
-
Harpy::Spec::Company.delete_from_id(1).
|
|
288
|
+
expect(Harpy::Spec::Company.delete_from_id(1)).to be_falsey
|
|
290
289
|
end
|
|
291
290
|
end
|
|
292
291
|
end
|
|
293
292
|
describe ".urn(id)" do
|
|
294
293
|
it "raises NotImplementedError" do
|
|
295
|
-
|
|
294
|
+
expect{
|
|
296
295
|
Harpy::Spec::Company.urn(1)
|
|
297
|
-
}.
|
|
296
|
+
}.to raise_error NotImplementedError
|
|
298
297
|
end
|
|
299
298
|
end
|
|
300
299
|
describe ".resource_name" do
|
|
301
300
|
it "defaults to underscored class name" do
|
|
302
|
-
Harpy::Spec::Company.resource_name.
|
|
301
|
+
expect(Harpy::Spec::Company.resource_name).to eq("harpy/spec/company")
|
|
303
302
|
end
|
|
304
303
|
end
|
|
305
304
|
describe ".search(conditions)" do
|
|
306
305
|
let(:url){ "http://localhost/company" }
|
|
307
306
|
before do
|
|
308
307
|
Harpy.entry_point = double
|
|
309
|
-
Harpy.entry_point.
|
|
308
|
+
expect(Harpy.entry_point).to receive(:resource_url).with("harpy/spec/company").and_return url
|
|
310
309
|
end
|
|
311
310
|
it "return properly filled instances on 200" do
|
|
312
|
-
response =
|
|
311
|
+
response = httpx_response :code => 200, :body => <<-eos
|
|
313
312
|
{
|
|
314
313
|
"harpy/spec/company": [
|
|
315
314
|
{
|
|
@@ -325,50 +324,50 @@ describe "class including Harpy::Resource" do
|
|
|
325
324
|
]
|
|
326
325
|
}
|
|
327
326
|
eos
|
|
328
|
-
Harpy.client.
|
|
327
|
+
expect(Harpy.client).to receive(:get).with(url, :params => {"firstname" => "Anthony"}).and_return response
|
|
329
328
|
companies = Harpy::Spec::Company.search "firstname" => "Anthony"
|
|
330
|
-
companies.
|
|
331
|
-
companies.
|
|
332
|
-
companies.
|
|
333
|
-
companies.first.
|
|
334
|
-
companies.first.firstname.
|
|
335
|
-
companies.first.id.
|
|
336
|
-
companies.url.
|
|
337
|
-
companies.each do |company|
|
|
338
|
-
company.firstname.
|
|
339
|
-
end.
|
|
340
|
-
companies.each.
|
|
341
|
-
companies.to_a.
|
|
342
|
-
companies.detect{ true }.
|
|
343
|
-
companies.
|
|
344
|
-
companies.
|
|
329
|
+
expect(companies).to be_kind_of Harpy::Collection
|
|
330
|
+
expect(companies.size).to eq(1)
|
|
331
|
+
expect(companies.size).to eq(1)
|
|
332
|
+
expect(companies.first).to be_kind_of Harpy::Spec::Company
|
|
333
|
+
expect(companies.first.firstname).to eq("Anthony")
|
|
334
|
+
expect(companies.first.id).to eq("1")
|
|
335
|
+
expect(companies.url).to eq(url)
|
|
336
|
+
expect(companies.each do |company|
|
|
337
|
+
expect(company.firstname).to eq("Anthony")
|
|
338
|
+
end).to be_kind_of Array
|
|
339
|
+
expect(companies.each).to be_kind_of(defined?(Enumerator) ? Enumerator : Enumerable::Enumerator)
|
|
340
|
+
expect(companies.to_a).to eq([companies.first])
|
|
341
|
+
expect(companies.detect{ true }).to be companies.first
|
|
342
|
+
expect(companies.present?).to be true
|
|
343
|
+
expect(companies.blank?).not_to be true
|
|
345
344
|
companies.replace []
|
|
346
|
-
companies.
|
|
347
|
-
companies.
|
|
345
|
+
expect(companies.present?).not_to be true
|
|
346
|
+
expect(companies.blank?).to be true
|
|
348
347
|
end
|
|
349
348
|
it "delegates other response codes to client" do
|
|
350
|
-
response =
|
|
351
|
-
Harpy.client.
|
|
352
|
-
Harpy.client.
|
|
349
|
+
response = httpx_response :code => 500
|
|
350
|
+
expect(Harpy.client).to receive(:get).with(url, :params => {}).and_return response
|
|
351
|
+
expect(Harpy.client).to receive(:invalid_code).with response
|
|
353
352
|
Harpy::Spec::Company.search
|
|
354
353
|
end
|
|
355
354
|
end
|
|
356
355
|
describe ".with_url(url)" do
|
|
357
356
|
let(:url){ "http://localhost/user/1/company" }
|
|
358
357
|
it "overrides url used for searches" do
|
|
359
|
-
response =
|
|
360
|
-
Harpy.client.
|
|
361
|
-
Harpy.client.
|
|
358
|
+
response = httpx_response :code => 500
|
|
359
|
+
expect(Harpy.client).to receive(:get).with(url, :params => {}).and_return response
|
|
360
|
+
expect(Harpy.client).to receive(:invalid_code).with response
|
|
362
361
|
Harpy::Spec::Company.with_url(url) do
|
|
363
362
|
Harpy::Spec::Company.search
|
|
364
363
|
end
|
|
365
364
|
end
|
|
366
365
|
it "can be nested" do
|
|
367
366
|
url2 = "http://localhost/user/2/company"
|
|
368
|
-
response =
|
|
369
|
-
Harpy.client.
|
|
370
|
-
Harpy.client.
|
|
371
|
-
Harpy.client.
|
|
367
|
+
response = httpx_response :code => 500
|
|
368
|
+
expect(Harpy.client).to receive(:get).ordered.with(url2, :params => {}).and_return response
|
|
369
|
+
expect(Harpy.client).to receive(:get).ordered.with(url, :params => {}).and_return response
|
|
370
|
+
expect(Harpy.client).to receive(:invalid_code).twice.with response
|
|
372
371
|
Harpy::Spec::Company.with_url(url) do
|
|
373
372
|
Harpy::Spec::Company.with_url(url2) do
|
|
374
373
|
Harpy::Spec::Company.search
|
|
@@ -380,43 +379,43 @@ describe "class including Harpy::Resource" do
|
|
|
380
379
|
describe "mass assignment" do
|
|
381
380
|
it "sets any attribute on initialization" do
|
|
382
381
|
company = Harpy::Spec::Company.new "name" => "Harpy Ltd"
|
|
383
|
-
company.name.
|
|
382
|
+
expect(company.name).to eq("Harpy Ltd")
|
|
384
383
|
end
|
|
385
384
|
it "initialization works with string keys only" do
|
|
386
385
|
company = Harpy::Spec::Company.new :name => "Harpy Ltd"
|
|
387
|
-
company.name.
|
|
386
|
+
expect(company.name).to be_nil
|
|
388
387
|
end
|
|
389
388
|
it "#attributes=(attrs) merges new attributes with previous ones" do
|
|
390
389
|
company = Harpy::Spec::Company.new "name" => "Harpy Ltd", "deleted" => false
|
|
391
390
|
company.attributes = {"name" => "Harpy Inc", "custom" => true}
|
|
392
|
-
company.name.
|
|
393
|
-
company.deleted.
|
|
394
|
-
company.custom.
|
|
391
|
+
expect(company.name).to eql "Harpy Inc"
|
|
392
|
+
expect(company.deleted).to be_falsey
|
|
393
|
+
expect(company.custom).to be_truthy
|
|
395
394
|
end
|
|
396
395
|
it "#attributes=(attrs) works with string keys only" do
|
|
397
396
|
company = Harpy::Spec::Company.new "name" => "Harpy Ltd"
|
|
398
397
|
company.attributes = {:name => "Harpy Inc"}
|
|
399
|
-
company.name.
|
|
398
|
+
expect(company.name).to eql "Harpy Ltd"
|
|
400
399
|
end
|
|
401
400
|
it "doesn't define writers for each attribute" do
|
|
402
401
|
company = Harpy::Spec::Company.new "name" => "Harpy Ltd"
|
|
403
|
-
|
|
404
|
-
|
|
402
|
+
expect{ company.name = "test" }.to raise_error NoMethodError
|
|
403
|
+
expect{ company[1] }.to raise_error NoMethodError
|
|
405
404
|
end
|
|
406
405
|
it "allows accessing undefined attributes when not persisted" do
|
|
407
|
-
subject.name.
|
|
406
|
+
expect(subject.name).to be_nil
|
|
408
407
|
end
|
|
409
408
|
it "doesn't allows accessing undefined attributes when not persisted" do
|
|
410
|
-
subject.
|
|
411
|
-
|
|
409
|
+
expect(subject).to receive(:persisted?).and_return true
|
|
410
|
+
expect{ subject.name }.to raise_error NoMethodError
|
|
412
411
|
end
|
|
413
412
|
it "use existing setters if available" do
|
|
414
413
|
company = Harpy::Spec::Company.new "tax_id" => "123"
|
|
415
|
-
company.tax_id.
|
|
416
|
-
company.as_json.
|
|
414
|
+
expect(company.tax_id).to eq("123")
|
|
415
|
+
expect(company.as_json).to eq({})
|
|
417
416
|
company.attributes = {:tax_id => 123}
|
|
418
|
-
company.tax_id.
|
|
419
|
-
company.as_json.
|
|
417
|
+
expect(company.tax_id).to eq(123)
|
|
418
|
+
expect(company.as_json).to eq({})
|
|
420
419
|
end
|
|
421
420
|
end
|
|
422
421
|
describe "advanced attribute readers" do
|
|
@@ -424,111 +423,111 @@ describe "class including Harpy::Resource" do
|
|
|
424
423
|
subject{ Harpy::Spec::Company.new "link" => [{"rel" => "self", "href" => url}] }
|
|
425
424
|
describe "#link(rel)" do
|
|
426
425
|
it "searches link with matching rel and return href" do
|
|
427
|
-
subject.link("self").
|
|
426
|
+
expect(subject.link("self")).to eq(url)
|
|
428
427
|
end
|
|
429
428
|
it "works with symbols too" do
|
|
430
|
-
subject.link(:self).
|
|
429
|
+
expect(subject.link(:self)).to eq(url)
|
|
431
430
|
end
|
|
432
431
|
it "is nil if no matching link can be found" do
|
|
433
|
-
subject.link("user").
|
|
432
|
+
expect(subject.link("user")).to be_nil
|
|
434
433
|
end
|
|
435
434
|
end
|
|
436
435
|
describe "#url" do
|
|
437
436
|
it "searches url to self inside links" do
|
|
438
|
-
subject.
|
|
439
|
-
subject.url.
|
|
437
|
+
expect(subject).to receive(:link).with("self").and_return (expected = double)
|
|
438
|
+
expect(subject.url).to be expected
|
|
440
439
|
end
|
|
441
440
|
it "is nil when no link to self can be found" do
|
|
442
|
-
subject.
|
|
443
|
-
subject.url.
|
|
441
|
+
expect(subject).to receive(:link).with "self"
|
|
442
|
+
expect(subject.url).to be_nil
|
|
444
443
|
end
|
|
445
444
|
end
|
|
446
445
|
describe "#url_collection" do
|
|
447
446
|
it "defaults to entry_point link which rel matches resource name" do
|
|
448
447
|
Harpy.entry_point = double
|
|
449
|
-
Harpy.entry_point.
|
|
450
|
-
subject.url_collection.
|
|
448
|
+
expect(Harpy.entry_point).to receive(:resource_url).with("harpy/spec/company").and_return (expected = double)
|
|
449
|
+
expect(subject.url_collection).to be expected
|
|
451
450
|
end
|
|
452
451
|
end
|
|
453
452
|
describe "#id" do
|
|
454
453
|
it "extracts last part from urn" do
|
|
455
454
|
company = Harpy::Spec::Company.new "urn" => "urn:harpy:company:1"
|
|
456
|
-
company.id.
|
|
455
|
+
expect(company.id).to eq("1")
|
|
457
456
|
end
|
|
458
457
|
it "works with alphanumeric ids too" do
|
|
459
458
|
company = Harpy::Spec::Company.new "urn" => "urn:harpy:company:e{39,^"
|
|
460
|
-
company.id.
|
|
459
|
+
expect(company.id).to eq("e{39,^")
|
|
461
460
|
end
|
|
462
461
|
it "works with urns having more than 4 parts" do
|
|
463
462
|
company = Harpy::Spec::Company.new "urn" => "urn:harpy:api:company:1"
|
|
464
|
-
company.id.
|
|
463
|
+
expect(company.id).to eq("1")
|
|
465
464
|
end
|
|
466
465
|
it "is nil without urn" do
|
|
467
|
-
subject.id.
|
|
466
|
+
expect(subject.id).to be_nil
|
|
468
467
|
end
|
|
469
468
|
it "is nil if urn is an empty string" do
|
|
470
469
|
company = Harpy::Spec::Company.new "urn" => ""
|
|
471
|
-
company.id.
|
|
470
|
+
expect(company.id).to be_nil
|
|
472
471
|
end
|
|
473
472
|
it "never uses manually assigned id attribute" do
|
|
474
473
|
company = Harpy::Spec::Company.new "id" => "1"
|
|
475
|
-
company.id.
|
|
474
|
+
expect(company.id).to be_nil
|
|
476
475
|
end
|
|
477
476
|
end
|
|
478
477
|
describe "#persisted?" do
|
|
479
478
|
it "defaults to false when no urn is defined" do
|
|
480
479
|
company = Harpy::Spec::Company.new
|
|
481
|
-
company.persisted
|
|
480
|
+
expect(company.persisted?).to be_falsey
|
|
482
481
|
end
|
|
483
482
|
it "is true when an urn is present" do
|
|
484
483
|
company = Harpy::Spec::Company.new "urn" => "urn:harpy:company:1"
|
|
485
|
-
company.persisted
|
|
484
|
+
expect(company.persisted?).to be_truthy
|
|
486
485
|
end
|
|
487
486
|
it "is false when urn is an empty string" do
|
|
488
487
|
company = Harpy::Spec::Company.new "urn" => ""
|
|
489
|
-
company.persisted
|
|
488
|
+
expect(company.persisted?).to be_falsey
|
|
490
489
|
end
|
|
491
490
|
end
|
|
492
491
|
describe "#inspect" do
|
|
493
492
|
subject { Harpy::Spec::Company.new "firstname" => "Anthony" }
|
|
494
493
|
it "shows class name, attributes, errors and persisted state" do
|
|
495
|
-
subject.inspect.
|
|
494
|
+
expect(subject.inspect).to include("Harpy::Spec::Company", "firstname", "Anthony", "@errors:[] persisted:false>")
|
|
496
495
|
end
|
|
497
496
|
end
|
|
498
497
|
describe "#has_key?(key)" do
|
|
499
498
|
it "is true when attribute is present" do
|
|
500
|
-
subject.has_key?("link").
|
|
499
|
+
expect(subject.has_key?("link")).to be_truthy
|
|
501
500
|
end
|
|
502
501
|
it "does accept symbols too" do
|
|
503
|
-
subject.has_key?(:link).
|
|
502
|
+
expect(subject.has_key?(:link)).to be_truthy
|
|
504
503
|
end
|
|
505
504
|
it "is true when attribute is an empty string" do
|
|
506
505
|
company = Harpy::Spec::Company.new "urn" => ""
|
|
507
|
-
company.has_key?("urn").
|
|
506
|
+
expect(company.has_key?("urn")).to be_truthy
|
|
508
507
|
end
|
|
509
508
|
it "is false when attribute is not defined" do
|
|
510
|
-
subject.has_key?("custom").
|
|
509
|
+
expect(subject.has_key?("custom")).to be_falsey
|
|
511
510
|
end
|
|
512
511
|
it "is false when key matches an existing method but not an attribute" do
|
|
513
|
-
subject.has_key?(:url).
|
|
512
|
+
expect(subject.has_key?(:url)).to be_falsey
|
|
514
513
|
end
|
|
515
514
|
it "does not raise error when checking for an undefined attribute even when persisted" do
|
|
516
|
-
subject.
|
|
517
|
-
subject.has_key?("custom").
|
|
515
|
+
allow(subject).to receive(:persisted?).and_return true
|
|
516
|
+
expect(subject.has_key?("custom")).to be_falsey
|
|
518
517
|
end
|
|
519
518
|
end
|
|
520
519
|
end
|
|
521
520
|
describe "#valid?" do
|
|
522
521
|
it "has ActiveModel validations" do
|
|
523
522
|
user = Harpy::Spec::User.new "lastname" => "Stark"
|
|
524
|
-
user.
|
|
525
|
-
user.
|
|
526
|
-
user.errors[:firstname].
|
|
523
|
+
expect(user).not_to be_valid
|
|
524
|
+
expect(user.errors.size).to eq(1)
|
|
525
|
+
expect(user.errors[:firstname]).to match_array(["can't be blank"])
|
|
527
526
|
end
|
|
528
|
-
it "calls before_validation which prevents validation on
|
|
527
|
+
it "calls before_validation which prevents validation on throw" do
|
|
529
528
|
user = Harpy::Spec::User.new
|
|
530
|
-
user.
|
|
531
|
-
user.
|
|
529
|
+
expect(user).not_to be_valid
|
|
530
|
+
expect(user.errors.size).to eq(0)
|
|
532
531
|
end
|
|
533
532
|
end
|
|
534
533
|
describe "#as_json" do
|
|
@@ -542,31 +541,31 @@ describe "class including Harpy::Resource" do
|
|
|
542
541
|
end
|
|
543
542
|
|
|
544
543
|
it "exclude link and urn from attributes" do
|
|
545
|
-
subject.as_json.
|
|
544
|
+
expect(subject.as_json).to eq({"company_name" => "Stark Enterprises"})
|
|
546
545
|
end
|
|
547
546
|
it "does not remove link and urn from object attributes" do
|
|
548
547
|
subject.as_json
|
|
549
|
-
subject.urn.
|
|
550
|
-
subject.url.
|
|
548
|
+
expect(subject.urn).to eq("urn:harpy:user:1")
|
|
549
|
+
expect(subject.url).to eq(url)
|
|
551
550
|
end
|
|
552
551
|
end
|
|
553
552
|
describe "#save" do
|
|
554
553
|
subject{ Harpy::Spec::User.new "company_name" => "Stark Enterprises" }
|
|
555
554
|
it "is false and does not call before_save callback if not valid" do
|
|
556
|
-
subject.
|
|
557
|
-
subject.save.
|
|
558
|
-
subject.callbacks.
|
|
555
|
+
expect(subject).to receive :valid?
|
|
556
|
+
expect(subject.save).to be_falsey
|
|
557
|
+
expect(subject.callbacks).to match_array([])
|
|
559
558
|
end
|
|
560
559
|
context "on create (valid, not persisted)" do
|
|
561
560
|
let(:url) { "http://localhost/user" }
|
|
562
561
|
let(:body) { '{"company_name":"Stark Enterprises"}' }
|
|
563
562
|
before do
|
|
564
|
-
subject.
|
|
565
|
-
subject.
|
|
563
|
+
expect(subject).to receive(:valid?).and_return true
|
|
564
|
+
expect(subject).to receive(:url_collection).and_return url
|
|
566
565
|
end
|
|
567
566
|
[200, 201, 302].each do |response_code|
|
|
568
567
|
it "is true and merges response attributes on #{response_code}" do
|
|
569
|
-
response =
|
|
568
|
+
response = httpx_response :code => response_code, :body => <<-eos
|
|
570
569
|
{
|
|
571
570
|
"firstname": "Anthony",
|
|
572
571
|
"urn": "urn:harpy:user:1",
|
|
@@ -575,28 +574,28 @@ describe "class including Harpy::Resource" do
|
|
|
575
574
|
]
|
|
576
575
|
}
|
|
577
576
|
eos
|
|
578
|
-
Harpy.client.
|
|
579
|
-
subject.save.
|
|
580
|
-
subject.callbacks.
|
|
581
|
-
subject.firstname.
|
|
582
|
-
subject.company_name.
|
|
583
|
-
subject.urn.
|
|
577
|
+
expect(Harpy.client).to receive(:post).with(url, :body => body).and_return response
|
|
578
|
+
expect(subject.save).to be_truthy
|
|
579
|
+
expect(subject.callbacks).to match_array([:save, :create])
|
|
580
|
+
expect(subject.firstname).to eq("Anthony")
|
|
581
|
+
expect(subject.company_name).to eq("Stark Enterprises")
|
|
582
|
+
expect(subject.urn).to eq("urn:harpy:user:1")
|
|
584
583
|
end
|
|
585
584
|
end
|
|
586
585
|
it "raises Harpy::InvalidResponseCode on 204" do
|
|
587
|
-
response =
|
|
588
|
-
Harpy.client.
|
|
589
|
-
|
|
590
|
-
subject.callbacks.
|
|
586
|
+
response = httpx_response :code => 204
|
|
587
|
+
expect(Harpy.client).to receive(:post).with(url, :body => body).and_return response
|
|
588
|
+
expect { subject.save }.to raise_error Harpy::InvalidResponseCode, "204"
|
|
589
|
+
expect(subject.callbacks).to match_array([:save, :create])
|
|
591
590
|
end
|
|
592
591
|
it "raises Harpy::Unauthorized on 401" do
|
|
593
|
-
response =
|
|
594
|
-
Harpy.client.
|
|
595
|
-
|
|
596
|
-
subject.callbacks.
|
|
592
|
+
response = httpx_response :code => 401
|
|
593
|
+
expect(Harpy.client).to receive(:post).with(url, :body => body).and_return response
|
|
594
|
+
expect { subject.save }.to raise_error Harpy::Unauthorized, "Server returned a 401 response code"
|
|
595
|
+
expect(subject.callbacks).to match_array([:save, :create])
|
|
597
596
|
end
|
|
598
597
|
it "is false and fills in errors on 422" do
|
|
599
|
-
response =
|
|
598
|
+
response = httpx_response :code => 422, :body => <<-eos
|
|
600
599
|
{
|
|
601
600
|
"firstname": "Anthony",
|
|
602
601
|
"errors": {
|
|
@@ -604,28 +603,28 @@ describe "class including Harpy::Resource" do
|
|
|
604
603
|
}
|
|
605
604
|
}
|
|
606
605
|
eos
|
|
607
|
-
Harpy.client.
|
|
608
|
-
subject.save.
|
|
609
|
-
subject.callbacks.
|
|
610
|
-
subject.
|
|
611
|
-
subject.errors[:lastname].
|
|
612
|
-
subject.firstname.
|
|
613
|
-
subject.company_name.
|
|
606
|
+
expect(Harpy.client).to receive(:post).with(url, :body => body).and_return response
|
|
607
|
+
expect(subject.save).to be_falsey
|
|
608
|
+
expect(subject.callbacks).to match_array([:save, :create])
|
|
609
|
+
expect(subject.errors.size).to eq(2)
|
|
610
|
+
expect(subject.errors[:lastname]).to match_array(["can't be blank", "must be unique"])
|
|
611
|
+
expect(subject.firstname).to be_nil
|
|
612
|
+
expect(subject.company_name).to eq("Stark Enterprises")
|
|
614
613
|
end
|
|
615
614
|
it "delegates other response codes to client" do
|
|
616
|
-
response =
|
|
617
|
-
Harpy.client.
|
|
618
|
-
Harpy.client.
|
|
615
|
+
response = httpx_response :code => 500
|
|
616
|
+
expect(Harpy.client).to receive(:post).with(url, :body => body).and_return response
|
|
617
|
+
expect(Harpy.client).to receive(:invalid_code).with(response)
|
|
619
618
|
subject.save
|
|
620
|
-
subject.callbacks.
|
|
619
|
+
expect(subject.callbacks).to match_array([:save, :create])
|
|
621
620
|
end
|
|
622
621
|
end
|
|
623
622
|
context "on update (valid, persisted but link to self is not present)" do
|
|
624
623
|
it "raises Harpy::UrlRequired" do
|
|
625
|
-
subject.
|
|
626
|
-
subject.
|
|
627
|
-
|
|
628
|
-
subject.callbacks.
|
|
624
|
+
expect(subject).to receive(:valid?).and_return true
|
|
625
|
+
expect(subject).to receive(:persisted?).and_return true
|
|
626
|
+
expect{ subject.save }.to raise_error Harpy::UrlRequired
|
|
627
|
+
expect(subject.callbacks).to match_array([:save, :update])
|
|
629
628
|
end
|
|
630
629
|
end
|
|
631
630
|
context "on update (valid, persisted and link to self is present)" do
|
|
@@ -639,11 +638,11 @@ describe "class including Harpy::Resource" do
|
|
|
639
638
|
})
|
|
640
639
|
end
|
|
641
640
|
before do
|
|
642
|
-
subject.
|
|
641
|
+
expect(subject).to receive(:valid?).and_return true
|
|
643
642
|
end
|
|
644
643
|
[200, 201, 302].each do |response_code|
|
|
645
644
|
it "is true and merges response attributes on #{response_code}" do
|
|
646
|
-
response =
|
|
645
|
+
response = httpx_response :code => response_code, :body => <<-eos
|
|
647
646
|
{
|
|
648
647
|
"firstname": "Anthony",
|
|
649
648
|
"urn": "urn:harpy:user:1",
|
|
@@ -652,28 +651,28 @@ describe "class including Harpy::Resource" do
|
|
|
652
651
|
]
|
|
653
652
|
}
|
|
654
653
|
eos
|
|
655
|
-
Harpy.client.
|
|
656
|
-
subject.save.
|
|
657
|
-
subject.callbacks.
|
|
658
|
-
subject.firstname.
|
|
659
|
-
subject.company_name.
|
|
654
|
+
expect(Harpy.client).to receive(:put).with(url, :body => body).and_return response
|
|
655
|
+
expect(subject.save).to be_truthy
|
|
656
|
+
expect(subject.callbacks).to match_array([:save, :update])
|
|
657
|
+
expect(subject.firstname).to eq("Anthony")
|
|
658
|
+
expect(subject.company_name).to eq("Stark Enterprises")
|
|
660
659
|
end
|
|
661
660
|
end
|
|
662
661
|
it "is true but doesn't touch attributes on 204" do
|
|
663
|
-
response =
|
|
664
|
-
Harpy.client.
|
|
665
|
-
subject.save.
|
|
666
|
-
subject.callbacks.
|
|
667
|
-
subject.company_name.
|
|
662
|
+
response = httpx_response :code => 204
|
|
663
|
+
expect(Harpy.client).to receive(:put).with(url, :body => body).and_return response
|
|
664
|
+
expect(subject.save).to be_truthy
|
|
665
|
+
expect(subject.callbacks).to match_array([:save, :update])
|
|
666
|
+
expect(subject.company_name).to eq("Stark Enterprises")
|
|
668
667
|
end
|
|
669
668
|
it "raises Harpy::Unauthorized on 401" do
|
|
670
|
-
response =
|
|
671
|
-
Harpy.client.
|
|
672
|
-
|
|
673
|
-
subject.callbacks.
|
|
669
|
+
response = httpx_response :code => 401
|
|
670
|
+
expect(Harpy.client).to receive(:put).with(url, :body => body).and_return response
|
|
671
|
+
expect { subject.save }.to raise_error Harpy::Unauthorized, "Server returned a 401 response code"
|
|
672
|
+
expect(subject.callbacks).to match_array([:save, :update])
|
|
674
673
|
end
|
|
675
674
|
it "is false and fills in errors on 422" do
|
|
676
|
-
response =
|
|
675
|
+
response = httpx_response :code => 422, :body => <<-eos
|
|
677
676
|
{
|
|
678
677
|
"firstname": "Anthony",
|
|
679
678
|
"errors": {
|
|
@@ -681,20 +680,20 @@ describe "class including Harpy::Resource" do
|
|
|
681
680
|
}
|
|
682
681
|
}
|
|
683
682
|
eos
|
|
684
|
-
Harpy.client.
|
|
685
|
-
subject.save.
|
|
686
|
-
subject.callbacks.
|
|
687
|
-
subject.
|
|
688
|
-
subject.errors[:lastname].
|
|
689
|
-
|
|
690
|
-
subject.company_name.
|
|
683
|
+
expect(Harpy.client).to receive(:put).with(url, :body => body).and_return response
|
|
684
|
+
expect(subject.save).to be_falsey
|
|
685
|
+
expect(subject.callbacks).to match_array([:save, :update])
|
|
686
|
+
expect(subject.errors.size).to eq(2)
|
|
687
|
+
expect(subject.errors[:lastname]).to match_array(["can't be blank", "must be unique"])
|
|
688
|
+
expect { subject.firstname }.to raise_error NoMethodError
|
|
689
|
+
expect(subject.company_name).to eq("Stark Enterprises")
|
|
691
690
|
end
|
|
692
691
|
it "delegates other response codes to client" do
|
|
693
|
-
response =
|
|
694
|
-
Harpy.client.
|
|
695
|
-
Harpy.client.
|
|
692
|
+
response = httpx_response :code => 500
|
|
693
|
+
expect(Harpy.client).to receive(:put).with(url, :body => body).and_return response
|
|
694
|
+
expect(Harpy.client).to receive(:invalid_code).with(response)
|
|
696
695
|
subject.save
|
|
697
|
-
subject.callbacks.
|
|
696
|
+
expect(subject.callbacks).to match_array([:save, :update])
|
|
698
697
|
end
|
|
699
698
|
end
|
|
700
699
|
end
|
|
@@ -702,8 +701,8 @@ describe "class including Harpy::Resource" do
|
|
|
702
701
|
subject{ Harpy::Spec::User.new "company_name" => "Stark Enterprises" }
|
|
703
702
|
context "when link to self is missing" do
|
|
704
703
|
it "raises Harpy::UrlRequired" do
|
|
705
|
-
|
|
706
|
-
subject.callbacks.
|
|
704
|
+
expect{ subject.destroy }.to raise_error Harpy::UrlRequired
|
|
705
|
+
expect(subject.callbacks).to match_array([])
|
|
707
706
|
end
|
|
708
707
|
end
|
|
709
708
|
context "when link to self is present" do
|
|
@@ -717,7 +716,7 @@ describe "class including Harpy::Resource" do
|
|
|
717
716
|
end
|
|
718
717
|
[200, 201, 302].each do |response_code|
|
|
719
718
|
it "is true and merges response attributes on #{response_code}" do
|
|
720
|
-
response =
|
|
719
|
+
response = httpx_response :code => response_code, :body => <<-eos
|
|
721
720
|
{
|
|
722
721
|
"firstname": "Anthony",
|
|
723
722
|
"urn": "urn:harpy:user:1",
|
|
@@ -726,28 +725,28 @@ describe "class including Harpy::Resource" do
|
|
|
726
725
|
]
|
|
727
726
|
}
|
|
728
727
|
eos
|
|
729
|
-
Harpy.client.
|
|
730
|
-
subject.destroy.
|
|
731
|
-
subject.callbacks.
|
|
732
|
-
subject.firstname.
|
|
733
|
-
subject.company_name.
|
|
728
|
+
expect(Harpy.client).to receive(:delete).with(url).and_return response
|
|
729
|
+
expect(subject.destroy).to be_truthy
|
|
730
|
+
expect(subject.callbacks).to match_array([:destroy])
|
|
731
|
+
expect(subject.firstname).to eq("Anthony")
|
|
732
|
+
expect(subject.company_name).to eq("Stark Enterprises")
|
|
734
733
|
end
|
|
735
734
|
end
|
|
736
735
|
it "is true but doesn't touch attributes on 204" do
|
|
737
|
-
response =
|
|
738
|
-
Harpy.client.
|
|
739
|
-
subject.destroy.
|
|
740
|
-
subject.callbacks.
|
|
741
|
-
subject.company_name.
|
|
736
|
+
response = httpx_response :code => 204
|
|
737
|
+
expect(Harpy.client).to receive(:delete).with(url).and_return response
|
|
738
|
+
expect(subject.destroy).to be_truthy
|
|
739
|
+
expect(subject.callbacks).to match_array([:destroy])
|
|
740
|
+
expect(subject.company_name).to eq("Stark Enterprises")
|
|
742
741
|
end
|
|
743
742
|
it "raises Harpy::Unauthorized on 401" do
|
|
744
|
-
response =
|
|
745
|
-
Harpy.client.
|
|
746
|
-
|
|
747
|
-
subject.callbacks.
|
|
743
|
+
response = httpx_response :code => 401
|
|
744
|
+
expect(Harpy.client).to receive(:delete).with(url).and_return response
|
|
745
|
+
expect { subject.destroy }.to raise_error Harpy::Unauthorized, "Server returned a 401 response code"
|
|
746
|
+
expect(subject.callbacks).to match_array([:destroy])
|
|
748
747
|
end
|
|
749
748
|
it "is false and fills in errors on 422" do
|
|
750
|
-
response =
|
|
749
|
+
response = httpx_response :code => 422, :body => <<-eos
|
|
751
750
|
{
|
|
752
751
|
"firstname": "Anthony",
|
|
753
752
|
"errors": {
|
|
@@ -755,20 +754,20 @@ describe "class including Harpy::Resource" do
|
|
|
755
754
|
}
|
|
756
755
|
}
|
|
757
756
|
eos
|
|
758
|
-
Harpy.client.
|
|
759
|
-
subject.destroy.
|
|
760
|
-
subject.callbacks.
|
|
761
|
-
subject.
|
|
762
|
-
subject.errors[:lastname].
|
|
763
|
-
|
|
764
|
-
subject.company_name.
|
|
757
|
+
expect(Harpy.client).to receive(:delete).with(url).and_return response
|
|
758
|
+
expect(subject.destroy).to be_falsey
|
|
759
|
+
expect(subject.callbacks).to match_array([:destroy])
|
|
760
|
+
expect(subject.errors.size).to eq(2)
|
|
761
|
+
expect(subject.errors[:lastname]).to match_array(["can't be blank", "must be unique"])
|
|
762
|
+
expect { subject.firstname }.to raise_error NoMethodError
|
|
763
|
+
expect(subject.company_name).to eq("Stark Enterprises")
|
|
765
764
|
end
|
|
766
765
|
it "delegates other response codes to client" do
|
|
767
|
-
response =
|
|
768
|
-
Harpy.client.
|
|
769
|
-
Harpy.client.
|
|
766
|
+
response = httpx_response :code => 500
|
|
767
|
+
expect(Harpy.client).to receive(:delete).with(url).and_return response
|
|
768
|
+
expect(Harpy.client).to receive(:invalid_code).with(response)
|
|
770
769
|
subject.destroy
|
|
771
|
-
subject.callbacks.
|
|
770
|
+
expect(subject.callbacks).to match_array([:destroy])
|
|
772
771
|
end
|
|
773
772
|
end
|
|
774
773
|
end
|
|
@@ -776,22 +775,22 @@ describe "class including Harpy::Resource" do
|
|
|
776
775
|
it "is equal to itself even without urn" do
|
|
777
776
|
company1 = Harpy::Spec::Company.new
|
|
778
777
|
company2 = company1
|
|
779
|
-
company1.
|
|
778
|
+
expect(company1).to eq(company2)
|
|
780
779
|
end
|
|
781
780
|
it "two instances without urn are not equal" do
|
|
782
781
|
company1 = Harpy::Spec::Company.new
|
|
783
782
|
company2 = Harpy::Spec::Company.new
|
|
784
|
-
company1.
|
|
783
|
+
expect(company1).not_to eq(company2)
|
|
785
784
|
end
|
|
786
785
|
it "two instances of the same class with the same urn are equal" do
|
|
787
786
|
company1 = Harpy::Spec::Company.new "urn" => "urn:harpy:company:1"
|
|
788
787
|
company2 = Harpy::Spec::Company.new "urn" => "urn:harpy:company:1"
|
|
789
|
-
company1.
|
|
788
|
+
expect(company1).to eq(company2)
|
|
790
789
|
end
|
|
791
790
|
it "two instances of the same class with different urns are not equal" do
|
|
792
791
|
company1 = Harpy::Spec::Company.new "urn" => "urn:harpy:company:1"
|
|
793
792
|
company2 = Harpy::Spec::Company.new "urn" => "urn:harpy:company:2"
|
|
794
|
-
company1.
|
|
793
|
+
expect(company1).not_to eq(company2)
|
|
795
794
|
end
|
|
796
795
|
end
|
|
797
796
|
end
|