debian-mirror-gitlab/spec/lib/gitlab/danger/teammate_spec.rb

186 lines
6 KiB
Ruby
Raw Normal View History

2019-07-07 11:18:12 +05:30
# frozen_string_literal: true
2019-09-04 21:01:54 +05:30
require 'fast_spec_helper'
2019-12-21 20:55:43 +05:30
require 'rspec-parameterized'
2019-09-04 21:01:54 +05:30
require 'gitlab/danger/teammate'
2019-07-07 11:18:12 +05:30
describe Gitlab::Danger::Teammate do
2019-12-21 20:55:43 +05:30
subject { described_class.new(options.stringify_keys) }
let(:options) { { username: 'luigi', projects: projects, role: role } }
2019-07-07 11:18:12 +05:30
let(:projects) { { project => capabilities } }
2019-09-04 21:01:54 +05:30
let(:role) { 'Engineer, Manage' }
let(:labels) { [] }
2019-07-07 11:18:12 +05:30
let(:project) { double }
2019-09-04 21:01:54 +05:30
context 'when having multiple capabilities' do
let(:capabilities) { ['reviewer backend', 'maintainer frontend', 'trainee_maintainer qa'] }
2019-07-07 11:18:12 +05:30
it '#reviewer? supports multiple roles per project' do
2019-09-04 21:01:54 +05:30
expect(subject.reviewer?(project, :backend, labels)).to be_truthy
2019-07-07 11:18:12 +05:30
end
it '#traintainer? supports multiple roles per project' do
2019-09-04 21:01:54 +05:30
expect(subject.traintainer?(project, :qa, labels)).to be_truthy
2019-07-07 11:18:12 +05:30
end
it '#maintainer? supports multiple roles per project' do
2019-09-04 21:01:54 +05:30
expect(subject.maintainer?(project, :frontend, labels)).to be_truthy
end
2019-12-26 22:10:19 +05:30
context 'when labels contain devops::create and the category is test' do
2019-12-04 20:38:33 +05:30
let(:labels) { ['devops::create'] }
2019-09-04 21:01:54 +05:30
2020-01-01 13:55:28 +05:30
context 'when role is Software Engineer in Test, Create' do
let(:role) { 'Software Engineer in Test, Create' }
2019-09-04 21:01:54 +05:30
it '#reviewer? returns true' do
expect(subject.reviewer?(project, :test, labels)).to be_truthy
end
it '#maintainer? returns false' do
expect(subject.maintainer?(project, :test, labels)).to be_falsey
end
2019-10-12 21:52:04 +05:30
context 'when hyperlink is mangled in the role' do
2020-01-01 13:55:28 +05:30
let(:role) { '<a href="#">Software Engineer in Test</a>, Create' }
2019-10-12 21:52:04 +05:30
it '#reviewer? returns true' do
expect(subject.reviewer?(project, :test, labels)).to be_truthy
end
end
2019-09-04 21:01:54 +05:30
end
2020-01-01 13:55:28 +05:30
context 'when role is Software Engineer in Test' do
let(:role) { 'Software Engineer in Test' }
2019-12-04 20:38:33 +05:30
it '#reviewer? returns false' do
expect(subject.reviewer?(project, :test, labels)).to be_falsey
end
end
2020-01-01 13:55:28 +05:30
context 'when role is Software Engineer in Test, Manage' do
let(:role) { 'Software Engineer in Test, Manage' }
2019-09-04 21:01:54 +05:30
it '#reviewer? returns false' do
expect(subject.reviewer?(project, :test, labels)).to be_falsey
end
end
2019-12-04 20:38:33 +05:30
context 'when role is Backend Engineer, Engineering Productivity' do
let(:role) { 'Backend Engineer, Engineering Productivity' }
it '#reviewer? returns true' do
expect(subject.reviewer?(project, :engineering_productivity, labels)).to be_truthy
end
it '#maintainer? returns false' do
expect(subject.maintainer?(project, :engineering_productivity, labels)).to be_falsey
end
2019-12-26 22:10:19 +05:30
context 'when capabilities include maintainer backend' do
let(:capabilities) { ['maintainer backend'] }
it '#maintainer? returns true' do
expect(subject.maintainer?(project, :engineering_productivity, labels)).to be_truthy
end
end
context 'when capabilities include trainee_maintainer backend' do
let(:capabilities) { ['trainee_maintainer backend'] }
it '#traintainer? returns true' do
expect(subject.traintainer?(project, :engineering_productivity, labels)).to be_truthy
end
end
2019-12-04 20:38:33 +05:30
end
2019-07-07 11:18:12 +05:30
end
end
2019-09-04 21:01:54 +05:30
context 'when having single capability' do
2019-07-07 11:18:12 +05:30
let(:capabilities) { 'reviewer backend' }
it '#reviewer? supports one role per project' do
2019-09-04 21:01:54 +05:30
expect(subject.reviewer?(project, :backend, labels)).to be_truthy
2019-07-07 11:18:12 +05:30
end
it '#traintainer? supports one role per project' do
2019-09-04 21:01:54 +05:30
expect(subject.traintainer?(project, :database, labels)).to be_falsey
2019-07-07 11:18:12 +05:30
end
it '#maintainer? supports one role per project' do
2019-09-04 21:01:54 +05:30
expect(subject.maintainer?(project, :frontend, labels)).to be_falsey
2019-07-07 11:18:12 +05:30
end
end
2019-12-21 20:55:43 +05:30
describe '#status' do
let(:capabilities) { ['dish washing'] }
context 'with empty cache' do
context 'for successful request' do
it 'returns the response' do
mock_status = double(does_not: 'matter')
expect(Gitlab::Danger::RequestHelper).to receive(:http_get_json)
.and_return(mock_status)
expect(subject.status).to be mock_status
end
end
context 'for failing request' do
it 'returns nil' do
expect(Gitlab::Danger::RequestHelper).to receive(:http_get_json)
.and_raise(Gitlab::Danger::RequestHelper::HTTPError.new)
expect(subject.status).to be nil
end
end
end
context 'with filled cache' do
it 'returns the cached response' do
mock_status = double(does_not: 'matter')
expect(Gitlab::Danger::RequestHelper).to receive(:http_get_json)
.and_return(mock_status)
subject.status
expect(Gitlab::Danger::RequestHelper).not_to receive(:http_get_json)
expect(subject.status).to be mock_status
end
end
end
describe '#available?' do
using RSpec::Parameterized::TableSyntax
let(:capabilities) { ['dry head'] }
where(:status, :result) do
{} | true
{ message: 'dear reader' } | true
{ message: 'OOO: massage' } | false
{ message: 'love it SOOO much' } | false
{ emoji: 'red_circle' } | false
end
with_them do
before do
expect(Gitlab::Danger::RequestHelper).to receive(:http_get_json)
.and_return(status&.stringify_keys)
end
it { expect(subject.available?).to be result }
end
it 'returns true if request fails' do
expect(Gitlab::Danger::RequestHelper).to receive(:http_get_json)
2020-03-09 13:42:32 +05:30
.twice
2019-12-21 20:55:43 +05:30
.and_raise(Gitlab::Danger::RequestHelper::HTTPError.new)
expect(subject.available?).to be true
end
end
2019-07-07 11:18:12 +05:30
end