diff --git a/app/controllers/alerts/registrations_controller.rb b/app/controllers/alerts/registrations_controller.rb index b2da84d0b..7f4888957 100644 --- a/app/controllers/alerts/registrations_controller.rb +++ b/app/controllers/alerts/registrations_controller.rb @@ -18,16 +18,19 @@ def create super do # We're attaching the alert to the unconfirmed user now so that we don't have to pass around the alert # through the registration process which would involve doing unspeakable horrors to devise. - alert = Alert.new( + @alert = Alert.new( user: resource, address: params[:user][:address], radius_meters: params[:user][:radius_meters] ) - # TODO: Check that we're actually allowed to create an alert - # Ensures the address is normalised into a consistent form - alert.geocode_from_address - # TODO: Handle error state - alert.save! + + if resource.persisted? + # TODO: Check that we're actually allowed to create an alert + # Ensures the address is normalised into a consistent form + @alert.geocode_from_address + # TODO: Handle error state + @alert.save! + end end end diff --git a/spec/features/sign_up_for_alerts_spec.rb b/spec/features/sign_up_for_alerts_spec.rb index dc0812d4b..70b4965ff 100644 --- a/spec/features/sign_up_for_alerts_spec.rb +++ b/spec/features/sign_up_for_alerts_spec.rb @@ -260,6 +260,51 @@ expect(page).to have_content("you added a new alert for 24 Bruce Rd, Glenbrook") end + it "when via the homepage not registered in the alternate flow but email already used" do + use_ab_test logged_out_alert_flow_order: "create_alert_sign_in" + + create(:geocoded_application, address: "26 Bruce Rd, Glenbrook NSW 2773", lat: -33.772812, lng: 150.624252, lonlat: RGeo::Geographic.spherical_factory(srid: 4326).point(150.624252, -33.772812)) + user = create(:confirmed_user, email: "example@example.com", password: "mypassword") + sign_in user + visit root_path + sign_out user + + visit root_path + fill_in("Street address", with: "24 Bruce Rd, Glenbrook") + within("form") do + click_on("Search") + end + + expect(page).to have_content("Search results") + expect(page).to have_content("Save this search as an email alert") + click_on("Save", match: :first) + + expect(page).to have_content("You'll receive email alerts when new applications match this search") + click_on("Create an account") + + expect(page).to have_content("Create an account to save this search") + expect(page).to have_content("Applications within 2 km of 24 Bruce Rd, Glenbrook") + + fill_in("Email", with: "example@example.com") + fill_in("Create a password", with: "mypassword") + click_on("Create account and save") + + expect(page).to have_content("You already have an account with that email address") + fill_in("Email", with: "example2@example.com") + fill_in("Create a password", with: "mypassword") + click_on("Create account and save") + + open_email("example2@example.com") + expect(current_email).to have_subject("PlanningAlerts: Confirmation instructions") + expect(current_email.default_part_body.to_s).to include("Thanks for getting onboard!") + + click_email_link_matching(/confirmation/) + + expect(page).to have_content("Your email address has been successfully confirmed") + expect(page).to have_content("you are now logged in") + expect(page).to have_content("you added a new alert for 24 Bruce Rd, Glenbrook") + end + it "when via the homepage but not yet have an account" do create(:geocoded_application, address: "26 Bruce Rd, Glenbrook NSW 2773", lat: -33.772812, lng: 150.624252, lonlat: RGeo::Geographic.spherical_factory(srid: 4326).point(150.624252, -33.772812))