|
| 1 | +RSpec.describe Foobara::Auth::ResetPassword do |
| 2 | + after { Foobara.reset_alls } |
| 3 | + |
| 4 | + before do |
| 5 | + Foobara::Persistence.default_crud_driver = Foobara::Persistence::CrudDrivers::InMemory.new |
| 6 | + end |
| 7 | + |
| 8 | + let(:command) { described_class.new(inputs) } |
| 9 | + let(:outcome) { command.run } |
| 10 | + let(:result) { outcome.result } |
| 11 | + let(:errors) { outcome.errors } |
| 12 | + let(:errors_hash) { outcome.errors_hash } |
| 13 | + |
| 14 | + let(:user) do |
| 15 | + Foobara:: Auth:: CreateUser.run!(username: "Basil", email: "[email protected]", plaintext_password: ) |
| 16 | + end |
| 17 | + let(:plaintext_password) { "somepassword" } |
| 18 | + let(:old_password) { plaintext_password } |
| 19 | + let(:new_password) { "somenewpassword" } |
| 20 | + let(:reset_password_token_secret) do |
| 21 | + Foobara::Auth::CreateResetPasswordToken.run!(user: user.id) |
| 22 | + end |
| 23 | + |
| 24 | + context "when using a reset password token" do |
| 25 | + let(:inputs) do |
| 26 | + { reset_password_token_secret:, new_password: } |
| 27 | + end |
| 28 | + |
| 29 | + it "is successful and we can verify with the new password after resetting" do |
| 30 | + expect( |
| 31 | + Foobara::Auth::VerifyPassword.run!(user: user.id, plaintext_password: old_password) |
| 32 | + ).to be true |
| 33 | + expect( |
| 34 | + Foobara::Auth::VerifyPassword.run!(user: user.id, plaintext_password: new_password) |
| 35 | + ).to be false |
| 36 | + |
| 37 | + expect(outcome).to be_success |
| 38 | + expect(result).to be_a(Foobara::Auth::Types::User) |
| 39 | + |
| 40 | + expect( |
| 41 | + Foobara::Auth::VerifyPassword.run!(user: user.id, plaintext_password: old_password) |
| 42 | + ).to be false |
| 43 | + expect( |
| 44 | + Foobara::Auth::VerifyPassword.run!(user: user.id, plaintext_password: new_password) |
| 45 | + ).to be true |
| 46 | + end |
| 47 | + |
| 48 | + it "marks the original refresh token as used" do |
| 49 | + reset_password_token_secret |
| 50 | + |
| 51 | + reloaded_user = Foobara::Auth::FindUser.run!(id: user.id) |
| 52 | + reset_token_id = reloaded_user.reset_password_token.id |
| 53 | + |
| 54 | + expect { |
| 55 | + expect(outcome).to be_success |
| 56 | + }.to change { |
| 57 | + Foobara::Auth::Types::User.transaction do |
| 58 | + Foobara::Auth::Types::Token.load(reset_token_id).inactive? |
| 59 | + end |
| 60 | + }.from(false).to(true) |
| 61 | + end |
| 62 | + |
| 63 | + context "with an invalid refresh token" do |
| 64 | + let(:inputs) do |
| 65 | + { reset_password_token_secret: invalid_token, new_password: } |
| 66 | + end |
| 67 | + let(:invalid_token) do |
| 68 | + text = reset_password_token_secret.dup |
| 69 | + text[-5] = text[-5] == "x" ? "y" : "x" |
| 70 | + text |
| 71 | + end |
| 72 | + |
| 73 | + it "fails with appropriate error" do |
| 74 | + expect(outcome).to_not be_success |
| 75 | + expect(outcome.errors_hash).to include("runtime.invalid_reset_password_token") |
| 76 | + end |
| 77 | + end |
| 78 | + |
| 79 | + context "when the reset password token has been replaced" do |
| 80 | + it "fails with the expected error" do |
| 81 | + reset_password_token_secret |
| 82 | + Foobara::Auth::CreateResetPasswordToken.run!(user: user.id) |
| 83 | + |
| 84 | + expect(outcome).to_not be_success |
| 85 | + expect(outcome.errors_hash.keys).to include("runtime.user_not_found_for_reset_token") |
| 86 | + end |
| 87 | + end |
| 88 | + end |
| 89 | + |
| 90 | + context "when using the old password to reset" do |
| 91 | + let(:inputs) do |
| 92 | + { user: user.id, old_password:, new_password: } |
| 93 | + end |
| 94 | + |
| 95 | + it "is successful and we can verify with the new password after resetting" do |
| 96 | + expect( |
| 97 | + Foobara::Auth::VerifyPassword.run!(user: user.id, plaintext_password: old_password) |
| 98 | + ).to be true |
| 99 | + expect( |
| 100 | + Foobara::Auth::VerifyPassword.run!(user: user.id, plaintext_password: new_password) |
| 101 | + ).to be false |
| 102 | + |
| 103 | + expect(outcome).to be_success |
| 104 | + expect(result).to be_a(Foobara::Auth::Types::User) |
| 105 | + |
| 106 | + expect( |
| 107 | + Foobara::Auth::VerifyPassword.run!(user: user.id, plaintext_password: old_password) |
| 108 | + ).to be false |
| 109 | + expect( |
| 110 | + Foobara::Auth::VerifyPassword.run!(user: user.id, plaintext_password: new_password) |
| 111 | + ).to be true |
| 112 | + end |
| 113 | + |
| 114 | + context "when the old password is bad" do |
| 115 | + let(:inputs) do |
| 116 | + { user: user.id, old_password: "badpassword", new_password: } |
| 117 | + end |
| 118 | + |
| 119 | + it "gives the expected error" do |
| 120 | + expect(outcome).to_not be_success |
| 121 | + expect(outcome.errors_hash.keys).to include("runtime.incorrect_old_password") |
| 122 | + end |
| 123 | + end |
| 124 | + |
| 125 | + context "when omitting the user" do |
| 126 | + let(:inputs) do |
| 127 | + { old_password:, new_password: } |
| 128 | + end |
| 129 | + |
| 130 | + it "gives the expected error" do |
| 131 | + expect(outcome).to_not be_success |
| 132 | + expect(outcome.errors_hash.keys).to include("runtime.user_not_given") |
| 133 | + end |
| 134 | + end |
| 135 | + end |
| 136 | + |
| 137 | + context "when giving everything" do |
| 138 | + let(:inputs) do |
| 139 | + { user: user.id, old_password:, new_password:, reset_password_token_secret: } |
| 140 | + end |
| 141 | + |
| 142 | + it "gives the expected error" do |
| 143 | + expect(outcome).to_not be_success |
| 144 | + expect(outcome.errors_hash.keys).to include("runtime.both_password_reset_token_and_old_password_given") |
| 145 | + end |
| 146 | + end |
| 147 | + |
| 148 | + context "when only giving the user and new_password" do |
| 149 | + let(:inputs) do |
| 150 | + { user: user.id, new_password: } |
| 151 | + end |
| 152 | + |
| 153 | + it "gives the expected error" do |
| 154 | + expect(outcome).to_not be_success |
| 155 | + expect(outcome.errors_hash.keys).to include("runtime.no_password_reset_token_or_old_password_given") |
| 156 | + end |
| 157 | + end |
| 158 | +end |
0 commit comments