project_member_spec.rb 4.75 KB
Newer Older
gitlabhq's avatar
gitlabhq committed
1 2
require 'spec_helper'

Douwe Maan's avatar
Douwe Maan committed
3
describe ProjectMember, models: true do
4
  describe 'associations' do
5
    it { is_expected.to belong_to(:project).with_foreign_key(:source_id) }
6 7 8 9 10
  end

  describe 'validations' do
    it { is_expected.to allow_value('Project').for(:source_type) }
    it { is_expected.not_to allow_value('project').for(:source_type) }
11
    it { is_expected.to validate_inclusion_of(:access_level).in_array(Gitlab::Access.values) }
12 13 14 15 16 17
  end

  describe 'modules' do
    it { is_expected.to include_module(Gitlab::ShellAdapter) }
  end

18 19 20 21 22 23 24
  describe '.access_level_roles' do
    it 'returns Gitlab::Access.options' do
      expect(described_class.access_level_roles).to eq(Gitlab::Access.options)
    end
  end

  describe '.add_user' do
25 26 27
    it 'adds the user as a member' do
      user = create(:user)
      project = create(:empty_project)
28

29
      expect(project.users).not_to include(user)
30

31
      described_class.add_user(project, user, :master, current_user: project.owner)
32

33
      expect(project.users.reload).to include(user)
34 35 36
    end
  end

37 38 39 40 41 42
  describe '#real_source_type' do
    subject { create(:project_member).real_source_type }

    it { is_expected.to eq 'Project' }
  end

43
  describe "#destroy" do
44
    let(:owner)   { create(:project_member, access_level: ProjectMember::MASTER) }
45 46 47 48 49 50 51 52 53 54 55
    let(:project) { owner.project }
    let(:master)  { create(:project_member, project: project) }

    let(:owner_todos)  { (0...2).map { create(:todo, user: owner.user, project: project) } }
    let(:master_todos) { (0...3).map { create(:todo, user: master.user, project: project) } }

    before do
      owner_todos
      master_todos
    end

56 57 58
    it "creates an expired event when left due to expiry" do
      expired = create(:project_member, project: project, expires_at: Time.now - 6.days)
      expired.destroy
59
      expect(Event.recent.first.action).to eq(Event::EXPIRED)
60 61 62 63
    end

    it "creates a left event when left due to leave" do
      master.destroy
64
      expect(Event.recent.first.action).to eq(Event::LEFT)
65 66
    end

67
    it "destroys itself and delete associated todos" do
Long Nguyen's avatar
Long Nguyen committed
68 69
      expect(owner.user.todos.size).to eq(2)
      expect(master.user.todos.size).to eq(3)
70 71 72 73 74
      expect(Todo.count).to eq(5)

      master_todo_ids = master_todos.map(&:id)
      master.destroy

Long Nguyen's avatar
Long Nguyen committed
75
      expect(owner.user.todos.size).to eq(2)
76 77 78 79 80 81 82
      expect(Todo.count).to eq(2)
      master_todo_ids.each do |id|
        expect(Todo.exists?(id)).to eq(false)
      end
    end
  end

83
  describe '.import_team' do
84
    before do
85 86
      @project_1 = create(:empty_project)
      @project_2 = create(:empty_project)
87 88 89 90

      @user_1 = create :user
      @user_2 = create :user

91 92
      @project_1.team << [@user_1, :developer]
      @project_2.team << [@user_2, :reporter]
93

94
      @status = @project_2.team.import(@project_1)
95 96
    end

97
    it { expect(@status).to be_truthy }
98 99

    describe 'project 2 should get user 1 as developer. user_2 should not be changed' do
100 101
      it { expect(@project_2.users).to include(@user_1) }
      it { expect(@project_2.users).to include(@user_2) }
102

103 104
      it { expect(Ability.allowed?(@user_1, :create_project, @project_2)).to be_truthy }
      it { expect(Ability.allowed?(@user_2, :read_project, @project_2)).to be_truthy }
105 106 107
    end

    describe 'project 1 should not be changed' do
108 109
      it { expect(@project_1.users).to include(@user_1) }
      it { expect(@project_1.users).not_to include(@user_2) }
110 111
    end
  end
112

113
  describe '.add_users_to_projects' do
114 115 116
    it 'adds the given users to the given projects' do
      projects = create_list(:empty_project, 2)
      users = create_list(:user, 2)
117

118
      described_class.add_users_to_projects(
119
        [projects.first.id, projects.second.id],
120 121
        [users.first.id, users.second],
        described_class::MASTER)
122

123 124
      expect(projects.first.users).to include(users.first)
      expect(projects.first.users).to include(users.second)
125

126 127 128
      expect(projects.second.users).to include(users.first)
      expect(projects.second.users).to include(users.second)
    end
129 130
  end

131
  describe '.truncate_teams' do
132
    before do
133 134
      @project_1 = create(:empty_project)
      @project_2 = create(:empty_project)
135 136 137 138

      @user_1 = create :user
      @user_2 = create :user

139 140
      @project_1.team << [@user_1, :developer]
      @project_2.team << [@user_2, :reporter]
141

142
      ProjectMember.truncate_teams([@project_1.id, @project_2.id])
143 144
    end

145 146
    it { expect(@project_1.users).to be_empty }
    it { expect(@project_2.users).to be_empty }
147
  end
148 149

  describe 'notifications' do
150 151 152
    describe '#after_accept_request' do
      it 'calls NotificationService.new_project_member' do
        member = create(:project_member, user: build_stubbed(:user), requested_at: Time.now)
153 154 155

        expect_any_instance_of(NotificationService).to receive(:new_project_member)

156
        member.__send__(:after_accept_request)
157 158 159
      end
    end
  end
gitlabhq's avatar
gitlabhq committed
160
end