Commit 42423500 authored by Illya Klymov's avatar Illya Klymov

Introduce fork_namespace entity and serializer

This serializer and entity will be consumed by fork controller
representing available namespaces for forking via JSON
parent 501b56ec
# frozen_string_literal: true
class ForkNamespaceEntity < Grape::Entity
include ActionView::Helpers::NumberHelper
include RequestAwareEntity
include MarkupHelper
expose :id, :name, :description, :visibility, :full_name,
:created_at, :updated_at, :avatar_url
expose :fork_path do |namespace, options|
project_forks_path(options[:project], namespace_key: namespace.id)
end
expose :forked_project_path do |namespace, options|
if forked_project = namespace.find_fork_of(options[:project])
project_path(forked_project)
end
end
expose :permission do |namespace, options|
membership(options[:current_user], namespace)&.human_access
end
expose :relative_path do |namespace|
polymorphic_path(namespace)
end
expose :markdown_description do |namespace|
markdown_description(namespace)
end
expose :can_create_project do |namespace, options|
options[:current_user].can?(:create_projects, namespace)
end
private
# rubocop: disable CodeReuse/ActiveRecord
def membership(user, object)
return unless user
@membership ||= user.members.find_by(source: object)
end
# rubocop: enable CodeReuse/ActiveRecord
def markdown_description(namespace)
markdown_field(namespace, :description)
end
end
ForkNamespaceEntity.prepend_if_ee('EE::ForkNamespaceEntity')
# frozen_string_literal: true
class ForkNamespaceSerializer < BaseSerializer
entity ForkNamespaceEntity
end
# frozen_string_literal: true
module EE
module ForkNamespaceEntity
extend ActiveSupport::Concern
prepended do
expose :marked_for_deletion do |namespace|
namespace.marked_for_deletion?
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe ForkNamespaceEntity do
include Gitlab::Routing.url_helpers
include ProjectForksHelper
let_it_be(:user) { create(:user) }
let_it_be(:project) { create(:project) }
let(:namespace) { create(:group, :with_avatar, description: 'test') }
let(:entity) { described_class.new(namespace, current_user: user, project: project) }
subject(:json) { entity.as_json }
before do
project.add_maintainer(user)
end
it 'renders json' do
is_expected.not_to be_nil
end
%w[id
name
description
markdown_description
visibility
full_name
created_at
updated_at
avatar_url].each do |attribute|
it "includes #{attribute}" do
expect(json[attribute.to_sym]).to be_present
end
end
it 'exposes path for forking project to the namespace' do
expect(json[:fork_path]).to eq project_forks_path(project, namespace_key: namespace.id)
end
it 'exposes forked_project_path when fork exists in namespace' do
namespace.add_maintainer(user)
fork_in_namespace = fork_project(project, user, namespace: namespace)
expect(json[:forked_project_path]).to eql project_path(fork_in_namespace)
end
it 'exposes relative path to the namespace' do
expect(json[:relative_path]).to eql polymorphic_path(namespace)
end
it 'sets can_create_project to true when user can create projects in namespace' do
allow(user).to receive(:can?).with(:create_projects, namespace).and_return(true)
expect(json[:can_create_project]).to be true
end
it 'sets can_create_project to false when user is not allowed create projects in namespace' do
allow(user).to receive(:can?).with(:create_projects, namespace).and_return(false)
expect(json[:can_create_project]).to be false
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe ForkNamespaceSerializer do
it 'represents WebIdeTerminalEntity entities' do
expect(described_class.entity_class).to eq(ForkNamespaceEntity)
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