Skip to content

Commit

Permalink
add session mailer and spec
Browse files Browse the repository at this point in the history
  • Loading branch information
JensRavens committed Jan 14, 2025
1 parent 6c82d03 commit 5de1c03
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 0 deletions.
5 changes: 5 additions & 0 deletions app/views/layouts/mailer.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
html xmlns="http://www.w3.org/1999/xhtml"
head
meta http-equiv="Content-Type" content="text/html; charset=utf-8"
meta name="viewport" content="width=device-width, initial-scale=1.0"
body lang=I18n.locale = yield
1 change: 1 addition & 0 deletions app/views/user_mailer/login_code.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
p Your login code is #{@code}
49 changes: 49 additions & 0 deletions spec/support/mail_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# frozen_string_literal: true

class TestMail
attr_reader :to, :from, :subject, :body

def initialize(mail)
@to = mail["to"]
@from = mail["from"]
@subject = mail.subject
@original_body = mail.body.to_s
@body = Nokogiri::HTML(mail.body.to_s)
end

def link_urls
body.css("a").pluck("href")
end
end

module MailHelper
class NoMailSentError < StandardError; end

def last_mail
return nil unless ActionMailer::Base.deliveries.count > 0

TestMail.new(ActionMailer::Base.deliveries.last)
end

def last_mail!
last_mail || raise(NoMailSentError)
end

def reset_mails
ActionMailer::Base.deliveries = []
end

def run_jobs
# recursivly run the job queue until there are no jobs remaining
count = perform_enqueued_jobs
run_jobs if count.to_i > 0
end
end

RSpec.configure do |config|
config.before do
ActionMailer::Base.deliveries = []
end
config.include MailHelper, type: :system
config.include MailHelper, type: :model
end
29 changes: 29 additions & 0 deletions spec/system/session_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

require "system_helper"

RSpec.describe "Sessions" do
fixtures :all

it "logs a user in via a code" do
visit root_path
expect(page).to have_content "Login"
fill_in "Email", with: "[email protected]"
click_on "Login"
expect(page).to have_content "Code"

run_jobs
expect(last_mail!.body).to have_content "Your login code is"
code = last_mail.body.to_s.scan(/\d{6}/).first
expect(code).to be_present
fill_in "Code", with: code
click_on "Login"
expect(page).to have_content "Hello"
end

it "logs the user out" do
login :john
visit logout_path
expect(page).to have_content "Login"
end
end

0 comments on commit 5de1c03

Please sign in to comment.