From da9608a906d4369afb70e042069e219bc2e2d282 Mon Sep 17 00:00:00 2001 From: ankonym Date: Tue, 6 Oct 2015 13:27:32 +0200 Subject: Integrated feedback on multi-invite codes Removing some superfluous code, mostly, and structuring tests better. --- app/models/invite_code.rb | 3 +- app/models/invite_code_validator.rb | 2 +- test/unit/invite_code_test.rb | 57 --------------------------------- test/unit/invite_code_validator_test.rb | 56 ++++++++++++++++++++++++++++++++ 4 files changed, 58 insertions(+), 60 deletions(-) diff --git a/app/models/invite_code.rb b/app/models/invite_code.rb index 0086804..13c3a39 100644 --- a/app/models/invite_code.rb +++ b/app/models/invite_code.rb @@ -4,14 +4,13 @@ class InviteCode < CouchRest::Model::Base use_database 'invite_codes' property :invite_code, String, :read_only => true property :invite_count, Integer, :default => 0, :accessible => true - property :invite_max_uses, Integer, :default => 1, :accessible => true + property :max_uses, Integer, :default => 1 timestamps! design do view :by_invite_code view :by_invite_count - view :by_invite_max_uses end def initialize(attributes = {}, options = {}) diff --git a/app/models/invite_code_validator.rb b/app/models/invite_code_validator.rb index 9dfe6fa..676e4fa 100644 --- a/app/models/invite_code_validator.rb +++ b/app/models/invite_code_validator.rb @@ -18,7 +18,7 @@ class InviteCodeValidator < ActiveModel::Validator end def has_no_uses_left?(code) - code.invite_count >= code.invite_max_uses + code.invite_count >= code.max_uses end def add_error_to_user(error, user) diff --git a/test/unit/invite_code_test.rb b/test/unit/invite_code_test.rb index adaf397..fd93f2f 100644 --- a/test/unit/invite_code_test.rb +++ b/test/unit/invite_code_test.rb @@ -20,63 +20,6 @@ class InviteCodeTest < ActiveSupport::TestCase assert_equal code1.invite_count, 0 end - test "Invite count >= invite max uses is not accepted for new account signup" do - validator = InviteCodeValidator.new nil - - user_code = InviteCode.new - user_code.invite_count = 1 - user_code.invite_max_uses = 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 < invite max uses is accepted for new account signup" do - validator = InviteCodeValidator.new nil - - user_code = InviteCode.create - user_code.invite_count = 0 - user_code.invite_max_uses = 1 - user_code.save - - user = FactoryGirl.build :user - user.invite_code = user_code.invite_code - - validator.validate(user) - - assert_equal [], user.errors[:invite_code] - end - - test "Invite count 0 is accepted for new account signup" do - validator = InviteCodeValidator.new nil - - user_code = InviteCode.create - - 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 diff --git a/test/unit/invite_code_validator_test.rb b/test/unit/invite_code_validator_test.rb index ee8f1b3..62eeae6 100644 --- a/test/unit/invite_code_validator_test.rb +++ b/test/unit/invite_code_validator_test.rb @@ -27,4 +27,60 @@ class InviteCodeValidatorTest < ActiveSupport::TestCase assert_equal errors, invalid_user.errors.messages end end + + + test "Invite count >= invite max uses 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 < invite max uses is accepted for new account signup" do + validator = InviteCodeValidator.new nil + + user_code = InviteCode.create + user_code.save + + user = FactoryGirl.build :user + user.invite_code = user_code.invite_code + + validator.validate(user) + + assert_equal [], user.errors[:invite_code] + end + + test "Invite count 0 is accepted for new account signup" do + validator = InviteCodeValidator.new nil + + user_code = InviteCode.create + + 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 \ No newline at end of file -- cgit v1.2.3