summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/auth/result_spec.rb
blob: f8de4b80db2ea1a7e69c2f26ebe604ddce02786d (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Auth::Result do
  let_it_be(:actor) { create(:user) }

  subject { described_class.new(actor, nil, nil, []) }

  context 'when actor is User' do
    let_it_be(:actor) { create(:user) }

    it 'returns auth_user' do
      expect(subject.auth_user).to eq(actor)
    end

    it 'does not return deploy token' do
      expect(subject.deploy_token).to be_nil
    end
  end

  context 'when actor is Deploy token' do
    let_it_be(:actor) { create(:deploy_token) }

    it 'returns deploy token' do
      expect(subject.deploy_token).to eq(actor)
    end

    it 'does not return auth_user' do
      expect(subject.auth_user).to be_nil
    end
  end

  describe '#authentication_abilities_include?' do
    context 'when authentication abilities are empty' do
      it 'returns false' do
        expect(subject.authentication_abilities_include?(:read_code)).to be_falsey
      end
    end

    context 'when authentication abilities are not empty' do
      subject { described_class.new(actor, nil, nil, [:push_code]) }

      it 'returns false when ability is not allowed' do
        expect(subject.authentication_abilities_include?(:read_code)).to be_falsey
      end

      it 'returns true when ability is allowed' do
        expect(subject.authentication_abilities_include?(:push_code)).to be_truthy
      end
    end
  end

  describe '#can_perform_action_on_project?' do
    let(:project) { double }

    it 'returns if actor can do perform given action on given project' do
      expect(Ability).to receive(:allowed?).with(actor, :push_code, project).and_return(true)
      expect(subject.can_perform_action_on_project?(:push_code, project)).to be_truthy
    end

    it 'returns if actor cannot do perform given action on given project' do
      expect(Ability).to receive(:allowed?).with(actor, :push_code, project).and_return(false)
      expect(subject.can_perform_action_on_project?(:push_code, project)).to be_falsey
    end
  end

  describe '#can?' do
    it 'returns if actor can do perform given action on given project' do
      expect(actor).to receive(:can?).with(:push_code).and_return(true)
      expect(subject.can?(:push_code)).to be_truthy
    end

    it 'returns if actor cannot do perform given action on given project' do
      expect(actor).to receive(:can?).with(:push_code).and_return(false)
      expect(subject.can?(:push_code)).to be_falsey
    end
  end
end