Welcome to mirror list, hosted at ThFree Co, Russian Federation.

alerts_service_spec.rb « project_services « models « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4e63ece26d87e7c6a0fab7d71fe049205588cf52 (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
# frozen_string_literal: true

require 'spec_helper'

describe AlertsService do
  let_it_be(:project) { create(:project) }
  let(:service_params) { { project: project, active: active } }
  let(:active) { true }
  let(:service) { described_class.new(service_params) }

  shared_context 'when active' do
    let(:active) { true }
  end

  shared_context 'when inactive' do
    let(:active) { false }
  end

  shared_context 'when persisted' do
    before do
      service.save!
      service.reload
    end
  end

  describe '#url' do
    include Gitlab::Routing

    subject { service.url }

    it { is_expected.to eq(project_alerts_notify_url(project, format: :json)) }
  end

  describe '#json_fields' do
    subject { service.json_fields }

    it { is_expected.to eq(%w(active token)) }
  end

  describe '#as_json' do
    subject { service.as_json(only: service.json_fields) }

    it { is_expected.to eq('active' => true, 'token' => nil) }
  end

  describe '#token' do
    shared_context 'reset token' do
      before do
        service.token = ''
        service.valid?
      end
    end

    shared_context 'assign token' do |token|
      before do
        service.token = token
        service.valid?
      end
    end

    shared_examples 'valid token' do
      it { is_expected.to match(/\A\h{32}\z/) }
    end

    shared_examples 'no token' do
      it { is_expected.to be_blank }
    end

    subject { service.token }

    context 'when active' do
      include_context 'when active'

      context 'when resetting' do
        let!(:previous_token) { service.token }

        include_context 'reset token'

        it_behaves_like 'valid token'

        it { is_expected.not_to eq(previous_token) }
      end

      context 'when assigning' do
        include_context 'assign token', 'random token'

        it_behaves_like 'valid token'
      end
    end

    context 'when inactive' do
      include_context 'when inactive'

      context 'when resetting' do
        let!(:previous_token) { service.token }

        include_context 'reset token'

        it_behaves_like 'no token'
      end
    end

    context 'when persisted' do
      include_context 'when persisted'

      it_behaves_like 'valid token'
    end
  end
end