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

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

require 'spec_helper'

RSpec.describe Gitlab::FeatureCategories do
  let(:fake_categories) { %w(foo bar) }

  subject { described_class.new(fake_categories) }

  describe "#valid?" do
    it "returns true if category is known", :aggregate_failures do
      expect(subject.valid?('foo')).to be(true)
      expect(subject.valid?('zzz')).to be(false)
    end
  end

  describe "#from_request" do
    let(:request_env) { {} }
    let(:verified) { true }

    def fake_request(request_feature_category)
      double('request', env: request_env, headers: { "HTTP_X_GITLAB_FEATURE_CATEGORY" => request_feature_category })
    end

    before do
      allow(::Gitlab::RequestForgeryProtection).to receive(:verified?).with(request_env).and_return(verified)
    end

    it "returns category from request when valid, otherwise returns nil", :aggregate_failures do
      expect(subject.from_request(fake_request("foo"))).to be("foo")
      expect(subject.from_request(fake_request("zzz"))).to be_nil
    end

    context "when request is not verified" do
      let(:verified) { false }

      it "returns nil" do
        expect(subject.from_request(fake_request("foo"))).to be_nil
      end
    end
  end

  describe "#categories" do
    it "returns a set of the given categories" do
      expect(subject.categories).to be_a(Set)
      expect(subject.categories).to contain_exactly(*fake_categories)
    end
  end

  describe ".load_from_yaml" do
    subject { described_class.load_from_yaml }

    it "creates FeatureCategories from feature_categories.yml file" do
      contents = YAML.load_file(Rails.root.join('config', 'feature_categories.yml'))

      expect(subject.categories).to contain_exactly(*contents)
    end
  end

  describe ".default" do
    it "returns a memoization of load_from_yaml", :aggregate_failures do
      # FeatureCategories.default could have been referenced in another spec, so we need to clean it up here
      described_class.instance_variable_set(:@default, nil)

      expect(described_class).to receive(:load_from_yaml).once.and_call_original

      2.times { described_class.default }

      # Uses reference equality to verify memoization
      expect(described_class.default).to equal(described_class.default)
      expect(described_class.default).to be_a(described_class)
    end
  end
end