/
relationship_spec.rb
151 lines (114 loc) · 4.26 KB
/
relationship_spec.rb
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
require 'rails_helper'
RSpec.describe Relationship do
let(:admin_user) { create(:admin_user, login: 'admin') }
let(:global_role) { create(:role, title: 'global_role', global: true) }
let(:normal_role) { create(:role, title: 'normal_role', global: false) }
before do
@caching_state = ActionController::Base.perform_caching
ActionController::Base.perform_caching = true
end
after do
ActionController::Base.perform_caching = @caching_state
end
describe '.add_user' do
let(:role) { normal_role }
let(:user) { create(:confirmed_user, :with_home, login: 'other_user') }
let(:project) { user.home_project }
before do
login(admin_user)
end
subject { Relationship.add_user(project, user, role, true, true) }
context 'with a global role' do
let(:role) { global_role }
it { expect { subject }.to raise_error(Relationship::AddRole::SaveError, /tried to set global role/) }
end
context 'with an already existing relationship' do
before do
project.relationships.create(user: user, role: role)
end
it { expect { subject }.to raise_error(Relationship::AddRole::SaveError, 'Relationship already exists') }
end
context 'with invalid relationship data' do
skip('This is imposible to happen with the actual validations and how the object is created')
end
context 'with banned user' do
let(:nobody) { create(:user_nobody) }
subject { Relationship.add_user(project, nobody, role, true, true) }
it { expect { subject }.to raise_error(NotFoundError, "Couldn't find user #{nobody.login}") }
end
context 'with valid data' do
before do
subject
end
it { expect { project.store }.to change(Relationship, :count).by(1) }
it { expect { project.store }.to change(Event::RelationshipCreate, :count).by(1) }
end
end
describe '.add_group' do
let(:role) { normal_role }
let(:user) { admin_user }
let(:project) { user.home_project }
let(:group) { create(:group) }
before do
login(admin_user)
end
subject { Relationship.add_group(project, group, role, true, true) }
context 'with a global role' do
let(:role) { global_role }
it { expect { subject }.to raise_error(Relationship::AddRole::SaveError, /tried to set global role/) }
end
context 'with an already existing relationship' do
before do
project.relationships.create(group: group, role: role)
end
it { expect { subject }.to raise_error(Relationship::AddRole::SaveError, 'Relationship already exists') }
end
context 'with invalid relationship data' do
skip('This is imposible to happen with the actual validations and how the object is created')
end
context 'with valid data' do
before do
subject
end
it { expect { project.store }.to change(Relationship, :count).by(1) }
it { expect { project.store }.to change(Event::RelationshipCreate, :count).by(1) }
end
end
describe '.forbidden_project_ids' do
let(:confirmed_user) { create(:confirmed_user) }
let(:project) { create(:forbidden_project) }
let(:allowed_users) { create(:group_with_user, user: confirmed_user) }
context 'for admins' do
before do
login(admin_user)
end
it { expect(Relationship.forbidden_project_ids).to eq([0]) }
end
context 'for users' do
let(:confirmed_user2) { create(:confirmed_user) }
before do
login(confirmed_user)
create(:relationship_project_user, project: project, user: confirmed_user)
login(confirmed_user2)
end
it { expect(Relationship.forbidden_project_ids).to include(project.id) }
end
context 'for whitelisted users' do
before do
login(confirmed_user)
create(:relationship_project_user, project: project, user: confirmed_user)
end
it { expect(Relationship.forbidden_project_ids).not_to include(project.id) }
end
context 'for users in whitelisted groups' do
before do
login(confirmed_user)
create(:relationship_project_group, project: project, group: allowed_users)
end
it { expect(Relationship.forbidden_project_ids).not_to include(project.id) }
end
end
it '.discard_cache' do
skip
end
end