Commit 2ab3bc6b authored by Tetiana Chupryna's avatar Tetiana Chupryna Committed by James Lopez

Fix tests for license entities

License report doesn't have info about dep url
parent a4f6d929
# frozen_string_literal: true
class DependencyListEntity < Grape::Entity
include RequestAwareEntity
class DependencyListEntity < ReportListEntity
present_collection true, :dependencies
expose :dependencies, using: DependencyEntity
expose :report do
expose :status do |list, options|
status(list[:dependencies], options[:build])
end
expose :job_path, if: ->(_, options) { options[:build] && can_read_job_path? } do |_, options|
project_build_path(project, options[:build].id)
end
expose :generated_at, if: ->(_, options) { options[:build] && can_read_job_path? } do |_, options|
options[:build].finished_at
end
end
private
def can_read_job_path?
can?(request.user, :read_pipeline, project)
end
def project
request.project
end
def status(dependencies, build)
if build&.success?
if dependencies.any?
:ok
else
:no_dependencies
end
elsif build&.failed?
:job_failed
else
:job_not_set_up
end
def items_name
:dependencies
end
end
# frozen_string_literal: true
class LicenseEntity < Grape::Entity
class ComponentEntity < Grape::Entity
expose :name
end
expose :name
expose :url
expose :dependencies, using: ComponentEntity, as: :components
end
# frozen_string_literal: true
class LicensesListEntity < ReportListEntity
present_collection true, :licenses
expose :licenses, using: LicenseEntity
private
def items_name
:licenses
end
end
# frozen_string_literal: true
class LicensesListSerializer < BaseSerializer
include WithPagination
entity LicensesListEntity
end
# frozen_string_literal: true
class ReportListEntity < Grape::Entity
include RequestAwareEntity
expose :report do
expose :status do |list, options|
status(list[items_name], options[:build])
end
expose :job_path, if: ->(_, options) { options[:build] && can_read_job_path? } do |_, options|
project_build_path(project, options[:build].id)
end
expose :generated_at, if: ->(_, options) { options[:build] && can_read_job_path? } do |_, options|
options[:build].finished_at
end
end
private
def can_read_job_path?
can?(request.user, :read_pipeline, project)
end
def items_name
raise NotImplementedError
end
def project
request.project
end
def status(dependencies, build)
if build&.success?
if dependencies.any?
:ok
else
"no_#{items_name}".to_sym
end
elsif build&.failed?
:job_failed
else
:job_not_set_up
end
end
end
......@@ -12,9 +12,7 @@
}
},
"report": {
"status": { "type": "string" },
"job_path": { "type": "string" },
"generated_at": { "type": "string" }
"$ref": "./report_list.json"
}
},
"additionalProperties": false
......
{
"type": "object",
"required": [
"licenses",
"report"
],
"properties": {
"licenses": {
"type": "array",
"items": {
"type": "object",
"required": [
"name",
"url",
"components"
],
"properties": {
"name": {
"type": "string"
},
"url": {
"type": "string"
},
"components": {
"type": "array",
"properties": {
"name": {
"type": "string"
}
}
}
}
}
},
"report": {
"$ref": "./report_list.json"
}
},
"additionalProperties": false
}
{
"type": "object",
"required": [
"status",
"job_path",
"generated_at"
],
"properties": {
"status": {
"type": "string"
},
"job_path": {
"type": "string"
},
"generated_at": {
"type": "string"
}
}
}
......@@ -3,88 +3,9 @@
require 'spec_helper'
describe DependencyListEntity do
describe '#as_json' do
let(:entity) do
described_class.represent(dependencies, build: ci_build, request: request)
end
let(:request) { double('request') }
set(:project) { create(:project, :repository, :private) }
set(:developer) { create(:user) }
subject { entity.as_json }
before do
project.add_developer(developer)
allow(request).to receive(:project).and_return(project)
allow(request).to receive(:user).and_return(user)
end
context 'with success build' do
let(:user) { developer }
let(:ci_build) { create(:ee_ci_build, :success) }
context 'with provided dependencies' do
let(:dependencies) { [build(:dependency)] }
it 'has array of dependencies with status ok' do
job_path = "/#{project.full_path}/builds/#{ci_build.id}"
expect(subject[:dependencies][0][:name]).to eq('nokogiri')
expect(subject[:report][:status]).to eq(:ok)
expect(subject[:report][:job_path]).to eq(job_path)
expect(subject[:report][:generated_at]).to eq(ci_build.finished_at)
end
end
context 'with no dependencies' do
let(:user) { developer }
let(:dependencies) { [] }
it 'has empty array of dependencies with status no_dependencies' do
job_path = "/#{project.full_path}/builds/#{ci_build.id}"
expect(subject[:dependencies].length).to eq(0)
expect(subject[:report][:status]).to eq(:no_dependencies)
expect(subject[:report][:job_path]).to eq(job_path)
end
end
end
context 'with failed build' do
let(:ci_build) { create(:ee_ci_build, :failed) }
let(:dependencies) { [] }
context 'with authorized user' do
let(:user) { developer }
it 'has job_path with status failed_job' do
expect(subject[:report][:status]).to eq(:job_failed)
expect(subject[:report]).to include(:job_path)
end
end
context 'without authorized user' do
let(:user) { create(:user) }
it 'has only status failed_job' do
expect(subject[:report][:status]).to eq(:job_failed)
expect(subject[:report]).not_to include(:job_path)
expect(subject[:report]).not_to include(:generated_at)
end
end
end
context 'with no build' do
let(:user) { developer }
let(:ci_build) { nil }
let(:dependencies) { [] }
it 'has status job_not_set_up and no job_path' do
expect(subject[:report][:status]).to eq(:job_not_set_up)
expect(subject[:report][:job_path]).not_to be_present
expect(subject[:report][:generated_at]).not_to be_present
end
end
it_behaves_like 'report list' do
let(:name) { :dependencies }
let(:collection) { [build(:dependency)] }
let(:no_items_status) { :no_dependencies }
end
end
# frozen_string_literal: true
require 'spec_helper'
describe LicenseEntity do
describe '#as_json' do
subject { described_class.represent(license).as_json }
let(:license) { build(:ci_reports_license_management_report, :mit).licenses.first }
let(:assert_license) do
{
name: 'MIT',
url: 'https://opensource.org/licenses/mit',
components: [{ name: 'rails' }]
}
end
it { is_expected.to eq(assert_license) }
end
end
# frozen_string_literal: true
require 'spec_helper'
describe LicensesListEntity do
let(:report) { build(:ci_reports_license_management_report, :mit) }
it_behaves_like 'report list' do
let(:name) { :licenses }
let(:collection) { report.licenses }
let(:no_items_status) { :no_licenses }
end
end
# frozen_string_literal: true
require 'spec_helper'
describe LicensesListSerializer do
describe '#to_json' do
subject do
described_class.new(project: project, user: user)
.represent(report.licenses, build: ci_build)
.to_json
end
let(:project) { create(:project, :repository) }
let(:user) { create(:user) }
let(:ci_build) { create(:ee_ci_build, :success) }
let(:report) { build(:ci_reports_license_management_report, :mit) }
before do
project.add_guest(user)
end
it { is_expected.to match_schema('licenses_list', dir: 'ee') }
end
end
# frozen_string_literal: true
shared_examples 'report list' do
describe '#as_json' do
let(:entity) do
described_class.represent(items, build: ci_build, request: request)
end
let(:request) { double('request') }
set(:project) { create(:project, :repository, :private) }
set(:developer) { create(:user) }
subject { entity.as_json }
before do
project.add_developer(developer)
allow(request).to receive(:project).and_return(project)
allow(request).to receive(:user).and_return(user)
end
context 'with success build' do
let(:user) { developer }
let(:ci_build) { create(:ee_ci_build, :success) }
context 'with provided items' do
let(:items) { collection }
it 'has array of items with status ok' do
job_path = "/#{project.full_path}/builds/#{ci_build.id}"
expect(subject[name]).to be_kind_of(Array)
expect(subject[:report][:status]).to eq(:ok)
expect(subject[:report][:job_path]).to eq(job_path)
expect(subject[:report][:generated_at]).to eq(ci_build.finished_at)
end
end
context 'with no items' do
let(:user) { developer }
let(:items) { [] }
it 'has empty array of items with status no_items' do
job_path = "/#{project.full_path}/builds/#{ci_build.id}"
expect(subject[name].length).to eq(0)
expect(subject[:report][:status]).to eq(no_items_status)
expect(subject[:report][:job_path]).to eq(job_path)
end
end
end
context 'with failed build' do
let(:ci_build) { create(:ee_ci_build, :failed) }
let(:items) { [] }
context 'with authorized user' do
let(:user) { developer }
it 'has job_path with status failed_job' do
expect(subject[:report][:status]).to eq(:job_failed)
expect(subject[:report]).to include(:job_path)
end
end
context 'without authorized user' do
let(:user) { create(:user) }
it 'has only status failed_job' do
expect(subject[:report][:status]).to eq(:job_failed)
expect(subject[:report]).not_to include(:job_path)
expect(subject[:report]).not_to include(:generated_at)
end
end
end
context 'with no build' do
let(:user) { developer }
let(:ci_build) { nil }
let(:items) { [] }
it 'has status job_not_set_up and no job_path' do
expect(subject[:report][:status]).to eq(:job_not_set_up)
expect(subject[:report][:job_path]).not_to be_present
expect(subject[:report][:generated_at]).not_to be_present
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