Commit 201254de authored by Heinrich Lee Yu's avatar Heinrich Lee Yu

Merge branch '250689-refactor-entity_leave_service_spec-to-make-it-more-testable' into 'master'

Refactor entity_leave_service_spec to make it more testable

Closes #250689

See merge request gitlab-org/gitlab!42608
parents eb8418a8 efad1f1a
...@@ -3,77 +3,111 @@ ...@@ -3,77 +3,111 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe Todos::Destroy::EntityLeaveService do RSpec.describe Todos::Destroy::EntityLeaveService do
let(:group) { create(:group, :private) } let_it_be(:group, reload: true) { create(:group, :private) }
let(:project) { create(:project, group: group) } let_it_be(:project, reload: true) { create(:project, group: group) }
let(:user) { create(:user) } let_it_be(:user, reload: true) { create(:user) }
let(:user2) { create(:user) } let_it_be(:user2, reload: true) { create(:user) }
let(:issue) { create(:issue, project: project, confidential: true) } let_it_be(:issue) { create(:issue, project: project) }
let(:mr) { create(:merge_request, source_project: project) } let_it_be(:issue_c) { create(:issue, project: project, confidential: true) }
let_it_be(:todo_group_user) { create(:todo, user: user, group: group) }
let!(:todo_mr_user) { create(:todo, user: user, target: mr, project: project) } let_it_be(:todo_group_user2) { create(:todo, user: user2, group: group) }
let!(:todo_issue_user) { create(:todo, user: user, target: issue, project: project) }
let!(:todo_group_user) { create(:todo, user: user, group: group) } let(:mr) { create(:merge_request, source_project: project) }
let!(:todo_issue_user2) { create(:todo, user: user2, target: issue, project: project) } let!(:todo_mr_user) { create(:todo, user: user, target: mr, project: project) }
let!(:todo_group_user2) { create(:todo, user: user2, group: group) } let!(:todo_issue_user) { create(:todo, user: user, target: issue, project: project) }
let!(:todo_issue_c_user) { create(:todo, user: user, target: issue_c, project: project) }
let!(:todo_issue_c_user2) { create(:todo, user: user2, target: issue_c, project: project) }
shared_examples 'using different access permissions' do |access_table|
using RSpec::Parameterized::TableSyntax
where(:group_access, :project_access, :c_todos, :mr_todos, :method, &access_table)
with_them do
before do
set_access(project, user, project_access) if project_access
set_access(group, user, group_access) if group_access
end
describe '#execute' do it "#{params[:method].to_s.humanize(capitalize: false)}" do
context 'when a user leaves a project' do send(method)
subject { described_class.new(user.id, project.id, 'Project').execute } end
end
end
context 'when project is private' do shared_examples 'does not remove any todos' do
it 'removes project todos for the provided user' do it { does_not_remove_any_todos }
expect { subject }.to change { Todo.count }.from(5).to(3) end
expect(user.todos).to match_array([todo_group_user]) shared_examples 'removes only confidential issues todos' do
expect(user2.todos).to match_array([todo_issue_user2, todo_group_user2]) it { removes_only_confidential_issues_todos }
end end
context 'when the user is member of the project' do shared_examples 'removes confidential issues and merge request todos' do
before do it { removes_confidential_issues_and_merge_request_todos }
project.add_developer(user) end
end
it 'does not remove any todos' do def does_not_remove_any_todos
expect { subject }.not_to change { Todo.count } expect { subject }.not_to change { Todo.count }
end end
end
context 'when the user is a project guest' do def removes_only_confidential_issues_todos
before do expect { subject }.to change { Todo.count }.from(6).to(5)
project.add_guest(user) end
end
it 'removes only confidential issues todos' do def removes_confidential_issues_and_merge_request_todos
expect { subject }.to change { Todo.count }.from(5).to(4) expect { subject }.to change { Todo.count }.from(6).to(4)
end expect(user.todos).to match_array([todo_issue_user, todo_group_user])
end end
context 'when the user is member of a parent group' do def set_access(object, user, access_name)
before do case access_name
group.add_developer(user) when :developer
end object.add_developer(user)
when :reporter
object.add_reporter(user)
when :guest
object.add_guest(user)
end
end
describe '#execute' do
describe 'updating a Project' do
subject { described_class.new(user.id, project.id, 'Project').execute }
context 'when project is private' do
context 'when a user leaves a project' do
it 'removes project todos for the provided user' do
expect { subject }.to change { Todo.count }.from(6).to(3)
it 'does not remove any todos' do expect(user.todos).to match_array([todo_group_user])
expect { subject }.not_to change { Todo.count } expect(user2.todos).to match_array([todo_issue_c_user2, todo_group_user2])
end end
end end
context 'when the user is guest of a parent group' do context 'access permissions' do
before do # rubocop:disable RSpec/LeakyConstantDeclaration
project.add_guest(user) PROJECT_PRIVATE_ACCESS_TABLE =
end lambda do |_|
[
# :group_access, :project_access, :c_todos, :mr_todos, :method
[nil, :reporter, :keep, :keep, :does_not_remove_any_todos],
[nil, :guest, :delete, :keep, :removes_only_confidential_issues_todos],
[:reporter, nil, :keep, :keep, :does_not_remove_any_todos],
[:guest, nil, :delete, :keep, :removes_only_confidential_issues_todos]
]
end
# rubocop:enable RSpec/LeakyConstantDeclaration
it 'removes only confidential issues todos' do it_behaves_like 'using different access permissions', PROJECT_PRIVATE_ACCESS_TABLE
expect { subject }.to change { Todo.count }.from(5).to(4)
end
end end
end end
context 'when project is not private' do context 'when project is not private' do
before do let_it_be(:group, reload: true) { create(:group, :internal) }
group.update!(visibility_level: Gitlab::VisibilityLevel::INTERNAL) let_it_be(:project, reload: true) { create(:project, :internal, group: group) }
project.update!(visibility_level: Gitlab::VisibilityLevel::INTERNAL) let_it_be(:issue, reload: true) { create(:issue, project: project) }
end let_it_be(:issue_c, reload: true) { create(:issue, project: project, confidential: true) }
it 'enqueues the PrivateFeaturesWorker' do it 'enqueues the PrivateFeaturesWorker' do
expect(TodosDestroyer::PrivateFeaturesWorker) expect(TodosDestroyer::PrivateFeaturesWorker)
...@@ -84,50 +118,41 @@ RSpec.describe Todos::Destroy::EntityLeaveService do ...@@ -84,50 +118,41 @@ RSpec.describe Todos::Destroy::EntityLeaveService do
context 'confidential issues' do context 'confidential issues' do
context 'when a user is not an author of confidential issue' do context 'when a user is not an author of confidential issue' do
it 'removes only confidential issues todos' do it_behaves_like 'removes only confidential issues todos'
expect { subject }.to change { Todo.count }.from(5).to(4)
end
end end
context 'when a user is an author of confidential issue' do context 'when a user is an author of confidential issue' do
before do before do
issue.update!(author: user) issue_c.update!(author: user)
end end
it 'does not remove any todos' do it_behaves_like 'does not remove any todos'
expect { subject }.not_to change { Todo.count }
end
end end
context 'when a user is an assignee of confidential issue' do context 'when a user is an assignee of confidential issue' do
before do before do
issue.assignees << user issue_c.assignees << user
end
it 'does not remove any todos' do
expect { subject }.not_to change { Todo.count }
end
end
context 'when a user is a project guest' do
before do
project.add_guest(user)
end end
it 'removes only confidential issues todos' do it_behaves_like 'does not remove any todos'
expect { subject }.to change { Todo.count }.from(5).to(4)
end
end end
context 'when a user is a project guest but group developer' do context 'access permissions' do
before do # rubocop:disable RSpec/LeakyConstantDeclaration
project.add_guest(user) PROJECT_NOT_PRIVATE_ACCESS_TABLE =
group.add_developer(user) lambda do |_|
end [
# :group_access, :project_access, :c_todos, :mr_todos, :method
it 'does not remove any todos' do [nil, :reporter, :keep, :keep, :does_not_remove_any_todos],
expect { subject }.not_to change { Todo.count } [nil, :guest, :delete, :keep, :removes_only_confidential_issues_todos],
end [:reporter, nil, :keep, :keep, :does_not_remove_any_todos],
[:guest, nil, :delete, :keep, :removes_only_confidential_issues_todos],
[:reporter, :guest, :keep, :keep, :does_not_remove_any_todos]
]
end
# rubocop:enable RSpec/LeakyConstantDeclaration
it_behaves_like 'using different access permissions', PROJECT_NOT_PRIVATE_ACCESS_TABLE
end end
end end
...@@ -138,42 +163,39 @@ RSpec.describe Todos::Destroy::EntityLeaveService do ...@@ -138,42 +163,39 @@ RSpec.describe Todos::Destroy::EntityLeaveService do
end end
it 'removes only users issue todos' do it 'removes only users issue todos' do
expect { subject }.to change { Todo.count }.from(5).to(4) expect { subject }.to change { Todo.count }.from(6).to(5)
end end
end end
end end
end end
end end
context 'when a user leaves a group' do describe 'updating a Group' do
subject { described_class.new(user.id, group.id, 'Group').execute } subject { described_class.new(user.id, group.id, 'Group').execute }
context 'when group is private' do context 'when group is private' do
it 'removes group and subproject todos for the user' do context 'when a user leaves a group' do
expect { subject }.to change { Todo.count }.from(5).to(2) it 'removes group and subproject todos for the user' do
expect { subject }.to change { Todo.count }.from(6).to(2)
expect(user.todos).to be_empty
expect(user2.todos).to match_array([todo_issue_user2, todo_group_user2])
end
context 'when the user is member of the group' do
before do
group.add_developer(user)
end
it 'does not remove any todos' do expect(user.todos).to be_empty
expect { subject }.not_to change { Todo.count } expect(user2.todos).to match_array([todo_issue_c_user2, todo_group_user2])
end end
end end
context 'when the user is member of the group project but not the group' do context 'access permissions' do
before do # rubocop:disable RSpec/LeakyConstantDeclaration
project.add_developer(user) GROUP_PRIVATE_ACCESS_TABLE =
end lambda do |_|
[
# :group_access, :project_access, :c_todos, :mr_todos, :method
[nil, :reporter, :keep, :keep, :does_not_remove_any_todos],
[:reporter, nil, :keep, :keep, :does_not_remove_any_todos]
]
end
# rubocop:enable RSpec/LeakyConstantDeclaration
it 'does not remove any todos' do it_behaves_like 'using different access permissions', GROUP_PRIVATE_ACCESS_TABLE
expect { subject }.not_to change { Todo.count }
end
end end
context 'with nested groups' do context 'with nested groups' do
...@@ -191,12 +213,12 @@ RSpec.describe Todos::Destroy::EntityLeaveService do ...@@ -191,12 +213,12 @@ RSpec.describe Todos::Destroy::EntityLeaveService do
context 'when the user is not a member of any groups/projects' do context 'when the user is not a member of any groups/projects' do
it 'removes todos for the user including subprojects todos' do it 'removes todos for the user including subprojects todos' do
expect { subject }.to change { Todo.count }.from(11).to(4) expect { subject }.to change { Todo.count }.from(12).to(4)
expect(user.todos).to be_empty expect(user.todos).to be_empty
expect(user2.todos) expect(user2.todos)
.to match_array( .to match_array(
[todo_issue_user2, todo_group_user2, todo_subproject_user2, todo_subpgroup_user2] [todo_issue_c_user2, todo_group_user2, todo_subproject_user2, todo_subpgroup_user2]
) )
end end
end end
...@@ -208,9 +230,7 @@ RSpec.describe Todos::Destroy::EntityLeaveService do ...@@ -208,9 +230,7 @@ RSpec.describe Todos::Destroy::EntityLeaveService do
parent_group.add_developer(user) parent_group.add_developer(user)
end end
it 'does not remove any todos' do it_behaves_like 'does not remove any todos'
expect { subject }.not_to change { Todo.count }
end
end end
context 'when the user is member of a subgroup' do context 'when the user is member of a subgroup' do
...@@ -219,12 +239,12 @@ RSpec.describe Todos::Destroy::EntityLeaveService do ...@@ -219,12 +239,12 @@ RSpec.describe Todos::Destroy::EntityLeaveService do
end end
it 'does not remove group and subproject todos' do it 'does not remove group and subproject todos' do
expect { subject }.to change { Todo.count }.from(11).to(7) expect { subject }.to change { Todo.count }.from(12).to(7)
expect(user.todos).to match_array([todo_group_user, todo_subgroup_user, todo_subproject_user]) expect(user.todos).to match_array([todo_group_user, todo_subgroup_user, todo_subproject_user])
expect(user2.todos) expect(user2.todos)
.to match_array( .to match_array(
[todo_issue_user2, todo_group_user2, todo_subproject_user2, todo_subpgroup_user2] [todo_issue_c_user2, todo_group_user2, todo_subproject_user2, todo_subpgroup_user2]
) )
end end
end end
...@@ -235,12 +255,12 @@ RSpec.describe Todos::Destroy::EntityLeaveService do ...@@ -235,12 +255,12 @@ RSpec.describe Todos::Destroy::EntityLeaveService do
end end
it 'does not remove subproject and group todos' do it 'does not remove subproject and group todos' do
expect { subject }.to change { Todo.count }.from(11).to(7) expect { subject }.to change { Todo.count }.from(12).to(7)
expect(user.todos).to match_array([todo_subgroup_user, todo_group_user, todo_subproject_user]) expect(user.todos).to match_array([todo_subgroup_user, todo_group_user, todo_subproject_user])
expect(user2.todos) expect(user2.todos)
.to match_array( .to match_array(
[todo_issue_user2, todo_group_user2, todo_subproject_user2, todo_subpgroup_user2] [todo_issue_c_user2, todo_group_user2, todo_subproject_user2, todo_subpgroup_user2]
) )
end end
end end
...@@ -248,10 +268,10 @@ RSpec.describe Todos::Destroy::EntityLeaveService do ...@@ -248,10 +268,10 @@ RSpec.describe Todos::Destroy::EntityLeaveService do
end end
context 'when group is not private' do context 'when group is not private' do
before do let_it_be(:group, reload: true) { create(:group, :internal) }
group.update!(visibility_level: Gitlab::VisibilityLevel::INTERNAL) let_it_be(:project, reload: true) { create(:project, :internal, group: group) }
project.update!(visibility_level: Gitlab::VisibilityLevel::INTERNAL) let_it_be(:issue, reload: true) { create(:issue, project: project) }
end let_it_be(:issue_c, reload: true) { create(:issue, project: project, confidential: true) }
it 'enqueues the PrivateFeaturesWorker' do it 'enqueues the PrivateFeaturesWorker' do
expect(TodosDestroyer::PrivateFeaturesWorker) expect(TodosDestroyer::PrivateFeaturesWorker)
...@@ -260,31 +280,20 @@ RSpec.describe Todos::Destroy::EntityLeaveService do ...@@ -260,31 +280,20 @@ RSpec.describe Todos::Destroy::EntityLeaveService do
subject subject
end end
context 'when user is not member' do context 'access permissions' do
it 'removes only confidential issues todos' do # rubocop:disable RSpec/LeakyConstantDeclaration
expect { subject }.to change { Todo.count }.from(5).to(4) GROUP_NOT_PRIVATE_ACCESS_TABLE =
end lambda do |_|
end [
# :group_access, :project_access, :c_todos, :mr_todos, :method
context 'when user is a project guest' do [nil, nil, :delete, :keep, :removes_only_confidential_issues_todos],
before do [nil, :guest, :delete, :keep, :removes_only_confidential_issues_todos],
project.add_guest(user) [:reporter, :guest, :keep, :keep, :does_not_remove_any_todos]
end ]
end
it 'removes only confidential issues todos' do # rubocop:enable RSpec/LeakyConstantDeclaration
expect { subject }.to change { Todo.count }.from(5).to(4)
end
end
context 'when user is a project guest & group developer' do
before do
project.add_guest(user)
group.add_developer(user)
end
it 'does not remove any todos' do it_behaves_like 'using different access permissions', GROUP_NOT_PRIVATE_ACCESS_TABLE
expect { subject }.not_to change { Todo.count }
end
end end
end end
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