|
| 1 | +# frozen_string_literal: true |
1 | 2 |
|
2 | 3 | require 'rails_helper' |
3 | 4 |
|
4 | 5 | RSpec.describe 'Sign Up process' do |
5 | 6 | include_context 'with graphql schema test' |
6 | 7 |
|
7 | | - let(:schema) { DummySchema } |
8 | | - let(:user) { create(:user, :confirmed) } |
| 8 | + let(:schema) { DummySchema } |
| 9 | + let(:user) { create(:user, :confirmed) } |
| 10 | + let(:field) { 'privateField' } |
| 11 | + let(:public_message) { 'Field does not require authentication' } |
| 12 | + let(:private_message) { 'Field will always require authentication' } |
| 13 | + let(:private_error) do |
| 14 | + { |
| 15 | + message: "#{field} field requires authentication", |
| 16 | + extensions: { code: 'AUTHENTICATION_ERROR' } |
| 17 | + } |
| 18 | + end |
9 | 19 |
|
10 | 20 | describe 'publicField' do |
11 | 21 | let(:query) do |
|
18 | 28 |
|
19 | 29 | context 'when using a regular schema' do |
20 | 30 | it 'does not require authentication' do |
21 | | - expect(response[:data][:publicField]).to eq('Field does not require authentication') |
| 31 | + expect(response[:data][:publicField]).to eq(public_message) |
22 | 32 | end |
23 | 33 | end |
24 | 34 | end |
|
37 | 47 | let(:resource) { user } |
38 | 48 |
|
39 | 49 | it 'allows to perform the query' do |
40 | | - expect(response[:data][:privateField]).to eq('Field will always require authentication') |
| 50 | + expect(response[:data][:privateField]).to eq(private_message) |
41 | 51 | end |
42 | 52 |
|
43 | 53 | context 'when using a SchemaUser' do |
44 | 54 | let(:resource) { create(:schema_user, :confirmed) } |
45 | 55 |
|
46 | 56 | it 'allows to perform the query' do |
47 | | - expect(response[:data][:privateField]).to eq('Field will always require authentication') |
| 57 | + expect(response[:data][:privateField]).to eq(private_message) |
48 | 58 | end |
49 | 59 | end |
50 | 60 | end |
51 | 61 |
|
52 | 62 | context 'when user is not authenticated' do |
53 | 63 | it 'returns a must sign in error' do |
54 | | - expect(response[:errors]).to contain_exactly( |
55 | | - hash_including( |
56 | | - message: 'privateField field requires authentication', |
57 | | - extensions: { code: 'AUTHENTICATION_ERROR' } |
58 | | - ) |
59 | | - ) |
| 64 | + expect(response[:errors]).to contain_exactly(hash_including(**private_error)) |
60 | 65 | end |
61 | 66 | end |
62 | 67 | end |
|
68 | 73 | let(:resource) { user } |
69 | 74 |
|
70 | 75 | it 'allows to perform the query' do |
71 | | - expect(response[:data][:privateField]).to eq('Field will always require authentication') |
| 76 | + expect(response[:data][:privateField]).to eq(private_message) |
72 | 77 | end |
73 | 78 | end |
74 | 79 |
|
75 | 80 | context 'when user is not authenticated' do |
76 | 81 | it 'returns a must sign in error' do |
77 | | - expect(response[:errors]).to contain_exactly( |
78 | | - hash_including( |
79 | | - message: 'privateField field requires authentication', |
80 | | - extensions: { code: 'AUTHENTICATION_ERROR' } |
81 | | - ) |
82 | | - ) |
| 82 | + expect(response[:errors]).to contain_exactly(hash_including(**private_error)) |
83 | 83 | end |
84 | 84 | end |
85 | 85 | end |
86 | 86 | end |
87 | 87 |
|
88 | 88 | describe 'user' do |
| 89 | + let(:user_data) { { email: user.email, id: user.id } } |
89 | 90 | let(:query) do |
90 | 91 | <<-GRAPHQL |
91 | 92 | query { |
|
102 | 103 | let(:resource) { user } |
103 | 104 |
|
104 | 105 | it 'allows to perform the query' do |
105 | | - expect(response[:data][:user]).to match( |
106 | | - email: user.email, |
107 | | - id: user.id |
108 | | - ) |
| 106 | + expect(response[:data][:user]).to match(**user_data) |
109 | 107 | end |
110 | 108 | end |
111 | 109 |
|
112 | 110 | context 'when user is not authenticated' do |
| 111 | + let(:field) { 'user' } |
| 112 | + |
113 | 113 | it 'returns a must sign in error' do |
114 | | - expect(response[:errors]).to contain_exactly( |
115 | | - hash_including( |
116 | | - message: 'user field requires authentication', |
117 | | - extensions: { code: 'AUTHENTICATION_ERROR' } |
118 | | - ) |
119 | | - ) |
| 114 | + expect(response[:errors]).to contain_exactly(hash_including(**private_error)) |
120 | 115 | end |
121 | 116 | end |
122 | 117 | end |
|
128 | 123 | let(:resource) { user } |
129 | 124 |
|
130 | 125 | it 'allows to perform the query' do |
131 | | - expect(response[:data][:user]).to match( |
132 | | - email: user.email, |
133 | | - id: user.id |
134 | | - ) |
| 126 | + expect(response[:data][:user]).to match(**user_data) |
135 | 127 | end |
136 | 128 | end |
137 | 129 |
|
138 | 130 | context 'when user is not authenticated' do |
139 | 131 | # Interpreter schema fields are public unless specified otherwise (plugin setting) |
140 | 132 | it 'allows to perform the query' do |
141 | | - expect(response[:data][:user]).to match( |
142 | | - email: user.email, |
143 | | - id: user.id |
144 | | - ) |
| 133 | + expect(response[:data][:user]).to match(**user_data) |
145 | 134 | end |
146 | 135 | end |
147 | 136 | end |
|
0 commit comments