-
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.
We test these two scenarios multiple times in three different controller tests. Now the underlying logic has been extracted to UserUpdatePermissionBuilder I think they can be replaced with simpler and more comprehensive tests of `UserUpdatePermissionBuilder#build`. Note the remaining tests of the update method in the three permissions controllers are still useful - they effectively test that the correct values are set by the `set_application` and `set_permissions` methods on those controllers. I considered refactoring these tests to `expect` that `UserUpdatePermissionBuilder` is called correctly, but decided to keep things as they are for now. This change still feels like an improvement.
- Loading branch information
Showing
4 changed files
with
62 additions
and
113 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,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 |