Packages
A web application in pursuit of meaning in life.
Current section
Files
Jump to
Current section
Files
test/models/user_test.exs
###############################################################################
# The contents of this file are subject to the Common Public Attribution
# License Version 1.0. (the "License"); you may not use this file except in
# compliance with the License. You may obtain a copy of the License at
# https://raw.githubusercontent.com/udia-software/udia/master/LICENSE.
# The License is based on the Mozilla Public License Version 1.1, but
# Sections 14 and 15 have been added to cover use of software over a computer
# network and provide for limited attribution for the Original Developer.
# In addition, Exhibit A has been modified to be consistent with Exhibit B.
#
# Software distributed under the License is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
# the specific language governing rights and limitations under the License.
#
# The Original Code is UDIA.
#
# The Original Developer is the Initial Developer. The Initial Developer of
# the Original Code is Udia Software Incorporated.
#
# All portions of the code written by UDIA are Copyright (c) 2016-2017
# Udia Software Incorporated. All Rights Reserved.
###############################################################################
defmodule Udia.UserTest do
use Udia.ModelCase, async: true
alias Udia.User
@valid_attrs %{username: "eva", password: "secret"}
@invalid_attrs %{}
test "changeset with valid attributes" do
changeset = User.changeset(%User{}, @valid_attrs)
assert changeset.valid?
end
test "changeset with invalid attributes" do
changeset = User.changeset(%User{}, @invalid_attrs)
refute changeset.valid?
end
test "changeset does not accept long usernames" do
attrs = Map.put(@valid_attrs, :username, String.duplicate("a", 30))
assert {:username, "should be at most 20 character(s)"} in errors_on(%User{}, attrs)
end
test "registration_changeset password must be at least 6 chars long" do
attrs = Map.put(@valid_attrs, :password, "12345")
changeset = User.registration_changeset(%User{}, attrs)
assert {:password, {"should be at least %{count} character(s)",
[count: 6, validation: :length, min: 6]}} in changeset.errors
end
test "registration_changeset with valid attributes hashes password" do
attrs = Map.put(@valid_attrs, :password, "123456")
changeset = User.registration_changeset(%User{}, attrs)
%{password: pass, password_hash: pass_hash} = changeset.changes
assert changeset.valid?
assert pass_hash
assert Comeonin.Bcrypt.checkpw(pass, pass_hash)
end
end