summaryrefslogtreecommitdiff
path: root/test/unit/user_test.rb
blob: 5fa1e4aa5dce18b45b5e10c50f79e351f3c94139 (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
71
72
73
74
75
76
77
78
79
80
81
82
require 'test_helper'

class UserTest < ActiveSupport::TestCase
  
  def test_should_have_a_valid_factory
    assert FactoryGirl.build(:user).valid?
  end
  
  def test_should_have_a_unique_email_address
    user = FactoryGirl.create(:user)
    assert !FactoryGirl.build(:user, :email => user.email).valid?
    assert FactoryGirl.build(:user, :email => "different_#{user.email}").valid?
  end
  
  def test_can_not_move_to_a_current_tenant_without_a_membership_relation
    super_tenant = FactoryGirl.create(:tenant)
    good_tenant = FactoryGirl.create(:tenant)
    evil_tenant = FactoryGirl.create(:tenant)
    
    user = FactoryGirl.create(:user)
    super_tenant.tenant_memberships.create(:user_id => user.id)
    good_tenant.tenant_memberships.create(:user_id => user.id)
    
    assert user.update_attributes(:current_tenant_id => super_tenant.id)
    assert !user.update_attributes(:current_tenant_id => evil_tenant.id)
    assert user.update_attributes(:current_tenant_id => good_tenant.id)
  end
  
  test "should be possible to modify the user without changing the PIN" do
    user = FactoryGirl.create(:user)
    pin_salt = user.pin_salt
    pin_hash = user.pin_hash
    user.middle_name = "#{user.middle_name} Foo"
    assert user.save, "Should be possible to save the user."
    user = User.where(:id => user.id).first
    assert user
    assert_equal pin_salt, user.pin_salt, "PIN salt should not change."
    assert_equal pin_hash, user.pin_hash, "PIN hash should not change."
  end
  
  test "should be possible to change the PIN" do
    user = FactoryGirl.create(:user)
    pin_salt = user.pin_salt
    pin_hash = user.pin_hash
    new_pin = '453267'
    user.new_pin              = new_pin
    user.new_pin_confirmation = new_pin
    assert user.save, "Should be possible to save the user."
    user = User.where(:id => user.id).first
    assert_not_equal "#{pin_salt}#{pin_hash}", "#{user.pin_salt}#{user.pin_hash}",
      "PIN salt/hash should have changed."
  end
  
  test "should not be possible to change the PIN if the confirmation does not match" do
    user = FactoryGirl.create(:user)
    pin_salt = user.pin_salt
    pin_hash = user.pin_hash
    user.new_pin              = '123001'
    user.new_pin_confirmation = '123002'
    assert ! user.save, "Should not be possible to save the user."
    assert ! user.valid?, "Should not be valid."
    assert user.errors && user.errors.messages
    assert (
      (user.errors.messages[:new_pin] && user.errors.messages[:new_pin].length > 0) ||
      (user.errors.messages[:new_pin_confirmation] && user.errors.messages[:new_pin_confirmation].length > 0)
    ), "There should be an error message because new_pin != new_pin_confirmation."
  end
  
  test "PIN must be numeric" do
    user = FactoryGirl.create(:user)
    new_pin = 'xxxx'
    user.new_pin              = new_pin
    user.new_pin_confirmation = new_pin
    assert ! user.save, "Should not be possible to save the user."
    assert ! user.valid?, "Should not be valid."
    assert (
      (user.errors.messages[:new_pin] && user.errors.messages[:new_pin].length > 0) ||
      (user.errors.messages[:new_pin_confirmation] && user.errors.messages[:new_pin_confirmation].length > 0)
    ), "There should be an error message because PIN isn't numeric."
  end
  
end