two_factor_steps.rb 1.9 KB
Newer Older
lislis's avatar
lislis committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
# frozen_string_literal: true

When /^I scan the QR code and fill in a valid TOTP token for "([^"]*)"$/ do |email|
  @me = find_user email
  fill_in "user_code", with: @me.current_otp
end

When /^I fill in a valid TOTP token for "([^"]*)"$/ do |username|
  @me = find_user username
  fill_in "user_otp_attempt", with: @me.current_otp
end

When /^I fill in an invalid TOTP token$/ do
  fill_in "user_otp_attempt", with: "c0ffee"
end

When /^I fill in a valid TOTP token to deactivate for "([^"]*)"$/ do |username|
  @me = find_user username
  fill_in "two_factor_authentication_code", with: @me.current_otp
end

When /^I fill in an invalid TOTP token to deactivate$/ do
  fill_in "two_factor_authentication_code", with: "c0ffee"
end

When /^I fill in a recovery code from "([^"]*)"$/ do |username|
  @me = find_user username
  @codes = @me.generate_otp_backup_codes!
  @me.save!
  fill_in "user_otp_attempt", with: @codes.first
end

When /^I fill in a recovery code to deactivate from "([^"]*)"$/ do |username|
  @me = find_user username
  @codes = @me.generate_otp_backup_codes!
  @me.save!
  fill_in "two_factor_authentication_code", with: @codes.first
end

When /^I confirm activation$/ do
  find(".btn-primary", match: :first).click
end

When /^2fa is activated for "([^"]*)"$/ do |username|
  @me = find_user username
  @me.otp_secret = User.generate_otp_secret(32)
  @me.otp_required_for_login = true
  @me.save!
end

When /^I fill in username "([^"]*)" and password "([^"]*)"$/ do |username, password|
  fill_in "user_username", with: username
  fill_in "user_password", with: password
end

Then /^I should see a list of recovery codes$/ do
  find(".recovery-codes", match: :first)
  find(".recovery-codes li samp", match: :first)
end

When /^I press the recovery code generate button$/ do
  find(".btn-default", match: :first).click
end

def find_user(username)
  User.find_by(username: username) || User.find_by(email: username)
end