Commit c551d38e authored by Fabio Pitino's avatar Fabio Pitino

Feedback from review

parent 22718b69
# frozen_string_literal: true
# The connection between a source project (which defines the job token scope)
# and a target project which is the one allowed to be accessed by the job token.
module Ci
module JobToken
class ScopeLink < ApplicationRecord
self.table_name = 'ci_job_token_scope_links'
belongs_to :source_project, class_name: 'Project'
belongs_to :target_project, class_name: 'Project'
belongs_to :added_by, class_name: 'User'
scope :from_project, ->(project) { where(source_project: project) }
scope :to_project, ->(project) { where(target_project: project) }
end
end
end
# frozen_string_literal: true
class CreateCiJobTokenScopeLinks < ActiveRecord::Migration[6.0]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
disable_ddl_transaction!
def up
unless table_exists?(:ci_job_token_scope_links)
with_lock_retries do
create_table :ci_job_token_scope_links do |t|
t.belongs_to :source_project, null: false, foreign_key: { to_table: :projects, on_delete: :cascade }
t.belongs_to :target_project, null: false, foreign_key: { to_table: :projects, on_delete: :cascade }
t.belongs_to :added_by, foreign_key: { to_table: :users, on_delete: :nullify }
t.datetime_with_timezone :created_at, null: false
t.index [:source_project_id, :target_project_id], unique: true, name: 'i_ci_job_token_scope_links_on_source_and_target_project'
end
end
end
end
def down
with_lock_retries do
drop_table :ci_job_token_scope_links
end
end
end
......@@ -27161,9 +27161,6 @@ ALTER TABLE ONLY operations_feature_flag_scopes
ALTER TABLE ONLY packages_helm_file_metadata
ADD CONSTRAINT fk_rails_a559865345 FOREIGN KEY (package_file_id) REFERENCES packages_package_files(id) ON DELETE CASCADE;
ALTER TABLE ONLY ci_job_token_scope_links
ADD CONSTRAINT fk_rails_a562b502cf FOREIGN KEY (added_by_id) REFERENCES users(id) ON DELETE SET NULL;
ALTER TABLE ONLY cluster_projects
ADD CONSTRAINT fk_rails_a5a958bca1 FOREIGN KEY (cluster_id) REFERENCES clusters(id) ON DELETE CASCADE;
......@@ -27455,9 +27452,6 @@ ALTER TABLE ONLY ci_running_builds
ALTER TABLE ONLY jira_imports
ADD CONSTRAINT fk_rails_da617096ce FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE SET NULL;
ALTER TABLE ONLY ci_job_token_scope_links
ADD CONSTRAINT fk_rails_dae96135e0 FOREIGN KEY (target_project_id) REFERENCES projects(id) ON DELETE CASCADE;
ALTER TABLE ONLY dependency_proxy_blobs
ADD CONSTRAINT fk_rails_db58bbc5d7 FOREIGN KEY (group_id) REFERENCES namespaces(id) ON DELETE CASCADE;
# frozen_string_literal: true
FactoryBot.define do
factory :ci_job_token_scope_link, class: 'Ci::JobToken::ScopeLink' do
source_project factory: :project
target_project factory: :project
added_by factory: :user
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Ci::JobToken::ScopeLink do
it { is_expected.to belong_to(:source_project) }
it { is_expected.to belong_to(:target_project) }
it { is_expected.to belong_to(:added_by) }
describe 'unique index' do
let!(:link) { create(:ci_job_token_scope_link) }
it 'raises an error' do
expect do
create(:ci_job_token_scope_link, link.attributes)
end.to raise_error(ActiveRecord::RecordNotUnique)
end
end
describe '.from_project' do
let(:project) { create(:project) }
subject { described_class.from_project(project) }
let!(:source_link) { create(:ci_job_token_scope_link, source_project: project) }
let!(:target_link) { create(:ci_job_token_scope_link, target_project: project) }
it 'returns only the links having the given source project' do
expect(subject).to contain_exactly(source_link)
end
end
describe '.to_project' do
let(:project) { create(:project) }
subject { described_class.to_project(project) }
let!(:source_link) { create(:ci_job_token_scope_link, source_project: project) }
let!(:target_link) { create(:ci_job_token_scope_link, target_project: project) }
it 'returns only the links having the given target project' do
expect(subject).to contain_exactly(target_link)
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