forked from houndci/hound
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This adds a route that admins can go to that will put them in a mode where they masquerade as the user they provided. They can then browse hound as if they were that user, and then click "Stop Masquerading" to return to their own account. Also add an administrate sidebar partial since we now have more than just administrate in the admin namespace, we need to exclude it from the sidebar manually since administrate does not provide a way to exclude it. There's some work to solve this but hasn't been merged yet. thoughtbot/administrate#514 https://trello.com/c/h9VtPK6F/800-as-an-admin-i-would-like-to-masquerade-as-a-user-so-that-i-may-help-debug-issues-that-they-are-running-into
- Loading branch information
1 parent
eaaf9ed
commit 2042bbc
Showing
10 changed files
with
123 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# frozen_string_literal: true | ||
module Admin | ||
class MasqueradesController < Admin::ApplicationController | ||
def show | ||
requested_user = User.find_by(username: params[:username]) | ||
session[:masqueraded_user_id] = requested_user.id | ||
|
||
redirect_to repos_path | ||
end | ||
|
||
def destroy | ||
session.delete(:masqueraded_user_id) | ||
|
||
redirect_to repos_path | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
%ul.sidebar__list | ||
- sidebar_resources.each do |resource| | ||
%li | ||
= link_to display_resource_name(resource), | ||
[namespace, resource], | ||
class: "sidebar__link sidebar__link--#{nav_link_state(resource)}" |
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
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,25 @@ | ||
# frozen_string_literal: true | ||
require "rails_helper" | ||
|
||
feature "Admin masquerades as user" do | ||
scenario "admin sees user repos and can stop masquerading" do | ||
repo = create(:repo) | ||
admin = create(:user, username: "admin", token: "admin") | ||
user = create(:user, repos: [repo], username: "admin", token: "user") | ||
stub_const("Hound::ADMIN_GITHUB_USERNAMES", ["admin"]) | ||
|
||
sign_in_as(admin, "admin") | ||
visit admin_masquerade_path(user.username) | ||
|
||
expect(current_path).to eq(repos_path) | ||
within "header .account" do | ||
expect(page).to have_text(user.username) | ||
end | ||
|
||
click_on "Stop Masquerading" | ||
|
||
within "header .account" do | ||
expect(page).to have_text(admin.username) | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# frozen_string_literal: true | ||
require "rails_helper" | ||
|
||
describe "GET /masqerades?username=:username" do | ||
context "as an admin" do | ||
it "redirects to repos as the maqueraded user" do | ||
admin = create(:user, username: "admin", token: "admin-token") | ||
user = create(:user, username: "us3rn4me", token: "user-token") | ||
stub_const("Hound::ADMIN_GITHUB_USERNAMES", ["admin"]) | ||
|
||
sign_in_as(admin) | ||
get admin_masquerade_path(username: user.username) | ||
|
||
expect(response).to redirect_to(repos_path) | ||
follow_redirect! | ||
expect(response.body).to include("us3rn4me") | ||
expect(session[:masqueraded_user_id]).to eq(user.id) | ||
expect(session[:remember_token]).to eq(admin.remember_token) | ||
end | ||
end | ||
|
||
context "as a non-admin user" do | ||
it "redirects to root and does not masquerade as user" do | ||
non_admin = create(:user, token: "non-admin-token") | ||
user = create(:user, username: "us3rn4me", token: "user-token") | ||
|
||
sign_in_as(non_admin) | ||
get admin_masquerade_path(username: user.username) | ||
|
||
expect(response).to redirect_to(root_path) | ||
follow_redirect! | ||
expect(response.body).not_to include("us3rn4me") | ||
expect(session[:masqueraded_user_id]).to be_nil | ||
expect(session[:remember_token]).to eq(non_admin.remember_token) | ||
end | ||
end | ||
|
||
def sign_in_as(user) | ||
stub_oauth(username: user.username, email: user.email, token: user.token) | ||
get "/auth/github" | ||
follow_redirect! | ||
end | ||
end |