From 04f1260bf5579bb2f978247d7c56391b68e9c577 Mon Sep 17 00:00:00 2001 From: Josh Puetz Date: Fri, 16 Oct 2020 00:10:56 -0500 Subject: [PATCH] System test (#10854) * System test * Update spec/system/authentication/user_logs_in_with_email_spec.rb Removed commented out line Co-authored-by: Jacob Herrington * Update spec/system/authentication/user_logs_in_with_email_spec.rb Co-authored-by: Jacob Herrington * Update spec/system/authentication/user_logs_in_with_email_spec.rb Co-authored-by: Jacob Herrington Co-authored-by: Jacob Herrington --- .../user_logs_in_with_email_spec.rb | 121 ++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 spec/system/authentication/user_logs_in_with_email_spec.rb diff --git a/spec/system/authentication/user_logs_in_with_email_spec.rb b/spec/system/authentication/user_logs_in_with_email_spec.rb new file mode 100644 index 000000000..3602d5eaa --- /dev/null +++ b/spec/system/authentication/user_logs_in_with_email_spec.rb @@ -0,0 +1,121 @@ +require "rails_helper" + +RSpec.describe "Authenticating with Email" do + let(:sign_in_link) { "Continue" } + let(:sign_up_link) { "Sign up with Email" } + + before do + allow(SiteConfig).to receive(:allow_email_password_registration).and_return(true) + allow(SiteConfig).to receive(:allow_email_password_login).and_return(true) + end + + context "when a user is new" do + let(:user) { build(:user) } + + context "when using valid credentials" do + it "creates a new user", js: true do + expect do + visit sign_up_path(state: "new-user") + click_link(sign_up_link, match: :first) + + fill_in_user(user) + click_button("Sign up", match: :first) + end.to change(User, :count).by(1) + end + + it "logs in and redirects to email confirmation" do + visit sign_up_path(state: "new-user") + click_link(sign_up_link, match: :first) + + fill_in_user(user) + click_button("Sign up", match: :first) + + expect(page).to have_current_path("/confirm-email", ignore_query: true) + end + end + + context "when trying to register with an already existing email" do + it "shows an error" do + email = "user@test.com" + user = create(:user, email: email) + + expect do + visit sign_up_path(state: "new-user") + click_link(sign_up_link, match: :first) + + fill_in_user(user) + click_button("Sign up", match: :first) + end.not_to change(User, :count) + + expect(page).to have_current_path("/users", ignore_query: true) + expect(page).to have_text("Email has already been taken") + end + end + + context "when using invalid credentials" do + it "does not log in" do + visit sign_up_path + fill_in("user_email", with: "foo@bar.com") + fill_in("user_password", with: "password") + click_button("Continue", match: :first) + + expect(page).to have_current_path("/users/sign_in") + expect(page).to have_text("Invalid Email or password.") + end + end + end + + context "when a user already exists" do + let(:password) { Faker::Internet.password(min_length: 8) } + let(:user) { create(:user, password: password, password_confirmation: password) } + + after do + sign_out user + end + + context "when using valid credentials" do + it "logs in" do + visit sign_up_path + log_in_user(user) + + expect(page).to have_current_path("/?signin=true") + end + end + + context "when already signed in" do + it "redirects to the feed" do + sign_in user + visit sign_up_path + + expect(page).to have_current_path("/?signin=true") + end + end + end + + context "when community is in invite only mode" do + before do + allow(SiteConfig).to receive(:invite_only_mode).and_return(true) + end + + it "doesn't present the authentication option" do + visit sign_up_path(state: "new-user") + expect(page).not_to have_text(sign_in_link) + expect(page).to have_text("invite only") + end + end + + def fill_in_user(user) + attach_file("user_profile_image", "spec/fixtures/files/podcast.png") + fill_in("user_name", with: user.name) + fill_in("user_username", with: user.username) + fill_in("user_email", with: user.email) + fill_in("user_password", with: "12345678") + fill_in("user_password_confirmation", with: "12345678") + end + + def log_in_user(user) + fill_in("user_email", with: user.email) + fill_in("user_password", with: user.password) + click_button("Continue", match: :first) + end +end