82 lines
2.5 KiB
Ruby
82 lines
2.5 KiB
Ruby
require 'rails_helper'
|
|
|
|
RSpec.describe User, type: :model do
|
|
|
|
it 'has a valid factory' do
|
|
expect(build(:user)).to be_valid
|
|
end
|
|
|
|
describe 'username test' do
|
|
it 'does not allow absence of username' do
|
|
expect(build(:user, name: nil)).not_to be_valid
|
|
end
|
|
|
|
it 'does not allow name length > 30' do
|
|
expect(build(:user_name_length_gt_30)).not_to be_valid
|
|
end
|
|
end
|
|
|
|
describe 'email test' do
|
|
it 'does not allow absence of email' do
|
|
expect(build(:user, email: nil)).not_to be_valid
|
|
end
|
|
|
|
it 'does not allow email length > 50' do
|
|
expect(build(:user_email_length_gt_50)).not_to be_valid
|
|
end
|
|
|
|
it 'email should match /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i' do
|
|
expect(build(:user, email: '123qq.com')).not_to be_valid
|
|
expect(build(:user, email: '@qq.com')).not_to be_valid
|
|
expect(build(:user, email: '123@qq')).not_to be_valid
|
|
expect(build(:user, email: '123@.com')).not_to be_valid
|
|
expect(build(:user, email: '123@qq.')).not_to be_valid
|
|
end
|
|
end
|
|
|
|
describe 'password test' do
|
|
it 'does not allow password length < 6' do
|
|
expect(build(:user_password_length_lt_6)).not_to be_valid
|
|
end
|
|
|
|
it 'password_confirmation should match password when password_confirmation not nil' do
|
|
expect(build(:user, password: '123456', password_confirmation: '654321')).to_not be_valid
|
|
end
|
|
|
|
it 'do not trigger match when password_confirmation nil' do
|
|
expect(build(:user, password: '123456', password_confirmation: nil)).to be_valid
|
|
end
|
|
|
|
it 'does not allow password absence on create' do
|
|
expect(build(:user, password: nil, password_confirmation: nil)).to_not be_valid
|
|
end
|
|
|
|
it 'can authenticate' do
|
|
user = create(:user, password: '123456', password_confirmation: '123456')
|
|
expect(user.authenticate('123456')).to be_instance_of User
|
|
expect(user.authenticate('654312')).to be false
|
|
end
|
|
end
|
|
|
|
describe 'phone test' do
|
|
|
|
it 'does not allow phone absence' do
|
|
expect(build(:user, phone: nil)).to_not be_valid
|
|
end
|
|
|
|
it 'does not allow character other than number' do
|
|
expect(build(:user, phone: '1371234567a')).to_not be_valid
|
|
end
|
|
|
|
it 'does not allow phone longer or shorter than 11' do
|
|
expect(build(:user, phone: '137123456789')).to_not be_valid
|
|
expect(build(:user, phone: '1371234567')).to_not be_valid
|
|
end
|
|
|
|
# 防js注入
|
|
it 'does not allow multiline' do
|
|
expect(build(:user, phone: '13712345\n78')).to_not be_valid
|
|
end
|
|
end
|
|
end
|