summaryrefslogtreecommitdiff
path: root/spec/services/import/github/gists_import_service_spec.rb
blob: c5d73e6479d3d37394f7e4a267039b96f96e9b1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Import::Github::GistsImportService, feature_category: :importer do
  subject(:import) { described_class.new(user, params) }

  let_it_be(:user) { create(:user) }
  let(:params) { { github_access_token: 'token' } }
  let(:import_status) { instance_double('Gitlab::GithubGistsImport::Status') }

  describe '#execute', :aggregate_failures do
    before do
      allow(Gitlab::GithubGistsImport::Status).to receive(:new).and_return(import_status)
    end

    context 'when import in progress' do
      let(:expected_result) do
        {
          status: :error,
          http_status: 422,
          message: 'Import already in progress'
        }
      end

      it 'returns error' do
        expect(import_status).to receive(:started?).and_return(true)
        expect(import.execute).to eq(expected_result)
      end
    end

    context 'when import was not started' do
      it 'returns success' do
        encrypted_token = Gitlab::CryptoHelper.aes256_gcm_encrypt(params[:github_access_token])
        expect(import_status).to receive(:started?).and_return(false)
        expect(Gitlab::CryptoHelper)
          .to receive(:aes256_gcm_encrypt).with(params[:github_access_token])
          .and_return(encrypted_token)
        expect(Gitlab::GithubGistsImport::StartImportWorker)
          .to receive(:perform_async).with(user.id, encrypted_token)
        expect(import_status).to receive(:start!)

        expect(import.execute).to eq({ status: :success })
      end
    end
  end
end