Commit 90dbfbb3 authored by Robert Speicher's avatar Robert Speicher

Merge branch '6045-extract-ee-specific-specs-lines' into 'master'

Resolve "Extract EE specific files/lines for some controller specs"

Closes #6045

See merge request gitlab-org/gitlab-ee!5803
parents 4e55bd46 ce9385a8
class Projects::ClustersController < Projects::ApplicationController
prepend EE::Projects::ClustersController
before_action :cluster, except: [:index, :new]
before_action :authorize_read_cluster!
before_action :authorize_create_cluster!, only: [:new]
......@@ -64,22 +66,6 @@ class Projects::ClustersController < Projects::ApplicationController
end
end
def metrics
return render_404 unless prometheus_adapter&.can_query?
respond_to do |format|
format.json do
metrics = prometheus_adapter.query(:cluster) || {}
if metrics.any?
render json: metrics
else
head :no_content
end
end
end
end
private
def cluster
......@@ -87,12 +73,6 @@ class Projects::ClustersController < Projects::ApplicationController
.present(current_user: current_user)
end
def prometheus_adapter
return unless cluster&.application_prometheus&.installed?
cluster.application_prometheus
end
def update_params
if cluster.managed?
params.require(:cluster).permit(
......
module EE
module Projects
module ClustersController
extend ActiveSupport::Concern
def metrics
return render_404 unless prometheus_adapter&.can_query?
respond_to do |format|
format.json do
metrics = prometheus_adapter.query(:cluster) || {}
if metrics.any?
render json: metrics
else
head :no_content
end
end
end
end
private
def prometheus_adapter
return unless cluster&.application_prometheus&.installed?
cluster.application_prometheus
end
end
end
end
require 'spec_helper'
describe Oauth::ApplicationsController do
let(:user) { create(:user) }
context 'project members' do
before do
sign_in(user)
end
describe 'POST #create' do
it 'logs the audit event' do
stub_licensed_features(extended_audit_events: true)
sign_in(user)
application = build(:oauth_application)
application_attributes = application.attributes.merge(scopes: [])
expect { post :create, doorkeeper_application: application_attributes }.to change { SecurityEvent.count }.by(1)
end
end
end
end
require 'spec_helper'
describe Profiles::KeysController do
let(:user) { create(:user) }
describe '#create' do
it 'logs the audit event' do
stub_licensed_features(extended_audit_events: true)
sign_in(user)
key = build(:key)
expect { post :create, key: key.attributes }.to change { SecurityEvent.count }.by(1)
end
end
end
require 'spec_helper'
describe Projects::ClustersController do
include AccessMatchersForController
set(:project) { create(:project) }
describe 'GET metrics' do
let(:cluster) { create(:cluster, :provided_by_gcp, projects: [project]) }
describe 'functionality' do
let(:user) { create(:user) }
before do
project.add_master(user)
sign_in(user)
end
context "Can't query Prometheus" do
it 'returns not found' do
go
expect(response).to have_gitlab_http_status(:not_found)
end
end
context 'can query Prometheus' do
let(:prometheus_adapter) { double('prometheus_adapter', can_query?: true, query: nil) }
before do
allow(controller).to receive(:prometheus_adapter).and_return(prometheus_adapter)
end
it 'queries cluster metrics' do
go
expect(prometheus_adapter).to have_received(:query).with(:cluster)
end
context 'when response has content' do
let(:query_response) { { response: nil } }
before do
allow(prometheus_adapter).to receive(:query).and_return(query_response)
end
it 'returns prometheus query response' do
go
expect(response).to have_gitlab_http_status(:ok)
expect(response.body).to eq(query_response.to_json)
end
end
context 'when response has no content' do
let(:query_response) { {} }
before do
allow(prometheus_adapter).to receive(:query).and_return(query_response)
end
it 'returns prometheus query response' do
go
expect(response).to have_gitlab_http_status(:no_content)
end
end
end
end
def go
get :metrics, format: :json,
namespace_id: project.namespace,
project_id: project,
id: cluster
end
describe 'security' do
let(:prometheus_adapter) { double('prometheus_adapter', can_query?: true, query: nil) }
before do
allow(controller).to receive(:prometheus_adapter).and_return(prometheus_adapter)
end
it { expect { go }.to be_allowed_for(:admin) }
it { expect { go }.to be_allowed_for(:owner).of(project) }
it { expect { go }.to be_allowed_for(:master).of(project) }
it { expect { go }.to be_denied_for(:developer).of(project) }
it { expect { go }.to be_denied_for(:reporter).of(project) }
it { expect { go }.to be_denied_for(:guest).of(project) }
it { expect { go }.to be_denied_for(:user) }
it { expect { go }.to be_denied_for(:external) }
end
end
end
require 'spec_helper'
describe Projects::EnvironmentsController do
include KubernetesHelpers
set(:user) { create(:user) }
set(:project) { create(:project) }
set(:environment) do
create(:environment, name: 'production', project: project)
end
before do
project.add_master(user)
sign_in(user)
end
describe 'GET index' do
context 'when requesting JSON response for folders' do
before do
allow_any_instance_of(Environment).to receive(:has_terminals?).and_return(true)
allow_any_instance_of(Environment).to receive(:rollout_status).and_return(kube_deployment_rollout_status)
create(:environment, project: project,
name: 'staging/review-1',
state: :available)
create(:environment, project: project,
name: 'staging/review-2',
state: :available)
create(:environment, project: project,
name: 'staging/review-3',
state: :stopped)
end
let(:environments) { json_response['environments'] }
context 'when requesting available environments scope' do
before do
stub_licensed_features(deploy_board: true)
get :index, environment_params(format: :json, scope: :available)
end
it 'responds with matching schema' do
expect(response).to match_response_schema('environments', dir: 'ee')
end
it 'responds with a payload describing available environments' do
expect(environments.count).to eq 2
expect(environments.first['name']).to eq 'production'
expect(environments.first['latest']['rollout_status']).to be_present
expect(environments.second['name']).to eq 'staging'
expect(environments.second['size']).to eq 2
expect(environments.second['latest']['name']).to eq 'staging/review-2'
expect(environments.second['latest']['rollout_status']).to be_present
end
end
context 'when license does not has the GitLab_DeployBoard add-on' do
before do
stub_licensed_features(deploy_board: false)
get :index, environment_params(format: :json)
end
it 'does not return the rollout_status_path attribute' do
expect(environments.first['latest']['rollout_status']).not_to be_present
expect(environments.second['latest']['rollout_status']).not_to be_present
end
end
end
end
def environment_params(opts = {})
opts.reverse_merge(namespace_id: project.namespace,
project_id: project,
id: environment.id)
end
end
require 'spec_helper'
describe Projects::ImportsController do
let(:user) { create(:user) }
let(:project) { create(:project) }
before do
sign_in(user)
project.add_master(user)
end
context 'POST #create' do
context 'mirror user is not the current user' do
it 'should only assign the current user' do
allow_any_instance_of(EE::Project).to receive(:add_import_job)
new_user = create(:user)
project.add_master(new_user)
post :create, namespace_id: project.namespace.to_param,
project_id: project,
project: { mirror: true, mirror_user_id: new_user.id, import_url: 'http://local.dev' },
format: :json
expect(project.reload.mirror).to eq(true)
expect(project.reload.mirror_user.id).to eq(user.id)
end
end
end
end
require('spec_helper')
require 'spec_helper'
describe Projects::IssuesController do
include Rails.application.routes.url_helpers
......@@ -248,4 +248,68 @@ describe Projects::IssuesController do
end
end
end
describe 'GET #discussions' do
let(:issue) { create(:issue, project: project) }
let!(:discussion) { create(:discussion_note_on_issue, noteable: issue, project: issue.project) }
context 'with a related system note' do
let(:confidential_issue) { create(:issue, :confidential, project: project) }
let!(:system_note) { SystemNoteService.relate_issue(issue, confidential_issue, user) }
shared_examples 'user can see confidential issue' do |access_level|
context "when a user is a #{access_level}" do
before do
project.add_user(user, access_level)
end
it 'displays related notes' do
get :discussions, namespace_id: project.namespace, project_id: project, id: issue.iid
discussions = json_response
notes = discussions.flat_map {|d| d['notes']}
expect(discussions.count).to equal(2)
expect(notes).to include(a_hash_including('id' => system_note.id))
end
end
end
shared_examples 'user cannot see confidential issue' do |access_level|
context "when a user is a #{access_level}" do
before do
project.add_user(user, access_level)
end
it 'redacts note related to a confidential issue' do
get :discussions, namespace_id: project.namespace, project_id: project, id: issue.iid
discussions = json_response
notes = discussions.flat_map {|d| d['notes']}
expect(discussions.count).to equal(1)
expect(notes).not_to include(a_hash_including('id' => system_note.id))
end
end
end
context 'when authenticated' do
before do
sign_in(user)
end
%i(reporter developer master).each do |access|
it_behaves_like 'user can see confidential issue', access
end
it_behaves_like 'user cannot see confidential issue', :guest
end
context 'when unauthenticated' do
let(:project) { create(:project, :public) }
it_behaves_like 'user cannot see confidential issue', Gitlab::Access::NO_ACCESS
end
end
end
end
require 'spec_helper'
describe Projects::Settings::IntegrationsController do
let(:project) { create(:project, :public) }
let(:user) { create(:user) }
before do
project.add_master(user)
sign_in(user)
end
shared_examples 'endpoint with some disabled services' do
it 'has some disabled services' do
get :show, namespace_id: project.namespace, project_id: project
expect(active_services).not_to include(*disabled_services)
end
end
shared_examples 'endpoint without disabled services' do
it 'does not have disabled services' do
get :show, namespace_id: project.namespace, project_id: project
expect(active_services).to include(*disabled_services)
end
end
context 'Sets correct services list' do
let(:active_services) { assigns(:services).map(&:type) }
let(:disabled_services) { %w(JenkinsService JenkinsDeprecatedService) }
it 'enables SlackSlashCommandsService and disables GitlabSlackApplication' do
get :show, namespace_id: project.namespace, project_id: project
expect(active_services).to include('SlackSlashCommandsService')
expect(active_services).not_to include('GitlabSlackApplicationService')
end
it 'enables GitlabSlackApplication and disables SlackSlashCommandsService' do
stub_application_setting(slack_app_enabled: true)
allow(::Gitlab).to receive(:com?).and_return(true)
get :show, namespace_id: project.namespace, project_id: project
expect(active_services).to include('GitlabSlackApplicationService')
expect(active_services).not_to include('SlackSlashCommandsService')
end
context 'without a license key' do
before do
License.destroy_all
end
it_behaves_like 'endpoint with some disabled services'
end
context 'with a license key' do
let(:namespace) { create(:group, :private) }
let(:project) { create(:project, :private, namespace: namespace) }
context 'when checking of namespace plan is enabled' do
before do
allow(Gitlab::CurrentSettings.current_application_settings).to receive(:should_check_namespace_plan?) { true }
end
context 'and namespace does not have a plan' do
it_behaves_like 'endpoint with some disabled services'
end
context 'and namespace has a plan' do
let(:namespace) { create(:group, :private, plan: :bronze_plan) }
it_behaves_like 'endpoint without disabled services'
end
end
context 'when checking of namespace plan is not enabled' do
before do
allow(Gitlab::CurrentSettings.current_application_settings).to receive(:should_check_namespace_plan?) { false }
end
it_behaves_like 'endpoint without disabled services'
end
end
end
end
......@@ -138,8 +138,8 @@ module Gitlab
def ee_branch_presence_check!
ee_remotes.keys.each do |remote|
[ee_branch_prefix, ee_branch_suffix].each do |branch|
_, status = step("Fetching #{remote}/#{ee_branch_prefix}", %W[git fetch #{remote} #{branch}])
[ce_branch, ee_branch_prefix, ee_branch_suffix].each do |branch|
_, status = step("Fetching #{remote}/#{branch}", %W[git fetch #{remote} #{branch}])
if status.zero?
@ee_remote_with_branch = remote
......
......@@ -24,18 +24,5 @@ describe Oauth::ApplicationsController do
expect(response).to redirect_to(profile_path)
end
end
describe 'POST #create' do
it 'logs the audit event' do
stub_licensed_features(extended_audit_events: true)
sign_in(user)
application = build(:oauth_application)
application_attributes = application.attributes.merge(scopes: [])
expect { post :create, doorkeeper_application: application_attributes }.to change { SecurityEvent.count }.by(1)
end
end
end
end
......@@ -66,16 +66,4 @@ describe Profiles::KeysController do
end
end
end
describe '#create' do
it 'logs the audit event' do
stub_licensed_features(extended_audit_events: true)
sign_in(user)
key = build(:key)
expect { post :create, key: key.attributes }.to change { SecurityEvent.count }.by(1)
end
end
end
......@@ -45,8 +45,7 @@ describe Projects::BoardsController do
context 'when format is JSON' do
it 'returns a list of project boards' do
create(:board, project: project, milestone: create(:milestone, project: project))
create(:board, project: project, milestone_id: Milestone::Upcoming.id)
create_list(:board, 2, project: project)
list_boards format: :json
......
......@@ -2,7 +2,6 @@ require 'spec_helper'
describe Projects::ClustersController do
include AccessMatchersForController
include GoogleApi::CloudPlatformHelpers
set(:project) { create(:project) }
......@@ -155,93 +154,6 @@ describe Projects::ClustersController do
end
end
describe 'GET metrics' do
let(:cluster) { create(:cluster, :provided_by_gcp, projects: [project]) }
describe 'functionality' do
let(:user) { create(:user) }
before do
project.add_master(user)
sign_in(user)
end
context "Can't query Prometheus" do
it 'returns not found' do
go
expect(response).to have_gitlab_http_status(:not_found)
end
end
context 'can query Prometheus' do
let(:prometheus_adapter) { double('prometheus_adapter', can_query?: true, query: nil) }
before do
allow(controller).to receive(:prometheus_adapter).and_return(prometheus_adapter)
end
it 'queries cluster metrics' do
go
expect(prometheus_adapter).to have_received(:query).with(:cluster)
end
context 'when response has content' do
let(:query_response) { { response: nil } }
before do
allow(prometheus_adapter).to receive(:query).and_return(query_response)
end
it 'returns prometheus query response' do
go
expect(response).to have_gitlab_http_status(:ok)
expect(response.body).to eq(query_response.to_json)
end
end
context 'when response has no content' do
let(:query_response) { {} }
before do
allow(prometheus_adapter).to receive(:query).and_return(query_response)
end
it 'returns prometheus query response' do
go
expect(response).to have_gitlab_http_status(:no_content)
end
end
end
end
def go
get :metrics, format: :json,
namespace_id: project.namespace,
project_id: project,
id: cluster
end
describe 'security' do
let(:prometheus_adapter) { double('prometheus_adapter', can_query?: true, query: nil) }
before do
allow(controller).to receive(:prometheus_adapter).and_return(prometheus_adapter)
end
it { expect { go }.to be_allowed_for(:admin) }
it { expect { go }.to be_allowed_for(:owner).of(project) }
it { expect { go }.to be_allowed_for(:master).of(project) }
it { expect { go }.to be_denied_for(:developer).of(project) }
it { expect { go }.to be_denied_for(:reporter).of(project) }
it { expect { go }.to be_denied_for(:guest).of(project) }
it { expect { go }.to be_denied_for(:user) }
it { expect { go }.to be_denied_for(:external) }
end
end
describe 'PUT update' do
context 'when cluster is provided by GCP' do
let(:cluster) { create(:cluster, :provided_by_gcp, projects: [project]) }
......
require 'spec_helper'
describe Projects::EnvironmentsController do
include KubernetesHelpers
set(:user) { create(:user) }
set(:project) { create(:project) }
......@@ -34,9 +32,6 @@ describe Projects::EnvironmentsController do
context 'when requesting JSON response for folders' do
before do
allow_any_instance_of(Environment).to receive(:has_terminals?).and_return(true)
allow_any_instance_of(Environment).to receive(:rollout_status).and_return(kube_deployment_rollout_status)
create(:environment, project: project,
name: 'staging/review-1',
state: :available)
......@@ -54,23 +49,15 @@ describe Projects::EnvironmentsController do
context 'when requesting available environments scope' do
before do
stub_licensed_features(deploy_board: true)
get :index, environment_params(format: :json, scope: :available)
end
it 'responds with matching schema' do
expect(response).to match_response_schema('environments', dir: 'ee')
end
it 'responds with a payload describing available environments' do
expect(environments.count).to eq 2
expect(environments.first['name']).to eq 'production'
expect(environments.first['latest']['rollout_status']).to be_present
expect(environments.second['name']).to eq 'staging'
expect(environments.second['size']).to eq 2
expect(environments.second['latest']['name']).to eq 'staging/review-2'
expect(environments.second['latest']['rollout_status']).to be_present
end
it 'contains values describing environment scopes sizes' do
......@@ -101,19 +88,6 @@ describe Projects::EnvironmentsController do
expect(json_response['stopped_count']).to eq 1
end
end
context 'when license does not has the GitLab_DeployBoard add-on' do
before do
stub_licensed_features(deploy_board: false)
get :index, environment_params(format: :json)
end
it 'does not return the rollout_status_path attribute' do
expect(environments.first['latest']['rollout_status']).not_to be_present
expect(environments.second['latest']['rollout_status']).not_to be_present
end
end
end
end
......
......@@ -11,13 +11,6 @@ describe Projects::ImportsController do
describe 'GET #show' do
context 'when repository does not exists' do
let(:project) { create(:project) }
before do
sign_in(user)
project.add_master(user)
end
it 'renders template' do
get :show, namespace_id: project.namespace.to_param, project_id: project
......@@ -126,23 +119,4 @@ describe Projects::ImportsController do
end
end
end
context 'POST #create' do
context 'mirror user is not the current user' do
it 'should only assign the current user' do
allow_any_instance_of(EE::Project).to receive(:add_import_job)
new_user = create(:user)
project.add_master(new_user)
post :create, namespace_id: project.namespace.to_param,
project_id: project,
project: { mirror: true, mirror_user_id: new_user.id, import_url: 'http://local.dev' },
format: :json
expect(project.reload.mirror).to eq(true)
expect(project.reload.mirror_user.id).to eq(user.id)
end
end
end
end
require('spec_helper')
require 'spec_helper'
describe Projects::IssuesController do
let(:project) { create(:project) }
......@@ -993,71 +993,6 @@ describe Projects::IssuesController do
expect(json_response.first.keys).to match_array(%w[id reply_id expanded notes diff_discussion individual_note resolvable resolved])
end
it 'filters notes that the user should not see' do
get :discussions, namespace_id: project.namespace, project_id: project, id: issue.iid
expect(JSON.parse(response.body).count).to eq(1)
end
end
context 'with a related system note' do
let(:confidential_issue) { create(:issue, :confidential, project: project) }
let!(:system_note) { SystemNoteService.relate_issue(issue, confidential_issue, user) }
shared_examples 'user can see confidential issue' do |access_level|
context "when a user is a #{access_level}" do
before do
project.add_user(user, access_level)
end
it 'displays related notes' do
get :discussions, namespace_id: project.namespace, project_id: project, id: issue.iid
discussions = json_response
notes = discussions.flat_map {|d| d['notes']}
expect(discussions.count).to equal(2)
expect(notes).to include(a_hash_including('id' => system_note.id))
end
end
end
shared_examples 'user cannot see confidential issue' do |access_level|
context "when a user is a #{access_level}" do
before do
project.add_user(user, access_level)
end
it 'redacts note related to a confidential issue' do
get :discussions, namespace_id: project.namespace, project_id: project, id: issue.iid
discussions = json_response
notes = discussions.flat_map {|d| d['notes']}
expect(discussions.count).to equal(1)
expect(notes).not_to include(a_hash_including('id' => system_note.id))
end
end
end
context 'when authenticated' do
before do
sign_in(user)
end
%i(reporter developer master).each do |access|
it_behaves_like 'user can see confidential issue', access
end
it_behaves_like 'user cannot see confidential issue', :guest
end
context 'when unauthenticated' do
let(:project) { create(:project, :public) }
it_behaves_like 'user cannot see confidential issue', Gitlab::Access::NO_ACCESS
end
context 'with cross-reference system note', :request_store do
let(:new_issue) { create(:issue) }
let(:cross_reference) { "mentioned in #{new_issue.to_reference(issue.project)}" }
......@@ -1066,6 +1001,12 @@ describe Projects::IssuesController do
create(:discussion_note_on_issue, :system, noteable: issue, project: issue.project, note: cross_reference)
end
it 'filters notes that the user should not see' do
get :discussions, namespace_id: project.namespace, project_id: project, id: issue.iid
expect(JSON.parse(response.body).count).to eq(1)
end
it 'does not result in N+1 queries' do
# Instantiate the controller variables to ensure QueryRecorder has an accurate base count
get :discussions, namespace_id: project.namespace, project_id: project, id: issue.iid
......
......@@ -275,7 +275,6 @@ describe Projects::MergeRequestsController do
namespace_id: project.namespace,
project_id: project,
id: merge_request.iid,
squash: false,
format: 'json'
}
end
......
......@@ -184,7 +184,7 @@ describe Projects::ProjectMembersController do
project.add_master(user)
end
it 'does not remove himself from the project' do
it 'cannot remove themselves from the project' do
delete :leave, namespace_id: project.namespace,
project_id: project
......
......@@ -7,8 +7,6 @@ describe Projects::Prometheus::MetricsController do
let(:prometheus_adapter) { double('prometheus_adapter', can_query?: true) }
before do
allow(controller).to receive(:project).and_return(project)
project.add_master(user)
sign_in(user)
end
......
......@@ -17,79 +17,4 @@ describe Projects::Settings::IntegrationsController do
expect(response).to render_template(:show)
end
end
shared_examples 'endpoint with some disabled services' do
it 'has some disabled services' do
get :show, namespace_id: project.namespace, project_id: project
expect(active_services).not_to include(*disabled_services)
end
end
shared_examples 'endpoint without disabled services' do
it 'does not have disabled services' do
get :show, namespace_id: project.namespace, project_id: project
expect(active_services).to include(*disabled_services)
end
end
context 'Sets correct services list' do
let(:active_services) { assigns(:services).map(&:type) }
let(:disabled_services) { %w(JenkinsService JenkinsDeprecatedService) }
it 'enables SlackSlashCommandsService and disables GitlabSlackApplication' do
get :show, namespace_id: project.namespace, project_id: project
expect(active_services).to include('SlackSlashCommandsService')
expect(active_services).not_to include('GitlabSlackApplicationService')
end
it 'enables GitlabSlackApplication and disables SlackSlashCommandsService' do
stub_application_setting(slack_app_enabled: true)
allow(::Gitlab).to receive(:com?).and_return(true)
get :show, namespace_id: project.namespace, project_id: project
expect(active_services).to include('GitlabSlackApplicationService')
expect(active_services).not_to include('SlackSlashCommandsService')
end
context 'without a license key' do
before do
License.destroy_all
end
it_behaves_like 'endpoint with some disabled services'
end
context 'with a license key' do
let(:namespace) { create(:group, :private) }
let(:project) { create(:project, :private, namespace: namespace) }
context 'when checking of namespace plan is enabled' do
before do
allow(Gitlab::CurrentSettings.current_application_settings).to receive(:should_check_namespace_plan?) { true }
end
context 'and namespace does not have a plan' do
it_behaves_like 'endpoint with some disabled services'
end
context 'and namespace has a plan' do
let(:namespace) { create(:group, :private, plan: :bronze_plan) }
it_behaves_like 'endpoint without disabled services'
end
end
context 'when checking of namespace plan is not enabled' do
before do
allow(Gitlab::CurrentSettings.current_application_settings).to receive(:should_check_namespace_plan?) { false }
end
it_behaves_like 'endpoint without disabled services'
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment