summaryrefslogtreecommitdiff
path: root/users/test/unit/user_test.rb
blob: 5d2a7ead84ce6d72c6e7c13c5c13c1eeca76445d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
require 'test_helper'

class UserTest < ActiveSupport::TestCase

  include SRP::Util
  setup do
    @attribs = User.valid_attributes_hash
    User.find_by_login(@attribs[:login]).try(:destroy)
    @user = User.new(@attribs)
  end

  test "test set of attributes should be valid" do
    @user.valid?
    assert_equal Hash.new, @user.errors.messages
  end

  test "test require hex for password_verifier" do
    @user.password_verifier = "QWER"
    assert !@user.valid?
  end

  test "test require alphanumerical for login" do
    @user.login = "qw#r"
    assert !@user.valid?
  end

  test "find_by_param gets User by id" do
    @user.save
    assert_equal @user, User.find_by_param(@user.id)
    @user.destroy
  end

  test "to_param gives user id" do
    assert_equal @user.id, @user.to_param
  end

  test "verifier returns number for the hex in password_verifier" do
    assert_equal @user.password_verifier.hex, @user.verifier
  end

  test "salt returns number for the hex in password_salt" do
    assert_equal @user.password_salt.hex, @user.salt
  end

  test "should include SRP" do
    client_rnd = bigrand(32).hex
    srp_session = @user.initialize_auth(client_rnd)
    assert srp_session.is_a? SRP::Session
    assert_equal client_rnd, srp_session.aa
  end

  test 'is user an admin' do
    admin_login = APP_CONFIG['admins'].first
    attribs = User.valid_attributes_hash
    attribs[:login] = admin_login
    admin_user = User.new(attribs)
    assert admin_user.is_admin?
    assert !@user.is_admin?

  end

  test "find user by email" do
    email = "tryto@find.me"
    @user.email = email
    @user.save
    assert_equal @user, User.find_by_email(email)
    assert_equal @user, User.find_by_email_or_alias(email)
    assert_nil User.find_by_email_alias(email)
  end
end