summaryrefslogtreecommitdiff
path: root/spec/graphql
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2020-01-30 21:08:47 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2020-01-30 21:08:47 +0000
commitc8f773a8593926f4f2dec6f446a3b3e59e9c9909 (patch)
tree4e5ea1d3b861ff99015f6112da567de7873868aa /spec/graphql
parent929b887e5391dea7cb53b88b77b9a35351c87d99 (diff)
downloadgitlab-ce-c8f773a8593926f4f2dec6f446a3b3e59e9c9909.tar.gz
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/graphql')
-rw-r--r--spec/graphql/features/authorization_spec.rb35
-rw-r--r--spec/graphql/features/feature_flag_spec.rb36
-rw-r--r--spec/graphql/types/base_field_spec.rb65
3 files changed, 103 insertions, 33 deletions
diff --git a/spec/graphql/features/authorization_spec.rb b/spec/graphql/features/authorization_spec.rb
index 7ad6a622b4b..5ef1bced179 100644
--- a/spec/graphql/features/authorization_spec.rb
+++ b/spec/graphql/features/authorization_spec.rb
@@ -3,6 +3,8 @@
require 'spec_helper'
describe 'Gitlab::Graphql::Authorization' do
+ include GraphqlHelpers
+
set(:user) { create(:user) }
let(:permission_single) { :foo }
@@ -300,37 +302,4 @@ describe 'Gitlab::Graphql::Authorization' do
allow(Ability).to receive(:allowed?).with(user, permission, test_object).and_return(true)
end
end
-
- def type_factory
- Class.new(Types::BaseObject) do
- graphql_name 'TestType'
-
- field :name, GraphQL::STRING_TYPE, null: true
-
- yield(self) if block_given?
- end
- end
-
- def query_factory
- Class.new(Types::BaseObject) do
- graphql_name 'TestQuery'
-
- yield(self) if block_given?
- end
- end
-
- def execute_query(query_type)
- schema = Class.new(GraphQL::Schema) do
- use Gitlab::Graphql::Authorize
- use Gitlab::Graphql::Connections
-
- query(query_type)
- end
-
- schema.execute(
- query_string,
- context: { current_user: user },
- variables: {}
- )
- end
end
diff --git a/spec/graphql/features/feature_flag_spec.rb b/spec/graphql/features/feature_flag_spec.rb
new file mode 100644
index 00000000000..13b1e472fab
--- /dev/null
+++ b/spec/graphql/features/feature_flag_spec.rb
@@ -0,0 +1,36 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+describe 'Graphql Field feature flags' do
+ include GraphqlHelpers
+
+ let_it_be(:user) { create(:user) }
+
+ let(:feature_flag) { 'test_feature' }
+ let(:test_object) { double(name: 'My name') }
+ let(:query_string) { '{ item() { name } }' }
+ let(:result) { execute_query(query_type)['data'] }
+
+ subject { result }
+
+ describe 'Feature flagged field' do
+ let(:type) { type_factory }
+
+ let(:query_type) do
+ query_factory do |query|
+ query.field :item, type, null: true, feature_flag: feature_flag, resolve: ->(obj, args, ctx) { test_object }
+ end
+ end
+
+ it 'returns the value when feature is enabled' do
+ expect(subject['item']).to eq('name' => test_object.name)
+ end
+
+ it 'returns nil when the feature is disabled' do
+ stub_feature_flags(feature_flag => false)
+
+ expect(subject).to be_nil
+ end
+ end
+end
diff --git a/spec/graphql/types/base_field_spec.rb b/spec/graphql/types/base_field_spec.rb
index 77ef8933717..1f82f316aa7 100644
--- a/spec/graphql/types/base_field_spec.rb
+++ b/spec/graphql/types/base_field_spec.rb
@@ -111,5 +111,70 @@ describe Types::BaseField do
end
end
end
+
+ describe '#visible?' do
+ context 'and has a feature_flag' do
+ let(:flag) { :test_feature }
+ let(:field) { described_class.new(name: 'test', type: GraphQL::STRING_TYPE, feature_flag: flag, null: false) }
+ let(:context) { {} }
+
+ it 'returns false if the feature is not enabled' do
+ stub_feature_flags(flag => false)
+
+ expect(field.visible?(context)).to eq(false)
+ end
+
+ it 'returns true if the feature is enabled' do
+ expect(field.visible?(context)).to eq(true)
+ end
+
+ context 'falsey feature_flag values' do
+ using RSpec::Parameterized::TableSyntax
+
+ where(:flag, :feature_value, :visible) do
+ '' | false | true
+ '' | true | true
+ nil | false | true
+ nil | true | true
+ end
+
+ with_them do
+ it 'returns the correct value' do
+ stub_feature_flags(flag => feature_value)
+
+ expect(field.visible?(context)).to eq(visible)
+ end
+ end
+ end
+ end
+ end
+
+ describe '#description' do
+ context 'feature flag given' do
+ let(:field) { described_class.new(name: 'test', type: GraphQL::STRING_TYPE, feature_flag: flag, null: false, description: 'Test description') }
+ let(:flag) { :test_flag }
+
+ it 'prepends the description' do
+ expect(field.description). to eq 'Test description. Available only when feature flag test_flag is enabled.'
+ end
+
+ context 'falsey feature_flag values' do
+ using RSpec::Parameterized::TableSyntax
+
+ where(:flag, :feature_value) do
+ '' | false
+ '' | true
+ nil | false
+ nil | true
+ end
+
+ with_them do
+ it 'returns the correct description' do
+ expect(field.description).to eq('Test description')
+ end
+ end
+ end
+ end
+ end
end
end