@ -2,13 +2,15 @@ require "rails_helper"
RSpec . describe " Admin Panel " do
RSpec . describe " Admin Panel " do
let! ( :admin ) { FactoryBot . create ( :admin_user ) }
let! ( :admin ) { FactoryBot . create ( :admin_user ) }
let ( :devise_notify_mailer ) { DeviseNotifyMailer . new }
let ( :notify_client ) { instance_double ( Notifications :: Client ) }
let ( :notify_client ) { instance_double ( Notifications :: Client ) }
let ( :mfa_template_id ) { AdminUser :: MFA_SMS_ TEMPLATE_ID }
let ( :mfa_template_id ) { AdminUser :: MFA_TEMPLATE_ID }
let ( :otp ) { " 999111 " }
let ( :otp ) { " 999111 " }
before do
before do
allow ( Sms ) . to receive ( :notify_client ) . and_return ( notify_client )
allow ( DeviseNotifyMailer ) . to receive ( :new ) . and_return ( devise_notify_mailer )
allow ( notify_client ) . to receive ( :send_sms ) . and_return ( true )
allow ( devise_notify_mailer ) . to receive ( :notify_client ) . and_return ( notify_client )
allow ( notify_client ) . to receive ( :send_email ) . and_return ( true )
end
end
it " shows the admin sign in page " do
it " shows the admin sign in page " do
@ -26,8 +28,12 @@ RSpec.describe "Admin Panel" do
end
end
it " authenticates successfully " do
it " authenticates successfully " do
expect ( notify_client ) . to receive ( :send_sms ) . with (
expect ( notify_client ) . to receive ( :send_email ) . with (
hash_including ( phone_number : admin . phone , template_id : mfa_template_id ) ,
{
email_address : admin . email ,
template_id : mfa_template_id ,
personalisation : { otp : } ,
} ,
)
)
click_button ( " Sign in " )
click_button ( " Sign in " )
fill_in ( " code " , with : otp )
fill_in ( " code " , with : otp )
@ -42,7 +48,7 @@ RSpec.describe "Admin Panel" do
admin . update! ( direct_otp_sent_at : 16 . minutes . ago )
admin . update! ( direct_otp_sent_at : 16 . minutes . ago )
fill_in ( " code " , with : otp )
fill_in ( " code " , with : otp )
click_button ( " Submit " )
click_button ( " Submit " )
expect ( page ) . to have_content ( " Check your phon e " )
expect ( page ) . to have_content ( " Check your email " )
expect ( page ) . to have_http_status ( :unprocessable_entity )
expect ( page ) . to have_http_status ( :unprocessable_entity )
expect ( page ) . to have_title ( " Error " )
expect ( page ) . to have_title ( " Error " )
expect ( page ) . to have_selector ( " # error-summary-title " )
expect ( page ) . to have_selector ( " # error-summary-title " )
@ -58,7 +64,7 @@ RSpec.describe "Admin Panel" do
click_button ( " Sign in " )
click_button ( " Sign in " )
fill_in ( " code " , with : otp )
fill_in ( " code " , with : otp )
click_button ( " Submit " )
click_button ( " Submit " )
expect ( page ) . to have_content ( " Check your phon e " )
expect ( page ) . to have_content ( " Check your email " )
expect ( page ) . to have_http_status ( :unprocessable_entity )
expect ( page ) . to have_http_status ( :unprocessable_entity )
expect ( page ) . to have_title ( " Error " )
expect ( page ) . to have_title ( " Error " )
expect ( page ) . to have_selector ( " # error-summary-title " )
expect ( page ) . to have_selector ( " # error-summary-title " )
@ -74,12 +80,12 @@ RSpec.describe "Admin Panel" do
end
end
it " displays the resend view " do
it " displays the resend view " do
click_link ( " Not received a text message ? " )
click_link ( " Not received an email ? " )
expect ( page ) . to have_button ( " Resend security code " )
expect ( page ) . to have_button ( " Resend security code " )
end
end
it " send a new OTP code and redirects back to the 2FA view " do
it " send a new OTP code and redirects back to the 2FA view " do
click_link ( " Not received a text message ? " )
click_link ( " Not received an email ? " )
expect { click_button ( " Resend security code " ) } . to ( change { admin . reload . direct_otp } )
expect { click_button ( " Resend security code " ) } . to ( change { admin . reload . direct_otp } )
expect ( page ) . to have_current_path ( " /admin/two-factor-authentication " )
expect ( page ) . to have_current_path ( " /admin/two-factor-authentication " )
end
end
@ -102,20 +108,15 @@ RSpec.describe "Admin Panel" do
fill_in ( " admin_user[email] " , with : admin . email )
fill_in ( " admin_user[email] " , with : admin . email )
fill_in ( " admin_user[password] " , with : admin . password )
fill_in ( " admin_user[password] " , with : admin . password )
click_button ( " Sign in " )
click_button ( " Sign in " )
expect ( page ) . to have_content ( " Check your phon e " )
expect ( page ) . to have_content ( " Check your email " )
end
end
end
end
context " when the admin has forgotten their password " do
context " when the admin has forgotten their password " do
let! ( :admin_user ) { FactoryBot . create ( :admin_user , last_sign_in_at : Time . zone . now ) }
let! ( :admin_user ) { FactoryBot . create ( :admin_user , last_sign_in_at : Time . zone . now ) }
let ( :notify_client ) { instance_double ( Notifications :: Client ) }
let ( :reset_password_token ) { " MCDH5y6Km-U7CFPgAMVS " }
let ( :reset_password_token ) { " MCDH5y6Km-U7CFPgAMVS " }
let ( :devise_notify_mailer ) { DeviseNotifyMailer . new }
before do
before do
allow ( DeviseNotifyMailer ) . to receive ( :new ) . and_return ( devise_notify_mailer )
allow ( devise_notify_mailer ) . to receive ( :notify_client ) . and_return ( notify_client )
allow ( notify_client ) . to receive ( :send_email ) . and_return ( true )
allow ( Devise . token_generator ) . to receive ( :generate ) . and_return ( reset_password_token )
allow ( Devise . token_generator ) . to receive ( :generate ) . and_return ( reset_password_token )
end
end