From d4157f50e57e906e941936a9721490fb8c301b8d Mon Sep 17 00:00:00 2001 From: natdeanlewissoftwire Date: Mon, 6 Feb 2023 16:59:02 +0000 Subject: [PATCH] test: add tests for path --- db/schema.rb | 1 - spec/mailers/devise_notify_mailer_spec.rb | 20 ++++++++++++++ .../auth/confirmations_controller_spec.rb | 26 +++++++++++++++++-- 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/db/schema.rb b/db/schema.rb index c428319fc..7c222f80c 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -510,7 +510,6 @@ ActiveRecord::Schema[7.0].define(version: 2023_02_03_174815) do t.integer "old_persons_shared_ownership_value_check" t.integer "staircase_bought_value_check" t.integer "monthly_charges_value_check" - t.integer "saledate_check" t.index ["bulk_upload_id"], name: "index_sales_logs_on_bulk_upload_id" t.index ["created_by_id"], name: "index_sales_logs_on_created_by_id" t.index ["owning_organisation_id"], name: "index_sales_logs_on_owning_organisation_id" diff --git a/spec/mailers/devise_notify_mailer_spec.rb b/spec/mailers/devise_notify_mailer_spec.rb index a086eddaf..7b0896e8b 100644 --- a/spec/mailers/devise_notify_mailer_spec.rb +++ b/spec/mailers/devise_notify_mailer_spec.rb @@ -51,5 +51,25 @@ RSpec.describe DeviseNotifyMailer do end end end + + context "when a user is invited for the first time" do + let(:email) { "test@example.com" } + + it "sends initial confirmation template" do + expect(notify_client).to receive(:send_email).with(hash_including(template_id: User::CONFIRMABLE_TEMPLATE_ID)) + User.create!(name:, organisation:, email:, password:, role:) + end + end + + context "when a user requests a new confirmation link" do + let(:email) { "test@example.com" } + + + it "sends re-confirmation template" do + user = User.create!(name:, organisation:, email:, password:, role:) + expect(notify_client).to receive(:send_email).with(hash_including(template_id: User::RECONFIRMABLE_TEMPLATE_ID)) + user.send_confirmation_instructions + end + end end end diff --git a/spec/requests/auth/confirmations_controller_spec.rb b/spec/requests/auth/confirmations_controller_spec.rb index da39f7ff8..aa7f93d65 100644 --- a/spec/requests/auth/confirmations_controller_spec.rb +++ b/spec/requests/auth/confirmations_controller_spec.rb @@ -5,7 +5,7 @@ RSpec.describe Auth::ConfirmationsController, type: :request do let(:page) { Capybara::Node::Simple.new(response.body) } let(:notify_client) { instance_double(Notifications::Client) } let(:devise_notify_mailer) { DeviseNotifyMailer.new } - let(:user) { FactoryBot.create(:user, :data_provider, sign_in_count: 0, confirmed_at: nil) } + let(:user) { FactoryBot.create(:user, :data_provider, sign_in_count: 0, confirmed_at: nil, initial_confirmation_sent: nil) } before do allow(DeviseNotifyMailer).to receive(:new).and_return(devise_notify_mailer) @@ -39,11 +39,33 @@ RSpec.describe Auth::ConfirmationsController, type: :request do get "/account/confirmation?confirmation_token=#{user.confirmation_token}" end - it "shows the error page" do + it "shows the expired page" do expect(page).to have_content("For security reasons, your join link expired - get another one using the button below (valid for 3 hours).") end end + context "when the token is blank" do + before do + user.send_confirmation_instructions + get "/account/confirmation" + end + + it "shows the invalid page" do + expect(page).to have_content("It looks like you have requested a newer join link than this one. Check your emails and follow the most recent link instead.") + end + end + + context "when the token is invalid" do + before do + user.send_confirmation_instructions + get "/account/confirmation?confirmation_token=SOMETHING_INVALID" + end + + it "shows the invalid page" do + expect(page).to have_content("It looks like you have requested a newer join link than this one. Check your emails and follow the most recent link instead.") + end + end + context "when the user has already been confirmed" do let(:user) { FactoryBot.create(:user, :data_provider, sign_in_count: 0, confirmed_at: Time.zone.now) }