summaryrefslogtreecommitdiff
path: root/spec/serializers/fork_namespace_entity_spec.rb
blob: 5e9918a89ffd982365b50c305062ea2b84f40f46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# 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_it_be(:namespace) { create(:group, :with_avatar, description: 'test') }

  let(:memberships) do
    user.members.index_by(&:source_id)
  end

  let(:entity) { described_class.new(namespace, current_user: user, project: project, memberships: memberships) }

  subject(:json) { entity.as_json }

  before do
    namespace.add_developer(user)
    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 'exposes human readable permission level' do
    expect(json[:permission]).to eql 'Developer'
  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