debian-mirror-gitlab/spec/requests/api/graphql/project/release_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

627 lines
17 KiB
Ruby
Raw Normal View History

2020-06-23 00:09:42 +05:30
# frozen_string_literal: true
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe 'Query.project(fullPath).release(tagName)' do
2020-06-23 00:09:42 +05:30
include GraphqlHelpers
include Presentable
let_it_be(:developer) { create(:user) }
2020-07-28 23:09:34 +05:30
let_it_be(:guest) { create(:user) }
let_it_be(:reporter) { create(:user) }
let_it_be(:stranger) { create(:user) }
2020-11-24 15:15:51 +05:30
let_it_be(:link_filepath) { '/direct/asset/link/path' }
let_it_be(:released_at) { Time.now - 1.day }
2020-06-23 00:09:42 +05:30
2021-01-29 00:20:46 +05:30
let(:base_url_params) { { scope: 'all', release_tag: release.tag } }
let(:opened_url_params) { { state: 'opened', **base_url_params } }
let(:merged_url_params) { { state: 'merged', **base_url_params } }
let(:closed_url_params) { { state: 'closed', **base_url_params } }
2020-07-28 23:09:34 +05:30
let(:post_query) { post_graphql(query, current_user: current_user) }
let(:path_prefix) { %w[project release] }
let(:data) { graphql_data.dig(*path) }
2020-06-23 00:09:42 +05:30
def query(rq = release_fields)
graphql_query_for(:project, { fullPath: project.full_path },
query_graphql_field(:release, { tagName: release.tag }, rq))
end
before do
2020-07-28 23:09:34 +05:30
stub_default_url_options(host: 'www.example.com')
2020-06-23 00:09:42 +05:30
end
2020-07-28 23:09:34 +05:30
shared_examples 'full access to the release field' do
describe 'scalar fields' do
let(:path) { path_prefix }
let(:release_fields) do
2021-02-22 17:27:13 +05:30
%{
2020-07-28 23:09:34 +05:30
tagName
tagPath
description
descriptionHtml
name
createdAt
releasedAt
2020-11-24 15:15:51 +05:30
upcomingRelease
2021-02-22 17:27:13 +05:30
}
2020-07-28 23:09:34 +05:30
end
before do
post_query
end
it 'finds all release data' do
expect(data).to eq({
'tagName' => release.tag,
'tagPath' => project_tag_path(project, release.tag),
'description' => release.description,
'descriptionHtml' => release.description_html,
'name' => release.name,
'createdAt' => release.created_at.iso8601,
2020-11-24 15:15:51 +05:30
'releasedAt' => release.released_at.iso8601,
'upcomingRelease' => false
2020-07-28 23:09:34 +05:30
})
end
2020-06-23 00:09:42 +05:30
end
2020-07-28 23:09:34 +05:30
describe 'milestones' do
let(:path) { path_prefix + %w[milestones nodes] }
let(:release_fields) do
query_graphql_field(:milestones, nil, 'nodes { id title }')
end
it 'finds all milestones associated to a release' do
post_query
2021-03-08 18:12:59 +05:30
expected = release.milestones.order_by_dates_and_title.map do |milestone|
2020-07-28 23:09:34 +05:30
{ 'id' => global_id_of(milestone), 'title' => milestone.title }
end
2021-03-08 18:12:59 +05:30
expect(data).to eq(expected)
2020-07-28 23:09:34 +05:30
end
2020-06-23 00:09:42 +05:30
end
2020-07-28 23:09:34 +05:30
describe 'author' do
let(:path) { path_prefix + %w[author] }
let(:release_fields) do
query_graphql_field(:author, nil, 'id username')
end
it 'finds the author of the release' do
post_query
expect(data).to eq(
'id' => global_id_of(release.author),
'username' => release.author.username
)
end
2020-06-23 00:09:42 +05:30
end
2020-07-28 23:09:34 +05:30
describe 'commit' do
let(:path) { path_prefix + %w[commit] }
let(:release_fields) do
query_graphql_field(:commit, nil, 'sha')
end
it 'finds the commit associated with the release' do
post_query
expect(data).to eq('sha' => release.commit.sha)
end
2020-06-23 00:09:42 +05:30
end
2020-07-28 23:09:34 +05:30
describe 'assets' do
describe 'count' do
let(:path) { path_prefix + %w[assets] }
let(:release_fields) do
query_graphql_field(:assets, nil, 'count')
end
it 'returns the number of assets associated to the release' do
post_query
expect(data).to eq('count' => release.sources.size + release.links.size)
end
end
describe 'links' do
let(:path) { path_prefix + %w[assets links nodes] }
2020-06-23 00:09:42 +05:30
2020-07-28 23:09:34 +05:30
let(:release_fields) do
query_graphql_field(:assets, nil,
2020-11-24 15:15:51 +05:30
query_graphql_field(:links, nil, 'nodes { id name url external, directAssetUrl }'))
2020-07-28 23:09:34 +05:30
end
it 'finds all release links' do
post_query
expected = release.links.map do |link|
{
'id' => global_id_of(link),
'name' => link.name,
'url' => link.url,
2020-11-24 15:15:51 +05:30
'external' => link.external?,
2021-01-29 00:20:46 +05:30
'directAssetUrl' => link.filepath ? Gitlab::Routing.url_helpers.project_release_url(project, release) << "/downloads#{link.filepath}" : link.url
2020-07-28 23:09:34 +05:30
}
end
expect(data).to match_array(expected)
end
2020-06-23 00:09:42 +05:30
end
2020-07-28 23:09:34 +05:30
describe 'sources' do
let(:path) { path_prefix + %w[assets sources nodes] }
let(:release_fields) do
query_graphql_field(:assets, nil,
query_graphql_field(:sources, nil, 'nodes { format url }'))
end
it 'finds all release sources' do
post_query
expected = release.sources.map do |source|
{
'format' => source.format,
'url' => source.url
}
end
expect(data).to match_array(expected)
end
end
end
describe 'links' do
let(:path) { path_prefix + %w[links] }
let(:release_fields) do
query_graphql_field(:links, nil, %{
selfUrl
2021-01-29 00:20:46 +05:30
openedMergeRequestsUrl
mergedMergeRequestsUrl
closedMergeRequestsUrl
openedIssuesUrl
closedIssuesUrl
2020-07-28 23:09:34 +05:30
})
end
it 'finds all release links' do
post_query
expect(data).to eq(
'selfUrl' => project_release_url(project, release),
2021-01-29 00:20:46 +05:30
'openedMergeRequestsUrl' => project_merge_requests_url(project, opened_url_params),
'mergedMergeRequestsUrl' => project_merge_requests_url(project, merged_url_params),
'closedMergeRequestsUrl' => project_merge_requests_url(project, closed_url_params),
'openedIssuesUrl' => project_issues_url(project, opened_url_params),
'closedIssuesUrl' => project_issues_url(project, closed_url_params)
2020-07-28 23:09:34 +05:30
)
end
end
describe 'evidences' do
let(:path) { path_prefix + %w[evidences] }
let(:release_fields) do
query_graphql_field(:evidences, nil, 'nodes { id sha filepath collectedAt }')
end
it 'finds all evidence fields' do
post_query
evidence = release.evidences.first.present
expect(data["nodes"].first).to eq(
'id' => global_id_of(evidence),
'sha' => evidence.sha,
'filepath' => evidence.filepath,
'collectedAt' => evidence.collected_at.utc.iso8601
)
end
end
end
2021-12-11 22:18:48 +05:30
shared_examples 'restricted access to release fields' do
describe 'scalar fields' do
let(:path) { path_prefix }
let(:release_fields) do
%{
tagName
tagPath
description
descriptionHtml
name
createdAt
releasedAt
upcomingRelease
}
end
before do
post_query
end
it 'finds all release data' do
expect(data).to eq({
'tagName' => release.tag,
'tagPath' => nil,
'description' => release.description,
'descriptionHtml' => release.description_html,
'name' => release.name,
'createdAt' => release.created_at.iso8601,
'releasedAt' => release.released_at.iso8601,
'upcomingRelease' => false
})
end
end
describe 'milestones' do
let(:path) { path_prefix + %w[milestones nodes] }
let(:release_fields) do
query_graphql_field(:milestones, nil, 'nodes { id title }')
end
it 'finds milestones associated to a release' do
post_query
expected = release.milestones.order_by_dates_and_title.map do |milestone|
{ 'id' => global_id_of(milestone), 'title' => milestone.title }
end
expect(data).to eq(expected)
end
end
describe 'author' do
let(:path) { path_prefix + %w[author] }
let(:release_fields) do
query_graphql_field(:author, nil, 'id username')
end
it 'finds the author of the release' do
post_query
expect(data).to eq(
'id' => global_id_of(release.author),
'username' => release.author.username
)
end
end
describe 'commit' do
let(:path) { path_prefix + %w[commit] }
let(:release_fields) do
query_graphql_field(:commit, nil, 'sha')
end
it 'restricts commit associated with the release' do
post_query
expect(data).to eq(nil)
end
end
describe 'assets' do
describe 'count' do
let(:path) { path_prefix + %w[assets] }
let(:release_fields) do
query_graphql_field(:assets, nil, 'count')
end
it 'returns non source release links count' do
post_query
expect(data).to eq('count' => release.assets_count(except: [:sources]))
end
end
describe 'links' do
let(:path) { path_prefix + %w[assets links nodes] }
let(:release_fields) do
query_graphql_field(:assets, nil,
query_graphql_field(:links, nil, 'nodes { id name url external, directAssetUrl }'))
end
it 'finds all non source external release links' do
post_query
expected = release.links.map do |link|
{
'id' => global_id_of(link),
'name' => link.name,
'url' => link.url,
'external' => true,
'directAssetUrl' => link.filepath ? Gitlab::Routing.url_helpers.project_release_url(project, release) << "/downloads#{link.filepath}" : link.url
}
end
expect(data).to match_array(expected)
end
end
describe 'sources' do
let(:path) { path_prefix + %w[assets sources nodes] }
let(:release_fields) do
query_graphql_field(:assets, nil,
query_graphql_field(:sources, nil, 'nodes { format url }'))
end
it 'restricts release sources' do
post_query
expect(data).to match_array([])
end
end
end
describe 'links' do
let(:path) { path_prefix + %w[links] }
let(:release_fields) do
query_graphql_field(:links, nil, %{
selfUrl
openedMergeRequestsUrl
mergedMergeRequestsUrl
closedMergeRequestsUrl
openedIssuesUrl
closedIssuesUrl
})
end
it 'finds only selfUrl' do
post_query
expect(data).to eq(
'selfUrl' => project_release_url(project, release),
'openedMergeRequestsUrl' => nil,
'mergedMergeRequestsUrl' => nil,
'closedMergeRequestsUrl' => nil,
'openedIssuesUrl' => nil,
'closedIssuesUrl' => nil
)
end
end
describe 'evidences' do
let(:path) { path_prefix + %w[evidences] }
let(:release_fields) do
query_graphql_field(:evidences, nil, 'nodes { id sha filepath collectedAt }')
end
it 'restricts all evidence fields' do
post_query
expect(data).to eq('nodes' => [])
end
end
end
2020-07-28 23:09:34 +05:30
shared_examples 'no access to the release field' do
describe 'repository-related fields' do
let(:path) { path_prefix }
let(:release_fields) do
2021-02-22 17:27:13 +05:30
'description'
2020-07-28 23:09:34 +05:30
end
before do
post_query
end
it 'returns nil' do
expect(data).to eq(nil)
end
2020-06-23 00:09:42 +05:30
end
end
2020-07-28 23:09:34 +05:30
shared_examples 'access to editUrl' do
let(:path) { path_prefix + %w[links] }
2020-06-23 00:09:42 +05:30
let(:release_fields) do
2020-07-28 23:09:34 +05:30
query_graphql_field(:links, nil, 'editUrl')
2020-06-23 00:09:42 +05:30
end
2020-07-28 23:09:34 +05:30
before do
2020-06-23 00:09:42 +05:30
post_query
2020-07-28 23:09:34 +05:30
end
2020-06-23 00:09:42 +05:30
2020-07-28 23:09:34 +05:30
it 'returns editUrl' do
expect(data).to eq('editUrl' => edit_project_release_url(project, release))
2020-06-23 00:09:42 +05:30
end
end
2020-07-28 23:09:34 +05:30
shared_examples 'no access to editUrl' do
let(:path) { path_prefix + %w[links] }
2020-06-23 00:09:42 +05:30
let(:release_fields) do
2020-07-28 23:09:34 +05:30
query_graphql_field(:links, nil, 'editUrl')
2020-06-23 00:09:42 +05:30
end
2020-07-28 23:09:34 +05:30
before do
2020-06-23 00:09:42 +05:30
post_query
2020-07-28 23:09:34 +05:30
end
2020-06-23 00:09:42 +05:30
2020-07-28 23:09:34 +05:30
it 'does not return editUrl' do
expect(data).to eq('editUrl' => nil)
2020-06-23 00:09:42 +05:30
end
end
2020-07-28 23:09:34 +05:30
describe "ensures that the correct data is returned based on the project's visibility and the user's access level" do
context 'when the project is private' do
let_it_be(:project) { create(:project, :repository, :private) }
let_it_be(:milestone_1) { create(:milestone, project: project) }
let_it_be(:milestone_2) { create(:milestone, project: project) }
2021-03-11 19:13:27 +05:30
let_it_be(:release, reload: true) { create(:release, :with_evidence, project: project, milestones: [milestone_1, milestone_2], released_at: released_at) }
2020-07-28 23:09:34 +05:30
let_it_be(:release_link_1) { create(:release_link, release: release) }
2020-11-24 15:15:51 +05:30
let_it_be(:release_link_2) { create(:release_link, release: release, filepath: link_filepath) }
2020-07-28 23:09:34 +05:30
before_all do
project.add_developer(developer)
project.add_guest(guest)
project.add_reporter(reporter)
2020-06-23 00:09:42 +05:30
end
2020-07-28 23:09:34 +05:30
context 'when the user is not logged in' do
let(:current_user) { stranger }
2020-06-23 00:09:42 +05:30
2020-07-28 23:09:34 +05:30
it_behaves_like 'no access to the release field'
2020-06-23 00:09:42 +05:30
end
2020-07-28 23:09:34 +05:30
context 'when the user has Guest permissions' do
let(:current_user) { guest }
2021-12-11 22:18:48 +05:30
it_behaves_like 'restricted access to release fields'
it_behaves_like 'no access to editUrl'
2020-06-23 00:09:42 +05:30
end
2020-07-28 23:09:34 +05:30
context 'when the user has Reporter permissions' do
let(:current_user) { reporter }
2020-06-23 00:09:42 +05:30
2020-07-28 23:09:34 +05:30
it_behaves_like 'full access to the release field'
it_behaves_like 'no access to editUrl'
end
2020-06-23 00:09:42 +05:30
2020-07-28 23:09:34 +05:30
context 'when the user has Developer permissions' do
let(:current_user) { developer }
it_behaves_like 'full access to the release field'
it_behaves_like 'access to editUrl'
2020-06-23 00:09:42 +05:30
end
end
2020-07-28 23:09:34 +05:30
context 'when the project is public' do
let_it_be(:project) { create(:project, :repository, :public) }
let_it_be(:milestone_1) { create(:milestone, project: project) }
let_it_be(:milestone_2) { create(:milestone, project: project) }
2021-03-11 19:13:27 +05:30
let_it_be(:release, reload: true) { create(:release, :with_evidence, project: project, milestones: [milestone_1, milestone_2], released_at: released_at) }
2020-07-28 23:09:34 +05:30
let_it_be(:release_link_1) { create(:release_link, release: release) }
2020-11-24 15:15:51 +05:30
let_it_be(:release_link_2) { create(:release_link, release: release, filepath: link_filepath) }
2020-07-28 23:09:34 +05:30
before_all do
project.add_developer(developer)
project.add_guest(guest)
project.add_reporter(reporter)
2020-06-23 00:09:42 +05:30
end
2020-07-28 23:09:34 +05:30
context 'when the user is not logged in' do
let(:current_user) { stranger }
2020-06-23 00:09:42 +05:30
2020-07-28 23:09:34 +05:30
it_behaves_like 'full access to the release field'
it_behaves_like 'no access to editUrl'
end
2020-06-23 00:09:42 +05:30
2020-07-28 23:09:34 +05:30
context 'when the user has Guest permissions' do
let(:current_user) { guest }
it_behaves_like 'full access to the release field'
it_behaves_like 'no access to editUrl'
2020-06-23 00:09:42 +05:30
end
2020-07-28 23:09:34 +05:30
context 'when the user has Reporter permissions' do
let(:current_user) { reporter }
it_behaves_like 'full access to the release field'
it_behaves_like 'no access to editUrl'
2020-06-23 00:09:42 +05:30
end
2020-07-28 23:09:34 +05:30
context 'when the user has Reporter permissions' do
let(:current_user) { reporter }
2020-06-23 00:09:42 +05:30
2020-07-28 23:09:34 +05:30
it_behaves_like 'full access to the release field'
end
2020-06-23 00:09:42 +05:30
2020-07-28 23:09:34 +05:30
context 'when the user has Developer permissions' do
let(:current_user) { developer }
it_behaves_like 'full access to the release field'
it_behaves_like 'access to editUrl'
2020-06-23 00:09:42 +05:30
end
2020-07-28 23:09:34 +05:30
end
end
2020-06-23 00:09:42 +05:30
2020-11-24 15:15:51 +05:30
describe 'upcoming release' do
let(:path) { path_prefix }
let(:project) { create(:project, :repository, :private) }
let(:release) { create(:release, :with_evidence, project: project, released_at: released_at) }
let(:current_user) { developer }
let(:release_fields) do
2021-02-22 17:27:13 +05:30
%{
2020-11-24 15:15:51 +05:30
releasedAt
upcomingRelease
2021-02-22 17:27:13 +05:30
}
2020-11-24 15:15:51 +05:30
end
before do
project.add_developer(developer)
post_query
end
context 'future release' do
let(:released_at) { Time.now + 1.day }
it 'finds all release data' do
expect(data).to eq({
'releasedAt' => release.released_at.iso8601,
'upcomingRelease' => true
})
end
end
context 'past release' do
let(:released_at) { Time.now - 1.day }
it 'finds all release data' do
expect(data).to eq({
'releasedAt' => release.released_at.iso8601,
'upcomingRelease' => false
})
end
end
end
2021-03-08 18:12:59 +05:30
describe 'milestone order' do
let(:path) { path_prefix }
let(:current_user) { stranger }
let_it_be(:project) { create(:project, :public) }
let_it_be_with_reload(:release) { create(:release, project: project) }
let(:release_fields) do
2021-03-11 19:13:27 +05:30
%{
2021-03-08 18:12:59 +05:30
milestones {
nodes {
title
}
}
2021-03-11 19:13:27 +05:30
}
2021-03-08 18:12:59 +05:30
end
let(:actual_milestone_title_order) do
post_query
data.dig('milestones', 'nodes').map { |m| m['title'] }
end
before do
release.update!(milestones: [milestone_2, milestone_1])
end
it_behaves_like 'correct release milestone order'
end
2020-06-23 00:09:42 +05:30
end