Skip to content

Commit

Permalink
check for EligibleEyProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
alkesh committed Aug 6, 2024
1 parent 925de1c commit 0f345b5
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 60 deletions.
2 changes: 1 addition & 1 deletion app/controllers/claims_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def check_page_is_in_sequence

def handle_magic_link
otp = OneTimePassword::Validator.new(params[:code], answers.sent_one_time_password_at)
if otp.valid? # && EligibleEyProvider.eligible_email?(journey_session.answers.email_address) # TODO eligible check once model merged: https://github.com/DFE-Digital/claim-additional-payments-for-teaching/pull/3050
if otp.valid?
journey_session.answers.assign_attributes(email_verified: true)
journey_session.save!
session[:slugs] << page_sequence.next_required_slug
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ class EmailAddressForm < Form
attribute :email_address, :string

def save
journey_session.answers.assign_attributes(
email_address: email_address,
sent_one_time_password_at: Time.now,
email_verified: email_verified
)
journey_session.save!
if EligibleEyProvider.eligible_email?(email_address)
journey_session.answers.assign_attributes(
email_address: email_address,
sent_one_time_password_at: Time.now,
email_verified: email_verified
)
journey_session.save!

ClaimMailer.early_years_payment_provider_email(answers, otp_code).deliver_now
ClaimMailer.early_years_payment_provider_email(answers, otp_code).deliver_now
end
end

private
Expand Down
6 changes: 6 additions & 0 deletions app/models/eligible_ey_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,10 @@ def self.csv
end
end
end

def self.eligible_email?(email_address)
where(primary_key_contact_email_address: email_address).or(
where(secondary_contact_email_address: email_address)
).exists?
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddEmailIndexesToEligibleEyProviders < ActiveRecord::Migration[7.0]
def change
add_index :eligible_ey_providers, :primary_key_contact_email_address, name: "index_eligible_ey_providers_on_primary_contact_email_address"
add_index :eligible_ey_providers, :secondary_contact_email_address
end
end
4 changes: 3 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.0].define(version: 2024_07_31_152713) do
ActiveRecord::Schema[7.0].define(version: 2024_08_06_121023) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_trgm"
enable_extension "pgcrypto"
Expand Down Expand Up @@ -190,6 +190,8 @@
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["local_authority_id"], name: "index_eligible_ey_providers_on_local_authority_id"
t.index ["primary_key_contact_email_address"], name: "index_eligible_ey_providers_on_primary_contact_email_address"
t.index ["secondary_contact_email_address"], name: "index_eligible_ey_providers_on_secondary_contact_email_address"
t.index ["urn"], name: "index_eligible_ey_providers_on_urn"
end

Expand Down
4 changes: 4 additions & 0 deletions spec/features/early_years_payment/provider/happy_path_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
let(:mail) { ActionMailer::Base.deliveries.last }
let(:magic_link) { mail[:personalisation].unparsed_value[:magic_link] }

before do
create(:eligible_ey_provider, primary_key_contact_email_address: "johndoe@example.com", secondary_contact_email_address: "janedoe@example.com")
end

scenario "happy path claim" do
when_early_years_payment_provider_journey_configuration_exists

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,75 +14,79 @@

it { should have_attributes(email_address: email_address) }

describe "#save" do
subject { form.save }
context "when the email address is eligible" do
before { create(:eligible_ey_provider, primary_key_contact_email_address: email_address) }

around do |example|
travel_to DateTime.new(2024, 1, 1, 12, 0, 0) do
example.run
end
end

before do
allow(OneTimePassword::Generator).to receive(:new).and_return(
instance_double(OneTimePassword::Generator, code: "111111")
)
end
describe "#save" do
subject { form.save }

let(:policy) { journey_session.answers.policy }
let(:claim_subject) { I18n.t("#{policy.locale_key}.claim_subject") }
around do |example|
travel_to DateTime.new(2024, 1, 1, 12, 0, 0) do
example.run
end
end

it { should be_truthy }
before do
allow(OneTimePassword::Generator).to receive(:new).and_return(
instance_double(OneTimePassword::Generator, code: "111111")
)
end

it "sets the email address" do
subject
expect(journey_session.reload.answers.email_address).to(
eq(email_address)
)
end
let(:policy) { journey_session.answers.policy }
let(:claim_subject) { I18n.t("#{policy.locale_key}.claim_subject") }

it "sends an email" do
subject
it { should be_truthy }

expect(email_address).to have_received_email(
"e0b78a08-601b-40ba-a97f-61fb00a7c951",
magic_link: "https://www.example.com/early-years-payment-provider/consent?code=111111"
)
end
it "sets the email address" do
subject
expect(journey_session.reload.answers.email_address).to(
eq(email_address)
)
end

it "updates sent_one_time_password_at" do
subject
expect(journey_session.answers.sent_one_time_password_at).to(
eq(DateTime.new(2024, 1, 1, 12, 0, 0))
)
end
it "sends an email" do
subject

it "resets email_verified" do
subject
expect(journey_session.answers.email_verified).to be_nil
end
expect(email_address).to have_received_email(
"e0b78a08-601b-40ba-a97f-61fb00a7c951",
magic_link: "https://www.example.com/early-years-payment-provider/consent?code=111111"
)
end

context "when the email address has been previously verified, and a new one is submitted" do
before do
journey_session.answers.assign_attributes(email_address: "new@example.com", email_verified: true)
journey_session.save!
it "updates sent_one_time_password_at" do
subject
expect(journey_session.answers.sent_one_time_password_at).to(
eq(DateTime.new(2024, 1, 1, 12, 0, 0))
)
end

it "resets email_verified" do
subject
expect(journey_session.answers.email_verified).to be_nil
end
end

context "when the email address submitted has been previously verified, and is the same" do
before do
journey_session.answers.assign_attributes(email_address: email_address, email_verified: true)
journey_session.save!
context "when the email address has been previously verified, and a new one is submitted" do
before do
journey_session.answers.assign_attributes(email_address: "new@example.com", email_verified: true)
journey_session.save!
end

it "resets email_verified" do
subject
expect(journey_session.answers.email_verified).to be_nil
end
end

it "returns email_verified" do
subject
expect(journey_session.answers.email_verified).to be true
context "when the email address submitted has been previously verified, and is the same" do
before do
journey_session.answers.assign_attributes(email_address: email_address, email_verified: true)
journey_session.save!
end

it "returns email_verified" do
subject
expect(journey_session.answers.email_verified).to be true
end
end
end
end
Expand Down
28 changes: 28 additions & 0 deletions spec/models/eligible_ey_provider_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require "rails_helper"

describe EligibleEyProvider do
describe ".eligible_email?" do
subject { described_class.eligible_email?(email) }

let!(:eligible_ey_provider) { create(:eligible_ey_provider) }

context "with a valid primary email address" do
let(:email) { eligible_ey_provider.primary_key_contact_email_address }

it { is_expected.to be true }
end

context "with a valid secondary email address" do
let(:email) { eligible_ey_provider.secondary_contact_email_address }

it { is_expected.to be true }
end

context "with an invalid address" do
let(:email) { "some.other.email@example.com" }
it { is_expected.to be false }
end

context "an empty email address"
end
end

0 comments on commit 0f345b5

Please sign in to comment.