Skip to content

Commit

Permalink
Improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
guilleiguaran committed Apr 26, 2023
1 parent 1deb128 commit c516671
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
26 changes: 14 additions & 12 deletions test/one_time_password_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require 'test_helper'

class OtpTest < MiniTest::Test
include ActiveSupport::Testing::TimeHelpers

def setup
@user = User.new
@user.email = '[email protected]'
Expand Down Expand Up @@ -131,7 +133,7 @@ def test_otp_code_with_specific_length

def test_otp_code_without_specific_length
assert_match(/^\d{6}$/, @user.otp_code(2160).to_s)
assert_operator(@user.otp_code(2160).to_s.length, :<=, 6)
assert @user.otp_code(2160).to_s.length <= 6
end

def test_provisioning_uri_with_provided_account
Expand Down Expand Up @@ -162,13 +164,8 @@ def test_provisioning_uri_with_options
&issuer=Example$
}x

assert_match(
account, @user.provisioning_uri('roberto', issuer: 'Example')
)

assert_match(
account, @visitor.provisioning_uri('roberto', issuer: 'Example')
)
assert_match account, @user.provisioning_uri('roberto', issuer: 'Example')
assert_match account, @visitor.provisioning_uri('roberto', issuer: 'Example')

assert_match email, @user.provisioning_uri(nil, issuer: 'Example')
assert_match email, @visitor.provisioning_uri(nil, issuer: 'Example')
Expand Down Expand Up @@ -202,17 +199,22 @@ def test_otp_interval
@interval_user.run_callbacks :create
otp_code = @interval_user.otp_code
2.times { assert_match(otp_code, @interval_user.otp_code) }
sleep 5
refute_match(otp_code, @interval_user.otp_code)

travel 5.seconds do
refute_match(otp_code, @interval_user.otp_code)
end
end

def test_otp_default_interval
@default_interval_user = DefaultIntervalUser.new
@default_interval_user.email = '[email protected]'
@default_interval_user.run_callbacks :create
otp_code = @default_interval_user.otp_code

2.times { assert_match(otp_code, @default_interval_user.otp_code) }
sleep 5
assert_match(otp_code, @default_interval_user.otp_code)

travel 5.seconds do
assert_match(otp_code, @default_interval_user.otp_code)
end
end
end
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
require "minitest/autorun"
require "minitest/unit"
require "active_record"
require "active_support/testing/time_helpers"

begin
require "activemodel-serializers-xml"
Expand Down

0 comments on commit c516671

Please sign in to comment.