BigW Consortium Gitlab

importer_spec.rb 3.42 KB
Newer Older
1 2
require 'spec_helper'

3
describe Gitlab::BitbucketImport::Importer do
4 5
  include ImportSpecHelper

6
  before do
7
    stub_omniauth_provider('bitbucket')
8 9 10 11 12 13 14 15 16 17 18 19 20
  end

  let(:statuses) do
    [
      "open",
      "resolved",
      "on hold",
      "invalid",
      "duplicate",
      "wontfix",
      "closed"  # undocumented status
    ]
  end
Valery Sizov committed
21

22 23 24 25 26
  let(:sample_issues_statuses) do
    issues = []

    statuses.map.with_index do |status, index|
      issues << {
27 28
        id: index,
        state: status,
29
        title: "Issue #{index}",
Valery Sizov committed
30
        kind: 'bug',
31 32 33 34 35
        content: {
            raw: "Some content to issue #{index}",
            markup: "markdown",
            html: "Some content to issue #{index}"
        }
36 37 38 39 40 41 42
      }
    end

    issues
  end

  let(:project_identifier) { 'namespace/repo' }
Valery Sizov committed
43

44 45
  let(:data) do
    {
46
      'bb_session' => {
47 48
        'bitbucket_token' => "123456",
        'bitbucket_refresh_token' => "secret"
49 50 51
      }
    }
  end
Valery Sizov committed
52

53 54
  let(:project) do
    create(
55
      :project,
56
      import_source: project_identifier,
57
      import_data_attributes: { credentials: data }
58 59
    )
  end
Valery Sizov committed
60

61
  let(:importer) { described_class.new(project) }
Valery Sizov committed
62

63 64 65
  let(:issues_statuses_sample_data) do
    {
      count: sample_issues_statuses.count,
66
      values: sample_issues_statuses
67 68 69 70 71
    }
  end

  context 'issues statuses' do
    before do
72 73 74
      # HACK: Bitbucket::Representation.const_get('Issue') seems to return ::Issue without this
      Bitbucket::Representation::Issue.new({})

75 76
      stub_request(
        :get,
77 78
        "https://api.bitbucket.org/2.0/repositories/#{project_identifier}"
      ).to_return(status: 200,
79
                  headers: { "Content-Type" => "application/json" },
80
                  body: { has_issues: true, full_name: project_identifier }.to_json)
81 82 83

      stub_request(
        :get,
84 85
        "https://api.bitbucket.org/2.0/repositories/#{project_identifier}/issues?pagelen=50&sort=created_on"
      ).to_return(status: 200,
86
                  headers: { "Content-Type" => "application/json" },
87
                  body: issues_statuses_sample_data.to_json)
88

89 90 91
      stub_request(:get, "https://api.bitbucket.org/2.0/repositories/namespace/repo?pagelen=50&sort=created_on")
        .with(headers: { 'Accept' => '*/*', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Authorization' => 'Bearer', 'User-Agent' => 'Faraday v0.9.2' })
        .to_return(status: 200, body: "", headers: {})
Valery Sizov committed
92

93 94 95
      sample_issues_statuses.each_with_index do |issue, index|
        stub_request(
          :get,
96
          "https://api.bitbucket.org/2.0/repositories/#{project_identifier}/issues/#{issue[:id]}/comments?pagelen=50&sort=created_on"
97 98
        ).to_return(
          status: 200,
99
          headers: { "Content-Type" => "application/json" },
100
          body: { author_info: { username: "username" }, utc_created_on: index }.to_json
101 102
        )
      end
103 104

      stub_request(
105 106
        :get,
        "https://api.bitbucket.org/2.0/repositories/#{project_identifier}/pullrequests?pagelen=50&sort=created_on&state=ALL"
107
      ).to_return(status: 200,
108
                  headers: { "Content-Type" => "application/json" },
109
                  body: {}.to_json)
110 111
    end

112
    it 'maps statuses to open or closed' do
113 114 115 116 117
      importer.execute

      expect(project.issues.where(state: "closed").size).to eq(5)
      expect(project.issues.where(state: "opened").size).to eq(2)
    end
118 119 120 121 122

    it 'calls import_wiki' do
      expect(importer).to receive(:import_wiki)
      importer.execute
    end
123 124
  end
end