-
Notifications
You must be signed in to change notification settings - Fork 57
/
static_data_spec.rb
368 lines (316 loc) · 12.6 KB
/
static_data_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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe AskVAApi::V0::StaticDataController, type: :request do
let(:logger) { instance_double(LogService) }
let(:span) { instance_double(Datadog::Tracing::Span) }
before do
allow(LogService).to receive(:new).and_return(logger)
allow(logger).to receive(:call).and_yield(span)
allow(span).to receive(:set_tag)
allow(Rails.logger).to receive(:error)
allow_any_instance_of(Crm::CrmToken).to receive(:call).and_return('token')
end
shared_examples_for 'common error handling' do |status, action, error_message|
it 'logs and renders error and sets datadog tags' do
expect(response).to have_http_status(status)
expect(JSON.parse(response.body)['error']).to eq(error_message)
expect(logger).to have_received(:call).with(action)
expect(span).to have_received(:set_tag).with('error', true)
expect(span).to have_received(:set_tag).with('error.msg', error_message)
expect(Rails.logger).to have_received(:error).with("Error during #{action}: #{error_message}")
end
end
describe 'GET #index' do
let(:index_path) { '/ask_va_api/v0/static_data?key=name&value=irish_country' }
let(:expected_response) { 'pong' }
let(:authorized_user) do
build(:user, :accountable_with_sec_id,
icn: YAML.load_file('./modules/ask_va_api/config/locales/constants.yml')['test_users']['test_user_228_icn'])
end
before do
sign_in(authorized_user)
entity = OpenStruct.new(id: nil, info: 'pong')
allow_any_instance_of(Crm::Service).to receive(:call).and_return(entity)
get index_path
end
context 'when successful' do
it 'returns status of 200 and the correct response data' do
result = JSON.parse(response.body)['table']['info']
expect(response).to have_http_status(:ok)
expect(result).to eq(expected_response)
end
end
end
describe 'GET #announcements' do
let(:announcements_path) { '/ask_va_api/v0/announcements' }
let(:expected_hash) do
{
'id' => nil,
'type' => 'announcements',
'attributes' => {
'text' => 'Test',
'start_date' => '8/18/2024 1:00:00 PM',
'end_date' => '8/18/2024 1:00:00 PM',
'is_portal' => false
}
}
end
context 'when successful' do
before do
get announcements_path, params: { user_mock_data: true }
end
it 'returns announcements data' do
expect(JSON.parse(response.body)['data']).to include(a_hash_including(expected_hash))
expect(response).to have_http_status(:ok)
end
end
context 'when an error occurs' do
let(:error_message) { 'service error' }
before do
allow_any_instance_of(Crm::Service)
.to receive(:call)
.and_raise(StandardError)
get announcements_path
end
it_behaves_like 'common error handling', :unprocessable_entity, 'service_error',
'StandardError: StandardError'
end
end
describe 'GET #categories' do
let(:categories_path) { '/ask_va_api/v0/categories' }
let(:expected_hash) do
{
'id' => '5a524deb-d864-eb11-bb24-000d3a579c45',
'type' => 'categories',
'attributes' => {
'name' => 'VA Center for Minority Veterans',
'allow_attachments' => false,
'description' => nil,
'display_name' => nil,
'parent_id' => nil,
'rank_order' => 18,
'requires_authentication' => false
}
}
end
context 'when successful' do
before do
get categories_path, params: { user_mock_data: true }
end
it 'returns categories data' do
expect(JSON.parse(response.body)['data']).to include(a_hash_including(expected_hash))
expect(response).to have_http_status(:ok)
end
end
context 'when an error occurs' do
let(:error_message) { 'service error' }
before do
allow_any_instance_of(Crm::CacheData)
.to receive(:call)
.and_raise(StandardError)
get categories_path
end
it_behaves_like 'common error handling', :unprocessable_entity, 'service_error',
'StandardError: StandardError'
end
end
describe 'GET #Topics' do
let(:category) do
AskVAApi::Categories::Entity.new({ Id: '60524deb-d864-eb11-bb24-000d3a579c45' })
end
let(:expected_response) do
{
'id' => 'a52a8586-e764-eb11-bb23-000d3a579c3f',
'type' => 'topics',
'attributes' => {
'name' => 'Supplemental Claim',
'allow_attachments' => false,
'description' => nil,
'display_name' => nil,
'parent_id' => '60524deb-d864-eb11-bb24-000d3a579c45',
'rank_order' => 0,
'requires_authentication' => false
}
}
end
let(:topics_path) { "/ask_va_api/v0/categories/#{category.id}/topics" }
context 'when successful' do
before { get topics_path, params: { user_mock_data: true } }
it 'returns topics data' do
expect(JSON.parse(response.body)['data']).to include(a_hash_including(expected_response))
expect(response).to have_http_status(:ok)
end
end
context 'when an error occurs' do
let(:error_message) { 'service error' }
before do
allow_any_instance_of(Crm::CacheData)
.to receive(:call)
.and_raise(StandardError)
get topics_path
end
it_behaves_like 'common error handling', :unprocessable_entity, 'service_error',
'StandardError: StandardError'
end
end
describe 'GET #SubTopics' do
let(:topic) do
AskVAApi::Topics::Entity.new({ Id: 'f0ba9562-e864-eb11-bb23-000d3a579c44' })
end
let(:expected_response) do
{
'id' => '7d2dbcee-eb64-eb11-bb23-000d3a579b83',
'type' => 'sub_topics',
'attributes' => {
'name' => 'Can I get a link on VA site to my site',
'allow_attachments' => false,
'description' => nil,
'display_name' => nil,
'parent_id' => 'f0ba9562-e864-eb11-bb23-000d3a579c44',
'rank_order' => 0,
'requires_authentication' => false
}
}
end
let(:subtopics_path) { "/ask_va_api/v0/topics/#{topic.id}/subtopics" }
context 'when successful' do
before { get subtopics_path, params: { user_mock_data: true } }
it 'returns subtopics data' do
expect(JSON.parse(response.body)['data']).to include(a_hash_including(expected_response))
expect(response).to have_http_status(:ok)
end
end
context 'when an error occurs' do
let(:error_message) { 'service error' }
before do
allow_any_instance_of(AskVAApi::SubTopics::Retriever)
.to receive(:call)
.and_raise(StandardError, 'standard error')
get subtopics_path, params: { mock: true }
end
it_behaves_like 'common error handling', :internal_server_error, 'unexpected_error',
'standard error'
end
end
describe 'GET #optionset' do
let(:optionset) do
AskVAApi::Optionset::Entity.new({ Id: 'f0ba9562-e864-eb11-bb23-000d3a579c44' })
end
let(:expected_response) do
{
'id' => '722310000',
'type' => 'optionsets',
'attributes' => {
'name' => 'Air Force'
}
}
end
let(:optionset_path) { '/ask_va_api/v0/optionset' }
context 'when successful' do
before { get optionset_path, params: { user_mock_data: true, name: 'branchofservice' } }
it 'returns optionset data' do
expect(JSON.parse(response.body)['data']).to include(a_hash_including(expected_response))
expect(response).to have_http_status(:ok)
end
end
context 'when an error occurs' do
let(:body) do
'{"Data":null,"Message":"Data Validation: Invalid OptionSet Name iris_branchofservic, valid' \
' values are iris_inquiryabout, iris_inquirysource, iris_inquirytype, iris_levelofauthentication,' \
' iris_suffix, iris_veteranrelationship, iris_branchofservice, iris_country, iris_province,' \
' iris_responsetype, iris_dependentrelationship, statuscode, iris_messagetype","ExceptionOccurred":' \
'true,"ExceptionMessage":"Data Validation: Invalid OptionSet Name iris_branchofservic, valid' \
' values are iris_inquiryabout, iris_inquirysource, iris_inquirytype, iris_levelofauthentication,' \
' iris_suffix, iris_veteranrelationship, iris_branchofservice, iris_country, iris_province,' \
' iris_responsetype, iris_dependentrelationship, statuscode, iris_messagetype","MessageId":' \
'"6dfa81bd-f04a-4f39-88c5-1422d88ed3ff"}'
end
let(:failure) { Faraday::Response.new(response_body: body, status: 400) }
before do
allow_any_instance_of(Crm::Service).to receive(:call)
.with(endpoint: 'optionset', payload: { name: 'iris_branchofservic' }).and_return(failure)
get optionset_path, params: { user_mock_data: nil, name: 'branchofservic' }
end
it_behaves_like 'common error handling', :unprocessable_entity, 'service_error',
'ErrorHandler::ServiceError: StandardError: Data Validation: ' \
'Invalid OptionSet Name iris_branchofservic, valid values are ' \
'iris_inquiryabout, iris_inquirysource, iris_inquirytype, ' \
'iris_levelofauthentication, iris_suffix, iris_veteranrelationship, ' \
'iris_branchofservice, iris_country, iris_province, iris_responsetype,' \
' iris_dependentrelationship, statuscode, iris_messagetype'
end
end
describe 'GET #Zipcode' do
let(:expected_response) do
[{ 'id' => nil,
'type' => 'zipcodes',
'attributes' =>
{ 'zipcode' => '36010', 'city' => 'Autaugaville', 'state' => 'AL', 'lat' => 32.4312, 'lng' => -86.6549 } },
{ 'id' => nil,
'type' => 'zipcodes',
'attributes' => { 'zipcode' => '36011', 'city' => 'Millbrook', 'state' => 'AL', 'lat' => 32.5002,
'lng' => -86.3691 } },
{ 'id' => nil,
'type' => 'zipcodes',
'attributes' => { 'zipcode' => '36012', 'city' => 'Deatsville', 'state' => 'AL', 'lat' => 32.5997,
'lng' => -86.324 } }]
end
let(:zipcodes_path) { '/ask_va_api/v0/zipcodes' }
context 'when successful' do
before { get zipcodes_path, params: { zipcode: zip, mock: true } }
context 'when zipcodes are found' do
let(:zip) { '3601' }
it 'returns zipcode data' do
expect(response).to have_http_status(:ok)
expect(JSON.parse(response.body)['data'].first(3)).to eq(expected_response)
end
end
context 'when no zipcode is found' do
let(:zip) { '4000' }
it 'returns an empty array' do
expect(response).to have_http_status(:ok)
expect(JSON.parse(response.body)['data']).to eq([])
end
end
end
context 'when an error occurs' do
let(:error_message) { 'standard error' }
before do
allow_any_instance_of(AskVAApi::Zipcodes::Retriever)
.to receive(:fetch_data)
.and_raise(StandardError.new(error_message))
get zipcodes_path, params: { mock: true }
end
it_behaves_like 'common error handling', :unprocessable_entity, 'service_error',
'StandardError: standard error'
end
end
describe 'GET #States' do
let(:states_path) { '/ask_va_api/v0/states' }
let(:scoped_response) do
[
{ 'id' => nil, 'type' => 'states', 'attributes' => { 'name' => 'Alabama', 'code' => 'AL' } },
{ 'id' => nil, 'type' => 'states', 'attributes' => { 'name' => 'Alaska', 'code' => 'AK' } },
{ 'id' => nil, 'type' => 'states', 'attributes' => { 'name' => 'Arizona', 'code' => 'AZ' } }
]
end
context 'when successful' do
before { get states_path, params: { mock: true } }
it 'returns all the states' do
expect(response).to have_http_status(:ok)
expect(JSON.parse(response.body)['data'].first(3)).to eq(scoped_response)
end
end
context 'when an error occurs' do
let(:error_message) { 'standard error' }
before do
allow_any_instance_of(AskVAApi::States::Retriever)
.to receive(:fetch_data)
.and_raise(StandardError.new(error_message))
get states_path, params: { mock: true }
end
it_behaves_like 'common error handling', :unprocessable_entity, 'service_error',
'StandardError: standard error'
end
end
end