tabletop-companion/test/models/user_test.rb

70 lines
1.9 KiB
Ruby

# frozen_string_literal: true
require "test_helper"
class UserTest < ActiveSupport::TestCase
test "full name returns first and last name" do
assert_equal "Trevor Vallender", users(:trevor).full_name
end
test "username must exist" do
assert_must_exist(users(:trevor), "username")
end
test "username must be unique" do
user1 = User.first
user2 = User.second
user1.username = user2.username
assert_not user1.valid?
end
test "email must exist" do
assert_must_exist(users(:trevor), "email")
end
test "email must be unique" do
user1 = User.first
user2 = User.second
user1.email = user2.email
assert_not user1.valid?
end
test "email must be a valid email" do
user = users(:trevor)
user.email = "trevor"
assert_not user.valid?
assert_includes user.errors[:email], "must be a valid email address"
end
test "first name must exist" do
assert_must_exist(users(:trevor), "first_name")
end
test "email verification token is invalid after email verified" do
user = users(:unverified)
token = user.generate_token_for(:email_verification)
assert_equal user, User.find_by_token_for(:email_verification, token)
user.update(verified: true)
assert_nil User.find_by_token_for(:email_verification, token)
end
test "password reset token is invalid after password changed" do
user = users(:trevor)
token = user.generate_token_for(:password_reset)
assert_equal user, User.find_by_token_for(:password_reset, token)
user.update(password: "new_password")
assert_nil User.find_by_token_for(:password_reset, token)
end
test "avatar is automatically deleted when flag set" do
user = users(:trevor)
assert user.avatar.attached?
user.first_name = "Newname"
user.save
assert user.avatar.attached?
user.delete_avatar = true
user.save
assert_not user.avatar.attached?
end
end