Skip to content

Commit 3ec3bdb

Browse files
MrSerthnenock
authored andcommitted
fix(nbp): specify JSON as a default format for relationship_status
This change is necessary to get proper error messages in case of (unhandled) exceptions. Previously, a `Pundit::NotAuthorizedError` would redirect to an HTML page. Now, such an exception with simply return a JSON with the proper status code of 401. Since we are using `fetch` with a path not containing any specific format, the previously used default would log `syntax error` messages in the JavaScript console (due to the followed redirect and the attempt to parse the then-received HTML as JSON).
1 parent 37bac9c commit 3ec3bdb

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

config/routes.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
collection do
3737
get '/nbp_wallet/connect', to: 'users/nbp_wallet#connect'
3838
get '/nbp_wallet/qr_code', to: 'users/nbp_wallet#qr_code'
39-
get '/nbp_wallet/relationship_status', to: 'users/nbp_wallet#relationship_status'
39+
get '/nbp_wallet/relationship_status', to: 'users/nbp_wallet#relationship_status', defaults: {format: :json}
4040
get '/nbp_wallet/finalize', to: 'users/nbp_wallet#finalize'
4141
end
4242
end

spec/requests/users/nbp_wallet/relationship_status_spec.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@
1212
set_session(session_params)
1313
end
1414

15+
shared_examples 'an unauthorized JSON request' do
16+
expect_json
17+
expect_http_status(:unauthorized)
18+
19+
it 'returns a JSON with the error message' do
20+
expect(response.parsed_body['error']).to eq I18n.t('common.errors.not_authorized')
21+
end
22+
end
23+
1524
context 'without errors' do
1625
context 'with a Relationship' do
1726
before { allow(Enmeshed::Relationship).to receive(:pending_for).with(uid).and_return(Enmeshed::Relationship) }
@@ -45,7 +54,7 @@
4554
relationship_status_request
4655
end
4756

48-
it_behaves_like 'an unauthorized request'
57+
it_behaves_like 'an unauthorized JSON request'
4958
end
5059

5160
context 'with a session for a completed user' do
@@ -54,7 +63,7 @@
5463
relationship_status_request
5564
end
5665

57-
it_behaves_like 'an unauthorized request'
66+
it_behaves_like 'an unauthorized JSON request'
5867
end
5968

6069
context 'when the connector is down' do

0 commit comments

Comments
 (0)