Browse Source

Feature: Mobile - Refactored GraphQL test support API.

Martin Gruner 2 years ago
parent
commit
f3567ac293

+ 7 - 8
spec/graphql/gql/mutations/account/avatar/add_spec.rb

@@ -11,23 +11,22 @@ RSpec.describe Gql::Mutations::Account::Avatar::Add, type: :graphql do
     let(:execute_query) { true }
 
     let(:query) do
-      read_graphql_file('apps/mobile/modules/account/avatar/graphql/mutations/add.graphql') +
-        read_graphql_file('shared/graphql/fragments/errors.graphql')
+      gql.read_files('apps/mobile/modules/account/avatar/graphql/mutations/add.graphql', 'shared/graphql/fragments/errors.graphql')
     end
 
     before do
       next if !execute_query
 
-      graphql_execute(query, variables: variables)
+      gql.execute(query, variables: variables)
     end
 
     context 'with valid image' do
       it 'returns the newly created avatar' do
-        expect(graphql_response['data']['accountAvatarAdd']['avatar']).not_to be_nil
+        expect(gql.result.data['avatar']).not_to be_nil
       end
 
       it 'updates the image for the user' do
-        avatar = Gql::ZammadSchema.verified_object_from_id(graphql_response['data']['accountAvatarAdd']['avatar']['id'], type: Avatar)
+        avatar = Gql::ZammadSchema.verified_object_from_id(gql.result.data['avatar']['id'], type: Avatar)
         expect(agent.reload.image).to eq(avatar.store_hash)
       end
 
@@ -35,7 +34,7 @@ RSpec.describe Gql::Mutations::Account::Avatar::Add, type: :graphql do
         let(:execute_query) { false }
 
         it 'increases the amount of records correctly' do
-          expect { graphql_execute(query, variables: variables) }.to change(Store, :count).by(2)
+          expect { gql.execute(query, variables: variables) }.to change(Store, :count).by(2)
         end
       end
     end
@@ -44,7 +43,7 @@ RSpec.describe Gql::Mutations::Account::Avatar::Add, type: :graphql do
       let(:base64_img) { 'invalid image' }
 
       it 'fails with error message' do
-        expect(graphql_response['data']['accountAvatarAdd']['errors'][0]).to include('message' => 'The image is invalid.')
+        expect(gql.result.data['errors'][0]).to include('message' => 'The image is invalid.')
       end
     end
 
@@ -52,7 +51,7 @@ RSpec.describe Gql::Mutations::Account::Avatar::Add, type: :graphql do
       let(:mime_type) { 'image/tiff' }
 
       it 'fails with error message' do
-        expect(graphql_response['data']['accountAvatarAdd']['errors'][0]).to include('message' => 'The MIME type of the image is invalid.')
+        expect(gql.result.data['errors'][0]).to include('message' => 'The MIME type of the image is invalid.')
       end
     end
 

+ 7 - 8
spec/graphql/gql/mutations/account/avatar/delete_spec.rb

@@ -5,24 +5,23 @@ require 'rails_helper'
 RSpec.describe Gql::Mutations::Account::Avatar::Delete, type: :graphql do
   context 'when creating a new avatar for the logged-in user', authenticated_as: :agent do
     let(:agent)         { create(:agent) }
-    let(:variables)     { { id: Gql::ZammadSchema.id_from_object(avatar) } }
+    let(:variables)     { { id: gql.id(avatar) } }
     let(:execute_query) { true }
     let(:avatar)        { create(:avatar, o_id: agent.id) }
 
     let(:query) do
-      read_graphql_file('apps/mobile/modules/account/avatar/graphql/mutations/delete.graphql') +
-        read_graphql_file('shared/graphql/fragments/errors.graphql')
+      gql.read_files('apps/mobile/modules/account/avatar/graphql/mutations/delete.graphql', 'shared/graphql/fragments/errors.graphql')
     end
 
     before do
       next if !execute_query
 
-      graphql_execute(query, variables: variables)
+      gql.execute(query, variables: variables)
     end
 
     context 'with existing avatar' do
       it 'returns success' do
-        expect(graphql_response['data']['accountAvatarDelete']['success']).to be true
+        expect(gql.result.data['success']).to be true
       end
 
       it 'does not find the avatar anymore' do
@@ -34,7 +33,7 @@ RSpec.describe Gql::Mutations::Account::Avatar::Delete, type: :graphql do
       let(:avatar) { create(:avatar, o_id: 1) }
 
       it 'fails with error message' do
-        expect(graphql_response['errors'][0]).to include('message' => 'Avatar could not be found.')
+        expect(gql.result.error_message).to eq('Avatar could not be found.')
       end
     end
 
@@ -42,11 +41,11 @@ RSpec.describe Gql::Mutations::Account::Avatar::Delete, type: :graphql do
       let(:variables) { { id: SecureRandom.random_number(1_000_000) + 123_456 } }
 
       it 'fails with error message' do
-        expect(graphql_response['errors'][0]).to include('message' => "Could not find Avatar #{variables[:id]}")
+        expect(gql.result.error_message).to eq("Could not find Avatar #{variables[:id]}")
       end
 
       it 'fails with error type' do
-        expect(graphql_response['errors'][0]['extensions']).to include({ 'type' => 'ActiveRecord::RecordNotFound' })
+        expect(gql.result.error_type).to eq(ActiveRecord::RecordNotFound)
       end
     end
 

+ 5 - 6
spec/graphql/gql/mutations/account/locale_spec.rb

@@ -7,19 +7,18 @@ RSpec.describe Gql::Mutations::Account::Locale, type: :graphql do
   context 'when updating language of the logged-in user', authenticated_as: :agent do
     let(:agent)        { create(:agent, preferences: { 'locale' => 'de-de' }) }
     let(:query)        do
-      read_graphql_file('apps/mobile/modules/account/graphql/mutations/locale.graphql') +
-        read_graphql_file('shared/graphql/fragments/errors.graphql')
+      gql.read_files('apps/mobile/modules/account/graphql/mutations/locale.graphql', 'shared/graphql/fragments/errors.graphql')
     end
     let(:locale) { 'en-us' }
     let(:variables) { { locale: locale } }
 
     before do
-      graphql_execute(query, variables: variables)
+      gql.execute(query, variables: variables)
     end
 
     context 'with valid locale' do
       it 'returns success' do
-        expect(graphql_response['data']['accountLocale']['success']).to be true
+        expect(gql.result.data['success']).to be true
       end
 
       it 'updates the locale' do
@@ -31,11 +30,11 @@ RSpec.describe Gql::Mutations::Account::Locale, type: :graphql do
       let(:locale) { 'nonexisting-locale' }
 
       it 'fails with error message' do
-        expect(graphql_response['errors'][0]).to include('message' => 'Locale could not be found.')
+        expect(gql.result.error_message).to eq('Locale could not be found.')
       end
 
       it 'fails with error type' do
-        expect(graphql_response['errors'][0]['extensions']).to include({ 'type' => 'ActiveRecord::RecordNotFound' })
+        expect(gql.result.error_type).to eq(ActiveRecord::RecordNotFound)
       end
     end
 

+ 4 - 4
spec/graphql/gql/mutations/form/upload_cache/add_spec.rb

@@ -6,7 +6,7 @@ RSpec.describe Gql::Mutations::Form::UploadCache::Add, type: :graphql do
 
   context 'when uploading files for a form', authenticated_as: :agent do
     let(:agent)        { create(:agent) }
-    let(:query)        { read_graphql_file('shared/components/Form/fields/FieldFile/graphql/mutations/uploadCache/add.graphql') }
+    let(:query)        { gql.read_files('shared/components/Form/fields/FieldFile/graphql/mutations/uploadCache/add.graphql') }
     let(:form_id)      { 12_345 }
     let(:file_name)    { 'my_testfile.pdf' }
     let(:file_type)    { 'application/pdf' }
@@ -26,18 +26,18 @@ RSpec.describe Gql::Mutations::Form::UploadCache::Add, type: :graphql do
 
     let(:expected_response) do
       [{
-        'id'   => Gql::ZammadSchema.id_from_object(UploadCache.new(form_id).attachments.first),
+        'id'   => gql.id(UploadCache.new(form_id).attachments.first),
         'name' => file_name,
         'type' => file_type,
       }]
     end
 
     before do
-      graphql_execute(query, variables: variables)
+      gql.execute(query, variables: variables)
     end
 
     it 'creates Store entry' do
-      expect(graphql_response['data']['formUploadCacheAdd']['uploadedFiles']).to eq(expected_response)
+      expect(gql.result.data['uploadedFiles']).to eq(expected_response)
     end
 
     it_behaves_like 'graphql responds with error if unauthenticated'

+ 6 - 6
spec/graphql/gql/mutations/form/upload_cache/remove_spec.rb

@@ -6,20 +6,20 @@ RSpec.describe Gql::Mutations::Form::UploadCache::Remove, type: :graphql do
 
   context 'when uploading files for a form', authenticated_as: :agent do
     let(:agent)             { create(:agent) }
-    let(:query)             { read_graphql_file('shared/components/Form/fields/FieldFile/graphql/mutations/uploadCache/remove.graphql') }
+    let(:query)             { gql.read_files('shared/components/Form/fields/FieldFile/graphql/mutations/uploadCache/remove.graphql') }
     let(:form_id)           { 12_345 }
     let(:upload_cache_file) { UploadCache.new(form_id).add(filename: file_name, data: file_content, created_by_id: 1) }
     let(:file_name)         { 'my_testfile.pdf' }
     let(:file_type)         { 'application/pdf' }
     let(:file_content)      { 'some test content' }
-    let(:variables)         { { formId: form_id, fileIds: [ Gql::ZammadSchema.id_from_object(upload_cache_file) ] } }
+    let(:variables)         { { formId: form_id, fileIds: [ gql.id(upload_cache_file) ] } }
 
     before do
-      graphql_execute(query, variables: variables)
+      gql.execute(query, variables: variables)
     end
 
     it 'responds with success' do
-      expect(graphql_response['data']['formUploadCacheRemove']).to eq({ 'success' => true })
+      expect(gql.result.data).to eq({ 'success' => true })
     end
 
     it 'deletes upload cache entry' do
@@ -29,11 +29,11 @@ RSpec.describe Gql::Mutations::Form::UploadCache::Remove, type: :graphql do
     context 'when trying to delete a missing file' do
       before do
         # Execute query a second time
-        graphql_execute(query, variables: variables)
+        gql.execute(query, variables: variables)
       end
 
       it 'fails' do
-        expect(graphql_response['errors'][0]['extensions']).to include('type' => 'ActiveRecord::RecordNotFound')
+        expect(gql.result.error_type).to eq(ActiveRecord::RecordNotFound)
       end
     end
 

+ 5 - 5
spec/graphql/gql/queries/account/avatar/active_spec.rb

@@ -5,7 +5,7 @@ require 'rails_helper'
 RSpec.describe Gql::Queries::Account::Avatar::Active, type: :graphql do
   context 'when fetching avatar' do
     let(:query) do
-      read_graphql_file('apps/mobile/modules/account/avatar/graphql/queries/active.graphql')
+      gql.read_files('apps/mobile/modules/account/avatar/graphql/queries/active.graphql')
     end
 
     context 'when authorized', authenticated_as: :agent do
@@ -13,12 +13,12 @@ RSpec.describe Gql::Queries::Account::Avatar::Active, type: :graphql do
       let!(:avatar) { nil }
 
       before do
-        graphql_execute(query)
+        gql.execute(query)
       end
 
       context 'when no avatar is available' do
         it 'returns nil' do
-          expect(graphql_response['data']['accountAvatarActive']).to be_nil
+          expect(gql.result.data).to be_nil
         end
       end
 
@@ -46,14 +46,14 @@ RSpec.describe Gql::Queries::Account::Avatar::Active, type: :graphql do
         end
 
         it 'returns data' do
-          expect(graphql_response['data']['accountAvatarActive']['id']).to eq(Gql::ZammadSchema.id_from_object(avatar))
+          expect(gql.result.data['id']).to eq(gql.id(avatar))
         end
       end
     end
 
     context 'when unauthenticated' do
       before do
-        graphql_execute(query)
+        gql.execute(query)
       end
 
       it_behaves_like 'graphql responds with error if unauthenticated'

+ 3 - 3
spec/graphql/gql/queries/application_build_checksum_spec.rb

@@ -17,14 +17,14 @@ RSpec.describe Gql::Queries::ApplicationBuildChecksum, type: :graphql, authentic
       end
       Digest::MD5.hexdigest(File.read(filename))
     end
-    let(:query) { read_graphql_file('shared/graphql/queries/applicationBuildChecksum.graphql') }
+    let(:query) { gql.read_files('shared/graphql/queries/applicationBuildChecksum.graphql') }
 
     before do
       File.open(filename, 'a') do |file|
         file.write("\n")
       end
 
-      graphql_execute(query)
+      gql.execute(query)
     end
 
     after do
@@ -34,7 +34,7 @@ RSpec.describe Gql::Queries::ApplicationBuildChecksum, type: :graphql, authentic
     end
 
     it 'returns the checksum of the manifest file' do
-      expect(graphql_response['data']['applicationBuildChecksum']).to not_eq(initial_checksum)
+      expect(gql.result.data).to not_eq(initial_checksum)
     end
   end
 end

+ 9 - 9
spec/graphql/gql/queries/application_config_spec.rb

@@ -6,42 +6,42 @@ RSpec.describe Gql::Queries::ApplicationConfig, type: :graphql do
 
   context 'when fetching the application config' do
     let(:agent) { create(:agent) }
-    let(:query) { read_graphql_file('shared/graphql/queries/applicationConfig.graphql') }
+    let(:query) { gql.read_files('shared/graphql/queries/applicationConfig.graphql') }
 
     before do
-      graphql_execute(query)
+      gql.execute(query)
     end
 
     context 'with authenticated session', authenticated_as: :agent do
       it 'returns public data' do
-        expect(graphql_response['data']['applicationConfig']).to include({ 'key' => 'product_name', 'value' => Setting.get('product_name') })
+        expect(gql.result.data).to include({ 'key' => 'product_name', 'value' => Setting.get('product_name') })
       end
 
       it 'returns internal data' do
-        expect(graphql_response['data']['applicationConfig']).to include({ 'key' => 'system_id', 'value' => Setting.get('system_id') })
+        expect(gql.result.data).to include({ 'key' => 'system_id', 'value' => Setting.get('system_id') })
       end
 
       it 'hides non-frontend data' do
-        expect(graphql_response['data']['applicationConfig'].select { |s| s['key'].eql?('storage_provider') }).to be_empty
+        expect(gql.result.data.select { |s| s['key'].eql?('storage_provider') }).to be_empty
       end
     end
 
     context 'without authenticated session', authenticated_as: false do
       it 'returns public data' do
-        expect(graphql_response['data']['applicationConfig']).to include({ 'key' => 'product_name', 'value' => Setting.get('product_name') })
+        expect(gql.result.data).to include({ 'key' => 'product_name', 'value' => Setting.get('product_name') })
       end
 
       it 'hides internal data' do
-        expect(graphql_response['data']['applicationConfig'].select { |s| s['key'].eql?('system_id') }).to be_empty
+        expect(gql.result.data.select { |s| s['key'].eql?('system_id') }).to be_empty
       end
       # Not sure why, but that's how it is implemented...
 
       it 'hides all false values' do
-        expect(graphql_response['data']['applicationConfig'].reject { |s| s['value'] }).to be_empty
+        expect(gql.result.data.reject { |s| s['value'] }).to be_empty
       end
 
       it 'hides non-frontend data' do
-        expect(graphql_response['data']['applicationConfig'].select { |s| s['key'].eql?('storage_provider') }).to be_empty
+        expect(gql.result.data.select { |s| s['key'].eql?('storage_provider') }).to be_empty
       end
     end
   end

+ 7 - 11
spec/graphql/gql/queries/autocomplete_search/user_spec.rb

@@ -7,18 +7,18 @@ RSpec.describe Gql::Queries::AutocompleteSearch::User, type: :graphql, authentic
   context 'when searching for users' do
     let(:agent)        { create(:agent) }
     let(:users)        { create_list(:agent, 3, lastname: 'AutocompleteSearch') }
-    let(:query)        { read_graphql_file('shared/graphql/queries/autocompleteSearch/user.graphql') }
+    let(:query)        { gql.read_files('shared/graphql/queries/autocompleteSearch/user.graphql') }
     let(:variables)    { { query: query_string, limit: limit } }
     let(:query_string) { users.last.lastname }
     let(:limit)        { nil }
 
     before do
-      graphql_execute(query, variables: variables)
+      gql.execute(query, variables: variables)
     end
 
     context 'without limit' do
       it 'finds all users' do
-        expect(graphql_response['data']['autocompleteSearchUser'].length).to eq(users.length)
+        expect(gql.result.data.length).to eq(users.length)
       end
     end
 
@@ -26,14 +26,14 @@ RSpec.describe Gql::Queries::AutocompleteSearch::User, type: :graphql, authentic
       let(:limit) { 1 }
 
       it 'respects the limit' do
-        expect(graphql_response['data']['autocompleteSearchUser'].length).to eq(limit)
+        expect(gql.result.data.length).to eq(limit)
       end
     end
 
     context 'with exact search' do
       let(:first_user_payload) do
         {
-          'value'              => Gql::ZammadSchema.id_from_object(users.first),
+          'value'              => gql.id(users.first),
           'label'              => users.first.fullname,
           'labelPlaceholder'   => nil,
           'heading'            => nil,
@@ -45,7 +45,7 @@ RSpec.describe Gql::Queries::AutocompleteSearch::User, type: :graphql, authentic
       let(:query_string) { users.first.login }
 
       it 'has data' do
-        expect(graphql_response['data']['autocompleteSearchUser']).to eq([first_user_payload])
+        expect(gql.result.data).to eq([first_user_payload])
       end
     end
 
@@ -53,11 +53,7 @@ RSpec.describe Gql::Queries::AutocompleteSearch::User, type: :graphql, authentic
       let(:query_string) { '   ' }
 
       it 'returns nothing' do
-        expect(graphql_response['data']['autocompleteSearchUser'].length).to eq(0)
-      end
-
-      it 'has no error' do
-        expect(graphql_response['data']['errors']).to be_nil
+        expect(gql.result.data.length).to eq(0)
       end
     end
 

+ 6 - 7
spec/graphql/gql/queries/current_user_spec.rb

@@ -8,30 +8,29 @@ RSpec.describe Gql::Queries::CurrentUser, type: :graphql do
     let(:organization) { create(:organization) }
     let(:agent)        { create(:agent, department: 'TestDepartment', organization: organization) }
     let(:query) do
-      read_graphql_file('shared/graphql/queries/currentUser.graphql') +
-        read_graphql_file('shared/graphql/fragments/objectAttributeValues.graphql')
+      gql.read_files('shared/graphql/queries/currentUser.graphql', 'shared/graphql/fragments/objectAttributeValues.graphql')
     end
 
     before do
-      graphql_execute(query)
+      gql.execute(query)
     end
 
     context 'with authenticated session', authenticated_as: :agent do
       it 'has data' do
-        expect(graphql_response['data']['currentUser']).to include('fullname' => agent.fullname)
+        expect(gql.result.data).to include('fullname' => agent.fullname)
       end
 
       it 'has objectAttributeValue data for User' do
-        oas = graphql_response['data']['currentUser']['objectAttributeValues']
+        oas = gql.result.data['objectAttributeValues']
         expect(oas.find { |oa| oa['attribute']['name'].eql?('department') }['value']).to eq('TestDepartment')
       end
 
       it 'has data for Organization' do
-        expect(graphql_response['data']['currentUser']['organization']).to include('name' => organization.name)
+        expect(gql.result.data['organization']).to include('name' => organization.name)
       end
 
       it 'has permission data' do
-        expect(graphql_response['data']['currentUser']['permissions']['names']).to eq(agent.permissions_with_child_names)
+        expect(gql.result.data['permissions']['names']).to eq(agent.permissions_with_child_names)
       end
     end
 

Some files were not shown because too many files changed in this diff