|
5 | 5 | RSpec.describe "Integrations with the user's controller" do |
6 | 6 | include_context 'with graphql query request' |
7 | 7 |
|
| 8 | + shared_examples 'returns a must authenticate error' do |field| |
| 9 | + it 'returns a must sign in error' do |
| 10 | + expect(json_response[:errors]).to contain_exactly( |
| 11 | + hash_including(message: "#{field} field requires authentication", extensions: { code: 'AUTHENTICATION_ERROR' }) |
| 12 | + ) |
| 13 | + end |
| 14 | + end |
| 15 | + |
8 | 16 | let(:user) { create(:user, :confirmed) } |
9 | 17 |
|
10 | 18 | describe 'publicField' do |
|
54 | 62 | end |
55 | 63 |
|
56 | 64 | context 'when user is not authenticated' do |
57 | | - it 'returns a must sign in error' do |
58 | | - expect(json_response[:errors]).to contain_exactly( |
59 | | - hash_including(message: 'privateField field requires authentication', extensions: { code: 'AUTHENTICATION_ERROR' }) |
60 | | - ) |
61 | | - end |
| 65 | + it_behaves_like 'returns a must authenticate error', 'privateField' |
62 | 66 | end |
63 | 67 | end |
64 | 68 |
|
|
82 | 86 | end |
83 | 87 |
|
84 | 88 | context 'when user is not authenticated' do |
85 | | - it 'returns a must sign in error' do |
86 | | - expect(json_response[:errors]).to contain_exactly( |
87 | | - hash_including(message: 'privateField field requires authentication', extensions: { code: 'AUTHENTICATION_ERROR' }) |
88 | | - ) |
89 | | - end |
| 89 | + it_behaves_like 'returns a must authenticate error', 'privateField' |
90 | 90 | end |
91 | 91 |
|
92 | 92 | context 'when using the failing route' do |
|
111 | 111 | end |
112 | 112 |
|
113 | 113 | context 'when user is not authenticated' do |
114 | | - it 'returns a must sign in error' do |
115 | | - expect(json_response[:errors]).to contain_exactly( |
116 | | - hash_including(message: 'privateField field requires authentication', extensions: { code: 'AUTHENTICATION_ERROR' }) |
117 | | - ) |
118 | | - end |
| 114 | + it_behaves_like 'returns a must authenticate error', 'privateField' |
119 | 115 | end |
120 | 116 | end |
121 | 117 | end |
|
141 | 137 | end |
142 | 138 |
|
143 | 139 | context 'when user is not authenticated' do |
144 | | - it 'returns a must sign in error' do |
145 | | - expect(json_response[:errors]).to contain_exactly( |
146 | | - hash_including(message: 'dummyMutation field requires authentication', extensions: { code: 'AUTHENTICATION_ERROR' }) |
147 | | - ) |
148 | | - end |
| 140 | + it_behaves_like 'returns a must authenticate error', 'dummyMutation' |
149 | 141 | end |
150 | 142 | end |
151 | 143 |
|
|
161 | 153 | end |
162 | 154 |
|
163 | 155 | context 'when user is not authenticated' do |
164 | | - it 'returns a must sign in error' do |
165 | | - expect(json_response[:errors]).to contain_exactly( |
166 | | - hash_including(message: 'dummyMutation field requires authentication', extensions: { code: 'AUTHENTICATION_ERROR' }) |
167 | | - ) |
168 | | - end |
| 156 | + it_behaves_like 'returns a must authenticate error', 'dummyMutation' |
169 | 157 | end |
170 | 158 | end |
171 | 159 | end |
|
199 | 187 | end |
200 | 188 |
|
201 | 189 | context 'when user is not authenticated' do |
202 | | - it 'returns a must sign in error' do |
203 | | - expect(json_response[:errors]).to contain_exactly( |
204 | | - hash_including(message: 'user field requires authentication', extensions: { code: 'AUTHENTICATION_ERROR' }) |
205 | | - ) |
206 | | - end |
| 190 | + it_behaves_like 'returns a must authenticate error', 'user' |
207 | 191 | end |
208 | 192 | end |
209 | 193 |
|
|
271 | 255 | ) |
272 | 256 | end |
273 | 257 | end |
| 258 | + |
| 259 | + describe 'vipField' do |
| 260 | + let(:error_message) { 'Field available only for VIP Users' } |
| 261 | + let(:query) do |
| 262 | + <<-GRAPHQL |
| 263 | + query { vipField } |
| 264 | + GRAPHQL |
| 265 | + end |
| 266 | + |
| 267 | + context 'when using a regular schema' do |
| 268 | + before { post_request('/api/v1/graphql') } |
| 269 | + |
| 270 | + context 'when user is authenticated' do |
| 271 | + let(:headers) { user.create_new_auth_token } |
| 272 | + |
| 273 | + context 'when schema user is VIP' do |
| 274 | + let(:user) { create(:user, :confirmed, vip: true) } |
| 275 | + |
| 276 | + it 'allows to perform the query' do |
| 277 | + expect(json_response[:data][:vipField]).to eq(error_message) |
| 278 | + end |
| 279 | + end |
| 280 | + |
| 281 | + context 'when schema user is not VIP' do |
| 282 | + it_behaves_like 'returns a must authenticate error', 'vipField' |
| 283 | + end |
| 284 | + end |
| 285 | + |
| 286 | + context 'when user is not authenticated' do |
| 287 | + it_behaves_like 'returns a must authenticate error', 'vipField' |
| 288 | + end |
| 289 | + end |
| 290 | + |
| 291 | + context 'when using the interpreter schema' do |
| 292 | + before { post_request('/api/v1/interpreter') } |
| 293 | + |
| 294 | + context 'when user is authenticated' do |
| 295 | + let(:headers) { user.create_new_auth_token } |
| 296 | + |
| 297 | + context 'when schema user is VIP' do |
| 298 | + let(:user) { create(:user, :confirmed, vip: true) } |
| 299 | + |
| 300 | + it 'allows to perform the query' do |
| 301 | + expect(json_response[:data][:vipField]).to eq(error_message) |
| 302 | + end |
| 303 | + end |
| 304 | + |
| 305 | + context 'when schema user is not VIP' do |
| 306 | + it_behaves_like 'returns a must authenticate error', 'vipField' |
| 307 | + end |
| 308 | + end |
| 309 | + |
| 310 | + context 'when user is not authenticated' do |
| 311 | + it_behaves_like 'returns a must authenticate error', 'vipField' |
| 312 | + end |
| 313 | + end |
| 314 | + end |
274 | 315 | end |
0 commit comments