Skip to content

Commit

Permalink
Show success banner when removing access from an application
Browse files Browse the repository at this point in the history
  • Loading branch information
Gweaton committed Sep 18, 2024
1 parent 4103b7c commit 4752dfd
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/controllers/account/signin_permissions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def destroy
params = { supported_permission_ids: current_user.supported_permissions.map(&:id) - [application.signin_permission.id] }
UserUpdate.new(current_user, params, current_user, user_ip_address).call

flash[:success_alert] = { message: "Access removed", description: access_removed_description(application.id) }
redirect_to account_applications_path
end

Expand Down
1 change: 1 addition & 0 deletions app/controllers/users/signin_permissions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def destroy
params = { supported_permission_ids: @user.supported_permissions.map(&:id) - [@application.signin_permission.id] }
UserUpdate.new(@user, params, current_user, user_ip_address).call

flash[:success_alert] = { message: "Access removed", description: access_removed_description(@application.id, @user) }
redirect_to user_applications_path(@user)
end

Expand Down
9 changes: 9 additions & 0 deletions app/helpers/application_access_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,13 @@ def access_granted_description(application_id, user = current_user)

"#{user.name} has been granted access to #{application.name}."
end

def access_removed_description(application_id, user = current_user)
application = Doorkeeper::Application.find_by(id: application_id)
return nil unless application

return "Your access to #{application.name} has been removed." if user == current_user

"#{user.name}'s access to #{application.name} has been removed."
end
end
25 changes: 25 additions & 0 deletions test/controllers/account/signin_permissions_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,31 @@ class Account::SigninPermissionsControllerTest < ActionController::TestCase
end

context "#destroy" do
should "assign the success alert message to flash" do
current_user = create(:admin_user)
sign_in current_user

application = create(:application)
current_user.grant_application_signin_permission(application)

stub_policy(
current_user,
application,
policy_class: Account::ApplicationPolicy,
remove_signin_permission?: true,
)

Account::SigninPermissionsController
.any_instance
.expects(:access_removed_description)
.with(application.id).returns("Removed access from myself")

delete :destroy, params: { application_id: application.id }

expected = { message: "Access removed", description: "Removed access from myself" }
assert_equal expected, flash[:success_alert]
end

should "prevent unauthenticated users" do
application = create(:application)

Expand Down
26 changes: 26 additions & 0 deletions test/controllers/users/signin_permissions_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,32 @@ class Users::SigninPermissionsControllerTest < ActionController::TestCase
assert_redirected_to user_applications_path(user)
end

should "assign the success alert message to flash" do
current_user = create(:admin_user)
sign_in current_user

user = create(:user)
application = create(:application)
user.grant_application_signin_permission(application)

stub_policy(
current_user,
{ application:, user: },
policy_class: Users::ApplicationPolicy,
remove_signin_permission?: true,
)

Users::SigninPermissionsController
.any_instance
.expects(:access_removed_description)
.with(application.id, user).returns("Removed access from another user")

delete :destroy, params: { user_id: user, application_id: application.id }

expected = { message: "Access removed", description: "Removed access from another user" }
assert_equal expected, flash[:success_alert]
end

should "prevent unauthenticated users" do
user = create(:user)
application = create(:application)
Expand Down
21 changes: 21 additions & 0 deletions test/helpers/application_access_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,25 @@ class ApplicationAccessHelperTest < ActionView::TestCase
end
end
end

context "#access_removed_description" do
context "when the user is removing their own access" do
should "return a message informing them that they no longer have access to the application" do
assert_equal "Your access to Whitehall has been removed.", access_removed_description(@application)
end
end

context "when the user is removing another's access" do
should "return a message informing them that the other user no longer has access to the application" do
user = create(:user, name: "Gerald")
assert_equal "Gerald's access to Whitehall has been removed.", access_removed_description(@application, user)
end
end

context "when the application does not exist" do
should "return nil" do
assert_nil access_removed_description(:made_up_id)
end
end
end
end

0 comments on commit 4752dfd

Please sign in to comment.