Commit a4d9e9f2 authored by Etienne Baqué's avatar Etienne Baqué

Updated release factory

Added a with_evidence trait for release factory.
Did some refactoring in Release controller.
parent df46e1db
......@@ -21,10 +21,9 @@ class Projects::ReleasesController < Projects::ApplicationController
end
def evidence
summary = release.evidence_summary || {}
respond_to do |format|
format.json do
render json: summary
render json: release.evidence_summary
end
end
end
......
......@@ -78,7 +78,7 @@ class Release < ApplicationRecord
end
def evidence_summary
evidence&.summary
evidence&.summary || {}
end
private
......
......@@ -167,7 +167,7 @@ describe Projects::ReleasesController do
end
describe 'GET #evidence' do
let!(:release) { create(:release, project: project) }
let!(:release) { create(:release, :with_evidence, project: project) }
let(:tag) { CGI.escape(release.tag) }
let(:format) { :json }
......@@ -184,12 +184,12 @@ describe Projects::ReleasesController do
sign_in(user)
end
it 'returns the correct evidence summary as a json', :sidekiq_inline do
it 'returns the correct evidence summary as a json' do
subject
expect(response.body).to eq(release.evidence.summary.to_json)
end
context 'when the release was created before evidence existed', :sidekiq_inline do
context 'when the release was created before evidence existed' do
it 'returns an empty json' do
release.evidence.destroy
subject
......
......@@ -14,5 +14,11 @@ FactoryBot.define do
sha { nil }
author { nil }
end
trait :with_evidence do
after(:create) do |release, _|
CreateEvidenceWorker.new.perform(release.id)
end
end
end
end
......@@ -53,6 +53,12 @@ RSpec.describe Release do
end
end
describe 'callbacks' do
it 'creates a new Evidence object on after_commit', :sidekiq_inline do
expect { release }.to change(Evidence, :count).by(1)
end
end
describe '#assets_count' do
subject { release.assets_count }
......@@ -92,20 +98,22 @@ RSpec.describe Release do
end
end
describe 'evidence', :sidekiq_might_not_need_inline do
describe 'evidence' do
let(:release_with_evidence) { create(:release, :with_evidence, project: project) }
describe '#create_evidence!' do
context 'when a release is created' do
it 'creates one Evidence object too' do
expect { release }.to change(Evidence, :count).by(1)
expect { release_with_evidence }.to change(Evidence, :count).by(1)
end
end
end
context 'when a release is deleted' do
it 'also deletes the associated evidence' do
release = create(:release)
release_with_evidence
expect { release.destroy }.to change(Evidence, :count).by(-1)
expect { release_with_evidence.destroy }.to change(Evidence, :count).by(-1)
end
end
end
......@@ -143,34 +151,34 @@ RSpec.describe Release do
end
describe '#evidence_sha' do
let!(:release) { create(:release) }
let!(:release) { create(:release, :with_evidence) }
context 'when a release was created before evidence collection existed' do
it 'is nil', :sidekiq_inline do
it 'is nil' do
allow(release).to receive(:evidence).and_return(nil)
expect(release.evidence_sha).to be_nil
end
end
context 'when a release was created with evidence collection' do
it 'returns the summary sha', :sidekiq_inline do
it 'returns the summary sha' do
expect(release.evidence_sha).to eq(release.evidence.summary_sha)
end
end
end
describe '#evidence_summary' do
let!(:release) { create(:release) }
let!(:release) { create(:release, :with_evidence) }
context 'when a release was created before evidence collection existed' do
it 'is nil', :sidekiq_inline do
it 'returns an empty hash' do
allow(release).to receive(:evidence).and_return(nil)
expect(release.evidence_summary).to be_nil
expect(release.evidence_summary).to eq({})
end
end
context 'when a release was created with evidence collection' do
it 'returns the summary', :sidekiq_inline do
it 'returns the summary' do
expect(release.evidence_summary).to eq(release.evidence.summary)
end
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment