2019-12-10 07:53:40 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 09:08:32 +00:00
|
|
|
RSpec.describe GitlabSchema.types['Snippet'] do
|
2020-03-19 09:09:27 +00:00
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
|
2019-12-10 07:53:40 +00:00
|
|
|
it 'has the correct fields' do
|
|
|
|
expected_fields = [:id, :title, :project, :author,
|
2020-01-22 12:08:40 +00:00
|
|
|
:file_name, :description,
|
2019-12-13 09:08:01 +00:00
|
|
|
:visibility_level, :created_at, :updated_at,
|
2020-03-19 09:09:27 +00:00
|
|
|
:web_url, :raw_url, :ssh_url_to_repo, :http_url_to_repo,
|
|
|
|
:notes, :discussions, :user_permissions,
|
2020-06-10 12:08:58 +00:00
|
|
|
:description_html, :blob, :blobs]
|
2019-12-10 07:53:40 +00:00
|
|
|
|
2020-03-18 18:09:35 +00:00
|
|
|
expect(described_class).to have_graphql_fields(*expected_fields)
|
2019-12-10 07:53:40 +00:00
|
|
|
end
|
|
|
|
|
2020-09-24 12:09:37 +00:00
|
|
|
describe 'blobs field' do
|
|
|
|
subject { described_class.fields['blobs'] }
|
|
|
|
|
|
|
|
it 'returns blobs' do
|
|
|
|
is_expected.to have_graphql_type(Types::Snippets::BlobType.connection_type)
|
|
|
|
is_expected.to have_graphql_resolver(Resolvers::Snippets::BlobsResolver)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-10 18:09:15 +00:00
|
|
|
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
|
2020-08-11 03:11:00 +00:00
|
|
|
|
2020-06-10 18:09:15 +00:00
|
|
|
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
|
|
|
|
|
2019-12-10 07:53:40 +00:00
|
|
|
describe 'authorizations' do
|
2020-05-07 06:09:38 +00:00
|
|
|
specify { expect(described_class).to require_graphql_authorizations(:read_snippet) }
|
2019-12-10 07:53:40 +00:00
|
|
|
end
|
2020-02-25 09:09:10 +00:00
|
|
|
|
2020-03-19 09:09:27 +00:00
|
|
|
shared_examples 'response without repository URLs' do
|
|
|
|
it 'does not respond with repository URLs' do
|
|
|
|
expect(response['sshUrlToRepo']).to be_nil
|
|
|
|
expect(response['httpUrlToRepo']).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-03 18:10:03 +00:00
|
|
|
shared_examples 'snippets with repositories' do
|
2020-03-19 09:09:27 +00:00
|
|
|
context 'when snippet has repository' do
|
2020-04-03 18:10:03 +00:00
|
|
|
let_it_be(:snippet) { create(:personal_snippet, :repository, :public, author: user) }
|
2020-03-19 09:09:27 +00:00
|
|
|
|
|
|
|
it 'responds with repository URLs' do
|
|
|
|
expect(response['sshUrlToRepo']).to eq(snippet.ssh_url_to_repo)
|
|
|
|
expect(response['httpUrlToRepo']).to eq(snippet.http_url_to_repo)
|
|
|
|
end
|
|
|
|
end
|
2020-04-03 18:10:03 +00:00
|
|
|
end
|
2020-03-19 09:09:27 +00:00
|
|
|
|
2020-04-03 18:10:03 +00:00
|
|
|
shared_examples 'snippets without repositories' do
|
2020-03-19 09:09:27 +00:00
|
|
|
context 'when snippet does not have a repository' do
|
2020-04-03 18:10:03 +00:00
|
|
|
let_it_be(:snippet) { create(:personal_snippet, :public, author: user) }
|
2020-03-19 09:09:27 +00:00
|
|
|
|
|
|
|
it_behaves_like 'response without repository URLs'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-03 18:10:03 +00:00
|
|
|
describe 'Repository URLs' do
|
|
|
|
let(:query) do
|
|
|
|
%(
|
|
|
|
{
|
|
|
|
snippets {
|
|
|
|
nodes {
|
|
|
|
sshUrlToRepo
|
|
|
|
httpUrlToRepo
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
end
|
2020-08-11 03:11:00 +00:00
|
|
|
|
2020-04-03 18:10:03 +00:00
|
|
|
let(:response) { subject.dig('data', 'snippets', 'nodes')[0] }
|
|
|
|
|
|
|
|
subject { GitlabSchema.execute(query, context: { current_user: user }).as_json }
|
|
|
|
|
|
|
|
context 'when RequestStore is disabled' do
|
|
|
|
it_behaves_like 'snippets with repositories'
|
|
|
|
it_behaves_like 'snippets without repositories'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when RequestStore is enabled', :request_store do
|
|
|
|
it_behaves_like 'snippets with repositories'
|
|
|
|
it_behaves_like 'snippets without repositories'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-25 09:09:10 +00:00
|
|
|
describe '#blob' do
|
|
|
|
let(:query_blob) { subject.dig('data', 'snippets', 'edges')[0]['node']['blob'] }
|
|
|
|
|
2020-06-10 12:08:58 +00:00
|
|
|
subject { GitlabSchema.execute(snippet_query_for(field: 'blob'), context: { current_user: user }).as_json }
|
2020-02-25 09:09:10 +00:00
|
|
|
|
|
|
|
context 'when snippet has repository' do
|
|
|
|
let!(:snippet) { create(:personal_snippet, :repository, :public, author: user) }
|
|
|
|
let(:blob) { snippet.blobs.first }
|
|
|
|
|
2020-06-10 12:08:58 +00:00
|
|
|
it 'returns the first blob from the repository' do
|
2020-02-25 09:09:10 +00:00
|
|
|
expect(query_blob['name']).to eq blob.name
|
|
|
|
expect(query_blob['path']).to eq blob.path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when snippet does not have a repository' do
|
|
|
|
let!(:snippet) { create(:personal_snippet, :public, author: user) }
|
|
|
|
let(:blob) { snippet.blob }
|
|
|
|
|
|
|
|
it 'returns SnippetBlob type' do
|
|
|
|
expect(query_blob['name']).to eq blob.name
|
|
|
|
expect(query_blob['path']).to eq blob.path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-06-10 12:08:58 +00:00
|
|
|
|
|
|
|
describe '#blobs' do
|
|
|
|
let_it_be(:snippet) { create(:personal_snippet, :public, author: user) }
|
2020-09-24 12:09:37 +00:00
|
|
|
let(:query_blobs) { subject.dig('data', 'snippets', 'edges')[0].dig('node', 'blobs', 'edges') }
|
|
|
|
let(:paths) { [] }
|
|
|
|
let(:query) do
|
|
|
|
%(
|
|
|
|
{
|
|
|
|
snippets {
|
|
|
|
edges {
|
|
|
|
node {
|
|
|
|
blobs(paths: #{paths}) {
|
|
|
|
edges {
|
|
|
|
node {
|
|
|
|
name
|
|
|
|
path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
end
|
2020-06-10 12:08:58 +00:00
|
|
|
|
2020-09-24 12:09:37 +00:00
|
|
|
subject { GitlabSchema.execute(query, context: { current_user: user }).as_json }
|
2020-06-10 12:08:58 +00:00
|
|
|
|
|
|
|
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
|
2020-09-24 12:09:37 +00:00
|
|
|
expect(query_blobs.first['node']['name']).to eq blob.name
|
|
|
|
expect(query_blobs.first['node']['path']).to eq blob.path
|
2020-06-10 12:08:58 +00:00
|
|
|
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
|
2020-09-24 12:09:37 +00:00
|
|
|
resulting_blobs_names = query_blobs.map { |b| b['node']['name'] }
|
2020-06-10 12:08:58 +00:00
|
|
|
|
|
|
|
expect(resulting_blobs_names).to match_array(blobs.map(&:name))
|
|
|
|
end
|
2020-09-24 12:09:37 +00:00
|
|
|
|
|
|
|
context 'when specific path is set' do
|
|
|
|
let(:paths) { ['CHANGELOG'] }
|
|
|
|
|
|
|
|
it_behaves_like 'an array'
|
|
|
|
|
|
|
|
it 'returns specific files' do
|
|
|
|
resulting_blobs_names = query_blobs.map { |b| b['node']['name'] }
|
|
|
|
|
|
|
|
expect(resulting_blobs_names).to match(paths)
|
|
|
|
end
|
|
|
|
end
|
2020-06-10 12:08:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def snippet_query_for(field:)
|
|
|
|
%(
|
|
|
|
{
|
|
|
|
snippets {
|
|
|
|
edges {
|
|
|
|
node {
|
|
|
|
#{field} {
|
|
|
|
name
|
|
|
|
path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
end
|
2019-12-10 07:53:40 +00:00
|
|
|
end
|