|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'rails_helper' |
| 4 | + |
| 5 | +RSpec.describe 'Users controller specs' do |
| 6 | + include_context 'with graphql schema test' |
| 7 | + |
| 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 |
| 19 | + |
| 20 | + describe 'publicField' do |
| 21 | + let(:query) do |
| 22 | + <<-GRAPHQL |
| 23 | + query { |
| 24 | + publicField |
| 25 | + } |
| 26 | + GRAPHQL |
| 27 | + end |
| 28 | + |
| 29 | + context 'when using a regular schema' do |
| 30 | + it 'does not require authentication' do |
| 31 | + expect(response[:data][:publicField]).to eq(public_message) |
| 32 | + end |
| 33 | + end |
| 34 | + end |
| 35 | + |
| 36 | + describe 'privateField' do |
| 37 | + let(:query) do |
| 38 | + <<-GRAPHQL |
| 39 | + query { |
| 40 | + privateField |
| 41 | + } |
| 42 | + GRAPHQL |
| 43 | + end |
| 44 | + |
| 45 | + context 'when using a regular schema' do |
| 46 | + context 'when user is authenticated' do |
| 47 | + let(:resource) { user } |
| 48 | + |
| 49 | + it 'allows to perform the query' do |
| 50 | + expect(response[:data][:privateField]).to eq(private_message) |
| 51 | + end |
| 52 | + |
| 53 | + context 'when using a SchemaUser' do |
| 54 | + let(:resource) { create(:schema_user, :confirmed) } |
| 55 | + |
| 56 | + it 'allows to perform the query' do |
| 57 | + expect(response[:data][:privateField]).to eq(private_message) |
| 58 | + end |
| 59 | + end |
| 60 | + end |
| 61 | + end |
| 62 | + |
| 63 | + context 'when using an interpreter schema' do |
| 64 | + let(:schema) { InterpreterSchema } |
| 65 | + |
| 66 | + context 'when user is authenticated' do |
| 67 | + let(:resource) { user } |
| 68 | + |
| 69 | + it 'allows to perform the query' do |
| 70 | + expect(response[:data][:privateField]).to eq(private_message) |
| 71 | + end |
| 72 | + end |
| 73 | + end |
| 74 | + end |
| 75 | + |
| 76 | + describe 'user' do |
| 77 | + let(:user_data) { { email: user.email, id: user.id } } |
| 78 | + let(:query) do |
| 79 | + <<-GRAPHQL |
| 80 | + query { |
| 81 | + user(id: #{user.id}) { |
| 82 | + id |
| 83 | + email |
| 84 | + } |
| 85 | + } |
| 86 | + GRAPHQL |
| 87 | + end |
| 88 | + |
| 89 | + context 'when using a regular schema' do |
| 90 | + context 'when user is authenticated' do |
| 91 | + let(:resource) { user } |
| 92 | + |
| 93 | + it 'allows to perform the query' do |
| 94 | + expect(response[:data][:user]).to match(**user_data) |
| 95 | + end |
| 96 | + end |
| 97 | + end |
| 98 | + |
| 99 | + context 'when using an interpreter schema' do |
| 100 | + let(:schema) { InterpreterSchema } |
| 101 | + |
| 102 | + context 'when user is authenticated' do |
| 103 | + let(:resource) { user } |
| 104 | + |
| 105 | + it 'allows to perform the query' do |
| 106 | + expect(response[:data][:user]).to match(**user_data) |
| 107 | + end |
| 108 | + end |
| 109 | + |
| 110 | + context 'when user is not authenticated' do |
| 111 | + # Interpreter schema fields are public unless specified otherwise (plugin setting) |
| 112 | + it 'allows to perform the query' do |
| 113 | + expect(response[:data][:user]).to match(**user_data) |
| 114 | + end |
| 115 | + end |
| 116 | + end |
| 117 | + end |
| 118 | +end |
0 commit comments