diff options
author | Jan Provaznik <jprovaznik@gitlab.com> | 2019-06-05 10:25:22 +0000 |
---|---|---|
committer | Jan Provaznik <jprovaznik@gitlab.com> | 2019-06-05 10:25:22 +0000 |
commit | d9a761ed14ab5025626dee17faf1528c1264a1b0 (patch) | |
tree | 8e5acee47fee5454b0d56efa2a70f18455d1a1cd /spec/graphql | |
parent | 2b7fb50f5d2015e11a78c0ff60d0f9ac8b7776aa (diff) | |
parent | f16b13113ff580fbde78f8f6ba42a3f86c04ca12 (diff) | |
download | gitlab-ce-d9a761ed14ab5025626dee17faf1528c1264a1b0.tar.gz |
Merge branch 'bvl-use-global-ids-graphql' into 'master'
Use global IDs when exposing GraphQL resources
Closes #62650
See merge request gitlab-org/gitlab-ce!29080
Diffstat (limited to 'spec/graphql')
-rw-r--r-- | spec/graphql/features/authorization_spec.rb | 2 | ||||
-rw-r--r-- | spec/graphql/gitlab_schema_spec.rb | 58 |
2 files changed, 59 insertions, 1 deletions
diff --git a/spec/graphql/features/authorization_spec.rb b/spec/graphql/features/authorization_spec.rb index f5eb628a982..c427893f9cc 100644 --- a/spec/graphql/features/authorization_spec.rb +++ b/spec/graphql/features/authorization_spec.rb @@ -282,7 +282,7 @@ describe 'Gitlab::Graphql::Authorization' do issue_ids = issue_edges.map { |issue_edge| issue_edge['node']&.fetch('id') } expect(issue_edges.size).to eq(visible_issues.size) - expect(issue_ids).to eq(visible_issues.map { |i| i.id.to_s }) + expect(issue_ids).to eq(visible_issues.map { |i| i.to_global_id.to_s }) end it 'does not check access on fields that will not be rendered' do diff --git a/spec/graphql/gitlab_schema_spec.rb b/spec/graphql/gitlab_schema_spec.rb index e9149f4250f..4076c1f824b 100644 --- a/spec/graphql/gitlab_schema_spec.rb +++ b/spec/graphql/gitlab_schema_spec.rb @@ -107,6 +107,64 @@ describe GitlabSchema do end end + describe '.id_from_object' do + it 'returns a global id' do + expect(described_class.id_from_object(build(:project, id: 1))).to be_a(GlobalID) + end + + it "raises a meaningful error if a global id couldn't be generated" do + expect { described_class.id_from_object(build(:commit)) } + .to raise_error(RuntimeError, /include `GlobalID::Identification` into/i) + end + end + + describe '.object_from_id' do + context 'for subclasses of `ApplicationRecord`' do + it 'returns the correct record' do + user = create(:user) + + result = described_class.object_from_id(user.to_global_id.to_s) + + expect(result.__sync).to eq(user) + end + + it 'batchloads the queries' do + user1 = create(:user) + user2 = create(:user) + + expect do + [described_class.object_from_id(user1.to_global_id), + described_class.object_from_id(user2.to_global_id)].map(&:__sync) + end.not_to exceed_query_limit(1) + end + end + + context 'for other classes' do + # We cannot use an anonymous class here as `GlobalID` expects `.name` not + # to return `nil` + class TestGlobalId + include GlobalID::Identification + attr_accessor :id + + def initialize(id) + @id = id + end + end + + it 'falls back to a regular find' do + result = TestGlobalId.new(123) + + expect(TestGlobalId).to receive(:find).with("123").and_return(result) + + expect(described_class.object_from_id(result.to_global_id)).to eq(result) + end + end + + it 'raises the correct error on invalid input' do + expect { described_class.object_from_id("bogus id") }.to raise_error(Gitlab::Graphql::Errors::ArgumentError) + end + end + def field_instrumenters described_class.instrumenters[:field] + described_class.instrumenters[:field_after_built_ins] end |