-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy patherrors_spec.rb
194 lines (177 loc) · 5.64 KB
/
errors_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
require 'spec_helper'
RSpec.describe NotesController, type: :request do
describe 'PUT /notes/:id' do
let(:note) { create_note }
let(:note_id) { note.id }
let(:user) { note.user }
let(:user_id) { user.id }
let(:note_params) do
{
data: {
attributes: { title: FFaker::Company.name },
relationships: { user: { data: { id: user_id } } }
}
}
end
let(:params) { note_params }
before do
put(note_path(note_id), params: params.to_json, headers: jsonapi_headers)
end
it do
expect(response).to have_http_status(:ok)
expect(response_json['data']).to have_id(note.id.to_s)
expect(response_json['meta']).to eq('single' => true)
end
context 'with a missing parameter in the payload' do
let(:params) { {} }
it do
expect(response).to have_http_status(:unprocessable_entity)
expect(response_json['errors'].size).to eq(1)
expect(response_json['errors']).to contain_exactly(
{
'status' => '422',
'source' => { 'pointer' => '' },
'title' => 'Unprocessable Content',
'detail' => nil,
'code' => nil
}
)
end
end
context 'with an invalid payload' do
let(:params) do
payload = note_params.dup
payload[:data][:relationships][:user][:data][:id] = nil
payload
end
it do
expect(response).to have_http_status(:unprocessable_entity)
expect(response_json['errors'].size).to eq(1)
expected_detail = if Rails.gem_version >= Gem::Version.new('6.1')
'User must exist'
else
'User can\'t be blank'
end
expect(response_json['errors']).to contain_exactly(
{
'status' => '422',
'source' => { 'pointer' => '/data/relationships/user' },
'title' => 'Unprocessable Content',
'detail' => expected_detail,
'code' => 'blank'
}
)
end
context 'required by validations' do
let(:params) do
payload = note_params.dup
payload[:data][:attributes][:title] = 'BAD_TITLE'
payload[:data][:attributes][:quantity] = 100 + rand(10)
payload
end
it do
expect(response).to have_http_status(:unprocessable_entity)
expect(response_json['errors'].size).to eq(3)
expect(response_json['errors']).to contain_exactly(
{
'status' => '422',
'source' => { 'pointer' => '/data/attributes/title' },
'title' => 'Unprocessable Content',
'detail' => 'Title is invalid',
'code' => 'invalid'
},
{
'status' => '422',
'source' => { 'pointer' => '/data/attributes/title' },
'title' => 'Unprocessable Content',
'detail' => 'Title has typos',
'code' => 'invalid'
},
{
'status' => '422',
'source' => { 'pointer' => '/data/attributes/quantity' },
'title' => 'Unprocessable Content',
'detail' => 'Quantity must be less than 100',
'code' => 'less_than'
}
)
end
end
context 'validations with non-interpolated messages' do
let(:params) do
payload = note_params.dup
payload[:data][:attributes][:title] = 'SLURS ARE GREAT'
payload
end
it do
expect(response).to have_http_status(:unprocessable_entity)
expect(response_json['errors'].size).to eq(1)
expect(response_json['errors']).to contain_exactly(
{
'status' => '422',
'source' => { 'pointer' => '' },
'title' => 'Unprocessable Content',
'detail' => 'Title has slurs',
'code' => 'title_has_slurs'
}
)
end
end
context 'as a param attribute' do
let(:params) do
payload = note_params.dup
payload[:data][:attributes].delete(:title)
# To have any attribtues in the payload...
payload[:data][:attributes][:created_at] = nil
payload
end
it do
expect(response).to have_http_status(:unprocessable_entity)
expect(response_json['errors']).to contain_exactly(
{
'status' => '422',
'source' => { 'pointer' => '/data/attributes/title' },
'title' => 'Unprocessable Content',
'detail' => nil,
'code' => nil
}
)
end
end
end
context 'with a bad note ID' do
let(:user_id) { nil }
let(:note_id) { rand(10) }
it do
expect(response).to have_http_status(:not_found)
expect(response_json['errors'].size).to eq(1)
expect(response_json['errors']).to contain_exactly(
{
'status' => '404',
'source' => nil,
'title' => 'Not Found',
'detail' => nil,
'code' => nil
}
)
end
end
context 'with an exception' do
let(:user_id) { nil }
let(:note_id) { 'tada' }
it do
expect(response).to have_http_status(:internal_server_error)
expect(response_json['errors'].size).to eq(1)
expect(response_json['errors']).to contain_exactly(
{
'status' => '500',
'source' => nil,
'title' => 'Internal Server Error',
'detail' => nil,
'code' => nil
}
)
end
end
end
end