Skip to content

Commit

Permalink
Add system specs for login and password reset flow (#68)
Browse files Browse the repository at this point in the history
* Add specs for login and password reset flow

* Configure action mailer for test env
  • Loading branch information
napster235 authored Sep 24, 2023
1 parent b409334 commit c72f35b
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 8 deletions.
3 changes: 3 additions & 0 deletions config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,7 @@

# Annotate rendered view with file names.
# config.action_view.annotate_rendered_view_with_filenames = true

# Configure action mailer for the test environment
config.action_mailer.default_url_options = {host: "localhost", port: 3000}
end
97 changes: 89 additions & 8 deletions spec/system/users_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,98 @@
driven_by(:rack_test)
end

describe "sign up flow" do
let(:user) { FactoryBot.create(:user) }
let(:sign_in_url) { "/users/sign_in" }
let(:visit_login_route) { visit sign_in_url }
let(:user) { FactoryBot.create(:user) }

it "works" do
visit "/users/sign_in"
describe "sign in flow" do
context "when input fields are correctly filled" do
it "logs in the user" do
visit_login_route

fill_in "Email", with: user.email
fill_in "Password", with: user.password
click_on "Log in"
fill_in "Email", with: user.email
fill_in "Password", with: user.password
click_on "Log in"

expect(page.current_path).to eq(root_path)
expect(page.current_path).to eq(root_path)
end
end

context "when email input is empty" do
it "does not login the user" do
visit_login_route

fill_in "Email", with: ""
fill_in "Password", with: user.password
click_on "Log in"

expect(page.current_path).to eq(sign_in_url)
expect(page.status_code).to eq(422)
end
end

context "when password input is empty" do
it "does not login the user" do
visit_login_route

fill_in "Email", with: user.email
fill_in "Password", with: ""
click_on "Log in"

expect(page.current_path).to eq(sign_in_url)
expect(page.status_code).to eq(422)
end
end

context "when email and password inputs are empty" do
it "does not login the user" do
visit_login_route

fill_in "Email", with: ""
fill_in "Password", with: ""
click_on "Log in"

expect(page.current_path).to eq(sign_in_url)
expect(page.status_code).to eq(422)
end
end
end

describe "forgot password flow" do
context "when email is correct" do
it "does send a password reset email" do
visit_login_route

click_link "Forgot your password?"
fill_in "Email", with: user.email

expect { click_on "Send me reset password instructions" }.to change(ActionMailer::Base.deliveries, :count).by(1)
expect(page.current_path).to eq(sign_in_url)
end
end

context "when the email address does not exist in the db" do
it "does not send the password reset email" do
visit_login_route

click_link "Forgot your password?"
fill_in "Email", with: "[email protected]"

expect { click_on "Send me reset password instructions" }.not_to change(ActionMailer::Base.deliveries, :count)
expect(page).to have_content("Email not found")
end
end

context "when the email address input is empty" do
it "does not send the password reset email" do
visit_login_route

click_link "Forgot your password?"
fill_in "Email", with: ""

expect { click_on "Send me reset password instructions" }.not_to change(ActionMailer::Base.deliveries, :count)
expect(page).to have_content("Email can't be blank")
end
end
end
end

0 comments on commit c72f35b

Please sign in to comment.