variables_spec.rb 5.66 KB
Newer Older
1 2
require 'spec_helper'

3
describe API::Variables, api: true do
4 5 6 7 8
  include ApiHelpers

  let(:user) { create(:user) }
  let(:user2) { create(:user) }
  let!(:project) { create(:project, creator_id: user.id) }
9 10
  let!(:master) { create(:project_member, :master, user: user, project: project) }
  let!(:developer) { create(:project_member, :developer, user: user2, project: project) }
11 12 13 14
  let!(:variable) { create(:ci_variable, project: project) }

  describe 'GET /projects/:id/variables' do
    context 'authorized user with proper permissions' do
15
      it 'returns project variables' do
16 17
        get api("/projects/#{project.id}/variables", user)

18
        expect(response).to have_http_status(200)
19 20 21 22 23
        expect(json_response).to be_a(Array)
      end
    end

    context 'authorized user with invalid permissions' do
24
      it 'does not return project variables' do
25 26
        get api("/projects/#{project.id}/variables", user2)

27
        expect(response).to have_http_status(403)
28 29 30 31
      end
    end

    context 'unauthorized user' do
32
      it 'does not return project variables' do
33 34
        get api("/projects/#{project.id}/variables")

35
        expect(response).to have_http_status(401)
36 37 38 39
      end
    end
  end

40
  describe 'GET /projects/:id/variables/:key' do
41
    context 'authorized user with proper permissions' do
42
      it 'returns project variable details' do
43
        get api("/projects/#{project.id}/variables/#{variable.key}", user)
44

45
        expect(response).to have_http_status(200)
46
        expect(json_response['value']).to eq(variable.value)
47
      end
48

49
      it 'responds with 404 Not Found if requesting non-existing variable' do
50
        get api("/projects/#{project.id}/variables/non_existing_variable", user)
51

52
        expect(response).to have_http_status(404)
53
      end
54 55 56
    end

    context 'authorized user with invalid permissions' do
57
      it 'does not return project variable details' do
58
        get api("/projects/#{project.id}/variables/#{variable.key}", user2)
59

60
        expect(response).to have_http_status(403)
61 62 63 64
      end
    end

    context 'unauthorized user' do
65
      it 'does not return project variable details' do
66
        get api("/projects/#{project.id}/variables/#{variable.key}")
67

68
        expect(response).to have_http_status(401)
69 70 71 72
      end
    end
  end

73 74
  describe 'POST /projects/:id/variables' do
    context 'authorized user with proper permissions' do
75
      it 'creates variable' do
76 77 78 79
        expect do
          post api("/projects/#{project.id}/variables", user), key: 'TEST_VARIABLE_2', value: 'VALUE_2'
        end.to change{project.variables.count}.by(1)

80
        expect(response).to have_http_status(201)
81 82 83 84
        expect(json_response['key']).to eq('TEST_VARIABLE_2')
        expect(json_response['value']).to eq('VALUE_2')
      end

85
      it 'does not allow to duplicate variable key' do
86
        expect do
87
          post api("/projects/#{project.id}/variables", user), key: variable.key, value: 'VALUE_2'
88 89
        end.to change{project.variables.count}.by(0)

90
        expect(response).to have_http_status(400)
91 92 93 94
      end
    end

    context 'authorized user with invalid permissions' do
95
      it 'does not create variable' do
96 97
        post api("/projects/#{project.id}/variables", user2)

98
        expect(response).to have_http_status(403)
99 100 101 102
      end
    end

    context 'unauthorized user' do
103
      it 'does not create variable' do
104 105
        post api("/projects/#{project.id}/variables")

106
        expect(response).to have_http_status(401)
107 108 109 110
      end
    end
  end

111
  describe 'PUT /projects/:id/variables/:key' do
112
    context 'authorized user with proper permissions' do
113
      it 'updates variable data' do
114 115 116
        initial_variable = project.variables.first
        value_before = initial_variable.value

117
        put api("/projects/#{project.id}/variables/#{variable.key}", user), value: 'VALUE_1_UP'
118 119 120

        updated_variable = project.variables.first

121
        expect(response).to have_http_status(200)
122 123 124
        expect(value_before).to eq(variable.value)
        expect(updated_variable.value).to eq('VALUE_1_UP')
      end
125

126
      it 'responds with 404 Not Found if requesting non-existing variable' do
127
        put api("/projects/#{project.id}/variables/non_existing_variable", user)
128

129
        expect(response).to have_http_status(404)
130
      end
131 132 133
    end

    context 'authorized user with invalid permissions' do
134
      it 'does not update variable' do
135
        put api("/projects/#{project.id}/variables/#{variable.key}", user2)
136

137
        expect(response).to have_http_status(403)
138 139 140 141
      end
    end

    context 'unauthorized user' do
142
      it 'does not update variable' do
143
        put api("/projects/#{project.id}/variables/#{variable.key}")
144

145
        expect(response).to have_http_status(401)
146 147 148
      end
    end
  end
149

150
  describe 'DELETE /projects/:id/variables/:key' do
151
    context 'authorized user with proper permissions' do
152
      it 'deletes variable' do
153
        expect do
154
          delete api("/projects/#{project.id}/variables/#{variable.key}", user)
155
        end.to change{project.variables.count}.by(-1)
156
        expect(response).to have_http_status(200)
157
      end
158

159
      it 'responds with 404 Not Found if requesting non-existing variable' do
160
        delete api("/projects/#{project.id}/variables/non_existing_variable", user)
161

162
        expect(response).to have_http_status(404)
163
      end
164 165 166
    end

    context 'authorized user with invalid permissions' do
167
      it 'does not delete variable' do
168
        delete api("/projects/#{project.id}/variables/#{variable.key}", user2)
169

170
        expect(response).to have_http_status(403)
171 172 173 174
      end
    end

    context 'unauthorized user' do
175
      it 'does not delete variable' do
176
        delete api("/projects/#{project.id}/variables/#{variable.key}")
177

178
        expect(response).to have_http_status(401)
179 180 181
      end
    end
  end
182
end