Skip to content

Commit

Permalink
620 tester trainer emulation (#755)
Browse files Browse the repository at this point in the history
* Creating emulation bar for tester-trainers
Creating emulation bar for non-production environments, and adding several roles as options to be emulated

* rubocop 🚨

* creating the tester-trainer emulation bar
moving the emulation menu to the emulation bar
spec test for new emulation banner
updated user factory to add trainers

* rubocop 🚨

* working on posting to emulate on the change of the emulation dropdown

* rubocop 🚨

* eslint

* passing an authenticity token along with the form

* rubocop

* updating welcome controller spec

* eslint

* restricting the emulation banner to the home page
  • Loading branch information
JaymeeH authored Jun 7, 2024
1 parent 1c6aaa7 commit 6a23610
Show file tree
Hide file tree
Showing 14 changed files with 117 additions and 27 deletions.
25 changes: 25 additions & 0 deletions app/assets/stylesheets/_emulator.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#emulator {
text-align: center;
background-color: rgb(231, 117, 0);
padding: 20px 0px 10px 0px;
border-bottom: #000000 solid 2px;
color: rgb(0, 0, 0);
box-shadow: inset 0px 9px 10px 2px rgba(0,0,0,0.5);

h1 {
font-size: 24px;
font-weight: bold;
}
p {
font-size: 18px;
}
a {
text-decoration: underline;
}
}

@media all and (max-width: 860px) {
#emulator {
padding: 10px 3px 5px 3px;
}
}
1 change: 1 addition & 0 deletions app/assets/stylesheets/application.scss
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

@import "settings";
@import "banner";
@import "emulator";
@import "header";
@import "footer";
@import "bootstrap";
11 changes: 6 additions & 5 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ def emulate_user
return if current_user.blank? || !current_user.trainer

if session[:emulation_role]
role = session[:emulation_role]
if role == "sponsor"
if session[:emulation_role] == "Eligible Data Sponsor"
emulate_sponsor
elsif role == "manager"
elsif session[:emulation_role] == "Eligible Data Manager"
emulate_manager
elsif role == "sysadmin"
elsif session[:emulation_role] == "System Administrator"
emulate_sysadmin
elsif role == "data_user"
elsif session[:emulation_role] == "Eligible Data User"
emulate_data_user
elsif session[:emulation_role] == "Return to Self"
emulate_data_user
end
end
Expand Down
2 changes: 0 additions & 2 deletions app/controllers/welcome_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ def emulate
if params.key?("emulation_menu")
session[:emulation_role] = params[:emulation_menu]
end
# TODO: POST TO EMULATE AND PASS THE UPDATED SESSION
# call emulate_user on the user object
end

def help; end
Expand Down
13 changes: 13 additions & 0 deletions app/helpers/emulator_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

require "yaml"

module EmulatorHelper
def emulator_content
@yaml_data = YAML.load_file("config/emulator.yml")
return false if @yaml_data[Rails.env].nil? || @yaml_data[Rails.env] == "production"
return false unless current_page?("/")
@emulator_title = @yaml_data[Rails.env]["title"]
@emulator_body = @yaml_data[Rails.env]["body"]
end
end
13 changes: 13 additions & 0 deletions app/javascript/entrypoints/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,18 @@ function showMoreLessSysAdmin() {
});
}

function emulate() {
$(document).ready(() => {
$("[name='emulation_menu']").on('change', () => {
$.ajax({
type: 'POST',
url: $('#emulation-form').attr('action'),
data: $('#emulation-form').serialize(),
});
});
});
}

function initPage() {
$('#test-jquery').click((event) => {
setTargetHtml(event, 'jQuery works!');
Expand All @@ -188,6 +200,7 @@ function initPage() {
initListContentsModal();
showMoreLessContent();
showMoreLessSysAdmin();
emulate();
}

window.addEventListener('load', () => initPage());
Expand Down
1 change: 1 addition & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<body>
<%= render partial: 'shared/banner' %>
<%= render partial: 'shared/header' %>
<%= render partial: 'shared/emulator' %>
<div class="container">
<% if flash[:alert] %>
<div class="row">
Expand Down
18 changes: 18 additions & 0 deletions app/views/shared/_emulator.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<% if emulator_content && !current_user.nil? && current_user.trainer%>
<div id="emulator">
<h1><%= sanitize @emulator_title %></h1>
<p><%= sanitize @emulator_body, attributes: %w(href target)%></p>
<form action="emulate" method="post" id="emulation-form">
<label for="emulation_menu" class="form-label">Current Role:</label>
<select id="emulation_menu" name="emulation_menu" class="form-dropdown" required>
<option disabled="disabled" <%= "selected" if current_user.trainer?%>>Trainer</option>
<option value="Eligible Data Sponsor" <%= "selected" if current_user.eligible_sponsor%>>Eligible Data Sponsor</option>
<option value="Eligible Data Manager" <%= "selected" if current_user.eligible_manager%>>Eligible Data Manager</option>
<option value="Eligible Data User" <%= "selected" if @eligible_sysadmin%>>Eligible Data User</option>
<option value="System Administrator" <%= "selected" if current_user.sysadmin%>>System Administrator</option>
<option value="Return to Self">Return to Self</option>
</select>
<%= token_tag nil %>
</form>
</div>
<% end %>
13 changes: 0 additions & 13 deletions app/views/welcome/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,6 @@
Please log in.
<% else %>
<h1>Welcome, <%= current_user.given_name %>!</h1>
<% if !Rails.env.production? && current_user.trainer?%>
<label for="emulation-menu" name= "emulation-menu" class="form-label">View As</label>
<select id="emulation-menu" name="emulation-menu" class="form-dropdown" required>
<option disabled="disabled" <%= "selected" if current_user.trainer?%>>Trainer</option>
<option value="Eligible Data Sponsor" <%= "selected" if current_user.eligible_sponsor%>>Eligible Data Sponsor</option>
<option value="Eligible Data Manager" <%= "selected" if current_user.eligible_manager%>>Eligible Data Manager</option>
<option value="Eligible Data User" <%= "selected" if @eligible_sysadmin%>>Eligible Data User</option>
<option value="System Administrator" <%= "selected" if current_user.sysadmin%>>System Administrator</option>
<option value="Return to Self">Return to Self</option>
</select>
<button type="submit" class="btn btn-primary">Emulate</button>

<% end %>
<div style="display: inline-flex">
<% if !current_user.eligible_sysadmin? or current_user.superuser? %>
<div id="dashboard-projects">
Expand Down
14 changes: 14 additions & 0 deletions config/emulator.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
development:
title: As an approved tester-trainer
body: You may select another role to emulate
qa:
title: As an approved tester-trainer
body: You may select another role to emulate
staging:
title: As an approved tester-trainer
body: You may select another role to emulate
test:
title: As an approved tester-trainer
body: This is a <b>test</b> <a href="mailto:[email protected]">message.</a>

12 changes: 6 additions & 6 deletions spec/controllers/welcome_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
it "accepts a post to change the emulation role" do
user.trainer = true
user.save!
post :emulate, params: { emulation_menu: "sysadmin" }
expect(session[:emulation_role]).to eq "sysadmin"
post :emulate, params: { emulation_menu: "System Administrator" }
expect(session[:emulation_role]).to eq "System Administrator"

get :index, session: session
expect(assigns(:current_user).sysadmin).to be_truthy
Expand All @@ -36,7 +36,7 @@
it "renders the index page" do
user.trainer = true
user.save!
get :index, session: { emulation_role: "sysadmin" }
get :index, session: { emulation_role: "System Administrator" }
expect(response).to render_template("index")

expect(assigns(:current_user).sysadmin).to be_truthy
Expand All @@ -48,7 +48,7 @@
it "renders the index page" do
user.trainer = true
user.save!
get :index, session: { emulation_role: "sponsor" }
get :index, session: { emulation_role: "Eligible Data Sponsor" }
expect(response).to render_template("index")
expect(assigns(:current_user).sysadmin).to be_falsey
expect(assigns(:current_user).eligible_sponsor).to be_truthy
Expand All @@ -59,7 +59,7 @@
it "renders the index page" do
user.trainer = true
user.save!
get :index, session: { emulation_role: "manager" }
get :index, session: { emulation_role: "Eligible Data Manager" }
expect(response).to render_template("index")

expect(assigns(:current_user).sysadmin).to be_falsey
Expand All @@ -71,7 +71,7 @@
it "renders the index page" do
user.trainer = true
user.save!
get :index, session: { emulation_role: "data_user" }
get :index, session: { emulation_role: "Eligible Data User" }
expect(response).to render_template("index")

expect(assigns(:current_user).sysadmin).to be_falsey
Expand Down
7 changes: 7 additions & 0 deletions spec/factories/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
eligible_manager { false }
sysadmin { false }
superuser { false }
trainer { false }

##
# A user who is allowed to sponsor a project
Expand Down Expand Up @@ -43,5 +44,11 @@
factory :superuser do
superuser { true }
end

##
# A user who is allowed to emulate other roles in the system
factory :trainer do
trainer { true }
end
end
end
12 changes: 12 additions & 0 deletions spec/system/emulator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

require "rails_helper"

describe "Website banner", type: :system, js: true do
let(:current_user) { FactoryBot.create(:trainer, uid: "pul123") }
it "has the banner on the homepage" do
sign_in current_user
visit "/"
expect(page).to have_css "#emulator"
end
end
2 changes: 1 addition & 1 deletion spec/system/welcome_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
current_user.trainer = true
current_user.save!
visit "/"
expect(page).to have_select("emulation-menu")
expect(page).to have_select("emulation_menu")
end
end
end
Expand Down

0 comments on commit 6a23610

Please sign in to comment.