Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions spec/system/devise/passwords/new_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@
end

describe "reset password page" do
let!(:user) { create(:user, email: "glados@aperture.labs", phone_number: "+16578900012") }

it "displays error messages for non-existent user" do
fill_in "Email", with: "tangerine@forward.com"
user = build(:user, email: "glados@example.com", phone_number: "+16578900012")

fill_in "Email", with: "tangerine@example.com"
fill_in "Phone number", with: user.phone_number

click_on "Send me reset password instructions"
expect(page).to have_content "If the account exists you will receive an email or SMS with instructions on how to reset your password in a few minutes."
end

it "displays phone number error messages for incorrect formatting" do
user = create(:user, email: "glados@example.com", phone_number: "+16578900012")

fill_in "Email", with: user.email
fill_in "Phone number", with: "2134567eee"

Expand All @@ -26,12 +28,14 @@
expect(page).to have_text("Phone number must be 10 digits or 12 digits including country code (+1)")
end

it "displays error if user tries to submit empty form" do
it "displays error if user tries to submit an empty form" do
click_on "Send me reset password instructions"
expect(page).to have_text("Please enter at least one field.")
end

it "redirects to sign up page for email" do
user = build(:user, email: "glados@example.com", phone_number: "+16578900012")

fill_in "Email", with: user.email

click_on "Send me reset password instructions"
Expand All @@ -45,24 +49,31 @@
it "sends user email" do
fill_in "Email", with: user.email

expect { click_on "Send me reset password instructions" }.to change { ActionMailer::Base.deliveries.count }.by(1)
expect(ActionMailer::Base.deliveries.last.to_addresses.map(&:address)).to include user.email
click_on "Send me reset password instructions"

expect(page).to have_content "If the account exists you will receive an email or SMS with instructions on how to reset your password in a few minutes."

expect(ActionMailer::Base.deliveries.count).to eq(1)
expect(ActionMailer::Base.deliveries.last.to).to eq([user.email])
end

it "has reset password url with token" do
fill_in "Email", with: user.email
click_on "Send me reset password instructions"

expect(page).to have_content "If the account exists you will receive an email or SMS with instructions on how to reset your password in a few minutes."
expect(reset_password_link(user.email)).to match(/http:\/\/localhost:3000\/users\/password\/edit\?reset_password_token=.*/)
end

it "url token matches user's encrypted token" do
fill_in "Email", with: user.email
click_on "Send me reset password instructions"

expect(page).to have_content "If the account exists you will receive an email or SMS with instructions on how to reset your password in a few minutes."

token = reset_password_link(user.email).gsub("http://localhost:3000/users/password/edit?reset_password_token=", "")
encrypted_token = Devise.token_generator.digest(User, :reset_password_token, token)
expect(User.find_by(reset_password_token: encrypted_token)).not_to be_nil
expect(User.find_by(reset_password_token: encrypted_token)).to be_present
end

it "user can update password" do
Expand All @@ -80,6 +91,7 @@
click_on "Log In"

expect(page).to have_text(user.display_name)
expect(page).to have_text("My Cases")
expect(page).not_to have_text("Sign in")
end
end
Expand Down
Loading