Refactoring: Migrate auth_test to RSpec

This commit is contained in:
Ryan Lue 2019-03-18 17:53:00 +08:00 committed by Thorsten Eckel
parent a2148ecc68
commit 33c44d3486
2 changed files with 22 additions and 84 deletions

View file

@ -29,11 +29,30 @@ RSpec.describe User, type: :model do
let(:password) { Faker::Internet.password } let(:password) { Faker::Internet.password }
context 'with valid credentials' do context 'with valid credentials' do
context 'using #login' do
it 'returns the matching user' do it 'returns the matching user' do
expect(described_class.authenticate(user.login, password)) expect(described_class.authenticate(user.login, password))
.to eq(user) .to eq(user)
end end
it 'is not case-sensitive' do
expect(described_class.authenticate(user.login.upcase, password))
.to eq(user)
end
end
context 'using #email' do
it 'returns the matching user' do
expect(described_class.authenticate(user.email, password))
.to eq(user)
end
it 'is not case-sensitive' do
expect(described_class.authenticate(user.email.upcase, password))
.to eq(user)
end
end
context 'but exceeding failed login limit' do context 'but exceeding failed login limit' do
before { user.update(login_failed: 999) } before { user.update(login_failed: 999) }

View file

@ -1,81 +0,0 @@
require 'test_helper'
class AuthTest < ActiveSupport::TestCase
test 'auth' do
user = User.find_by(email: 'nicole.braun@zammad.org')
user.update!(
login: 'nicole.braun',
firstname: 'Nicole',
lastname: 'Braun',
email: 'nicole.braun@zammad.org',
password: 'some_pass',
active: true,
updated_by_id: 1,
created_by_id: 1
)
tests = [
# test 1
{
username: 'not_existing',
password: 'password',
result: nil,
},
# test 2
{
username: 'nicole.braun@zammad.org',
password: 'some_pass',
result: true,
verify: {
firstname: 'Nicole',
lastname: 'Braun',
email: 'nicole.braun@zammad.org',
}
},
# test 3
{
username: 'nicole.bRaUn@zammad.org',
password: 'some_pass',
result: true,
verify: {
firstname: 'Nicole',
lastname: 'Braun',
email: 'nicole.braun@zammad.org',
}
},
# test 4
{
username: 'nicole.bRaUn',
password: 'some_pass',
result: true,
verify: {
firstname: 'Nicole',
lastname: 'Braun',
email: 'nicole.braun@zammad.org',
}
},
]
tests.each do |test|
user = User.authenticate(test[:username], test[:password])
if test[:result] == true
if !user
assert(false, 'auth failed')
else
test[:verify].each do |key, value|
assert_equal(user[key], value, 'verify')
end
end
elsif test[:result].nil?
assert_nil(user, 'failed or not existing')
else
assert_equal(test[:result], user, 'failed or not existing')
end
end
end
end