summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--app/models/account.rb5
-rw-r--r--app/models/invite_code_validator.rb4
-rw-r--r--test/unit/account_test.rb20
-rw-r--r--test/unit/invite_code_test.rb16
-rw-r--r--test/unit/invite_code_validator_test.rb2
5 files changed, 25 insertions, 22 deletions
diff --git a/app/models/account.rb b/app/models/account.rb
index a57e3f7..c398d93 100644
--- a/app/models/account.rb
+++ b/app/models/account.rb
@@ -19,8 +19,8 @@ class Account
identity = nil
user = nil
user = User.new(attrs)
-
user.save
+
if !user.tmp? && user.persisted?
identity = user.identity
identity.user_id = user.id
@@ -28,6 +28,9 @@ class Account
identity.errors.each do |attr, msg|
user.errors.add(attr, msg)
end
+ user_invite_code = InviteCode.find_by_invite_code user.invite_code
+ user_invite_code.invite_count += 1
+ user_invite_code.save
end
rescue StandardError => ex
user.errors.add(:base, ex.to_s) if user
diff --git a/app/models/invite_code_validator.rb b/app/models/invite_code_validator.rb
index d03d510..127dc57 100644
--- a/app/models/invite_code_validator.rb
+++ b/app/models/invite_code_validator.rb
@@ -8,10 +8,6 @@ class InviteCodeValidator < ActiveModel::Validator
elsif count_greater_than_zero?(user_invite_code.invite_count)
add_error_to_user("This code has already been used", user)
-
- else
- user_invite_code.invite_count += 1
- user_invite_code.save
end
end
diff --git a/test/unit/account_test.rb b/test/unit/account_test.rb
index 8c66853..0882c43 100644
--- a/test/unit/account_test.rb
+++ b/test/unit/account_test.rb
@@ -49,4 +49,24 @@ class AccountTest < ActiveSupport::TestCase
user.account.destroy
end
+ test "Invite code count goes up by 1 when the invite code is entered" do
+
+ user = Account.create(FactoryGirl.attributes_for(:user, :invite_code => @testcode.invite_code))
+ user_code = InviteCode.find_by_invite_code user.invite_code
+ user_code.save
+ user.save
+ assert user.persisted?
+ assert_equal 1, user_code.invite_count
+
+ end
+
+ test "Invite code stays zero when invite code is not used" do
+ #user = Account.create(FactoryGirl.attributes_for(:user, :invite_code => @testcode.invite_code))
+ invalid_user = FactoryGirl.build(:user, :invite_code => @testcode.invite_code)
+ invalid_user.save
+ user_code = InviteCode.find_by_invite_code invalid_user.invite_code
+ user_code.save
+
+ assert_equal 0, user_code.invite_count
+ end
end
diff --git a/test/unit/invite_code_test.rb b/test/unit/invite_code_test.rb
index 2c9e33f..1b6a002 100644
--- a/test/unit/invite_code_test.rb
+++ b/test/unit/invite_code_test.rb
@@ -20,22 +20,6 @@ class InviteCodeTest < ActiveSupport::TestCase
assert_equal code1.invite_count, 0
end
- 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
-
- user = FactoryGirl.build :user
- user_code = InviteCode.new
- user_code.save
- user.invite_code = user_code.invite_code
-
- validator.validate(user)
-
- 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
diff --git a/test/unit/invite_code_validator_test.rb b/test/unit/invite_code_validator_test.rb
index 62768e3..75e691d 100644
--- a/test/unit/invite_code_validator_test.rb
+++ b/test/unit/invite_code_validator_test.rb
@@ -16,7 +16,7 @@ class InviteCodeValidatorTest < ActiveSupport::TestCase
end
test "trying to create a user with invalid invite code should add error" do
- invalid_user = FactoryGirl.build(:user)
+ invalid_user = FactoryGirl.build(:user, :invite_code => "a non-existent code")
invalid_user.valid?