2019-09-30 05:06:31 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-03-24 06:11:36 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 02:09:01 -04:00
|
|
|
RSpec.describe DeployKeyEntity do
|
2017-04-28 15:59:20 -04:00
|
|
|
include RequestAwareEntity
|
2017-07-25 13:15:45 -04:00
|
|
|
|
2017-03-24 06:11:36 -04:00
|
|
|
let(:user) { create(:user) }
|
2017-08-02 15:55:11 -04:00
|
|
|
let(:project) { create(:project, :internal)}
|
|
|
|
let(:project_private) { create(:project, :private)}
|
2017-03-24 06:11:36 -04:00
|
|
|
let(:deploy_key) { create(:deploy_key) }
|
2020-07-15 08:09:26 -04:00
|
|
|
let(:options) { { user: user } }
|
2017-03-24 06:11:36 -04:00
|
|
|
|
2020-07-15 08:09:26 -04:00
|
|
|
let(:entity) { described_class.new(deploy_key, options) }
|
2017-03-24 06:11:36 -04:00
|
|
|
|
2019-10-16 20:07:27 -04:00
|
|
|
before do
|
|
|
|
project.deploy_keys << deploy_key
|
|
|
|
project_private.deploy_keys << deploy_key
|
|
|
|
end
|
|
|
|
|
2017-03-31 08:54:38 -04:00
|
|
|
describe 'returns deploy keys with projects a user can read' do
|
|
|
|
let(:expected_result) do
|
|
|
|
{
|
|
|
|
id: deploy_key.id,
|
|
|
|
user_id: deploy_key.user_id,
|
|
|
|
title: deploy_key.title,
|
|
|
|
fingerprint: deploy_key.fingerprint,
|
2020-01-13 19:08:14 -05:00
|
|
|
fingerprint_sha256: deploy_key.fingerprint_sha256,
|
2017-03-31 08:54:38 -04:00
|
|
|
destroyed_when_orphaned: true,
|
|
|
|
almost_orphaned: false,
|
|
|
|
created_at: deploy_key.created_at,
|
|
|
|
updated_at: deploy_key.updated_at,
|
|
|
|
can_edit: false,
|
2018-01-05 10:23:44 -05:00
|
|
|
deploy_keys_projects: [
|
2017-03-31 08:54:38 -04:00
|
|
|
{
|
2018-01-05 10:23:44 -05:00
|
|
|
can_push: false,
|
|
|
|
project:
|
|
|
|
{
|
|
|
|
id: project.id,
|
|
|
|
name: project.name,
|
|
|
|
full_path: project_path(project),
|
|
|
|
full_name: project.full_name
|
|
|
|
}
|
2017-03-31 08:54:38 -04:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(entity.as_json).to eq(expected_result) }
|
|
|
|
end
|
|
|
|
|
2019-10-16 20:07:27 -04:00
|
|
|
context 'user is an admin' do
|
|
|
|
let(:user) { create(:user, :admin) }
|
|
|
|
|
|
|
|
it { expect(entity.as_json).to include(can_edit: true) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'user is a project maintainer' do
|
2017-03-31 08:54:38 -04:00
|
|
|
before do
|
2018-07-11 10:36:08 -04:00
|
|
|
project.add_maintainer(user)
|
2017-03-31 08:54:38 -04:00
|
|
|
end
|
|
|
|
|
2019-10-16 20:07:27 -04:00
|
|
|
context 'project deploy key' do
|
|
|
|
it { expect(entity.as_json).to include(can_edit: true) }
|
|
|
|
end
|
2017-03-31 08:54:38 -04:00
|
|
|
|
2019-10-16 20:07:27 -04:00
|
|
|
context 'public deploy key' do
|
|
|
|
let(:deploy_key_public) { create(:deploy_key, public: true) }
|
|
|
|
let(:entity_public) { described_class.new(deploy_key_public, { user: user, project: project }) }
|
2017-03-31 08:54:38 -04:00
|
|
|
|
2019-10-16 20:07:27 -04:00
|
|
|
before do
|
|
|
|
project.deploy_keys << deploy_key_public
|
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(entity_public.as_json).to include(can_edit: true) }
|
|
|
|
end
|
2017-03-24 06:11:36 -04:00
|
|
|
end
|
2020-07-15 08:09:26 -04:00
|
|
|
|
|
|
|
describe 'with_owner option' do
|
|
|
|
it 'does not return an owner payload when it is set to false' do
|
|
|
|
options[:with_owner] = false
|
|
|
|
|
|
|
|
payload = entity.as_json
|
|
|
|
|
|
|
|
expect(payload[:owner]).not_to be_present
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'when with_owner is set to true' do
|
|
|
|
before do
|
|
|
|
options[:with_owner] = true
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns an owner payload' do
|
|
|
|
payload = entity.as_json
|
|
|
|
|
|
|
|
expect(payload[:owner]).to be_present
|
|
|
|
expect(payload[:owner].keys).to include(:id, :name, :username, :avatar_url)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not return an owner if current_user cannot read the owner' do
|
|
|
|
allow(Ability).to receive(:allowed?).and_call_original
|
|
|
|
allow(Ability).to receive(:allowed?).with(options[:user], :read_user, deploy_key.user).and_return(false)
|
|
|
|
|
|
|
|
payload = entity.as_json
|
|
|
|
|
|
|
|
expect(payload[:owner]).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not return an owner payload with_owner option not passed in' do
|
|
|
|
payload = entity.as_json
|
|
|
|
|
|
|
|
expect(payload[:owner]).not_to be_present
|
|
|
|
end
|
2017-03-24 06:11:36 -04:00
|
|
|
end
|