summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--app/models/invite_code_validator.rb3
-rw-r--r--app/models/user.rb2
-rw-r--r--test/unit/invite_code_test.rb67
3 files changed, 54 insertions, 18 deletions
diff --git a/app/models/invite_code_validator.rb b/app/models/invite_code_validator.rb
index 73c333a..d03d510 100644
--- a/app/models/invite_code_validator.rb
+++ b/app/models/invite_code_validator.rb
@@ -3,7 +3,7 @@ class InviteCodeValidator < ActiveModel::Validator
user_invite_code = InviteCode.find_by_invite_code user.invite_code
- if not_existent?(user_invite_code.invite_code)
+ if not_existent?(user.invite_code)
add_error_to_user("This is not a valid code", user)
elsif count_greater_than_zero?(user_invite_code.invite_count)
@@ -18,6 +18,7 @@ class InviteCodeValidator < ActiveModel::Validator
private
def not_existent?(code)
InviteCode.find_by_invite_code(code) == nil
+
end
def count_greater_than_zero?(code)
diff --git a/app/models/user.rb b/app/models/user.rb
index a4dd132..c0079d5 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -40,7 +40,7 @@ class User < CouchRest::Model::Base
:mx_with_fallback => true
- validates_with InviteCodeValidator
+ validates_with InviteCodeValidator, on: :create
timestamps!
diff --git a/test/unit/invite_code_test.rb b/test/unit/invite_code_test.rb
index b6044f4..2c9e33f 100644
--- a/test/unit/invite_code_test.rb
+++ b/test/unit/invite_code_test.rb
@@ -20,29 +20,64 @@ class InviteCodeTest < ActiveSupport::TestCase
assert_equal code1.invite_count, 0
end
- # TODO: does the count go up when code gets entered?
- test "Invite code count goes up by 1 when the invite code is entered" do
+ test "Invite code count goes up by 1 when the invite code is entered" do
+ #TODO but onlz if there are no other errors on the form
- validator = InviteCodeValidator.new nil
+ validator = InviteCodeValidator.new nil
- user = FactoryGirl.build :user
- user_code = InviteCode.new
- user_code.save
- user.invite_code = user_code.invite_code
+ user = FactoryGirl.build :user
+ user_code = InviteCode.new
+ user_code.save
+ user.invite_code = user_code.invite_code
+ validator.validate(user)
- validator.validate(user)
+ user_code.reload
+ assert_equal 1, user_code.invite_count
- user_code.reload
- assert_equal 1, user_code.invite_count
+ end
+
+ test "Invite count >0 is not accepted for new account signup" do
+ validator = InviteCodeValidator.new nil
+
+ user_code = InviteCode.new
+ user_code.invite_count = 1
+ user_code.save
+
+ user = FactoryGirl.build :user
+ user.invite_code = user_code.invite_code
+
+ validator.validate(user)
+
+ assert_equal ["This code has already been used"], user.errors[:invite_code]
+
+ end
+
+ test "Invite count 0 is accepted for new account signup" do
+ validator = InviteCodeValidator.new nil
+
+ user_code = InviteCode.create
- end
-#
-#
-# # TODO: count >0 is not accepted for signup
- # test "Invite count >0 is not accepted for new account signup" do
+ user = FactoryGirl.build :user
+ user.invite_code = user_code.invite_code
+
+ validator.validate(user)
+
+ assert_equal [], user.errors[:invite_code]
+ end
+
+ test "There is an error message if the invite code does not exist" do
+ validator = InviteCodeValidator.new nil
+
+ user = FactoryGirl.build :user
+ user.invite_code = "wrongcode"
+
+ validator.validate(user)
+
+ assert_equal ["This is not a valid code"], user.errors[:invite_code]
+
+ end
- # end
end