101 lines
3.2 KiB
Ruby
101 lines
3.2 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 duplicate username' do
|
|
create(:user_with_sequence_number, name: 'ccx')
|
|
user_duplicated_name = build(:user_with_sequence_number, name: 'ccx')
|
|
user_duplicated_name.valid?
|
|
expect(user_duplicated_name.errors[:name].size).to eq(1)
|
|
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 duplicate email' do
|
|
create(:user_with_sequence_number, email: '1261138729@qq.com')
|
|
user_duplicated_email = build(:user_with_sequence_number, email: '1261138729@qq.com')
|
|
user_duplicated_email.valid?
|
|
expect(user_duplicated_email.errors[:email].size).to eq(1)
|
|
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
|
|
|
|
it 'does not allow duplicated phone' do
|
|
create(:user)
|
|
expect(build(:user)).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
|