Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update_with_otp #147

Open
krtschmr opened this issue Jan 31, 2019 · 1 comment
Open

update_with_otp #147

krtschmr opened this issue Jan 31, 2019 · 1 comment

Comments

@krtschmr
Copy link

krtschmr commented Jan 31, 2019

checking an OTP to perform update method on the model

u = User.first
u.update_with_otp(secure_field: true, otp_attempt: 123456)
# => false
u.errors
# @details={:otp_attempt=>[{:error=>:invalid}]},
# @messages={:otp_attempt=>["is invalid"]}>

now the question is: does this OTP needs to be consumed?
i made it also consuming the OTP

pull request: #148

@krtschmr
Copy link
Author

krtschmr commented Jan 31, 2019

User can only change password if he also can give an OTP

image

following testcase pass green


Class User
  def change_password(params)
    update_with_otp_and_password(params)
  end



  factory :user do
    email { "testuser@mail.com" }
    password { "12345678" }
    otp_secret { "6fmq4ppm2eabwuphlrlskwae" }
  end



  describe "change password" do
    before do
      user.save
    end

    it "can change the password (without OTP)" do
      params = {current_password: "12345678", password: "aaaaaaaa", password_confirmation: "aaaaaaaa"}
      user.change_password(params)
      user.reload
      user.wont_be :valid_password?, "12345678"
      user.must_be :valid_password?, "aaaaaaaa"
    end

    it "can't change the password with wrong OTP" do
      user.update(otp_required_for_login: true)
      params = {current_password: "12345678", password: "aaaaaaaa", password_confirmation: "aaaaaaaa", otp_attempt: "faulty OTP"}
      user.change_password(params)
      user.errors[:otp_attempt].must_equal ["is invalid"]
    end

    it "can't change the password without OTP" do
      user.update(otp_required_for_login: true)
      params = {current_password: "12345678", password: "aaaaaaaa", password_confirmation: "aaaaaaaa"}
      user.change_password(params)
      user.errors[:otp_attempt].must_equal ["can't be blank"]
    end

    it "can change the password with correct OTP" do
      user.must_be :valid_password?, "12345678"
      user.update(otp_required_for_login: true)
      params = {current_password: "12345678", password: "aaaaaaaa", password_confirmation: "aaaaaaaa"}
      user.change_password( params.merge(otp_attempt: user.current_otp) )
      user.reload
      user.wont_be :valid_password?, "12345678"
      user.must_be :valid_password?, "aaaaaaaa"
    end
  end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants