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

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

require 'spec_helper'

RSpec.describe Organizations::OrganizationSetting, type: :model, feature_category: :cell do
  let_it_be(:organization) { create(:organization) }
  let_it_be(:organization_setting) { create(:organization_setting, organization: organization) }

  describe 'associations' do
    it { is_expected.to belong_to :organization }
  end

  describe 'validations' do
    context 'for json schema' do
      let(:restricted_visibility_levels) { [] }
      let(:settings) do
        {
          restricted_visibility_levels: restricted_visibility_levels
        }
      end

      it { is_expected.to allow_value(settings).for(:settings) }

      context 'when trying to store an unsupported key' do
        let(:settings) do
          {
            unsupported_key: 'some_value'
          }
        end

        it { is_expected.not_to allow_value(settings).for(:settings) }
      end

      context "when key 'restricted_visibility_levels' is invalid" do
        let(:restricted_visibility_levels) { ['some_string'] }

        it { is_expected.not_to allow_value(settings).for(:settings) }
      end
    end

    context 'when setting restricted_visibility_levels' do
      it 'is one or more of Gitlab::VisibilityLevel constants' do
        setting = build(:organization_setting)

        setting.restricted_visibility_levels = [123]

        expect(setting.valid?).to be false
        expect(setting.errors.full_messages).to include(
          "Restricted visibility levels '123' is not a valid visibility level"
        )

        setting.restricted_visibility_levels = [Gitlab::VisibilityLevel::PUBLIC, Gitlab::VisibilityLevel::PRIVATE,
          Gitlab::VisibilityLevel::INTERNAL]
        expect(setting.valid?).to be true
      end
    end
  end

  describe '.for_current_organization' do
    let(:dummy_class) { Class.new { extend Organization::CurrentOrganization } }

    subject(:settings) { described_class.for_current_organization }

    context 'when there is no current organization' do
      it { is_expected.to be_nil }
    end

    context 'when there is a current organization' do
      before do
        dummy_class.current_organization = organization
      end

      after do
        dummy_class.current_organization = nil
      end

      it 'returns current organization' do
        expect(settings).to eq(organization_setting)
      end

      context 'when current organization does not have settings' do
        before do
          allow(organization).to receive(:settings).and_return(nil)
        end

        it 'returns new settings record' do
          new_settings = settings

          expect(new_settings.organization).to eq(organization)
          expect(new_settings.new_record?).to eq(true)
        end
      end
    end
  end
end