Browse Source

Maintenance: Refactor Zendesk import tests

Mantas Masalskis 3 years ago
parent
commit
6b8655856e

+ 0 - 1
.gitlab/ci/browser-integration.yml

@@ -5,7 +5,6 @@ include:
   - local: '/.gitlab/ci/browser-integration/capybara_ff.yml'
   - local: '/.gitlab/ci/browser-integration/idoit_chrome.yml'
   - local: '/.gitlab/ci/browser-integration/otrs_chrome.yml'
-  - local: '/.gitlab/ci/browser-integration/zendesk_chrome.yml'
 
 .template_browser-integration_capybara: &template_browser-integration_capybara
   stage: browser-integration

+ 0 - 5
.gitlab/ci/browser-integration/zendesk_chrome.yml

@@ -1,5 +0,0 @@
-zendesk_chrome:
-  extends:
-    - .template_browser-integration_import_chrome
-  variables:
-    TEST_FILE: "test/integration/zendesk_import_browser_test.rb"

+ 0 - 1
.gitlab/ci/integration.yml

@@ -6,7 +6,6 @@ include:
   - local: '/.gitlab/ci/integration/otrs.yml'
   - local: '/.gitlab/ci/integration/slack.yml'
   - local: '/.gitlab/ci/integration/user_agent.yml'
-  - local: '/.gitlab/ci/integration/zendesk.yml'
 
 .template_integration: &template_integration
   extends:

+ 0 - 6
.gitlab/ci/integration/zendesk.yml

@@ -1,6 +0,0 @@
-zendesk:
-  extends:
-    - .template_integration
-  script:
-    - bundle exec rake zammad:db:unseeded
-    - bundle exec rails test test/integration/zendesk_import_test.rb

+ 11 - 0
.gitlab/ci/rspec.yml

@@ -20,6 +20,10 @@ include:
     - bundle exec rake zammad:db:init
     - bundle exec rspec --exclude-pattern "spec/{system,integration}/**/*_spec.rb" -t ~searchindex -t db_strategy:reset
 
+# RSpec 3rd party integration tests are independent of the database
+# therefore we need only one job and choose a random database for each run
+# Optionally pass INTEGRATION_TESTS=enabled to run them in private- branch
+
 rspec:integration:
   stage: test
   extends:
@@ -28,6 +32,13 @@ rspec:integration:
     - .rules_integration_manual_start
   variables:
     RAILS_ENV: "test"
+  rules:
+    - if: $CI_MERGE_REQUEST_ID
+      when: never
+    - if: '$CI_COMMIT_BRANCH =~ /^private/ && $INTEGRATION_TESTS != "enabled"'
+      when: manual
+      allow_failure: true
+    - when: always
   script:
     - bundle exec rake zammad:db:init
     - bundle exec rspec -t type:integration

+ 1 - 0
lib/sequencer/unit/import/zendesk/user/mapping.rb

@@ -15,6 +15,7 @@ class Sequencer
                 {
                   login:           login,
                   firstname:       resource.name,
+                  lastname:        '', # makes sure name guessing is triggered for updating existing users. Which happens when importing a big list of users due to overlapping pagination.
                   email:           resource.email,
                   phone:           resource.phone,
                   password:        password,

+ 71 - 0
spec/integration/zendesk_spec.rb

@@ -0,0 +1,71 @@
+# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
+
+require 'rails_helper'
+
+RSpec.describe 'Zendesk import', type: :integration, use_vcr: true, db_strategy: :reset, required_envs: %w[IMPORT_ZENDESK_ENDPOINT IMPORT_ZENDESK_ENDPOINT_KEY IMPORT_ZENDESK_ENDPOINT_USERNAME] do # rubocop:disable RSpec/DescribeClass
+  let(:job) { ImportJob.last }
+
+  before do
+    Setting.set('import_zendesk_endpoint', ENV['IMPORT_ZENDESK_ENDPOINT'])
+    Setting.set('import_zendesk_endpoint_key', ENV['IMPORT_ZENDESK_ENDPOINT_KEY'])
+    Setting.set('import_zendesk_endpoint_username', ENV['IMPORT_ZENDESK_ENDPOINT_USERNAME'])
+    Setting.set('import_mode', true)
+    Setting.set('system_init_done', false)
+
+    VCR.use_cassette 'zendesk_import' do
+      ImportJob.create(name: 'Import::Zendesk').start
+    end
+  end
+
+  context 'when performing the full Zendesk import' do
+    let(:job) { ImportJob.last }
+    let(:expected_stats) do
+      {
+        'Groups'        => {
+          'skipped'     => 0,
+          'created'     => 2,
+          'updated'     => 0,
+          'unchanged'   => 0,
+          'failed'      => 0,
+          'deactivated' => 0,
+          'sum'         => 2,
+          'total'       => 2,
+        },
+        'Users'         => {
+          'skipped'     => 0,
+          'created'     => 142,
+          'updated'     => 1,
+          'unchanged'   => 0,
+          'failed'      => 0,
+          'deactivated' => 0,
+          'sum'         => 143,
+          'total'       => 143,
+        },
+        'Organizations' => {
+          'skipped'     => 0,
+          'created'     => 1,
+          'updated'     => 0,
+          'unchanged'   => 1,
+          'failed'      => 0,
+          'deactivated' => 0,
+          'sum'         => 2,
+          'total'       => 2,
+        },
+        'Tickets'       => {
+          'skipped'     => 1,
+          'created'     => 142,
+          'updated'     => 2,
+          'unchanged'   => 0,
+          'failed'      => 0,
+          'deactivated' => 0,
+          'sum'         => 145,
+          'total'       => 145,
+        },
+      }
+    end
+
+    it 'imports the correct number of expected objects' do
+      expect(job.result).to eq expected_stats
+    end
+  end
+end

+ 63 - 0
spec/lib/sequencer/sequence/import/zendesk/group_spec.rb

@@ -0,0 +1,63 @@
+# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
+
+require 'rails_helper'
+require 'zendesk_api'
+
+RSpec.describe ::Sequencer::Sequence::Import::Zendesk::Group, sequencer: :sequence do
+
+  context 'when importing groups from Zendesk' do
+
+    let(:base_resource) do
+      ZendeskAPI::Group.new(
+        nil,
+        {
+          'id'          => 24_165_105,
+          'name'        => 'Additional Group',
+          'description' => '',
+          'default'     => false,
+          'created_at'  => '2015-12-04 13:11:59 UTC',
+          'updated_at'  => '2015-12-04 13:11:59 UTC'
+        }
+      )
+    end
+
+    let(:process_payload) do
+      {
+        import_job: build_stubbed(:import_job, name: 'Import::Zendesk', payload: {}),
+        dry_run:    false,
+        resource:   resource,
+        field_map:  {},
+      }
+    end
+
+    context 'with active group' do
+
+      let(:resource) do
+        base_resource.merge('deleted' => false)
+      end
+
+      it 'adds groups' do # rubocop:disable RSpec/MultipleExpectations
+        expect { process(process_payload) }.to change(Group, :count).by(1)
+        expect(Group.last).to have_attributes(
+          name:   'Additional Group',
+          active: true,
+        )
+      end
+    end
+
+    context 'with inactive group' do
+
+      let(:resource) do
+        base_resource.merge('deleted' => true)
+      end
+
+      it 'adds groups' do # rubocop:disable RSpec/MultipleExpectations
+        expect { process(process_payload) }.to change(Group, :count).by(1)
+        expect(Group.last).to have_attributes(
+          name:   'Additional Group',
+          active: false,
+        )
+      end
+    end
+  end
+end

+ 44 - 0
spec/lib/sequencer/sequence/import/zendesk/organization_field_spec.rb

@@ -0,0 +1,44 @@
+# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
+
+require 'rails_helper'
+require 'zendesk_api'
+
+RSpec.describe ::Sequencer::Sequence::Import::Zendesk::OrganizationField, sequencer: :sequence do
+
+  context 'when trying to import organization fields from Zendesk', db_strategy: :reset do
+
+    let(:process_payload) do
+      {
+        import_job: build_stubbed(:import_job, name: 'Import::Zendesk', payload: {}),
+        dry_run:    false,
+        resource:   resource,
+        field_map:  {},
+      }
+    end
+
+    let(:resource) do
+      ZendeskAPI::OrganizationField.new(
+        nil,
+        {
+          'id'                    => 207_489,
+          'type'                  => 'text',
+          'key'                   => 'api_key',
+          'title'                 => 'API Key',
+          'description'           => 'Der API Key für externe Zugriffe.',
+          'raw_title'             => 'API Key',
+          'raw_description'       => 'Der API Key für externe Zugriffe.',
+          'position'              => 0,
+          'active'                => true,
+          'system'                => false,
+          'regexp_for_validation' => nil,
+          'created_at'            => '2015-12-04 11:24:08 UTC',
+          'updated_at'            => '2015-12-04 11:24:08 UTC'
+        }
+      )
+    end
+
+    it 'adds custom fields' do
+      expect { process(process_payload) }.to change(Organization, :column_names).by(['api_key'])
+    end
+  end
+end

+ 78 - 0
spec/lib/sequencer/sequence/import/zendesk/organization_spec.rb

@@ -0,0 +1,78 @@
+# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
+
+require 'rails_helper'
+require 'zendesk_api'
+
+RSpec.describe ::Sequencer::Sequence::Import::Zendesk::Organization, sequencer: :sequence, db_strategy: :reset do
+
+  context 'when importing organizations from Zendesk' do
+
+    let(:resource) do
+      ZendeskAPI::Organization.new(
+        nil,
+        {
+          'id'                  => 154_755_561,
+          'name'                => 'Test Foundation',
+          'shared_tickets'      => false,
+          'shared_comments'     => false,
+          'external_id'         => nil,
+          'created_at'          => '2015-07-19 22:41:40 UTC',
+          'updated_at'          => '2016-05-19 12:24:21 UTC',
+          'domain_names'        => [],
+          'details'             => '',
+          'notes'               => '',
+          'group_id'            => nil,
+          'tags'                => ['b'],
+          'organization_fields' => {
+            'api_key'         => 'my api öäüß',
+            'custom_dropdown' => 'b'
+          },
+          'deleted_at'          => nil
+        }
+      )
+    end
+
+    let(:field_map) do
+      {
+        'Organization' => {
+          'api_key'         => 'api_key',
+          'custom_dropdown' => 'custom_dropdown',
+        }
+      }
+    end
+
+    let(:process_payload) do
+      {
+        import_job: build_stubbed(:import_job, name: 'Import::Zendesk', payload: {}),
+        dry_run:    false,
+        resource:   resource,
+        field_map:  field_map,
+      }
+    end
+
+    let(:imported_organization) do
+      {
+        name:            'Test Foundation',
+        note:            nil,
+        domain:          '',
+        api_key:         'my api öäüß',
+        custom_dropdown: 'b'
+      }
+    end
+
+    before do
+      create :object_manager_attribute_select, object_name: 'Organization', name: 'custom_dropdown'
+      create :object_manager_attribute_text, object_name: 'Organization', name: 'api_key'
+      ObjectManager::Attribute.migration_execute
+    end
+
+    it 'increased organization count' do
+      expect { process(process_payload) }.to change(Organization, :count).by(1)
+    end
+
+    it 'adds correct organization data' do
+      process(process_payload)
+      expect(Organization.last).to have_attributes(imported_organization)
+    end
+  end
+end

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