-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2657 from alphagov/refactor_build_user_update_params
Refactor build user update params
- Loading branch information
Showing
8 changed files
with
98 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class UserUpdatePermissionBuilder | ||
def initialize(user:, updatable_permission_ids:, selected_permission_ids:) | ||
@user = user | ||
@updatable_permission_ids = updatable_permission_ids | ||
@selected_permission_ids = selected_permission_ids | ||
end | ||
|
||
def build | ||
permissions_user_has = @user.supported_permissions.pluck(:id) | ||
permissions_to_add = @updatable_permission_ids.intersection(@selected_permission_ids) | ||
permissions_to_remove = @updatable_permission_ids.difference(@selected_permission_ids) | ||
|
||
(permissions_user_has + permissions_to_add - permissions_to_remove).sort | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
require "test_helper" | ||
|
||
class UserUpdatePermissionBuilderTest < ActiveSupport::TestCase | ||
def setup | ||
application = create(:application) | ||
create(:supported_permission, application:, name: "perm-1") | ||
@user = create(:user, with_permissions: { application => %w[perm-1] }) | ||
@existing_permission_id = @user.supported_permissions.first.id | ||
end | ||
|
||
context "#build" do | ||
should "should return users existing permission if not updatable and not selected" do | ||
builder = UserUpdatePermissionBuilder.new( | ||
user: @user, | ||
updatable_permission_ids: [], | ||
selected_permission_ids: [], | ||
) | ||
|
||
assert_equal [@existing_permission_id], builder.build | ||
end | ||
|
||
should "should remove users existing permission if updatable and not selected" do | ||
builder = UserUpdatePermissionBuilder.new( | ||
user: @user, | ||
updatable_permission_ids: [@existing_permission_id], | ||
selected_permission_ids: [], | ||
) | ||
|
||
assert_equal [], builder.build | ||
end | ||
|
||
should "should add new permission if updatable and selected" do | ||
builder = UserUpdatePermissionBuilder.new( | ||
user: @user, | ||
updatable_permission_ids: [1], | ||
selected_permission_ids: [1], | ||
) | ||
|
||
assert_equal [1, @existing_permission_id].sort, builder.build | ||
end | ||
|
||
should "should not add new permission if updatable and not selected" do | ||
builder = UserUpdatePermissionBuilder.new( | ||
user: @user, | ||
updatable_permission_ids: [1], | ||
selected_permission_ids: [], | ||
) | ||
|
||
assert_equal [@existing_permission_id], builder.build | ||
end | ||
|
||
should "should not add new permission if not updatable" do | ||
builder = UserUpdatePermissionBuilder.new( | ||
user: @user, | ||
updatable_permission_ids: [1], | ||
selected_permission_ids: [2], | ||
) | ||
|
||
assert_equal [@existing_permission_id], builder.build | ||
end | ||
end | ||
end |