Skip to content

Commit 7e48193

Browse files
authored
Add test route to avoid the shadow root/having to render the global nav (#70)
This PR adds a login page to use in testing to avoid having to render the rpf-global-nav in tests that don't need JS. # What's changed * Adds a page at `/rpi_auth/test` which shows a login and sign-up button that can be used in test scenarios * Adds a `log_in(user:)` and `stub_auth_for(user:)` methods that we've replicated in a lot of places. * Adds capybara to the development deps, and updated the gemfiles * Sets the `rubygems` version during CI because one of the Gems doesn't build with the older version of Rubygems that ships with Ruby 2.7
1 parent e662c77 commit 7e48193

21 files changed

+673
-386
lines changed

.github/workflows/test.yml

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ jobs:
2525
uses: ruby/setup-ruby@v1
2626
with:
2727
ruby-version: ${{ matrix.ruby-version }}
28+
rubygems: 3.4.10
2829
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
2930
- name: Run tests
3031
run: bundle exec rspec

.rubocop.yml

+3
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@ inherit_mode:
1010
- Exclude
1111

1212
AllCops:
13+
NewCops: enable
1314
TargetRubyVersion: 2.7
1415
Exclude:
1516
- 'spec/dummy/config/**/*'
1617
- 'spec/dummy/app/**/*'
1718
- 'spec/dummy/bin/**/*'
1819
- 'vendor/**/*'
20+
- 'gemfiles/vendor/**/*'
1921
- 'bin/rails'
22+

.rubocop_todo.yml

+32-13
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,46 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2021-09-14 13:25:06 UTC using RuboCop version 1.20.0.
3+
# on 2024-07-02 19:48:59 UTC using RuboCop version 1.64.1.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
77
# versions of RuboCop, may require this file to be generated again.
88

9-
# Offense count: 6
10-
# Configuration parameters: CountAsOne.
11-
RSpec/ExampleLength:
12-
Max: 8
9+
# Offense count: 11
10+
# Configuration parameters: EnforcedStyle, AllowedGems, Include.
11+
# SupportedStyles: Gemfile, gems.rb, gemspec
12+
# Include: **/*.gemspec, **/Gemfile, **/gems.rb
13+
Gemspec/DevelopmentDependencies:
14+
Exclude:
15+
- 'rpi_auth.gemspec'
1316

1417
# Offense count: 1
15-
# Configuration parameters: Include, CustomTransform, IgnoreMethods, SpecSuffixOnly.
16-
# Include: **/*_spec*rb*, **/spec/**/*
17-
RSpec/FilePath:
18-
Exclude:
19-
- 'spec/rpi_auth/models/authenticatable_spec.rb'
18+
# Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes.
19+
Metrics/AbcSize:
20+
Max: 23
2021

2122
# Offense count: 7
22-
RSpec/MultipleExpectations:
23+
# Configuration parameters: CountAsOne.
24+
RSpec/ExampleLength:
2325
Max: 8
2426

25-
# Offense count: 1
26-
RSpec/NestedGroups:
27+
# Offense count: 20
28+
RSpec/MultipleExpectations:
2729
Max: 4
30+
31+
# Offense count: 8
32+
# Configuration parameters: AllowSubject.
33+
RSpec/MultipleMemoizedHelpers:
34+
Max: 6
35+
36+
# Offense count: 10
37+
# Configuration parameters: AllowedGroups.
38+
RSpec/NestedGroups:
39+
Max: 5
40+
41+
# Offense count: 1
42+
# Configuration parameters: Include, CustomTransform, IgnoreMethods, IgnoreMetadata.
43+
# Include: **/*_spec.rb
44+
RSpec/SpecFilePathFormat:
45+
Exclude:
46+
- 'spec/rpi_auth/models/authenticatable_spec.rb'

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
- Test controller/view to allow apps to log in without having to interact with the RPF Global Nav component. (#70)
12+
1013
## [v3.5.0]
1114

1215
### Added

README.md

+16
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,22 @@ class in `config/application.rb`.
187187
config.railties_order = [RpiAuth::Engine, :main_app, :all]
188188
```
189189

190+
## Test helpers and routes
191+
192+
There are some standardised test helpers in `RpiAuth::SpecHelpers` that can be used when testing.
193+
194+
* `stub_auth_for(user:)` enables the Omniauth test mode, and makes sure any auth requests succeed, returning this user.
195+
* `log_in(user:)` goes through the log-in process for that user, either using Capybara-style `click_on` methods, or POST'ing directly to the auth endpoint.
196+
197+
There is also a page at `/rpi_auth/test` that has log-in and sign-up buttons which can be navigated to as part of the test suite to avoid having to render pages, or navigate into the shadow roots.
198+
199+
To user these helpers you should add this to your `spec/rails_helper.rb`, inside the `RSpec.configure do |config|` block.
200+
201+
```ruby
202+
config.include RpiAuth::SpecHelpers, type: :request
203+
config.include RpiAuth::SpecHelpers, type: :system
204+
```
205+
190206
## Troubleshooting
191207

192208
Diagnosing issues with OpenID Connect can be tricky, so here are some things to try.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# frozen_string_literal: true
2+
3+
require 'rpi_auth/controllers/current_user'
4+
5+
module RpiAuth
6+
class TestController < ActionController::Base
7+
include RpiAuth::Controllers::CurrentUser
8+
include Rails.application.routes.url_helpers
9+
10+
layout false
11+
12+
def show
13+
render locals: { login_params: login_params, logout_params: logout_params }
14+
end
15+
16+
private
17+
18+
def login_params
19+
params.permit(:returnTo)
20+
end
21+
22+
alias logout_params login_params
23+
end
24+
end

app/views/rpi_auth/test/show.html.erb

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!doctype html>
2+
<html lang="en" dir="ltr">
3+
<head>
4+
<%= stylesheet_link_tag 'https://static.raspberrypi.org/styles/design-system-core/releases/v1.1.0/design-system-core.css' %>
5+
<%= csp_meta_tag %>
6+
</head>
7+
<body>
8+
<div class='login-box'>
9+
<% if current_user.present? %>
10+
<p>Log out of your Raspberry Pi account</p>
11+
<p>Logged in as <%= current_user.user_id %></p>
12+
<%= link_to 'Log out', rpi_auth_logout_path(params: logout_params), class: "rpf-button" %>
13+
<% else %>
14+
<p>Log in with your Raspberry Pi account</p>
15+
<%= button_to 'Log in', rpi_auth_login_path, params: login_params, class: "rpf-button" %>
16+
<p>Sign up for a Raspberry Pi account</p>
17+
<%= button_to 'Sign up', rpi_auth_signup_path, params: login_params, class: "rpf-button" %>
18+
<% end %>
19+
</div>
20+
</body>
21+
</html>

config/routes.rb

+4
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,8 @@
99

1010
get RpiAuth::Engine::CALLBACK_PATH, to: 'rpi_auth/auth#callback', as: 'rpi_auth_callback'
1111
get RpiAuth::Engine::LOGOUT_PATH, to: 'rpi_auth/auth#destroy', as: 'rpi_auth_logout'
12+
13+
# This route can be used in testing to log in, avoiding the need to interact
14+
# with shadow root in the RPF global nav.
15+
get RpiAuth::Engine::TEST_PATH, to: 'rpi_auth/test#show', as: 'rpi_auth_test' if RpiAuth::Engine::ENABLE_TEST_PATH
1216
end

0 commit comments

Comments
 (0)