Commit 0e2f0750 authored by Max Woolf's avatar Max Woolf

Adds AuthorizeTypes cop to EE-only GraphQL types

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