diff options
Diffstat (limited to 'spec/graphql/types/snippet_type_spec.rb')
-rw-r--r-- | spec/graphql/types/snippet_type_spec.rb | 114 |
1 files changed, 95 insertions, 19 deletions
diff --git a/spec/graphql/types/snippet_type_spec.rb b/spec/graphql/types/snippet_type_spec.rb index adc13d4d651..f24419ce9cc 100644 --- a/spec/graphql/types/snippet_type_spec.rb +++ b/spec/graphql/types/snippet_type_spec.rb @@ -11,11 +11,49 @@ describe GitlabSchema.types['Snippet'] do :visibility_level, :created_at, :updated_at, :web_url, :raw_url, :ssh_url_to_repo, :http_url_to_repo, :notes, :discussions, :user_permissions, - :description_html, :blob] + :description_html, :blob, :blobs] expect(described_class).to have_graphql_fields(*expected_fields) end + context 'when restricted visibility level is set to public' do + let_it_be(:snippet) { create(:personal_snippet, :repository, :public, author: user) } + + let(:current_user) { user } + let(:query) do + %( + { + snippets { + nodes { + author { + id + } + } + } + } + ) + end + let(:response) { subject.dig('data', 'snippets', 'nodes')[0] } + + subject { GitlabSchema.execute(query, context: { current_user: current_user }).as_json } + + before do + stub_application_setting(restricted_visibility_levels: [Gitlab::VisibilityLevel::PUBLIC]) + end + + it 'returns snippet author' do + expect(response['author']).to be_present + end + + context 'when user is not logged in' do + let(:current_user) { nil } + + it 'returns snippet author as nil' do + expect(response['author']).to be_nil + end + end + end + describe 'authorizations' do specify { expect(described_class).to require_graphql_authorizations(:read_snippet) } end @@ -76,30 +114,14 @@ describe GitlabSchema.types['Snippet'] do describe '#blob' do let(:query_blob) { subject.dig('data', 'snippets', 'edges')[0]['node']['blob'] } - let(:query) do - %( - { - snippets { - edges { - node { - blob { - name - path - } - } - } - } - } - ) - end - subject { GitlabSchema.execute(query, context: { current_user: user }).as_json } + subject { GitlabSchema.execute(snippet_query_for(field: 'blob'), context: { current_user: user }).as_json } context 'when snippet has repository' do let!(:snippet) { create(:personal_snippet, :repository, :public, author: user) } let(:blob) { snippet.blobs.first } - it 'returns blob from the repository' do + it 'returns the first blob from the repository' do expect(query_blob['name']).to eq blob.name expect(query_blob['path']).to eq blob.path end @@ -115,4 +137,58 @@ describe GitlabSchema.types['Snippet'] do end end end + + describe '#blobs' do + let_it_be(:snippet) { create(:personal_snippet, :public, author: user) } + let(:query_blobs) { subject.dig('data', 'snippets', 'edges')[0]['node']['blobs'] } + + subject { GitlabSchema.execute(snippet_query_for(field: 'blobs'), context: { current_user: user }).as_json } + + shared_examples 'an array' do + it 'returns an array of snippet blobs' do + expect(query_blobs).to be_an(Array) + end + end + + context 'when snippet does not have a repository' do + let(:blob) { snippet.blob } + + it_behaves_like 'an array' + + it 'contains the first blob from the snippet' do + expect(query_blobs.first['name']).to eq blob.name + expect(query_blobs.first['path']).to eq blob.path + end + end + + context 'when snippet has repository' do + let_it_be(:snippet) { create(:personal_snippet, :repository, :public, author: user) } + let(:blobs) { snippet.blobs } + + it_behaves_like 'an array' + + it 'contains all the blobs from the repository' do + resulting_blobs_names = query_blobs.map { |b| b['name'] } + + expect(resulting_blobs_names).to match_array(blobs.map(&:name)) + end + end + end + + def snippet_query_for(field:) + %( + { + snippets { + edges { + node { + #{field} { + name + path + } + } + } + } + } + ) + end end |