From 201e2b76b1e518da12a0143993c7761a2f7b1c76 Mon Sep 17 00:00:00 2001 From: Chris Roos Date: Tue, 12 Sep 2023 12:00:47 +0100 Subject: [PATCH] Add JS-enabled integration tests for /users page The equivalent non-JS functionality is already being tested in controller and unit tests. These additional happy path tests should give us confidence that the JavaScript functionality continues to work as expected. --- test/integration/admin_user_index_test.rb | 94 ++++++++++++++++++++--- 1 file changed, 84 insertions(+), 10 deletions(-) diff --git a/test/integration/admin_user_index_test.rb b/test/integration/admin_user_index_test.rb index c0e386401..aab86e893 100644 --- a/test/integration/admin_user_index_test.rb +++ b/test/integration/admin_user_index_test.rb @@ -3,6 +3,8 @@ class AdminUserIndexTest < ActionDispatch::IntegrationTest context "logged in as an admin" do setup do + use_javascript_driver + current_time = Time.zone.now Timecop.freeze(current_time) @@ -13,14 +15,21 @@ class AdminUserIndexTest < ActionDispatch::IntegrationTest org1 = create(:organisation, name: "Org 1") org2 = create(:organisation, name: "Org 2") - create(:user, name: "Aardvark", email: "aardvark@example.com", current_sign_in_at: current_time - 5.minutes) - create(:two_step_enabled_user, name: "Abbey", email: "abbey@example.com") - create(:user, name: "Abbot", email: "mr_ab@example.com") - create(:user, name: "Bert", email: "bbbert@example.com") - create(:user, name: "Ed", email: "ed@example.com", organisation: org1) - create(:user, name: "Eddie", email: "eddie_bb@example.com") - create(:two_step_exempted_user, name: "Ernie", email: "ernie@example.com", organisation: org2) - create(:suspended_user, name: "Suspended McFee", email: "suspenders@example.com") + @aardvark = create(:user, name: "Aardvark", email: "aardvark@example.com", current_sign_in_at: current_time - 5.minutes) + @abbey = create(:two_step_enabled_user, name: "Abbey", email: "abbey@example.com") + @abbot = create(:user, name: "Abbot", email: "mr_ab@example.com") + @bert = create(:user, name: "Bert", email: "bbbert@example.com") + @ed = create(:user, name: "Ed", email: "ed@example.com", organisation: org1) + @eddie = create(:user, name: "Eddie", email: "eddie_bb@example.com") + @ernie = create(:two_step_exempted_user, name: "Ernie", email: "ernie@example.com", organisation: org2) + @suspended_mcfee = create(:suspended_user, name: "Suspended McFee", email: "suspenders@example.com") + + application = create(:application, name: "App name") + create(:supported_permission, application:, name: "App permission") + @aardvark.grant_application_signin_permission(application) + @bert.grant_application_permission(application, "App permission") + + visit "/users" end teardown do @@ -28,12 +37,77 @@ class AdminUserIndexTest < ActionDispatch::IntegrationTest end should "display the 2SV enrollment status for users" do - visit "/users" - within "table" do assert has_css?("td", text: "Enabled", count: 1) assert has_css?("td", text: "Not set up", count: 7) end end + + should "list all users" do + assert_results @admin, @aardvark, @abbey, @abbot, @bert, @ed, @eddie, @ernie, @suspended_mcfee + end + + should "filter users by search string" do + fill_in "Search", with: "bb" + click_on "Search" + + assert_results @abbey, @abbot, @bert, @eddie + end + + should "filter users by status" do + check "Suspended", allow_label_click: true + + assert_results @suspended_mcfee + end + + should "filter users by 2sv status" do + click_on "2SV Status" + + check "Exempted", allow_label_click: true + assert_results @ernie + + check "Enable", allow_label_click: true + assert_results @ernie, @abbey + end + + should "filter users by role" do + click_on "Role" + + check "Admin", allow_label_click: true + assert_results @admin + end + + should "filter users by organisation" do + click_on "Organisation" + + check "Org 1", allow_label_click: true + assert_results @ed + + check "Org 2", allow_label_click: true + assert_results @ed, @ernie + end + + should "filter users by permission" do + click_on "Permissions" + + check "App name signin", allow_label_click: true + assert_results @aardvark + + check "App name App permission", allow_label_click: true + assert_results @aardvark, @bert + end + end + +private + + def assert_results(*users) + expected_table_caption = [users.count, "user".pluralize(users.count)].join(" ") + + table = find("table caption", text: expected_table_caption).ancestor(:table) + assert table.has_css?("tbody tr", count: users.count) + + users.each do |user| + assert table.has_content?(user.name) + end end end