Skip to content

Commit 5b691f8

Browse files
committed
request test setup
1 parent 35e4901 commit 5b691f8

File tree

6 files changed

+51
-9
lines changed

6 files changed

+51
-9
lines changed

app/controllers/application_controller.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
# frozen_string_literal: true
22

33
class ApplicationController < ActionController::Base
4+
# around_action do |_controller, block|
5+
# binding.irb
6+
# block.call
7+
# end
8+
49
# Only allow modern browsers supporting webp images, web push, badges, import maps, CSS nesting, and CSS :has.
5-
# allow_browser versions: :modern
10+
allow_browser versions: :modern
611

712
include ApplicationHelper
813
include I18nHelper
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# frozen_string_literal: true
2+
3+
# Helper to sign in users in request tests.
4+
class TestSessionsController < ApplicationController
5+
skip_after_action :verify_authorized
6+
skip_before_action :require_fully_authenticated_user!
7+
8+
def create
9+
raise 'test authorisation called outside of test environment' unless Rails.env.test?
10+
11+
user = params[:user_class].constantize.find(params[:user_id])
12+
auto_login(user)
13+
head :ok
14+
end
15+
end

config/routes.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,4 +199,8 @@
199199

200200
# Defines the root path route ("/")
201201
root to: 'application#welcome'
202+
203+
if Rails.env.test?
204+
post '/test_login', to: 'test_sessions#create'
205+
end
202206
end
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# frozen_string_literal: true
2+
3+
require 'rails_helper'
4+
5+
RSpec.describe 'POST /request_for_comments/:rfc_id/report', type: :request do
6+
it 'sends an email to let admins know about the report' do
7+
login_as(create(:learner))
8+
rfc = create(:rfc)
9+
10+
expect { post(report_request_for_comment_path(rfc, session: {foo: 'bar'})) }
11+
.to have_enqueued_mail(ReportMailer, :report_content)
12+
.with(params: {reported_content: rfc}, args: [])
13+
end
14+
end

spec/support/authentication.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ def sign_in(user, password)
66
end
77
end
88

9+
module RequestLoginHelper
10+
def login_as(user)
11+
post '/test_login', params: {user_id: user.id, user_class: user.class.name}
12+
end
13+
end
14+
915
RSpec.configure do |config|
1016
config.include Authentication, type: :system
17+
config.include RequestLoginHelper, type: :request
1118
end

spec/system/request_for_comments_filter_system_spec.rb

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,10 @@
4242
request_for_comment = create(:rfc)
4343
visit(request_for_comment_path(request_for_comment))
4444

45-
expect do
46-
accept_confirm do
47-
click_on I18n.t('request_for_comments.report.report')
48-
end
49-
50-
expect(page).to have_text(I18n.t('request_for_comments.report.reported'))
51-
end.to have_enqueued_mail(ReportMailer, :report_content)
52-
.with(params: {reported_content: request_for_comment}, args: [])
45+
accept_confirm do
46+
click_on I18n.t('request_for_comments.report.report')
47+
end
48+
49+
expect(page).to have_text(I18n.t('request_for_comments.report.reported'))
5350
end
5451
end

0 commit comments

Comments
 (0)