summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/danger/roulette_spec.rb
blob: 121c5d8ecd9ae572bc81d9fa8d9d09be4322a3fa (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# frozen_string_literal: true

require 'fast_spec_helper'
require 'webmock/rspec'

require 'gitlab/danger/roulette'

describe Gitlab::Danger::Roulette do
  let(:teammate_json) do
    <<~JSON
    [
      {
        "username": "in-gitlab-ce",
        "name": "CE maintainer",
        "projects":{ "gitlab-ce": "maintainer backend" }
      },
      {
        "username": "in-gitlab-ee",
        "name": "EE reviewer",
        "projects":{ "gitlab-ee": "reviewer frontend" }
      }
    ]
    JSON
  end

  let(:ce_teammate_matcher) do
    satisfy do |teammate|
      teammate.username == 'in-gitlab-ce' &&
        teammate.name == 'CE maintainer' &&
        teammate.projects == { 'gitlab-ce' => 'maintainer backend' }
    end
  end

  let(:ee_teammate_matcher) do
    satisfy do |teammate|
      teammate.username == 'in-gitlab-ee' &&
        teammate.name == 'EE reviewer' &&
        teammate.projects == { 'gitlab-ee' => 'reviewer frontend' }
    end
  end

  subject(:roulette) { Object.new.extend(described_class) }

  describe '#team' do
    subject(:team) { roulette.team }

    context 'HTTP failure' do
      before do
        WebMock
          .stub_request(:get, described_class::ROULETTE_DATA_URL)
          .to_return(status: 404)
      end

      it 'raises a pretty error' do
        expect { team }.to raise_error(/Failed to read/)
      end
    end

    context 'JSON failure' do
      before do
        WebMock
          .stub_request(:get, described_class::ROULETTE_DATA_URL)
          .to_return(body: 'INVALID JSON')
      end

      it 'raises a pretty error' do
        expect { team }.to raise_error(/Failed to parse/)
      end
    end

    context 'success' do
      before do
        WebMock
          .stub_request(:get, described_class::ROULETTE_DATA_URL)
          .to_return(body: teammate_json)
      end

      it 'returns an array of teammates' do
        is_expected.to contain_exactly(ce_teammate_matcher, ee_teammate_matcher)
      end

      it 'memoizes the result' do
        expect(team.object_id).to eq(roulette.team.object_id)
      end
    end
  end

  describe '#project_team' do
    subject { roulette.project_team('gitlab-ce') }

    before do
      WebMock
        .stub_request(:get, described_class::ROULETTE_DATA_URL)
        .to_return(body: teammate_json)
    end

    it 'filters team by project_name' do
      is_expected.to contain_exactly(ce_teammate_matcher)
    end
  end

  describe '#spin_for_person' do
    let(:person1) { Gitlab::Danger::Teammate.new('username' => 'rymai') }
    let(:person2) { Gitlab::Danger::Teammate.new('username' => 'godfat') }
    let(:author) { Gitlab::Danger::Teammate.new('username' => 'filipa') }
    let(:ooo) { Gitlab::Danger::Teammate.new('username' => 'jacopo-beschi') }

    before do
      stub_person_message(person1, 'making GitLab magic')
      stub_person_message(person2, 'making GitLab magic')
      stub_person_message(ooo, 'OOO till 15th')
      # we don't stub Filipa, as she is the author and
      # we should not fire request checking for her

      allow(subject).to receive_message_chain(:gitlab, :mr_author).and_return(author.username)
    end

    it 'returns a random person' do
      persons = [person1, person2]

      selected = subject.spin_for_person(persons, random: Random.new)

      expect(selected.username).to be_in(persons.map(&:username))
    end

    it 'excludes OOO persons' do
      expect(subject.spin_for_person([ooo], random: Random.new)).to be_nil
    end

    it 'excludes mr.author' do
      expect(subject.spin_for_person([author], random: Random.new)).to be_nil
    end

    private

    def stub_person_message(person, message)
      body = { message: message }.to_json

      WebMock
        .stub_request(:get, "https://gitlab.com/api/v4/users/#{person.username}/status")
        .to_return(body: body)
    end
  end
end