-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
api_client_spec.rb
221 lines (180 loc) 路 6.58 KB
/
api_client_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
describe Spaceship::ConnectAPI::APIClient do
describe "#build_params" do
let(:mock_token) { double('token') }
let(:client) { Spaceship::ConnectAPI::APIClient.new(token: mock_token) }
before(:each) do
allow(mock_token).to receive(:text).and_return("ewfawef")
end
context 'build_params' do
let(:path) { "betaAppReviewDetails" }
let(:nil_filter) { { build: nil } }
let(:one_filter) { { build: "123" } }
let(:two_filters) { { build: "123", app: "321" } }
let(:includes) { "model.attribute" }
let(:limit) { "30" }
let(:sort) { "asc" }
it 'builds params with nothing' do
params = client.build_params
expect(params).to eq({})
end
it 'builds params with nil filter' do
params = client.build_params(filter: nil_filter)
expect(params).to eq({})
end
it 'builds params with one filter' do
params = client.build_params(filter: one_filter)
expect(params).to eq({
filter: one_filter
})
end
it 'builds params with two filters' do
params = client.build_params(filter: two_filters)
expect(params).to eq({
filter: two_filters
})
end
it 'builds params with includes' do
params = client.build_params(includes: includes)
expect(params).to eq({
include: includes
})
end
it 'builds params with limit' do
params = client.build_params(limit: limit)
expect(params).to eq({
limit: limit
})
end
it 'builds params with sort' do
params = client.build_params(sort: sort)
expect(params).to eq({
sort: sort
})
end
it 'builds params with one filter, includes, limit, and sort' do
params = client.build_params(filter: one_filter, includes: includes, limit: limit, sort: sort)
expect(params).to eq({
filter: one_filter,
include: includes,
limit: limit,
sort: sort
})
end
end
end
describe "#request" do
let(:mock_token) { double('token') }
let(:client) { Spaceship::ConnectAPI::APIClient.new(token: mock_token) }
let(:unauth_error) { Spaceship::Client::UnauthorizedAccessError.new }
let(:default_body) { '{foo: "bar"}' }
def stub_client_request(uri, status, body)
stub_request(:get, uri).
to_return(status: status, body: body, headers: { "Content-Type": "application/json" })
end
before(:each) do
allow(mock_token).to receive(:text).and_return("ewfawef")
allow(mock_token).to receive(:expired?).and_return(false)
end
it 'not raise on 200' do
body = JSON.generate({ "data": { "hello": "world" } })
stub_client_request(client.hostname, 200, body)
client.get('')
end
it 'raise on 401' do
body = JSON.generate({ "errors": [] })
stub_client_request(client.hostname, 401, body)
expect do
client.get('')
end.to raise_error(Spaceship::UnauthorizedAccessError)
end
it 'raise on 403 with program license agreement updated' do
body = JSON.generate({ "errors": [{ "code": "FORBIDDEN.REQUIRED_AGREEMENTS_MISSING_OR_EXPIRED" }] })
stub_client_request(client.hostname, 403, body)
expect do
client.get('')
end.to raise_error(Spaceship::ProgramLicenseAgreementUpdated)
end
it 'raise on 403' do
body = JSON.generate({ "errors": [] })
stub_client_request(client.hostname, 403, body)
expect do
client.get('')
end.to raise_error(Spaceship::AccessForbiddenError)
end
end
describe "#handle_error" do
let(:mock_token) { double('token') }
let(:client) { Spaceship::ConnectAPI::APIClient.new(token: mock_token) }
let(:mock_response) { double('response') }
describe "status of 200" do
before(:each) do
allow(mock_response).to receive(:status).and_return(200)
end
it 'does not raise' do
allow(mock_response).to receive(:body).and_return({})
expect do
client.send(:handle_error, mock_response)
end.to_not(raise_error)
end
end
describe "status of 401" do
before(:each) do
allow(mock_response).to receive(:status).and_return(401)
end
it 'raises UnauthorizedAccessError with no errors in body' do
allow(mock_response).to receive(:body).and_return({})
expect do
client.send(:handle_error, mock_response)
end.to raise_error(Spaceship::UnauthorizedAccessError, /Unknown error/)
end
it 'raises UnauthorizedAccessError when body is string' do
allow(mock_response).to receive(:body).and_return('{"errors":[{"title": "Some title", "detail": "some detail"}]}')
expect do
client.send(:handle_error, mock_response)
end.to raise_error(Spaceship::UnauthorizedAccessError, /Some title - some detail/)
end
end
describe "status of 403" do
before(:each) do
allow(mock_response).to receive(:status).and_return(403)
end
it 'raises ProgramLicenseAgreementUpdated with no errors in body FORBIDDEN.REQUIRED_AGREEMENTS_MISSING_OR_EXPIRED' do
allow(mock_response).to receive(:body).and_return({
"errors" => [
{
"code" => "FORBIDDEN.REQUIRED_AGREEMENTS_MISSING_OR_EXPIRED"
}
]
})
expect do
client.send(:handle_error, mock_response)
end.to raise_error(Spaceship::ProgramLicenseAgreementUpdated)
end
it 'raises AccessForbiddenError with no errors in body' do
allow(mock_response).to receive(:body).and_return({})
expect do
client.send(:handle_error, mock_response)
end.to raise_error(Spaceship::AccessForbiddenError, /Unknown error/)
end
it 'raises AccessForbiddenError when body is string' do
allow(mock_response).to receive(:body).and_return('{"errors":[{"title": "Some title", "detail": "some detail"}]}')
expect do
client.send(:handle_error, mock_response)
end.to raise_error(Spaceship::AccessForbiddenError, /Some title - some detail/)
end
it 'raises AccessForbiddenError with errors in body' do
allow(mock_response).to receive(:body).and_return({
"errors" => [
{
"title" => "Some title",
"detail" => "some detail"
}
]
})
expect do
client.send(:handle_error, mock_response)
end.to raise_error(Spaceship::AccessForbiddenError, /Some title - some detail/)
end
end
end
end