|
29 | 29 | expect(user.phone_number_authentication.otp_expires_at).to eq(Time.at(0)) |
30 | 30 | expect(user.otp_valid?).to be false |
31 | 31 | end |
| 32 | + |
| 33 | + context "with fixed_otp feature flag enabled" do |
| 34 | + before { Flipper.enable(:fixed_otp) } |
| 35 | + after { Flipper.disable(:fixed_otp) } |
| 36 | + |
| 37 | + it "returns success when logging in with 000000" do |
| 38 | + user = FactoryBot.create(:user, password: "5489") |
| 39 | + result = PhoneNumberAuthentication::Authenticate.call(otp: "000000", |
| 40 | + password: "5489", |
| 41 | + phone_number: user.phone_number) |
| 42 | + expect(result).to be_success |
| 43 | + expect(result.error_message).to be_nil |
| 44 | + end |
| 45 | + |
| 46 | + it "returns success with 000000 even when user has different OTP in database" do |
| 47 | + user = FactoryBot.create(:user, password: "5489") |
| 48 | + # Simulate a user already created locally with some random OTP |
| 49 | + user.phone_number_authentication.update!(otp: "123456", otp_expires_at: 1.hour.from_now) |
| 50 | + |
| 51 | + result = PhoneNumberAuthentication::Authenticate.call(otp: "000000", |
| 52 | + password: "5489", |
| 53 | + phone_number: user.phone_number) |
| 54 | + expect(result).to be_success |
| 55 | + expect(result.error_message).to be_nil |
| 56 | + end |
| 57 | + |
| 58 | + it "returns success with 000000 even when stored OTP is expired" do |
| 59 | + user = FactoryBot.create(:user, password: "5489") |
| 60 | + # Simulate an expired OTP in the database |
| 61 | + user.phone_number_authentication.update!(otp: "123456", otp_expires_at: 1.hour.ago) |
| 62 | + expect(user.otp_valid?).to be false |
| 63 | + |
| 64 | + result = PhoneNumberAuthentication::Authenticate.call(otp: "000000", |
| 65 | + password: "5489", |
| 66 | + phone_number: user.phone_number) |
| 67 | + expect(result).to be_success |
| 68 | + expect(result.error_message).to be_nil |
| 69 | + end |
| 70 | + |
| 71 | + it "generates access token when using 000000" do |
| 72 | + user = FactoryBot.create(:user, password: "5489") |
| 73 | + user.phone_number_authentication.update!(otp: "987654", otp_expires_at: 1.hour.from_now) |
| 74 | + |
| 75 | + expect { |
| 76 | + PhoneNumberAuthentication::Authenticate.call(otp: "000000", |
| 77 | + password: "5489", |
| 78 | + phone_number: user.phone_number) |
| 79 | + }.to change { user.phone_number_authentication.reload.access_token } |
| 80 | + end |
| 81 | + |
| 82 | + it "invalidates OTP after successful login with 000000" do |
| 83 | + user = FactoryBot.create(:user, password: "5489") |
| 84 | + user.phone_number_authentication.update!(otp: "456789", otp_expires_at: 1.hour.from_now) |
| 85 | + |
| 86 | + expect(user.otp_valid?).to be true |
| 87 | + PhoneNumberAuthentication::Authenticate.call(otp: "000000", |
| 88 | + password: "5489", |
| 89 | + phone_number: user.phone_number) |
| 90 | + expect(user.phone_number_authentication.reload.otp_expires_at).to eq(Time.at(0)) |
| 91 | + expect(user.otp_valid?).to be false |
| 92 | + end |
| 93 | + |
| 94 | + it "does NOT work in non-development environments" do |
| 95 | + user = FactoryBot.create(:user, password: "5489") |
| 96 | + user.phone_number_authentication.update!(otp: "123456", otp_expires_at: 1.hour.from_now) |
| 97 | + |
| 98 | + allow(Rails).to receive(:env).and_return(ActiveSupport::StringInquirer.new("production")) |
| 99 | + |
| 100 | + result = PhoneNumberAuthentication::Authenticate.call(otp: "000000", |
| 101 | + password: "5489", |
| 102 | + phone_number: user.phone_number) |
| 103 | + expect(result).to_not be_success |
| 104 | + expect(result.error_message).to eq("Your OTP does not match. Try again?") |
| 105 | + end |
| 106 | + end |
32 | 107 | end |
33 | 108 |
|
34 | 109 | context "fails when" do |
|
0 commit comments