summaryrefslogtreecommitdiff
path: root/spec/models/users/namespace_commit_email_spec.rb
blob: 23fed85ab3e2b9af3ca0a3ffa00a3b36eda3f68c (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Users::NamespaceCommitEmail, type: :model, feature_category: :source_code_management do
  subject { build(:namespace_commit_email) }

  describe 'associations' do
    it { is_expected.to belong_to(:user) }
    it { is_expected.to belong_to(:namespace) }
    it { is_expected.to belong_to(:email) }
  end

  describe 'validations' do
    it { is_expected.to validate_presence_of(:user) }
    it { is_expected.to validate_presence_of(:namespace) }
    it { is_expected.to validate_presence_of(:email) }

    it { is_expected.to validate_uniqueness_of(:user).scoped_to(:namespace_id) }

    describe 'validate_root_group' do
      let_it_be(:root_group) { create(:group) }

      context 'when root group' do
        subject { build(:namespace_commit_email, namespace: root_group) }

        it { is_expected.to be_valid }
      end

      context 'when subgroup' do
        subject { build(:namespace_commit_email, namespace: create(:group, parent: root_group)) }

        it 'is invalid and reports the relevant error' do
          expect(subject).to be_invalid
          expect(subject.errors[:namespace]).to include('must be a root group.')
        end
      end
    end
  end

  it { is_expected.to be_valid }

  describe '.delete_for_namespace' do
    let_it_be(:group) { create(:group) }

    it 'deletes all records for namespace' do
      create_list(:namespace_commit_email, 3, namespace: group)
      create(:namespace_commit_email)

      expect { described_class.delete_for_namespace(group) }.to change { described_class.count }.by(-3)
    end
  end
end