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 ...@@ -21,10 +21,9 @@ class Projects::ReleasesController < Projects::ApplicationController
end end
def evidence def evidence
summary = release.evidence_summary || {}
respond_to do |format| respond_to do |format|
format.json do format.json do
render json: summary render json: release.evidence_summary
end end
end end
end end
......
...@@ -78,7 +78,7 @@ class Release < ApplicationRecord ...@@ -78,7 +78,7 @@ class Release < ApplicationRecord
end end
def evidence_summary def evidence_summary
evidence&.summary evidence&.summary || {}
end end
private private
......
...@@ -167,7 +167,7 @@ describe Projects::ReleasesController do ...@@ -167,7 +167,7 @@ describe Projects::ReleasesController do
end end
describe 'GET #evidence' do 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(:tag) { CGI.escape(release.tag) }
let(:format) { :json } let(:format) { :json }
...@@ -184,12 +184,12 @@ describe Projects::ReleasesController do ...@@ -184,12 +184,12 @@ describe Projects::ReleasesController do
sign_in(user) sign_in(user)
end end
it 'returns the correct evidence summary as a json', :sidekiq_inline do it 'returns the correct evidence summary as a json' do
subject subject
expect(response.body).to eq(release.evidence.summary.to_json) expect(response.body).to eq(release.evidence.summary.to_json)
end 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 it 'returns an empty json' do
release.evidence.destroy release.evidence.destroy
subject subject
......
...@@ -14,5 +14,11 @@ FactoryBot.define do ...@@ -14,5 +14,11 @@ FactoryBot.define do
sha { nil } sha { nil }
author { nil } author { nil }
end end
trait :with_evidence do
after(:create) do |release, _|
CreateEvidenceWorker.new.perform(release.id)
end
end
end end
end end
...@@ -53,6 +53,12 @@ RSpec.describe Release do ...@@ -53,6 +53,12 @@ RSpec.describe Release do
end end
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 describe '#assets_count' do
subject { release.assets_count } subject { release.assets_count }
...@@ -92,20 +98,22 @@ RSpec.describe Release do ...@@ -92,20 +98,22 @@ RSpec.describe Release do
end end
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 describe '#create_evidence!' do
context 'when a release is created' do context 'when a release is created' do
it 'creates one Evidence object too' 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 end
end end
context 'when a release is deleted' do context 'when a release is deleted' do
it 'also deletes the associated evidence' 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 end
end end
...@@ -143,34 +151,34 @@ RSpec.describe Release do ...@@ -143,34 +151,34 @@ RSpec.describe Release do
end end
describe '#evidence_sha' do 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 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) allow(release).to receive(:evidence).and_return(nil)
expect(release.evidence_sha).to be_nil expect(release.evidence_sha).to be_nil
end end
end end
context 'when a release was created with evidence collection' do 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) expect(release.evidence_sha).to eq(release.evidence.summary_sha)
end end
end end
end end
describe '#evidence_summary' do 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 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) allow(release).to receive(:evidence).and_return(nil)
expect(release.evidence_summary).to be_nil expect(release.evidence_summary).to eq({})
end end
end end
context 'when a release was created with evidence collection' do 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) expect(release.evidence_summary).to eq(release.evidence.summary)
end end
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