Read about our upcoming Code of Conduct on this issue

deploy_token_spec.rb 1.98 KB
Newer Older
1
2
3
require 'spec_helper'

describe DeployToken do
Mayra Cabrera's avatar
Mayra Cabrera committed
4
  subject(:deploy_token) { create(:deploy_token) }
5

Mayra Cabrera's avatar
Mayra Cabrera committed
6
7
  it { is_expected.to have_many :project_deploy_tokens }
  it { is_expected.to have_many(:projects).through(:project_deploy_tokens) }
8

Mayra Cabrera's avatar
Mayra Cabrera committed
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
  describe '#ensure_token' do
    it 'should ensure a token' do
      deploy_token.token = nil
      deploy_token.save

      expect(deploy_token.token).not_to be_empty
    end
  end

  describe '#ensure_at_least_one_scope' do
    context 'with at least one scope' do
      it 'should be valid' do
        is_expected.to be_valid
      end
    end

    context 'with no scopes' do
      it 'should be invalid' do
        deploy_token = build(:deploy_token, read_repository: false, read_registry: false)
28

29
        expect(deploy_token).not_to be_valid
Mayra Cabrera's avatar
Mayra Cabrera committed
30
        expect(deploy_token.errors[:base].first).to eq("Scopes can't be blank")
31
32
33
34
      end
    end
  end

Mayra Cabrera's avatar
Mayra Cabrera committed
35
36
37
38
39
40
  describe '#scopes' do
    context 'with all the scopes' do
      it 'should return scopes assigned to DeployToken' do
        expect(deploy_token.scopes).to eq([:read_repository, :read_registry])
      end
    end
41

Mayra Cabrera's avatar
Mayra Cabrera committed
42
43
44
45
46
    context 'with only one scope' do
      it 'should return scopes assigned to DeployToken' do
        deploy_token = create(:deploy_token, read_registry: false)
        expect(deploy_token.scopes).to eq([:read_repository])
      end
47
48
49
50
51
    end
  end

  describe '#revoke!' do
    it 'should update revoke attribute' do
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
      deploy_token.revoke!
      expect(deploy_token.revoked?).to be_truthy
    end
  end

  describe "#active?" do
    context "when it has been revoked" do
      it 'should return false' do
        deploy_token.revoke!
        expect(deploy_token.active?).to be_falsy
      end
    end

    context "when it hasn't been revoked" do
      it 'should return true' do
        expect(deploy_token.active?).to be_truthy
      end
    end
  end

  describe '#username' do
    it 'returns Ghost username' do
Mayra Cabrera's avatar
Mayra Cabrera committed
74
      expect(deploy_token.username).to eq("gitlab+deploy-token-#{deploy_token.id}")
75
76
77
    end
  end
end