importer_spec.rb 2.2 KB
Newer Older
1 2 3
require 'spec_helper'

describe Gitlab::BitbucketImport::Importer, lib: true 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 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
  end

  let(:statuses) do
    [
      "open",
      "resolved",
      "on hold",
      "invalid",
      "duplicate",
      "wontfix",
      "closed"  # undocumented status
    ]
  end
  let(:sample_issues_statuses) do
    issues = []

    statuses.map.with_index do |status, index|
      issues << {
        local_id: index,
        status: status,
        title: "Issue #{index}",
        content: "Some content to issue #{index}"
      }
    end

    issues
  end

  let(:project_identifier) { 'namespace/repo' }
  let(:data) do
    {
39 40 41
      'bb_session' => {
        'bitbucket_access_token' => "123456",
        'bitbucket_access_token_secret' => "secret"
42 43 44 45 46 47 48
      }
    }
  end
  let(:project) do
    create(
      :project,
      import_source: project_identifier,
49
      import_data: ProjectImportData.new(credentials: data)
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
    )
  end
  let(:importer) { Gitlab::BitbucketImport::Importer.new(project) }
  let(:issues_statuses_sample_data) do
    {
      count: sample_issues_statuses.count,
      issues: sample_issues_statuses
    }
  end

  context 'issues statuses' do
    before do
      stub_request(
        :get,
        "https://bitbucket.org/api/1.0/repositories/#{project_identifier}"
      ).to_return(status: 200, body: { has_issues: true }.to_json)

      stub_request(
        :get,
        "https://bitbucket.org/api/1.0/repositories/#{project_identifier}/issues?limit=50&sort=utc_created_on&start=0"
      ).to_return(status: 200, body: issues_statuses_sample_data.to_json)

      sample_issues_statuses.each_with_index do |issue, index|
        stub_request(
          :get,
          "https://bitbucket.org/api/1.0/repositories/#{project_identifier}/issues/#{issue[:local_id]}/comments"
        ).to_return(
          status: 200,
          body: [{ author_info: { username: "username" }, utc_created_on: index }].to_json
        )
      end
    end

    it 'map statuses to open or closed' do
      importer.execute

      expect(project.issues.where(state: "closed").size).to eq(5)
      expect(project.issues.where(state: "opened").size).to eq(2)
    end
  end
end