diff options
author | Sean McGivern <sean@gitlab.com> | 2019-02-06 13:39:23 +0300 |
---|---|---|
committer | Sean McGivern <sean@gitlab.com> | 2019-02-06 13:39:23 +0300 |
commit | 561ce0c09516cf62125a0832ff3656a1ed93470f (patch) | |
tree | f84194c4ea185e94dd6a58d3d5c1226d7e05fcec /spec | |
parent | 880afe2246440739fa71fc80d6446679074fc325 (diff) | |
parent | e8d9df83a6f5638b3207885220b9441b7b894e46 (diff) |
Merge branch 'introduce-environment-search-endpoint' into 'master'
Introduce Internal API for searching environment names
See merge request gitlab-org/gitlab-ce!24923
Diffstat (limited to 'spec')
-rw-r--r-- | spec/controllers/projects/environments_controller_spec.rb | 73 | ||||
-rw-r--r-- | spec/models/environment_spec.rb | 70 |
2 files changed, 143 insertions, 0 deletions
diff --git a/spec/controllers/projects/environments_controller_spec.rb b/spec/controllers/projects/environments_controller_spec.rb index a4d494a820f..aa97a417a98 100644 --- a/spec/controllers/projects/environments_controller_spec.rb +++ b/spec/controllers/projects/environments_controller_spec.rb @@ -422,6 +422,79 @@ describe Projects::EnvironmentsController do end end + describe 'GET #search' do + before do + create(:environment, name: 'staging', project: project) + create(:environment, name: 'review/patch-1', project: project) + create(:environment, name: 'review/patch-2', project: project) + end + + let(:query) { 'pro' } + + it 'responds with status code 200' do + get :search, params: environment_params(format: :json, query: query) + + expect(response).to have_gitlab_http_status(:ok) + end + + it 'returns matched results' do + get :search, params: environment_params(format: :json, query: query) + + expect(json_response).to contain_exactly('production') + end + + context 'when query is review' do + let(:query) { 'review' } + + it 'returns matched results' do + get :search, params: environment_params(format: :json, query: query) + + expect(json_response).to contain_exactly('review/patch-1', 'review/patch-2') + end + end + + context 'when query is empty' do + let(:query) { '' } + + it 'returns matched results' do + get :search, params: environment_params(format: :json, query: query) + + expect(json_response) + .to contain_exactly('production', 'staging', 'review/patch-1', 'review/patch-2') + end + end + + context 'when query is review/patch-3' do + let(:query) { 'review/patch-3' } + + it 'responds with status code 204' do + get :search, params: environment_params(format: :json, query: query) + + expect(response).to have_gitlab_http_status(:no_content) + end + end + + context 'when query is partially matched in the middle of environment name' do + let(:query) { 'patch' } + + it 'responds with status code 204' do + get :search, params: environment_params(format: :json, query: query) + + expect(response).to have_gitlab_http_status(:no_content) + end + end + + context 'when query contains a wildcard character' do + let(:query) { 'review%' } + + it 'prevents wildcard injection' do + get :search, params: environment_params(format: :json, query: query) + + expect(response).to have_gitlab_http_status(:no_content) + end + end + end + def environment_params(opts = {}) opts.reverse_merge(namespace_id: project.namespace, project_id: project, diff --git a/spec/models/environment_spec.rb b/spec/models/environment_spec.rb index 9a3f1f1c5a1..2d554326f05 100644 --- a/spec/models/environment_spec.rb +++ b/spec/models/environment_spec.rb @@ -41,6 +41,76 @@ describe Environment do end end + describe '.for_name_like' do + subject { project.environments.for_name_like(query, limit: limit) } + + let!(:environment) { create(:environment, name: 'production', project: project) } + let(:query) { 'pro' } + let(:limit) { 5 } + + it 'returns a found name' do + is_expected.to include(environment) + end + + context 'when query is production' do + let(:query) { 'production' } + + it 'returns a found name' do + is_expected.to include(environment) + end + end + + context 'when query is productionA' do + let(:query) { 'productionA' } + + it 'returns empty array' do + is_expected.to be_empty + end + end + + context 'when query is empty' do + let(:query) { '' } + + it 'returns a found name' do + is_expected.to include(environment) + end + end + + context 'when query is nil' do + let(:query) { } + + it 'raises an error' do + expect { subject }.to raise_error(NoMethodError) + end + end + + context 'when query is partially matched in the middle of environment name' do + let(:query) { 'duction' } + + it 'returns empty array' do + is_expected.to be_empty + end + end + + context 'when query contains a wildcard character' do + let(:query) { 'produc%' } + + it 'prevents wildcard injection' do + is_expected.to be_empty + end + end + end + + describe '.pluck_names' do + subject { described_class.pluck_names } + + let!(:environment) { create(:environment, name: 'production', project: project) } + + it 'plucks names' do + is_expected.to eq(%w[production]) + end + end + describe '#expire_etag_cache' do let(:store) { Gitlab::EtagCaching::Store.new } |