2019-09-30 21:07:59 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'fast_spec_helper'
|
|
|
|
require 'rubocop'
|
2020-07-28 23:09:34 +05:30
|
|
|
|
2019-09-30 21:07:59 +05:30
|
|
|
require_relative '../../../../rubocop/cop/graphql/authorize_types'
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
RSpec.describe RuboCop::Cop::Graphql::AuthorizeTypes do
|
2019-09-30 21:07:59 +05:30
|
|
|
subject(:cop) { described_class.new }
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
it 'adds an offense when there is no authorize call' do
|
2021-03-11 19:13:27 +05:30
|
|
|
expect_offense(<<~TYPE)
|
2020-07-28 23:09:34 +05:30
|
|
|
module Types
|
|
|
|
class AType < BaseObject
|
2021-03-11 19:13:27 +05:30
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^^ Add an `authorize :ability` call to the type: https://docs.gitlab.com/ee/development/api_graphql_styleguide.html#type-authorization
|
2020-07-28 23:09:34 +05:30
|
|
|
field :a_thing
|
|
|
|
field :another_thing
|
2019-09-30 21:07:59 +05:30
|
|
|
end
|
2020-07-28 23:09:34 +05:30
|
|
|
end
|
|
|
|
TYPE
|
|
|
|
end
|
2019-09-30 21:07:59 +05:30
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
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'
|
2019-09-30 21:07:59 +05:30
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
authorize :an_ability, :second_ability
|
2019-09-30 21:07:59 +05:30
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
field :a_thing
|
2019-09-30 21:07:59 +05:30
|
|
|
end
|
2020-07-28 23:09:34 +05:30
|
|
|
end
|
|
|
|
TYPE
|
|
|
|
end
|
2019-09-30 21:07:59 +05:30
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
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
|
2019-09-30 21:07:59 +05:30
|
|
|
end
|
2020-07-28 23:09:34 +05:30
|
|
|
end
|
|
|
|
TYPE
|
|
|
|
end
|
2019-09-30 21:07:59 +05:30
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
it 'does not add an offense for base types' do
|
|
|
|
expect_no_offenses(<<~TYPE)
|
|
|
|
module Types
|
|
|
|
class AType < BaseEnum
|
|
|
|
field :a_thing
|
2019-09-30 21:07:59 +05:30
|
|
|
end
|
2020-07-28 23:09:34 +05:30
|
|
|
end
|
|
|
|
TYPE
|
|
|
|
end
|
2020-01-01 13:55:28 +05:30
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
it 'does not add an offense for Enums' do
|
|
|
|
expect_no_offenses(<<~TYPE)
|
|
|
|
module Types
|
|
|
|
class ATypeEnum < AnotherEnum
|
|
|
|
field :a_thing
|
2020-01-01 13:55:28 +05:30
|
|
|
end
|
2020-07-28 23:09:34 +05:30
|
|
|
end
|
|
|
|
TYPE
|
2019-09-30 21:07:59 +05:30
|
|
|
end
|
|
|
|
end
|