Commit 48652311 authored by Peter Leitzen's avatar Peter Leitzen

Merge branch '224499-add-graphql-authorizetype-cop-to-ee-namespaced-types' into 'master'

Resolve "Add Graphql/AuthorizeType cop to EE namespaced types"

See merge request gitlab-org/gitlab!35531
parents 8e6f1709 0e2f0750
......@@ -328,6 +328,9 @@ Cop/SidekiqOptionsQueue:
Graphql/AuthorizeTypes:
Enabled: true
Include:
- 'app/graphql/types/**/*'
- 'ee/app/graphql/types/**/*'
Exclude:
- 'spec/**/*.rb'
- 'ee/spec/**/*.rb'
......
......@@ -7,8 +7,6 @@ module RuboCop
MSG = 'Add an `authorize :ability` call to the type: '\
'https://docs.gitlab.com/ee/development/api_graphql_styleguide.html#type-authorization'
TYPES_DIR = 'app/graphql/types'
# We want to exclude our own basetypes and scalars
WHITELISTED_TYPES = %w[BaseEnum BaseScalar BasePermissionType MutationType
QueryType GraphQL::Schema BaseUnion].freeze
......@@ -18,7 +16,6 @@ module RuboCop
PATTERN
def on_class(node)
return unless in_type?(node)
return if whitelisted?(class_constant(node))
return if whitelisted?(superclass_constant(node))
......@@ -27,12 +24,6 @@ module RuboCop
private
def in_type?(node)
path = node.location.expression.source_buffer.name
path.include? TYPES_DIR
end
def whitelisted?(class_node)
class_const = class_node&.const_name
......
......@@ -10,83 +10,60 @@ RSpec.describe RuboCop::Cop::Graphql::AuthorizeTypes, type: :rubocop do
subject(:cop) { described_class.new }
context 'when NOT in a type folder' do
before do
allow(cop).to receive(:in_type?).and_return(false)
end
it 'does not add an offense even though there is no authorize call' do
expect_no_offenses(<<~TYPE.strip)
module Types
class AType < BaseObject
field :a_thing
field :another_thing
end
end
TYPE
end
end
context 'when in a type folder' do
before do
allow(cop).to receive(:in_type?).and_return(true)
end
it 'adds an offense when there is no authorize call' do
inspect_source(<<~TYPE)
module Types
class AType < BaseObject
field :a_thing
field :another_thing
end
it 'adds an offense when there is no authorize call' do
inspect_source(<<~TYPE)
module Types
class AType < BaseObject
field :a_thing
field :another_thing
end
TYPE
end
TYPE
expect(cop.offenses.size).to eq 1
end
expect(cop.offenses.size).to eq 1
end
it 'does not add an offense for classes that have an authorize call' do
expect_no_offenses(<<~TYPE.strip)
module Types
class AType < BaseObject
graphql_name 'ATypeName'
it 'does not add an offense for classes that have an authorize call' do
expect_no_offenses(<<~TYPE.strip)
module Types
class AType < BaseObject
graphql_name 'ATypeName'
authorize :an_ability, :second_ability
authorize :an_ability, :second_ability
field :a_thing
end
field :a_thing
end
TYPE
end
end
TYPE
end
it 'does not add an offense for classes that only have an authorize call' do
expect_no_offenses(<<~TYPE.strip)
module Types
class AType < SuperClassWithFields
authorize :an_ability
end
it 'does not add an offense for classes that only have an authorize call' do
expect_no_offenses(<<~TYPE.strip)
module Types
class AType < SuperClassWithFields
authorize :an_ability
end
TYPE
end
end
TYPE
end
it 'does not add an offense for base types' do
expect_no_offenses(<<~TYPE)
module Types
class AType < BaseEnum
field :a_thing
end
it 'does not add an offense for base types' do
expect_no_offenses(<<~TYPE)
module Types
class AType < BaseEnum
field :a_thing
end
TYPE
end
end
TYPE
end
it 'does not add an offense for Enums' do
expect_no_offenses(<<~TYPE)
module Types
class ATypeEnum < AnotherEnum
field :a_thing
end
it 'does not add an offense for Enums' do
expect_no_offenses(<<~TYPE)
module Types
class ATypeEnum < AnotherEnum
field :a_thing
end
TYPE
end
end
TYPE
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