forked from driggl/jsonapi_errors_handler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsonapi_errors_handler_spec.rb
151 lines (125 loc) · 3.89 KB
/
jsonapi_errors_handler_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
# frozen_string_literal: true
require 'spec_helper'
class TestJsonapiErrorsHandler
include JsonapiErrorsHandler
def config
JsonapiErrorsHandler::Configuration.instance
end
def render(json: {}, status:)
json.to_h.merge(status: status)
end
end
class DummyErrorLogger
include JsonapiErrorsHandler
def render(json: {}, status:)
json.to_h.merge(status: status)
end
def log_error(_error); end
end
RSpec.describe JsonapiErrorsHandler do
let(:dummy) { TestJsonapiErrorsHandler.new }
it 'expects ErrorMapper to have already mapped some errors' do
dummy
expect(JsonapiErrorsHandler::ErrorMapper.mapped_errors).not_to be_empty
end
describe '.handle_error' do
let(:mapped_error) { JsonapiErrorsHandler::Errors::Forbidden.new }
let(:subject) { dummy.handle_error(mapped_error) }
context 'when error is mapped' do
let(:expected_result) do
{
errors: [
{
detail: 'You have no rights to access this resource',
source: { 'pointer' => '/request/headers/authorization' },
status: 403,
title: 'Forbidden request'
}
],
status: 403
}
end
it 'renders mapped error' do
expect(subject).to include(expected_result)
end
context 'when responds to log_error method' do
let(:dummy) { DummyErrorLogger.new }
let(:subject) { dummy.handle_error(mapped_error) }
it 'does not call error that is mapped' do
expect(dummy).to receive(:log_error)
subject
end
end
end
context 'when error is not mapped' do
let(:unmapped_error) { 'Error' }
let(:subject) { dummy.handle_error(unmapped_error) }
let(:expected_result) do
{
errors: [
{
detail: "We've encountered unexpected error, but our developers had been already notified about it",
source: {},
status: 500,
title: 'Something went wrong'
}
],
status: 500
}
end
it 'raises the original error by default' do
expect { subject }.to raise_error(unmapped_error)
end
it 'renders 500 error' do
JsonapiErrorsHandler.configure do |config|
config.handle_unexpected = true
end
expect(subject).to include(expected_result)
end
context 'when responds to log_error method' do
let(:dummy) { DummyErrorLogger.new }
let(:subject) { dummy.handle_error(unmapped_error) }
it 'logs unmapped error' do
JsonapiErrorsHandler.configure do |config|
config.handle_unexpected = true
end
expect(dummy).to receive(:log_error).with(unmapped_error)
expect(subject)
end
end
context 'when unexpected error is not handled' do
let(:dummy) { DummyErrorLogger.new }
let(:subject) { dummy.handle_error(unmapped_error) }
it 'logs unmapped error' do
JsonapiErrorsHandler.configure do |config|
config.handle_unexpected = false
end
expect(dummy).to receive(:log_error).with(unmapped_error)
expect { subject }.to raise_error(unmapped_error)
end
end
end
end
describe '.render_error' do
let(:subject) { dummy.render_error(error) }
context 'renders the error' do
let(:error) { JsonapiErrorsHandler::Errors::Forbidden.new }
let(:expected_result) do
{
errors: [
{
detail: 'You have no rights to access this resource',
source: { 'pointer' => '/request/headers/authorization' },
status: 403,
title: 'Forbidden request'
}
],
status: 403
}
end
it 'returns error' do
expect(subject).to include(expected_result)
end
end
end
end