summaryrefslogtreecommitdiff
path: root/spec/workers/gitlab/github_gists_import/import_gist_worker_spec.rb
blob: 1c24cdcccae996cc77d5130e91dc1de4ffbb883e (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::GithubGistsImport::ImportGistWorker, feature_category: :importers do
  subject { described_class.new }

  let_it_be(:user) { create(:user) }
  let(:token) { 'token' }
  let(:gist_hash) do
    {
      id: '055b70',
      git_pull_url: 'https://gist.github.com/foo/bar.git',
      files: {
        'random.txt': {
          filename: 'random.txt',
          type: 'text/plain',
          language: 'Text',
          raw_url: 'https://gist.githubusercontent.com/user_name/055b70/raw/66a7be0d/random.txt',
          size: 166903
        }
      },
      is_public: false,
      created_at: '2022-09-06T11:38:18Z',
      updated_at: '2022-09-06T11:38:18Z',
      description: 'random text'
    }
  end

  let(:importer) { instance_double('Gitlab::GithubGistsImport::Importer::GistImporter') }
  let(:importer_result) { instance_double('ServiceResponse', success?: true) }
  let(:gist_object) do
    instance_double('Gitlab::GithubGistsImport::Representation::Gist',
      gist_hash.merge(github_identifiers: { id: '055b70' }, truncated_title: 'random text', visibility_level: 0))
  end

  let(:log_attributes) do
    {
      'user_id' => user.id,
      'github_identifiers' => { 'id': gist_object.id },
      'class' => 'Gitlab::GithubGistsImport::ImportGistWorker',
      'correlation_id' => 'new-correlation-id',
      'jid' => nil,
      'job_status' => 'running',
      'queue' => 'github_gists_importer:github_gists_import_import_gist'
    }
  end

  describe '#perform' do
    before do
      allow(Gitlab::GithubGistsImport::Representation::Gist)
        .to receive(:from_json_hash)
        .with(gist_hash)
        .and_return(gist_object)

      allow(Gitlab::GithubGistsImport::Importer::GistImporter)
        .to receive(:new)
        .with(gist_object, user.id)
        .and_return(importer)

      allow(Gitlab::ApplicationContext).to receive(:current).and_return('correlation_id' => 'new-correlation-id')
      allow(described_class).to receive(:queue).and_return('github_gists_importer:github_gists_import_import_gist')
    end

    context 'when success' do
      it 'imports gist' do
        expect(Gitlab::GithubImport::Logger)
          .to receive(:info)
          .with(log_attributes.merge('message' => 'start importer'))
        expect(importer).to receive(:execute).and_return(importer_result)
        expect(Gitlab::JobWaiter).to receive(:notify).with('some_key', subject.jid)
        expect(Gitlab::GithubImport::Logger)
          .to receive(:info)
          .with(log_attributes.merge('message' => 'importer finished'))

        subject.perform(user.id, gist_hash, 'some_key')
      end
    end

    context 'when importer raised an error' do
      it 'raises an error' do
        exception = StandardError.new('_some_error_')

        expect(importer).to receive(:execute).and_raise(exception)
        expect(Gitlab::GithubImport::Logger)
          .to receive(:error)
          .with(log_attributes.merge('message' => 'importer failed', 'error.message' => '_some_error_'))
        expect(Gitlab::ErrorTracking).to receive(:track_exception)

        expect { subject.perform(user.id, gist_hash, 'some_key') }.to raise_error(StandardError)
      end
    end
  end
end