summaryrefslogtreecommitdiff
path: root/spec/requests/api/features_spec.rb
blob: f169e6661d19cd156f1ad390f5c13dcf6c77fd81 (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
require 'spec_helper'

describe API::Features do
  let(:user)  { create(:user) }
  let(:admin) { create(:admin) }

  describe 'GET /features' do
    let(:expected_features) do
      [
        {
          'name' => 'feature_1',
          'state' => 'on',
          'gates' => [{ 'key' => 'boolean', 'value' => true }]
        },
        {
          'name' => 'feature_2',
          'state' => 'off',
          'gates' => [{ 'key' => 'boolean', 'value' => false }]
        }
      ]
    end

    before do
      Feature.get('feature_1').enable
      Feature.get('feature_2').disable
    end

    it 'returns a 401 for anonymous users' do
      get api('/features')

      expect(response).to have_http_status(401)
    end

    it 'returns a 403 for users' do
      get api('/features', user)

      expect(response).to have_http_status(403)
    end

    it 'returns the feature list for admins' do
      get api('/features', admin)

      expect(response).to have_http_status(200)
      expect(json_response).to match_array(expected_features)
    end
  end

  describe 'POST /feature' do
    let(:feature_name) { 'my_feature' }
    it 'returns a 401 for anonymous users' do
      post api("/features/#{feature_name}")

      expect(response).to have_http_status(401)
    end

    it 'returns a 403 for users' do
      post api("/features/#{feature_name}", user)

      expect(response).to have_http_status(403)
    end

    it 'creates an enabled feature if passed true' do
      post api("/features/#{feature_name}", admin), value: 'true'

      expect(response).to have_http_status(201)
      expect(Feature.get(feature_name)).to be_enabled
    end

    it 'creates a feature with the given percentage if passed an integer' do
      post api("/features/#{feature_name}", admin), value: '50'

      expect(response).to have_http_status(201)
      expect(Feature.get(feature_name).percentage_of_time_value).to be(50)
    end

    context 'when the feature exists' do
      let(:feature) { Feature.get(feature_name) }

      before do
        feature.disable # This also persists the feature on the DB
      end

      it 'enables the feature if passed true' do
        post api("/features/#{feature_name}", admin), value: 'true'

        expect(response).to have_http_status(201)
        expect(feature).to be_enabled
      end

      context 'with a pre-existing percentage value' do
        before do
          feature.enable_percentage_of_time(50)
        end

        it 'updates the percentage of time if passed an integer' do
          post api("/features/#{feature_name}", admin), value: '30'

          expect(response).to have_http_status(201)
          expect(Feature.get(feature_name).percentage_of_time_value).to be(30)
        end
      end
    end
  end
end