-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6c82d03
commit 5de1c03
Showing
4 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
p Your login code is #{@code} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |