Skip to content
Snippets Groups Projects
Commit e207f4ad authored by Brett Walker's avatar Brett Walker
Browse files

Fix specs checking for an error to be raised

now those exceptions are being returned in GraphQL,
not raised
parent 18fce204
No related branches found
No related tags found
No related merge requests found
Pipeline #348717749 failed
Pipeline: GitLab

#348717817

    Showing
    with 100 additions and 89 deletions
    ...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
    module Types module Types
    # rubocop: disable Graphql/AuthorizeTypes # rubocop: disable Graphql/AuthorizeTypes
    class QueryComplexityType < ::Types::BaseObject class QueryComplexityType < ::Types::BaseObject
    ANALYZER = GraphQL::Analysis::QueryComplexity.new { |_query, complexity| complexity } ANALYZER = GraphQL::Analysis::AST::QueryComplexity
    graphql_name 'QueryComplexity' graphql_name 'QueryComplexity'
    ...@@ -23,7 +23,7 @@ class QueryComplexityType < ::Types::BaseObject ...@@ -23,7 +23,7 @@ class QueryComplexityType < ::Types::BaseObject
    description: 'GraphQL query complexity score.' description: 'GraphQL query complexity score.'
    def score def score
    ::GraphQL::Analysis.analyze_query(query, [ANALYZER]).first ::GraphQL::Analysis::AST.analyze_query(query, [ANALYZER]).first
    end end
    end end
    # rubocop: enable Graphql/AuthorizeTypes # rubocop: enable Graphql/AuthorizeTypes
    ......
    ...@@ -28,10 +28,9 @@ ...@@ -28,10 +28,9 @@
    end end
    shared_examples 'raises error on mutually exclusive arguments' do shared_examples 'raises error on mutually exclusive arguments' do
    it 'raises an exception if mutually exclusive arguments are present' do it 'returns an error if mutually exclusive arguments are present' do
    expect do expect(resolve_board_list_issues({ filters: filters }))
    resolve_board_list_issues({ filters: filters }) .to be_instance_of(Gitlab::Graphql::Errors::ArgumentError)
    end.to raise_error(Gitlab::Graphql::Errors::ArgumentError)
    end end
    end end
    ......
    ...@@ -115,10 +115,11 @@ ...@@ -115,10 +115,11 @@
    expect(resolve_issues(iteration_wildcard_id: 'NONE')).to contain_exactly(issue2, issue4) expect(resolve_issues(iteration_wildcard_id: 'NONE')).to contain_exactly(issue2, issue4)
    end end
    it 'raises a mutually exclusive filter error when wildcard and list are provided' do it 'returns a mutually exclusive filter error when wildcard and list are provided' do
    expect do result = resolve_issues(iteration_id: [iteration1.to_global_id], iteration_wildcard_id: 'CURRENT')
    resolve_issues(iteration_id: [iteration1.to_global_id], iteration_wildcard_id: 'CURRENT')
    end.to raise_error(Gitlab::Graphql::Errors::ArgumentError, 'only one of [iterationId, iterationWildcardId] arguments is allowed at the same time.') expect(result).to be_instance_of(Gitlab::Graphql::Errors::ArgumentError)
    expect(result.message).to eq('only one of [iterationId, iterationWildcardId] arguments is allowed at the same time.')
    end end
    end end
    ...@@ -178,10 +179,11 @@ ...@@ -178,10 +179,11 @@
    expect(resolve_issues(not: { iteration_id: [iteration1.to_global_id] })).to contain_exactly(issue2, issue3, issue4) expect(resolve_issues(not: { iteration_id: [iteration1.to_global_id] })).to contain_exactly(issue2, issue3, issue4)
    end end
    it 'raises a mutually exclusive filter error when wildcard and list are provided' do it 'returns a mutually exclusive filter error when wildcard and list are provided' do
    expect do result = resolve_issues(not: { iteration_id: [iteration1.to_global_id], iteration_wildcard_id: 'CURRENT' })
    resolve_issues(not: { iteration_id: [iteration1.to_global_id], iteration_wildcard_id: 'CURRENT' })
    end.to raise_error(Gitlab::Graphql::Errors::ArgumentError, 'only one of [iterationId, iterationWildcardId] arguments is allowed at the same time.') expect(result).to be_instance_of(Gitlab::Graphql::Errors::ArgumentError)
    expect(result.message).to eq('only one of [iterationId, iterationWildcardId] arguments is allowed at the same time.')
    end end
    end end
    ......
    ...@@ -46,7 +46,7 @@ ...@@ -46,7 +46,7 @@
    context 'when user does not have permissions' do context 'when user does not have permissions' do
    it 'does not allow the move' do it 'does not allow the move' do
    expect { subject }.to raise_error expect(subject).to be_instance_of(Gitlab::Graphql::Errors::ResourceNotAvailable)
    end end
    end end
    ......
    ...@@ -20,12 +20,11 @@ def resolve_current_license(current_user: admin) ...@@ -20,12 +20,11 @@ def resolve_current_license(current_user: admin)
    end end
    context 'when current user is unauthorized' do context 'when current user is unauthorized' do
    it 'raises error' do it 'returns an error' do
    unauthorized_user = create(:user) unauthorized_user = create(:user)
    expect do expect(resolve_current_license(current_user: unauthorized_user))
    resolve_current_license(current_user: unauthorized_user) .to be_instance_of(Gitlab::Graphql::Errors::ResourceNotAvailable)
    end.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable)
    end end
    end end
    ......
    ...@@ -20,12 +20,11 @@ def resolve_license_history_entries(current_user: admin) ...@@ -20,12 +20,11 @@ def resolve_license_history_entries(current_user: admin)
    end end
    context 'when current user is unauthorized' do context 'when current user is unauthorized' do
    it 'raises error' do it 'returns an error' do
    unauthorized_user = create(:user) unauthorized_user = create(:user)
    expect do expect(resolve_license_history_entries(current_user: unauthorized_user))
    resolve_license_history_entries(current_user: unauthorized_user) .to be_instance_of(Gitlab::Graphql::Errors::ResourceNotAvailable)
    end.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable)
    end end
    end end
    ......
    ...@@ -40,8 +40,8 @@ def resolve_enabled_namespaces(args = {}, context = {}) ...@@ -40,8 +40,8 @@ def resolve_enabled_namespaces(args = {}, context = {})
    context 'as a non-admin user' do context 'as a non-admin user' do
    let(:current_user) { user } let(:current_user) { user }
    it 'raises ResourceNotAvailable error' do it 'returns ResourceNotAvailable error' do
    expect { resolved_enabled_namespaces }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable) expect(resolved_enabled_namespaces).to be_instance_of(Gitlab::Graphql::Errors::ResourceNotAvailable)
    end end
    end end
    ...@@ -50,8 +50,8 @@ def resolve_enabled_namespaces(args = {}, context = {}) ...@@ -50,8 +50,8 @@ def resolve_enabled_namespaces(args = {}, context = {})
    stub_licensed_features(instance_level_devops_adoption: false) stub_licensed_features(instance_level_devops_adoption: false)
    end end
    it 'raises ResourceNotAvailable error' do it 'returns ResourceNotAvailable error' do
    expect { resolved_enabled_namespaces }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable) expect(resolved_enabled_namespaces).to be_instance_of(Gitlab::Graphql::Errors::ResourceNotAvailable)
    end end
    end end
    end end
    ...@@ -75,8 +75,8 @@ def resolve_enabled_namespaces(args = {}, context = {}) ...@@ -75,8 +75,8 @@ def resolve_enabled_namespaces(args = {}, context = {})
    root_group_1.add_guest(user) root_group_1.add_guest(user)
    end end
    it 'raises ResourceNotAvailable error' do it 'returns ResourceNotAvailable error' do
    expect { resolved_enabled_namespaces }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable) expect(resolved_enabled_namespaces).to be_instance_of(Gitlab::Graphql::Errors::ResourceNotAvailable)
    end end
    end end
    ...@@ -85,8 +85,8 @@ def resolve_enabled_namespaces(args = {}, context = {}) ...@@ -85,8 +85,8 @@ def resolve_enabled_namespaces(args = {}, context = {})
    stub_licensed_features(instance_level_devops_adoption: false) stub_licensed_features(instance_level_devops_adoption: false)
    end end
    it 'raises ResourceNotAvailable error' do it 'returns ResourceNotAvailable error' do
    expect { resolved_enabled_namespaces }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable) expect(resolved_enabled_namespaces).to be_instance_of(Gitlab::Graphql::Errors::ResourceNotAvailable)
    end end
    end end
    end end
    ......
    ...@@ -93,10 +93,9 @@ ...@@ -93,10 +93,9 @@
    resolve_board_epics(group_board, { issue_filters: filters }) resolve_board_epics(group_board, { issue_filters: filters })
    end end
    it 'raises an exception if both epic_id and epic_wildcard_id are present' do it 'returns an exception if both epic_id and epic_wildcard_id are present' do
    expect do expect(resolve_board_epics(group_board, { issue_filters: { epic_id: epic1.to_global_id, epic_wildcard_id: 'NONE' } }))
    resolve_board_epics(group_board, { issue_filters: { epic_id: epic1.to_global_id, epic_wildcard_id: 'NONE' } }) .to be_instance_of(Gitlab::Graphql::Errors::ArgumentError)
    end.to raise_error(Gitlab::Graphql::Errors::ArgumentError)
    end end
    it 'accepts epic global id' do it 'accepts epic global id' do
    ......
    ...@@ -32,8 +32,8 @@ ...@@ -32,8 +32,8 @@
    stub_licensed_features(epics: true) stub_licensed_features(epics: true)
    end end
    it 'raises an error if user cannot read epic boards' do it 'returns an error if user cannot read epic boards' do
    expect { result }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable) expect(result).to be_instance_of(Gitlab::Graphql::Errors::ResourceNotAvailable)
    end end
    context 'when user is member of the group' do context 'when user is member of the group' do
    ......
    ...@@ -25,8 +25,8 @@ ...@@ -25,8 +25,8 @@
    stub_licensed_features(epics: true) stub_licensed_features(epics: true)
    end end
    it 'raises an error if user cannot read epic lists' do it 'returns an error if user cannot read epic lists' do
    expect { result }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable) expect(result).to be_instance_of(Gitlab::Graphql::Errors::ResourceNotAvailable)
    end end
    context 'when user is member of the group' do context 'when user is member of the group' do
    ......
    ...@@ -162,16 +162,16 @@ ...@@ -162,16 +162,16 @@
    context 'when the requested date range is too large' do context 'when the requested date range is too large' do
    let(:args) { { metric: 'deployment_frequency', start_date: '2020-01-01'.to_datetime, end_date: '2021-05-01'.to_datetime } } let(:args) { { metric: 'deployment_frequency', start_date: '2020-01-01'.to_datetime, end_date: '2021-05-01'.to_datetime } }
    it 'raises an error' do it 'returns an error' do
    expect { resolve_metrics }.to raise_error('Date range must be shorter than 92 days.') expect(resolve_metrics.message).to eq('Date range must be shorter than 92 days.')
    end end
    end end
    context 'when the start date equal to or later than the end date' do context 'when the start date equal to or later than the end date' do
    let(:args) { { metric: 'deployment_frequency', start_date: '2021-04-01'.to_datetime, end_date: '2021-03-01'.to_datetime } } let(:args) { { metric: 'deployment_frequency', start_date: '2021-04-01'.to_datetime, end_date: '2021-03-01'.to_datetime } }
    it 'raises an error' do it 'returns an error' do
    expect { resolve_metrics }.to raise_error('The start date must be ealier than the end date.') expect(resolve_metrics.message).to eq('The start date must be ealier than the end date.')
    end end
    end end
    ......
    ...@@ -118,9 +118,11 @@ ...@@ -118,9 +118,11 @@
    context 'with in param' do context 'with in param' do
    it 'returns an error if param search is missing' do it 'returns an error if param search is missing' do
    result = resolve_epics(in: ['title'])
    error_message = "`search` should be present when including the `in` argument" error_message = "`search` should be present when including the `in` argument"
    expect { resolve_epics(in: ['title']) }
    .to raise_error(Gitlab::Graphql::Errors::ArgumentError, error_message) expect(result).to be_instance_of(Gitlab::Graphql::Errors::ArgumentError)
    expect(result.message).to eq(error_message)
    end end
    it 'filters epics by description only' do it 'filters epics by description only' do
    ......
    ...@@ -93,7 +93,10 @@ ...@@ -93,7 +93,10 @@
    end end
    it 'raises a GraphQL exception' do it 'raises a GraphQL exception' do
    expect { batch_sync { resolve_external_issue({}) } }.to raise_error(GraphQL::ExecutionError, 'Jira service not configured.') result = batch_sync { resolve_external_issue({}) }
    expect(result).to be_instance_of(GraphQL::ExecutionError)
    expect(result.message).to eq('Jira service not configured.')
    end end
    end end
    ...@@ -105,7 +108,10 @@ ...@@ -105,7 +108,10 @@
    end end
    it 'raises a GraphQL exception' do it 'raises a GraphQL exception' do
    expect { batch_sync { resolve_external_issue({}) } }.to raise_error(GraphQL::ExecutionError, 'Jira service unavailable.') result = batch_sync { resolve_external_issue({}) }
    expect(result).to be_instance_of(GraphQL::ExecutionError)
    expect(result.message).to eq('Jira service unavailable.')
    end end
    end end
    ......
    ...@@ -31,8 +31,8 @@ ...@@ -31,8 +31,8 @@
    context 'when an error occurs while finding shifts' do context 'when an error occurs while finding shifts' do
    subject(:shifts) { sync(resolve_oncall_shifts(args, current_user: nil)) } subject(:shifts) { sync(resolve_oncall_shifts(args, current_user: nil)) }
    it 'raises ResourceNotAvailable error' do it 'returns ResourceNotAvailable error' do
    expect { subject }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable) expect(subject).to be_instance_of(Gitlab::Graphql::Errors::ResourceNotAvailable)
    end end
    end end
    ......
    ...@@ -13,10 +13,9 @@ ...@@ -13,10 +13,9 @@
    shared_examples 'fetches iteration cadences' do shared_examples 'fetches iteration cadences' do
    context 'when user does not have permissions to read iterations cadences' do context 'when user does not have permissions to read iterations cadences' do
    it 'raises error' do it 'returns error' do
    expect do expect(resolve_group_iteration_cadences)
    resolve_group_iteration_cadences .to be_instance_of(Gitlab::Graphql::Errors::ResourceNotAvailable)
    end.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable)
    end end
    end end
    ...@@ -52,9 +51,8 @@ ...@@ -52,9 +51,8 @@
    it 'raises error' do it 'raises error' do
    project.add_developer(current_user) project.add_developer(current_user)
    expect do expect(resolve_group_iteration_cadences({}, project, { current_user: current_user }))
    resolve_group_iteration_cadences({}, project, { current_user: current_user }) .to be_instance_of(Gitlab::Graphql::Errors::ResourceNotAvailable)
    end.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable)
    end end
    end end
    end end
    ......
    ...@@ -122,10 +122,11 @@ def resolve_group_iterations(args = {}, obj = group, context = { current_user: c ...@@ -122,10 +122,11 @@ def resolve_group_iterations(args = {}, obj = group, context = { current_user: c
    context 'by timeframe' do context 'by timeframe' do
    context 'when start_date and end_date are present' do context 'when start_date and end_date are present' do
    context 'when start date is after end_date' do context 'when start date is after end_date' do
    it 'raises error' do it 'returns an error' do
    expect do result = resolve_group_iterations(timeframe: { start: now, end: now - 2.days })
    resolve_group_iterations(timeframe: { start: now, end: now - 2.days })
    end.to raise_error(Gitlab::Graphql::Errors::ArgumentError, "start must be before end") expect(result).to be_instance_of(Gitlab::Graphql::Errors::ArgumentError)
    expect(result.messsage).to eq('start must be before end')
    end end
    end end
    end end
    ...@@ -134,38 +135,40 @@ def resolve_group_iterations(args = {}, obj = group, context = { current_user: c ...@@ -134,38 +135,40 @@ def resolve_group_iterations(args = {}, obj = group, context = { current_user: c
    context 'by dates' do context 'by dates' do
    context 'when start_date and end_date are present' do context 'when start_date and end_date are present' do
    context 'when start date is after end_date' do context 'when start date is after end_date' do
    it 'raises error' do it 'returns an error' do
    expect do result = resolve_group_iterations(start_date: now, end_date: now - 2.days)
    resolve_group_iterations(start_date: now, end_date: now - 2.days)
    end.to raise_error(Gitlab::Graphql::Errors::ArgumentError, "startDate is after endDate") expect(result).to be_instance_of(Gitlab::Graphql::Errors::ArgumentError)
    expect(result.message).to eq('startDate is after endDate')
    end end
    end end
    end end
    context 'when only start_date is present' do context 'when only start_date is present' do
    it 'raises error' do it 'returns an error' do
    expect do result = resolve_group_iterations(start_date: now)
    resolve_group_iterations(start_date: now)
    end.to raise_error(Gitlab::Graphql::Errors::ArgumentError, /Both startDate and endDate/) expect(result).to be_instance_of(Gitlab::Graphql::Errors::ArgumentError)
    expect(result.message).to match(/Both startDate and endDate/)
    end end
    end end
    context 'when only end_date is present' do context 'when only end_date is present' do
    it 'raises error' do it 'returns an error' do
    expect do result = resolve_group_iterations(end_date: now)
    resolve_group_iterations(end_date: now)
    end.to raise_error(Gitlab::Graphql::Errors::ArgumentError, /Both startDate and endDate/) expect(result).to be_instance_of(Gitlab::Graphql::Errors::ArgumentError)
    expect(result.message).to match(/Both startDate and endDate/)
    end end
    end end
    end end
    context 'when user cannot read iterations' do context 'when user cannot read iterations' do
    it 'raises error' do it 'returns an error' do
    unauthorized_user = create(:user) unauthorized_user = create(:user)
    expect do expect(resolve_group_iterations({}, group, { current_user: unauthorized_user }))
    resolve_group_iterations({}, group, { current_user: unauthorized_user }) .to be_instance_of(Gitlab::Graphql::Errors::ResourceNotAvailable)
    end.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable)
    end end
    end end
    end end
    ......
    ...@@ -37,8 +37,9 @@ ...@@ -37,8 +37,9 @@
    allow(kas_client).to receive(:list_agent_config_files).and_raise(GRPC::DeadlineExceeded) allow(kas_client).to receive(:list_agent_config_files).and_raise(GRPC::DeadlineExceeded)
    end end
    it 'raises a graphql error' do it 'returns a graphql error' do
    expect { subject }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable, 'GRPC::DeadlineExceeded') expect(subject).to be_instance_of(Gitlab::Graphql::Errors::ResourceNotAvailable)
    expect(subject.message).to eq('GRPC::DeadlineExceeded')
    end end
    end end
    ......
    ...@@ -54,8 +54,8 @@ ...@@ -54,8 +54,8 @@
    stub_licensed_features(threat_monitoring: false) stub_licensed_features(threat_monitoring: false)
    end end
    it 'raises ResourceNotAvailable error' do it 'returns ResourceNotAvailable error' do
    expect { resolve_network_policies }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable) expect(resolve_network_policies).to be_instance_of(Gitlab::Graphql::Errors::ResourceNotAvailable)
    end end
    end end
    ...@@ -67,8 +67,11 @@ ...@@ -67,8 +67,11 @@
    context 'when NetworkPolicies::ResourcesService is not executed successfully' do context 'when NetworkPolicies::ResourcesService is not executed successfully' do
    let(:service_result) { instance_double(ServiceResponse, success?: false, message: 'Error fetching the result') } let(:service_result) { instance_double(ServiceResponse, success?: false, message: 'Error fetching the result') }
    it 'raises Gitlab::Graphql::Errors::BaseError' do it 'returns Gitlab::Graphql::Errors::BaseError' do
    expect { resolve_network_policies }.to raise_error(Gitlab::Graphql::Errors::BaseError, 'Error fetching the result') result = resolve_network_policies
    expect(result).to be_instance_of(Gitlab::Graphql::Errors::BaseError)
    expect(result.message).to eq('Error fetching the result')
    end end
    end end
    ...@@ -123,8 +126,8 @@ ...@@ -123,8 +126,8 @@
    context 'when user is unauthorized' do context 'when user is unauthorized' do
    let(:user) { create(:user) } let(:user) { create(:user) }
    it 'raises ResourceNotAvailable error' do it 'returns ResourceNotAvailable error' do
    expect { resolve_network_policies }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable) expect(resolve_network_policies).to be_instance_of(Gitlab::Graphql::Errors::ResourceNotAvailable)
    end end
    end end
    end end
    ......
    ...@@ -37,7 +37,7 @@ ...@@ -37,7 +37,7 @@
    context 'user is unauthorized' do context 'user is unauthorized' do
    let(:user) { create(:user) } let(:user) { create(:user) }
    it { expect { resolve_path_locks }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable) } it { expect(resolve_path_locks).to be_instance_of(Gitlab::Graphql::Errors::ResourceNotAvailable) }
    end end
    end end
    end end
    ......
    ...@@ -41,8 +41,8 @@ ...@@ -41,8 +41,8 @@
    stub_licensed_features(security_orchestration_policies: false) stub_licensed_features(security_orchestration_policies: false)
    end end
    it 'raises ResourceNotAvailable error' do it 'returns ResourceNotAvailable error' do
    expect { resolve_scan_policies }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable) expect(resolve_scan_policies).to be_instance_of(Gitlab::Graphql::Errors::ResourceNotAvailable)
    end end
    end end
    ...@@ -67,8 +67,8 @@ ...@@ -67,8 +67,8 @@
    context 'when user is unauthorized' do context 'when user is unauthorized' do
    let(:user) { create(:user) } let(:user) { create(:user) }
    it 'raises ResourceNotAvailable error' do it 'returns ResourceNotAvailable error' do
    expect { resolve_scan_policies }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable) expect(resolve_scan_policies).to be_instance_of(Gitlab::Graphql::Errors::ResourceNotAvailable)
    end end
    end end
    ......
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Finish editing this message first!
    Please register or to comment