summaryrefslogtreecommitdiff
path: root/test/unit/user_test.rb
diff options
context:
space:
mode:
authorStefan Wintermeyer <stefan.wintermeyer@amooma.de>2012-12-17 12:05:14 +0100
committerStefan Wintermeyer <stefan.wintermeyer@amooma.de>2012-12-17 12:05:14 +0100
commiteaad37485fe59d0306c37cc038dda6d210052910 (patch)
tree072c4b0e33d442528555b82c415f5e7a1712b2b0 /test/unit/user_test.rb
parent3e706c2025ecc5523e81ad649639ef2ff75e7bac (diff)
parentb80bd744ad873f6fc43018bc4bfb90677de167bd (diff)
Merge branch 'develop'
Diffstat (limited to 'test/unit/user_test.rb')
-rw-r--r--test/unit/user_test.rb82
1 files changed, 82 insertions, 0 deletions
diff --git a/test/unit/user_test.rb b/test/unit/user_test.rb
new file mode 100644
index 0000000..bb89e77
--- /dev/null
+++ b/test/unit/user_test.rb
@@ -0,0 +1,82 @@
+require 'test_helper'
+
+class UserTest < ActiveSupport::TestCase
+
+ def test_should_have_a_valid_factory
+ assert Factory.build(:user).valid?
+ end
+
+ def test_should_have_a_unique_email_address
+ user = Factory.create(:user)
+ assert !Factory.build(:user, :email => user.email).valid?
+ assert Factory.build(:user, :email => "different_#{user.email}").valid?
+ end
+
+ def test_can_not_move_to_a_current_tenant_without_a_membership_relation
+ super_tenant = Factory.create(:tenant)
+ good_tenant = Factory.create(:tenant)
+ evil_tenant = Factory.create(:tenant)
+
+ user = Factory.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 = Factory.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 = Factory.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 = Factory.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 = Factory.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