diff options
Diffstat (limited to 'spec/requests/api/variables_spec.rb')
-rw-r--r-- | spec/requests/api/variables_spec.rb | 151 |
1 files changed, 150 insertions, 1 deletions
diff --git a/spec/requests/api/variables_spec.rb b/spec/requests/api/variables_spec.rb index f209a1d2e6e..7bb73e9664b 100644 --- a/spec/requests/api/variables_spec.rb +++ b/spec/requests/api/variables_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe API::Variables do +RSpec.describe API::Variables do let(:user) { create(:user) } let(:user2) { create(:user) } let!(:project) { create(:project, creator_id: user.id) } @@ -54,6 +54,59 @@ describe API::Variables do expect(response).to have_gitlab_http_status(:not_found) end + + context 'when there are two variables with the same key on different env' do + let!(:var1) { create(:ci_variable, project: project, key: 'key1', environment_scope: 'staging') } + let!(:var2) { create(:ci_variable, project: project, key: 'key1', environment_scope: 'production') } + + context 'when filter[environment_scope] is not passed' do + context 'FF ci_variables_api_filter_environment_scope is enabled' do + it 'returns 409' do + get api("/projects/#{project.id}/variables/key1", user) + + expect(response).to have_gitlab_http_status(:conflict) + end + end + + context 'FF ci_variables_api_filter_environment_scope is disabled' do + before do + stub_feature_flags(ci_variables_api_filter_environment_scope: false) + end + + it 'returns random one' do + get api("/projects/#{project.id}/variables/key1", user) + + expect(response).to have_gitlab_http_status(:ok) + expect(json_response['key']).to eq('key1') + end + end + end + + context 'when filter[environment_scope] is passed' do + it 'returns the variable' do + get api("/projects/#{project.id}/variables/key1", user), params: { 'filter[environment_scope]': 'production' } + + expect(response).to have_gitlab_http_status(:ok) + expect(json_response['value']).to eq(var2.value) + end + end + + context 'when wrong filter[environment_scope] is passed' do + it 'returns not_found' do + get api("/projects/#{project.id}/variables/key1", user), params: { 'filter[environment_scope]': 'invalid' } + + expect(response).to have_gitlab_http_status(:not_found) + end + + context 'when there is only one variable with provided key' do + it 'returns not_found' do + get api("/projects/#{project.id}/variables/#{variable.key}", user), params: { 'filter[environment_scope]': 'invalid' } + + expect(response).to have_gitlab_http_status(:not_found) + end + end + end + end end context 'authorized user with invalid permissions' do @@ -173,6 +226,52 @@ describe API::Variables do expect(response).to have_gitlab_http_status(:not_found) end + + context 'when there are two variables with the same key on different env' do + let!(:var1) { create(:ci_variable, project: project, key: 'key1', environment_scope: 'staging') } + let!(:var2) { create(:ci_variable, project: project, key: 'key1', environment_scope: 'production') } + + context 'when filter[environment_scope] is not passed' do + context 'FF ci_variables_api_filter_environment_scope is enabled' do + it 'returns 409' do + get api("/projects/#{project.id}/variables/key1", user) + + expect(response).to have_gitlab_http_status(:conflict) + end + end + + context 'FF ci_variables_api_filter_environment_scope is disabled' do + before do + stub_feature_flags(ci_variables_api_filter_environment_scope: false) + end + + it 'updates random one' do + put api("/projects/#{project.id}/variables/key1", user), params: { value: 'new_val' } + + expect(response).to have_gitlab_http_status(:ok) + expect(json_response['value']).to eq('new_val') + end + end + end + + context 'when filter[environment_scope] is passed' do + it 'updates the variable' do + put api("/projects/#{project.id}/variables/key1", user), params: { value: 'new_val', 'filter[environment_scope]': 'production' } + + expect(response).to have_gitlab_http_status(:ok) + expect(var1.reload.value).not_to eq('new_val') + expect(var2.reload.value).to eq('new_val') + end + end + + context 'when wrong filter[environment_scope] is passed' do + it 'returns not_found' do + put api("/projects/#{project.id}/variables/key1", user), params: { value: 'new_val', 'filter[environment_scope]': 'invalid' } + + expect(response).to have_gitlab_http_status(:not_found) + end + end + end end context 'authorized user with invalid permissions' do @@ -207,6 +306,56 @@ describe API::Variables do expect(response).to have_gitlab_http_status(:not_found) end + + context 'when there are two variables with the same key on different env' do + let!(:var1) { create(:ci_variable, project: project, key: 'key1', environment_scope: 'staging') } + let!(:var2) { create(:ci_variable, project: project, key: 'key1', environment_scope: 'production') } + + context 'when filter[environment_scope] is not passed' do + context 'FF ci_variables_api_filter_environment_scope is enabled' do + it 'returns 409' do + get api("/projects/#{project.id}/variables/key1", user) + + expect(response).to have_gitlab_http_status(:conflict) + end + end + + context 'FF ci_variables_api_filter_environment_scope is disabled' do + before do + stub_feature_flags(ci_variables_api_filter_environment_scope: false) + end + + it 'deletes random one' do + expect do + delete api("/projects/#{project.id}/variables/key1", user), params: { 'filter[environment_scope]': 'production' } + + expect(response).to have_gitlab_http_status(:no_content) + end.to change {project.variables.count}.by(-1) + end + end + end + + context 'when filter[environment_scope] is passed' do + it 'deletes the variable' do + expect do + delete api("/projects/#{project.id}/variables/key1", user), params: { 'filter[environment_scope]': 'production' } + + expect(response).to have_gitlab_http_status(:no_content) + end.to change {project.variables.count}.by(-1) + + expect(var1.reload).to be_present + expect { var2.reload }.to raise_error(ActiveRecord::RecordNotFound) + end + end + + context 'when wrong filter[environment_scope] is passed' do + it 'returns not_found' do + delete api("/projects/#{project.id}/variables/key1", user), params: { 'filter[environment_scope]': 'invalid' } + + expect(response).to have_gitlab_http_status(:not_found) + end + end + end end context 'authorized user with invalid permissions' do |