BigW Consortium Gitlab

issue_formatter_spec.rb 5.84 KB
Newer Older
1 2
require 'spec_helper'

3
describe Gitlab::GithubImport::IssueFormatter do
4
  let(:client) { double }
5
  let!(:project) { create(:project, namespace: create(:namespace, path: 'octocat')) }
6
  let(:octocat) { double(id: 123456, login: 'octocat', email: 'octocat@example.com') }
7 8 9 10 11 12
  let(:created_at) { DateTime.strptime('2011-01-26T19:01:12Z') }
  let(:updated_at) { DateTime.strptime('2011-01-27T19:01:12Z') }

  let(:base_data) do
    {
      number: 1347,
13
      milestone: nil,
14 15 16 17 18 19 20 21 22 23 24 25 26
      state: 'open',
      title: 'Found a bug',
      body: "I'm having a problem with this.",
      assignee: nil,
      user: octocat,
      comments: 0,
      pull_request: nil,
      created_at: created_at,
      updated_at: updated_at,
      closed_at: nil
    }
  end

27 28 29 30 31
  subject(:issue) { described_class.new(project, raw_data, client) }

  before do
    allow(client).to receive(:user).and_return(octocat)
  end
32

33
  shared_examples 'Gitlab::GithubImport::IssueFormatter#attributes' do
34
    context 'when issue is open' do
35
      let(:raw_data) { double(base_data.merge(state: 'open')) }
36 37 38

      it 'returns formatted attributes' do
        expected = {
39
          iid: 1347,
40
          project: project,
41
          milestone: nil,
42 43 44 45
          title: 'Found a bug',
          description: "*Created by: octocat*\n\nI'm having a problem with this.",
          state: 'opened',
          author_id: project.creator_id,
46
          assignee_ids: [],
47 48 49 50 51 52 53 54 55
          created_at: created_at,
          updated_at: updated_at
        }

        expect(issue.attributes).to eq(expected)
      end
    end

    context 'when issue is closed' do
56
      let(:raw_data) { double(base_data.merge(state: 'closed')) }
57 58 59

      it 'returns formatted attributes' do
        expected = {
60
          iid: 1347,
61
          project: project,
62
          milestone: nil,
63 64 65 66
          title: 'Found a bug',
          description: "*Created by: octocat*\n\nI'm having a problem with this.",
          state: 'closed',
          author_id: project.creator_id,
67
          assignee_ids: [],
68
          created_at: created_at,
69
          updated_at: updated_at
70 71 72 73 74 75 76
        }

        expect(issue.attributes).to eq(expected)
      end
    end

    context 'when it is assigned to someone' do
77
      let(:raw_data) { double(base_data.merge(assignee: octocat)) }
78 79

      it 'returns nil as assignee_id when is not a GitLab user' do
80
        expect(issue.attributes.fetch(:assignee_ids)).to be_empty
81 82
      end

83
      it 'returns GitLab user id associated with GitHub id as assignee_id' do
84 85
        gl_user = create(:omniauth_user, extern_uid: octocat.id, provider: 'github')

86
        expect(issue.attributes.fetch(:assignee_ids)).to eq [gl_user.id]
87
      end
88 89 90 91

      it 'returns GitLab user id associated with GitHub email as assignee_id' do
        gl_user = create(:user, email: octocat.email)

92
        expect(issue.attributes.fetch(:assignee_ids)).to eq [gl_user.id]
93
      end
94 95
    end

96
    context 'when it has a milestone' do
97
      let(:milestone) { double(id: 42, number: 42) }
98
      let(:raw_data) { double(base_data.merge(milestone: milestone)) }
99 100 101 102 103 104

      it 'returns nil when milestone does not exist' do
        expect(issue.attributes.fetch(:milestone)).to be_nil
      end

      it 'returns milestone when it exists' do
105
        milestone = create(:milestone, project: project, iid: 42)
106 107 108 109 110

        expect(issue.attributes.fetch(:milestone)).to eq milestone
      end
    end

111
    context 'when author is a GitLab user' do
112
      let(:raw_data) { double(base_data.merge(user: octocat)) }
113

114
      it 'returns project creator_id as author_id when is not a GitLab user' do
115 116 117
        expect(issue.attributes.fetch(:author_id)).to eq project.creator_id
      end

118
      it 'returns GitLab user id associated with GitHub id as author_id' do
119 120 121 122
        gl_user = create(:omniauth_user, extern_uid: octocat.id, provider: 'github')

        expect(issue.attributes.fetch(:author_id)).to eq gl_user.id
      end
123

124 125 126 127 128 129
      it 'returns GitLab user id associated with GitHub email as author_id' do
        gl_user = create(:user, email: octocat.email)

        expect(issue.attributes.fetch(:author_id)).to eq gl_user.id
      end

130 131 132 133 134
      it 'returns description without created at tag line' do
        create(:omniauth_user, extern_uid: octocat.id, provider: 'github')

        expect(issue.attributes.fetch(:description)).to eq("I'm having a problem with this.")
      end
135 136 137
    end
  end

138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
  shared_examples 'Gitlab::GithubImport::IssueFormatter#number' do
    let(:raw_data) { double(base_data.merge(number: 1347)) }

    it 'returns issue number' do
      expect(issue.number).to eq 1347
    end
  end

  context 'when importing a GitHub project' do
    it_behaves_like 'Gitlab::GithubImport::IssueFormatter#attributes'
    it_behaves_like 'Gitlab::GithubImport::IssueFormatter#number'
  end

  context 'when importing a Gitea project' do
    before do
      project.update(import_type: 'gitea')
    end

    it_behaves_like 'Gitlab::GithubImport::IssueFormatter#attributes'
    it_behaves_like 'Gitlab::GithubImport::IssueFormatter#number'
  end

160 161
  describe '#has_comments?' do
    context 'when number of comments is greater than zero' do
162
      let(:raw_data) { double(base_data.merge(comments: 1)) }
163 164 165 166 167 168 169

      it 'returns true' do
        expect(issue.has_comments?).to eq true
      end
    end

    context 'when number of comments is equal to zero' do
170
      let(:raw_data) { double(base_data.merge(comments: 0)) }
171 172 173 174 175 176 177

      it 'returns false' do
        expect(issue.has_comments?).to eq false
      end
    end
  end

178
  describe '#pull_request?' do
179
    context 'when mention a pull request' do
180
      let(:raw_data) { double(base_data.merge(pull_request: double)) }
181

182 183
      it 'returns true' do
        expect(issue.pull_request?).to eq true
184 185 186 187
      end
    end

    context 'when does not mention a pull request' do
188
      let(:raw_data) { double(base_data.merge(pull_request: nil)) }
189

190 191
      it 'returns false' do
        expect(issue.pull_request?).to eq false
192 193 194 195
      end
    end
  end
end