diff options
author | Shinya Maeda <shinya@gitlab.com> | 2018-12-25 12:48:26 +0300 |
---|---|---|
committer | Shinya Maeda <shinya@gitlab.com> | 2018-12-31 08:35:57 +0300 |
commit | 8f1e96c89bd3ffe6ae47c275df2d1e919d42c39a (patch) | |
tree | 783bdd82084985736a8a07ab13d0e9f335f2fcd0 /spec/services | |
parent | dc8a8c7d998e2c1f78fcf60f8dc45b572f62abe8 (diff) |
Add spec for Release API
Add spec for all release API - GET, POST, PUT, DELETE.
Also, fixes some minior bugs.
Diffstat (limited to 'spec/services')
-rw-r--r-- | spec/services/create_release_service_spec.rb | 78 | ||||
-rw-r--r-- | spec/services/delete_release_service_spec.rb | 1 | ||||
-rw-r--r-- | spec/services/releases/create_service_spec.rb | 72 | ||||
-rw-r--r-- | spec/services/releases/destroy_service_spec.rb | 61 | ||||
-rw-r--r-- | spec/services/releases/update_service_spec.rb | 50 | ||||
-rw-r--r-- | spec/services/update_release_service_spec.rb | 53 |
6 files changed, 183 insertions, 132 deletions
diff --git a/spec/services/create_release_service_spec.rb b/spec/services/create_release_service_spec.rb deleted file mode 100644 index 1dd74a1bcdb..00000000000 --- a/spec/services/create_release_service_spec.rb +++ /dev/null @@ -1,78 +0,0 @@ -require 'spec_helper' - -describe CreateReleaseService do - let(:project) { create(:project, :repository) } - let(:user) { create(:user) } - let(:tag_name) { project.repository.tag_names.first } - let(:name) { 'Bionic Beaver'} - let(:description) { 'Awesome release!' } - let(:params) { { tag: tag_name, name: name, description: description } } - let(:service) { described_class.new(project, user, params) } - let(:ref) { nil } - - before do - project.add_maintainer(user) - end - - shared_examples 'a successful release creation' do - it 'creates a new release' do - result = service.execute(ref) - expect(result[:status]).to eq(:success) - release = project.releases.find_by(tag: tag_name) - expect(release).not_to be_nil - expect(release.description).to eq(description) - expect(release.name).to eq(name) - expect(release.author).to eq(user) - end - end - - it_behaves_like 'a successful release creation' - - it 'raises an error if the tag does not exist' do - service.params[:tag] = 'foobar' - - result = service.execute - expect(result[:status]).to eq(:error) - end - - it 'keeps track of the commit sha' do - tag = project.repository.find_tag(tag_name) - sha = tag.dereferenced_target.sha - result = service.execute - - expect(result[:status]).to eq(:success) - expect(project.releases.find_by(tag: tag_name).sha).to eq(sha) - end - - context 'when ref is provided' do - let(:ref) { 'master' } - let(:tag_name) { 'foobar' } - - it_behaves_like 'a successful release creation' - - it 'creates a tag if the tag does not exist' do - expect(project.repository.ref_exists?("refs/tags/#{tag_name}")).to be_falsey - - result = service.execute(ref) - expect(result[:status]).to eq(:success) - expect(project.repository.ref_exists?("refs/tags/#{tag_name}")).to be_truthy - - release = project.releases.find_by(tag: tag_name) - expect(release).not_to be_nil - end - end - - context 'there already exists a release on a tag' do - before do - service.execute - end - - it 'raises an error and does not update the release' do - service.params[:description] = 'The best release!' - - result = service.execute - expect(result[:status]).to eq(:error) - expect(project.releases.find_by(tag: tag_name).description).to eq(description) - end - end -end diff --git a/spec/services/delete_release_service_spec.rb b/spec/services/delete_release_service_spec.rb deleted file mode 100644 index faefdb342bb..00000000000 --- a/spec/services/delete_release_service_spec.rb +++ /dev/null @@ -1 +0,0 @@ -#TODO:
\ No newline at end of file diff --git a/spec/services/releases/create_service_spec.rb b/spec/services/releases/create_service_spec.rb new file mode 100644 index 00000000000..612e9f152e7 --- /dev/null +++ b/spec/services/releases/create_service_spec.rb @@ -0,0 +1,72 @@ +require 'spec_helper' + +describe Releases::CreateService do + let(:project) { create(:project, :repository) } + let(:user) { create(:user) } + let(:tag_name) { project.repository.tag_names.first } + let(:tag_sha) { project.repository.find_tag(tag_name).dereferenced_target.sha } + let(:name) { 'Bionic Beaver' } + let(:description) { 'Awesome release!' } + let(:params) { { tag: tag_name, name: name, description: description, ref: ref } } + let(:ref) { nil } + let(:service) { described_class.new(project, user, params) } + + before do + project.add_maintainer(user) + end + + describe '#execute' do + shared_examples 'a successful release creation' do + it 'creates a new release' do + result = service.execute + expect(result[:status]).to eq(:success) + expect(result[:tag]).not_to be_nil + expect(result[:release]).not_to be_nil + expect(result[:release].description).to eq(description) + expect(result[:release].name).to eq(name) + expect(result[:release].author).to eq(user) + expect(result[:release].sha).to eq(tag_sha) + end + end + + it_behaves_like 'a successful release creation' + + context 'when the tag does not exist' do + let(:tag_name) { 'non-exist-tag' } + + it 'raises an error' do + result = service.execute + + expect(result[:status]).to eq(:error) + end + end + + context 'when ref is provided' do + let(:ref) { 'master' } + let(:tag_name) { 'foobar' } + + it_behaves_like 'a successful release creation' + + it 'creates a tag if the tag does not exist' do + expect(project.repository.ref_exists?("refs/tags/#{tag_name}")).to be_falsey + + result = service.execute + expect(result[:status]).to eq(:success) + expect(result[:tag]).not_to be_nil + expect(result[:release]).not_to be_nil + end + end + + context 'there already exists a release on a tag' do + let!(:release) do + create(:release, project: project, tag: tag_name, description: description) + end + + it 'raises an error and does not update the release' do + result = service.execute + expect(result[:status]).to eq(:error) + expect(project.releases.find_by(tag: tag_name).description).to eq(description) + end + end + end +end diff --git a/spec/services/releases/destroy_service_spec.rb b/spec/services/releases/destroy_service_spec.rb new file mode 100644 index 00000000000..dd5b8708f36 --- /dev/null +++ b/spec/services/releases/destroy_service_spec.rb @@ -0,0 +1,61 @@ +require 'spec_helper' + +describe Releases::DestroyService do + let(:project) { create(:project, :repository) } + let(:mainatiner) { create(:user) } + let(:repoter) { create(:user) } + let(:tag) { 'v1.1.0' } + let!(:release) { create(:release, project: project, tag: tag) } + let(:service) { described_class.new(project, user, params) } + let(:params) { { tag: tag } } + let(:user) { mainatiner } + + before do + project.add_maintainer(mainatiner) + project.add_reporter(repoter) + end + + describe '#execute' do + subject { service.execute } + + context 'when there is a release' do + it 'removes the release' do + expect { subject }.to change { project.releases.count }.by(-1) + end + + it 'returns the destroyed object' do + is_expected.to include(status: :success, release: release) + end + end + + context 'when tag is not found' do + let(:tag) { 'v1.1.1' } + + it 'returns an error' do + is_expected.to include(status: :error, + message: 'Tag does not exist', + http_status: 404) + end + end + + context 'when release is not found' do + let!(:release) { } + + it 'returns an error' do + is_expected.to include(status: :error, + message: 'Release does not exist', + http_status: 404) + end + end + + context 'when user does not have permission' do + let(:user) { repoter } + + it 'returns an error' do + is_expected.to include(status: :error, + message: 'Access Denied', + http_status: 403) + end + end + end +end diff --git a/spec/services/releases/update_service_spec.rb b/spec/services/releases/update_service_spec.rb new file mode 100644 index 00000000000..6c68f364739 --- /dev/null +++ b/spec/services/releases/update_service_spec.rb @@ -0,0 +1,50 @@ +require 'spec_helper' + +describe Releases::UpdateService do + let(:project) { create(:project, :repository) } + let(:user) { create(:user) } + let(:new_name) { 'A new name' } + let(:new_description) { 'The best release!' } + let(:params) { { name: new_name, description: new_description, tag: tag_name } } + let(:service) { described_class.new(project, user, params) } + let!(:release) { create(:release, project: project, author: user, tag: tag_name) } + let(:tag_name) { 'v1.1.0' } + + before do + project.add_developer(user) + end + + describe '#execute' do + shared_examples 'a failed update' do + it 'raises an error' do + result = service.execute + expect(result[:status]).to eq(:error) + end + end + + it 'successfully updates an existing release' do + result = service.execute + expect(result[:status]).to eq(:success) + expect(result[:release].name).to eq(new_name) + expect(result[:release].description).to eq(new_description) + end + + context 'when the tag does not exists' do + let(:tag_name) { 'foobar' } + + it_behaves_like 'a failed update' + end + + context 'when the release does not exist' do + let!(:release) { } + + it_behaves_like 'a failed update' + end + + context 'with an invalid update' do + let(:new_description) { '' } + + it_behaves_like 'a failed update' + end + end +end diff --git a/spec/services/update_release_service_spec.rb b/spec/services/update_release_service_spec.rb deleted file mode 100644 index 4378d5d072a..00000000000 --- a/spec/services/update_release_service_spec.rb +++ /dev/null @@ -1,53 +0,0 @@ -require 'spec_helper' - -describe UpdateReleaseService do - let(:project) { create(:project, :repository) } - let(:user) { create(:user) } - let(:tag_name) { project.repository.tag_names.first } - let(:description) { 'Awesome release!' } - let(:new_name) { 'A new name' } - let(:new_description) { 'The best release!' } - let(:params) { { name: new_name, description: new_description, tag: tag_name } } - let(:service) { described_class.new(project, user, params) } - let(:create_service) { CreateReleaseService.new(project, user, tag: tag_name, description: description) } - - before do - create_service.execute - end - - shared_examples 'a failed update' do - it 'raises an error' do - result = service.execute - expect(result[:status]).to eq(:error) - end - end - - it 'successfully updates an existing release' do - result = service.execute - expect(result[:status]).to eq(:success) - - release = project.releases.find_by(tag: tag_name) - expect(release.name).to eq(new_name) - expect(release.description).to eq(new_description) - end - - context 'when the tag does not exists' do - let(:tag_name) { 'foobar' } - - it_behaves_like 'a failed update' - end - - context 'when the release does not exist' do - before do - project.releases.delete_all - end - - it_behaves_like 'a failed update' - end - - context 'with an invalid update' do - let(:new_description) { '' } - - it_behaves_like 'a failed update' - end -end |