Skip to content

A platform for community discussion. Free, open, simple.

DEV: Improve errors for failed impersonation (#34509)

Ted Johansson337cfeabbb25
diff --git a/app/assets/javascripts/admin/addon/controllers/admin-user-index.js b/app/assets/javascripts/admin/addon/controllers/admin-user-index.js
index 3deda1dd730..8a9afd4bb50 100644
--- a/app/assets/javascripts/admin/addon/controllers/admin-user-index.js
+++ b/app/assets/javascripts/admin/addon/controllers/admin-user-index.js
@@ -179,10 +179,14 @@ export default class AdminUserIndexController extends Controller {
.impersonate()
.then(() => DiscourseURL.redirectTo("/"))
.catch((e) => {
- if (e.status === 404) {
+ const status = e.jqXHR.status;
+
+ if (status === 404) {
this.dialog.alert(i18n("admin.impersonate.not_found"));
- } else {
+ } else if (status === 403) {
this.dialog.alert(i18n("admin.impersonate.invalid"));
+ } else {
+ this.dialog.alert(i18n("admin.impersonate.error"));
}
this.set("isLoading", false);
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml
index b57c8b63857..453debbf088 100644
--- a/config/locales/client.en.yml
+++ b/config/locales/client.en.yml
@@ -7668,6 +7668,7 @@ en:
help: "Use this tool to impersonate a user account for debugging purposes. You will have to log out once finished."
not_found: "That user can't be found."
invalid: "Sorry, you may not impersonate that user."
+ error: "Oops. An unexpected error occurred."
users:
title: "Users"
diff --git a/spec/system/impersonation_spec.rb b/spec/system/impersonation_spec.rb
index 96a658bb286..b15739483e9 100644
--- a/spec/system/impersonation_spec.rb
+++ b/spec/system/impersonation_spec.rb
@@ -4,6 +4,8 @@ describe "Impersonation", type: :system do
fab!(:admin)
fab!(:user)
+ let(:dialog) { PageObjects::Components::Dialog.new }
+
before do
SiteSetting.experimental_impersonation = true
@@ -28,4 +30,37 @@ describe "Impersonation", type: :system do
expect(page).to have_current_path("/")
expect(page).to have_no_css(".impersonation-notice")
end
+
+ it "shows a helpful error when the user is not found" do
+ Admin::ImpersonateController.any_instance.stubs(:create).raises(Discourse::NotFound)
+
+ visit("/admin/users/#{user.id}/#{user.username}")
+
+ page.find(".btn-impersonate").click
+
+ expect(dialog).to be_open
+ expect(dialog).to have_content(I18n.t("admin_js.admin.impersonate.not_found"))
+ end
+
+ it "shows a helpful error when impersonation of that user is not allowed" do
+ Admin::ImpersonateController.any_instance.stubs(:create).raises(Discourse::InvalidAccess)
+
+ visit("/admin/users/#{user.id}/#{user.username}")
+
+ page.find(".btn-impersonate").click
+
+ expect(dialog).to be_open
+ expect(dialog).to have_content(I18n.t("admin_js.admin.impersonate.invalid"))
+ end
+
+ it "shows a helpful error when there's an unexpected server error" do
+ Admin::ImpersonateController.any_instance.stubs(:create).raises(StandardError)
+
+ visit("/admin/users/#{user.id}/#{user.username}")
+
+ page.find(".btn-impersonate").click
+
+ expect(dialog).to be_open
+ expect(dialog).to have_content(I18n.t("admin_js.admin.impersonate.error"))
+ end
end