forked from wmlele/devise-otp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
integration_tests_helper.rb
82 lines (67 loc) · 1.81 KB
/
integration_tests_helper.rb
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
class ActionDispatch::IntegrationTest
include Warden::Test::Helpers
def warden
request.env["warden"]
end
def create_full_user
@user ||= begin
user = User.create!(
email: "user@email.invalid",
password: "12345678",
password_confirmation: "12345678"
)
user
end
end
def create_full_admin
@admin ||= begin
admin = Admin.create!(
email: "admin@email.invalid",
password: "12345678",
password_confirmation: "12345678"
)
admin
end
end
def enable_otp_and_sign_in_with_otp
enable_otp_and_sign_in.tap do |user|
fill_in "token", with: ROTP::TOTP.new(user.otp_auth_secret).at(Time.now)
click_button "Submit Token"
end
end
def enable_otp_and_sign_in
user = create_full_user
user.populate_otp_secrets!
sign_user_in(user)
visit edit_user_otp_token_path
fill_in "confirmation_code", with: ROTP::TOTP.new(user.otp_auth_secret).at(Time.now)
click_button "Continue..."
Capybara.reset_sessions!
sign_user_in(user)
user
end
def otp_challenge_for(user)
fill_in "token", with: ROTP::TOTP.new(user.otp_auth_secret).at(Time.now)
click_button "Submit Token"
end
def disable_otp
visit user_otp_token_path
click_button "Disable Two-Factor Authentication"
end
def reset_otp
visit user_otp_token_path
click_button "Reset Token Secret"
end
def sign_out
logout :user
end
def sign_user_in(user = nil)
user ||= create_full_user
resource_name = user.class.name.underscore
visit send("new_#{resource_name}_session_path")
fill_in "#{resource_name}_email", with: user.email
fill_in "#{resource_name}_password", with: user.password
page.has_content?("Log in") ? click_button("Log in") : click_button("Sign in")
user
end
end