From 33c44d34866d42aba43dd9185e741930692deaa7 Mon Sep 17 00:00:00 2001 From: Ryan Lue Date: Mon, 18 Mar 2019 17:53:00 +0800 Subject: [PATCH] Refactoring: Migrate auth_test to RSpec --- spec/models/user_spec.rb | 25 +++++++++++-- test/unit/auth_test.rb | 81 ---------------------------------------- 2 files changed, 22 insertions(+), 84 deletions(-) delete mode 100644 test/unit/auth_test.rb diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index d9648ff17..18b12460a 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -29,9 +29,28 @@ RSpec.describe User, type: :model do let(:password) { Faker::Internet.password } context 'with valid credentials' do - it 'returns the matching user' do - expect(described_class.authenticate(user.login, password)) - .to eq(user) + context 'using #login' do + it 'returns the matching user' do + expect(described_class.authenticate(user.login, password)) + .to eq(user) + 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 diff --git a/test/unit/auth_test.rb b/test/unit/auth_test.rb deleted file mode 100644 index 79df7994e..000000000 --- a/test/unit/auth_test.rb +++ /dev/null @@ -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