summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/cross_project_access/check_info_spec.rb
blob: 239fa364f5e05f0abf2b98f311d8ce76c2a001ff (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
require 'spec_helper'

describe Gitlab::CrossProjectAccess::CheckInfo do
  let(:dummy_controller) { double }

  before do
    allow(dummy_controller).to receive(:action_name).and_return('index')
  end

  describe '#should_run?' do
    it 'runs when an action is defined' do
      info = described_class.new({ index: true }, nil, nil, false)

      expect(info.should_run?(dummy_controller)).to be_truthy
    end

    it 'runs when the action is missing' do
      info = described_class.new({}, nil, nil, false)

      expect(info.should_run?(dummy_controller)).to be_truthy
    end

    it 'does not run when the action is excluded' do
      info = described_class.new({ index: false }, nil, nil, false)

      expect(info.should_run?(dummy_controller)).to be_falsy
    end

    it 'runs when the `if` conditional is true' do
      info = described_class.new({}, -> { true }, nil, false)

      expect(info.should_run?(dummy_controller)).to be_truthy
    end

    it 'does not run when the if condition is false' do
      info = described_class.new({}, -> { false }, nil, false)

      expect(info.should_run?(dummy_controller)).to be_falsy
    end

    it 'does not run when the `unless` check is true' do
      info = described_class.new({}, nil, -> { true }, false)

      expect(info.should_run?(dummy_controller)).to be_falsy
    end

    it 'runs when the `unless` check is false' do
      info = described_class.new({}, nil, -> { false }, false)

      expect(info.should_run?(dummy_controller)).to be_truthy
    end

    it 'returns the the opposite of #should_skip? when the check is a skip' do
      info = described_class.new({}, nil, nil, true)

      expect(info).to receive(:should_skip?).with(dummy_controller).and_return(false)
      expect(info.should_run?(dummy_controller)).to be_truthy
    end
  end

  describe '#should_skip?' do
    it 'skips when an action is defined' do
      info = described_class.new({ index: true }, nil, nil, true)

      expect(info.should_skip?(dummy_controller)).to be_truthy
    end

    it 'does not skip when the action is not defined' do
      info = described_class.new({}, nil, nil, true)

      expect(info.should_skip?(dummy_controller)).to be_falsy
    end

    it 'does not skip when the action is excluded' do
      info = described_class.new({ index: false }, nil, nil, true)

      expect(info.should_skip?(dummy_controller)).to be_falsy
    end

    it 'skips when the `if` conditional is true' do
      info = described_class.new({ index: true }, -> { true }, nil, true)

      expect(info.should_skip?(dummy_controller)).to be_truthy
    end

    it 'does not skip the `if` conditional is false' do
      info = described_class.new({ index: true }, -> { false }, nil, true)

      expect(info.should_skip?(dummy_controller)).to be_falsy
    end

    it 'does not skip when the `unless` check is true' do
      info = described_class.new({ index: true }, nil, -> { true }, true)

      expect(info.should_skip?(dummy_controller)).to be_falsy
    end

    it 'skips when `unless` check is false' do
      info = described_class.new({ index: true }, nil, -> { false }, true)

      expect(info.should_skip?(dummy_controller)).to be_truthy
    end

    it 'returns the the opposite of #should_run? when the check is not a skip' do
      info = described_class.new({}, nil, nil, false)

      expect(info).to receive(:should_run?).with(dummy_controller).and_return(false)
      expect(info.should_skip?(dummy_controller)).to be_truthy
    end
  end
end