From e6e1187fdc44766aa4336e05aa12d4e74db65d1d Mon Sep 17 00:00:00 2001 From: ankonym Date: Wed, 5 Aug 2015 16:55:39 +0200 Subject: Adding invite code field to signup with validation for hardcoded invite code --- test/unit/user_test.rb | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'test/unit') diff --git a/test/unit/user_test.rb b/test/unit/user_test.rb index c301923..cd290d5 100644 --- a/test/unit/user_test.rb +++ b/test/unit/user_test.rb @@ -70,6 +70,11 @@ class UserTest < ActiveSupport::TestCase assert_equal key, @user.public_key end + test "user should have an invite token" do + user = User.new + assert_nil(user.invite_code) + end + # ## Regression tests # -- cgit v1.2.3 From 9156f1354f404c569e7fd337cff0171f6f43842e Mon Sep 17 00:00:00 2001 From: Aya Jaff Date: Wed, 12 Aug 2015 15:18:34 +0200 Subject: Added an 'invite code' to all the tests for the sign-up form so we have a valid user for the tests again --- test/unit/tmp_user_test.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'test/unit') diff --git a/test/unit/tmp_user_test.rb b/test/unit/tmp_user_test.rb index 55b117f..af2218e 100644 --- a/test/unit/tmp_user_test.rb +++ b/test/unit/tmp_user_test.rb @@ -8,13 +8,13 @@ class TmpUserTest < ActiveSupport::TestCase assert_difference('User.database.info["doc_count"]') do normal_user = User.create!(:login => 'a'+SecureRandom.hex(5).downcase, - :password_verifier => 'ABCDEF0010101', :password_salt => 'ABCDEF') + :password_verifier => 'ABCDEF0010101', :password_salt => 'ABCDEF', :invite_code => 'testcode') refute normal_user.database.to_s.include?('tmp') end assert_difference('User.tmp_database.info["doc_count"]') do tmp_user = User.create!(:login => 'test_user_'+SecureRandom.hex(5).downcase, - :password_verifier => 'ABCDEF0010101', :password_salt => 'ABCDEF') + :password_verifier => 'ABCDEF0010101', :password_salt => 'ABCDEF', :invite_code => 'testcode') assert tmp_user.database.to_s.include?('tmp') end ensure -- cgit v1.2.3 From 8b5665b857edc460ef6105c3ba0f106dd99a25d5 Mon Sep 17 00:00:00 2001 From: ankonym Date: Thu, 13 Aug 2015 17:24:31 +0200 Subject: Fix test based on actual invite code validation --- test/unit/tmp_user_test.rb | 8 ++++++-- test/unit/token_test.rb | 1 + test/unit/user_test.rb | 23 ++++++++++++++++++++--- 3 files changed, 27 insertions(+), 5 deletions(-) (limited to 'test/unit') diff --git a/test/unit/tmp_user_test.rb b/test/unit/tmp_user_test.rb index af2218e..0a9ad68 100644 --- a/test/unit/tmp_user_test.rb +++ b/test/unit/tmp_user_test.rb @@ -2,19 +2,23 @@ require 'test_helper' class TmpUserTest < ActiveSupport::TestCase + setup do + InviteCodeValidator.any_instance.stubs(:not_existent?).returns(false) + end + test "test_user saved to tmp_users" do begin assert User.ancestors.include?(TemporaryUser) assert_difference('User.database.info["doc_count"]') do normal_user = User.create!(:login => 'a'+SecureRandom.hex(5).downcase, - :password_verifier => 'ABCDEF0010101', :password_salt => 'ABCDEF', :invite_code => 'testcode') + :password_verifier => 'ABCDEF0010101', :password_salt => 'ABCDEF') refute normal_user.database.to_s.include?('tmp') end assert_difference('User.tmp_database.info["doc_count"]') do tmp_user = User.create!(:login => 'test_user_'+SecureRandom.hex(5).downcase, - :password_verifier => 'ABCDEF0010101', :password_salt => 'ABCDEF', :invite_code => 'testcode') + :password_verifier => 'ABCDEF0010101', :password_salt => 'ABCDEF') assert tmp_user.database.to_s.include?('tmp') end ensure diff --git a/test/unit/token_test.rb b/test/unit/token_test.rb index 5468650..dcf3fc4 100644 --- a/test/unit/token_test.rb +++ b/test/unit/token_test.rb @@ -4,6 +4,7 @@ class TokenTest < ActiveSupport::TestCase include StubRecordHelper setup do + InviteCodeValidator.any_instance.stubs(:not_existent?).returns(false) @user = find_record :user end diff --git a/test/unit/user_test.rb b/test/unit/user_test.rb index cd290d5..e28bef6 100644 --- a/test/unit/user_test.rb +++ b/test/unit/user_test.rb @@ -4,6 +4,7 @@ class UserTest < ActiveSupport::TestCase include SRP::Util setup do + InviteCodeValidator.any_instance.stubs(:not_existent?).returns(false) @user = FactoryGirl.build(:user) end @@ -70,9 +71,25 @@ class UserTest < ActiveSupport::TestCase assert_equal key, @user.public_key end - test "user should have an invite token" do - user = User.new - assert_nil(user.invite_code) + test "user should not be created with invalid invite code" do + InviteCodeValidator.any_instance.stubs(:not_existent?).returns(true) + invalid_user = FactoryGirl.build(:user) + + assert !invalid_user.valid? + end + + test "user should be created with valid invite code" do + assert @user.valid? + end + + test "trying to create a user with invalid invite code should add error" do + InviteCodeValidator.any_instance.stubs(:not_existent?).returns(true) + invalid_user = FactoryGirl.build(:user) + + invalid_user.valid? + + errors = {invite_code: ["This is not a valid code"]} + assert_equal errors, invalid_user.errors.messages end # -- cgit v1.2.3 From 0543217b433a8f4809f08018c1a11c20119fa85d Mon Sep 17 00:00:00 2001 From: ankonym Date: Fri, 21 Aug 2015 17:49:36 +0200 Subject: assign random invite code when creating new invite codes --- test/unit/invite_code_test.rb | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 test/unit/invite_code_test.rb (limited to 'test/unit') diff --git a/test/unit/invite_code_test.rb b/test/unit/invite_code_test.rb new file mode 100644 index 0000000..2684f8e --- /dev/null +++ b/test/unit/invite_code_test.rb @@ -0,0 +1,21 @@ +require 'test_helper' + +class InviteCodeTest < ActiveSupport::TestCase + + test "it is created with an invite code" do + code = InviteCode.new + assert_not_nil code.invite_code + end + + test "the invite code can be read from couch db correctly" do + code1 = InviteCode.new + code1.save + + code2 = InviteCode.find_by__id code1.id + + assert_equal code1.invite_code, code2.invite_code + + end + + +end \ No newline at end of file -- cgit v1.2.3 From 45c3fadd930a474951bd918a50e1ea2b00af75c7 Mon Sep 17 00:00:00 2001 From: ankonym Date: Tue, 25 Aug 2015 14:11:00 +0200 Subject: Make sure codes can only be used once, fix validations We introduced a count on invite codes to make sure that (at the moment) codes can only be used once. (The code will also allow multi-use codes in the future.) Also, some of our validations weren't validating against the correct data, which is now fixed. --- test/unit/invite_code_test.rb | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) (limited to 'test/unit') diff --git a/test/unit/invite_code_test.rb b/test/unit/invite_code_test.rb index 2684f8e..b6044f4 100644 --- a/test/unit/invite_code_test.rb +++ b/test/unit/invite_code_test.rb @@ -10,12 +10,39 @@ class InviteCodeTest < ActiveSupport::TestCase test "the invite code can be read from couch db correctly" do code1 = InviteCode.new code1.save - code2 = InviteCode.find_by__id code1.id - assert_equal code1.invite_code, code2.invite_code + end + test "the invite code count gets set to 0 upon creation" do + code1 = InviteCode.new + code1.save + 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 + + 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 +# +# +# # TODO: count >0 is not accepted for signup + # test "Invite count >0 is not accepted for new account signup" do + + # end + +end -end \ No newline at end of file -- cgit v1.2.3 From a8f07fb18518fd95fd701e8c0e550a3cd70520b0 Mon Sep 17 00:00:00 2001 From: ankonym Date: Mon, 31 Aug 2015 17:15:27 +0200 Subject: Fixes for the invite code validator Validation should only happen for new records User invite code was nil for invalid invite codes Adding missing tests --- test/unit/invite_code_test.rb | 67 ++++++++++++++++++++++++++++++++----------- 1 file changed, 51 insertions(+), 16 deletions(-) (limited to 'test/unit') 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 -- cgit v1.2.3 From 06ebc254bc4537e81c1336627ba8a54c881a1765 Mon Sep 17 00:00:00 2001 From: ankonym Date: Mon, 31 Aug 2015 17:49:47 +0200 Subject: Separate user and invite code validator tests --- test/unit/invite_code_validator_test.rb | 26 ++++++++++++++++++++++++++ test/unit/user_test.rb | 21 +-------------------- 2 files changed, 27 insertions(+), 20 deletions(-) create mode 100644 test/unit/invite_code_validator_test.rb (limited to 'test/unit') diff --git a/test/unit/invite_code_validator_test.rb b/test/unit/invite_code_validator_test.rb new file mode 100644 index 0000000..62768e3 --- /dev/null +++ b/test/unit/invite_code_validator_test.rb @@ -0,0 +1,26 @@ +require 'test_helper' + +class InviteCodeValidatorTest < ActiveSupport::TestCase + test "user should not be created with invalid invite code" do + invalid_user = FactoryGirl.build(:user) + + assert !invalid_user.valid? + end + + test "user should be created with valid invite code" do + valid_user = FactoryGirl.build(:user) + valid_code = InviteCode.create + valid_user.invite_code = valid_code.invite_code + + assert valid_user.valid? + end + + test "trying to create a user with invalid invite code should add error" do + invalid_user = FactoryGirl.build(:user) + + invalid_user.valid? + + errors = {invite_code: ["This is not a valid code"]} + assert_equal errors, invalid_user.errors.messages + end +end \ No newline at end of file diff --git a/test/unit/user_test.rb b/test/unit/user_test.rb index e28bef6..9501d34 100644 --- a/test/unit/user_test.rb +++ b/test/unit/user_test.rb @@ -4,7 +4,7 @@ class UserTest < ActiveSupport::TestCase include SRP::Util setup do - InviteCodeValidator.any_instance.stubs(:not_existent?).returns(false) + InviteCodeValidator.any_instance.stubs(:validate) @user = FactoryGirl.build(:user) end @@ -71,26 +71,7 @@ class UserTest < ActiveSupport::TestCase assert_equal key, @user.public_key end - test "user should not be created with invalid invite code" do - InviteCodeValidator.any_instance.stubs(:not_existent?).returns(true) - invalid_user = FactoryGirl.build(:user) - assert !invalid_user.valid? - end - - test "user should be created with valid invite code" do - assert @user.valid? - end - - test "trying to create a user with invalid invite code should add error" do - InviteCodeValidator.any_instance.stubs(:not_existent?).returns(true) - invalid_user = FactoryGirl.build(:user) - - invalid_user.valid? - - errors = {invite_code: ["This is not a valid code"]} - assert_equal errors, invalid_user.errors.messages - end # ## Regression tests -- cgit v1.2.3 From c48e921c101d49bf68fa1af489b8012517b1a105 Mon Sep 17 00:00:00 2001 From: ankonym Date: Tue, 1 Sep 2015 10:48:25 +0200 Subject: Fix several test failures by stubbing invite code validation --- test/unit/tmp_user_test.rb | 2 +- test/unit/token_test.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'test/unit') diff --git a/test/unit/tmp_user_test.rb b/test/unit/tmp_user_test.rb index 0a9ad68..9494377 100644 --- a/test/unit/tmp_user_test.rb +++ b/test/unit/tmp_user_test.rb @@ -3,7 +3,7 @@ require 'test_helper' class TmpUserTest < ActiveSupport::TestCase setup do - InviteCodeValidator.any_instance.stubs(:not_existent?).returns(false) + InviteCodeValidator.any_instance.stubs(:validate) end test "test_user saved to tmp_users" do diff --git a/test/unit/token_test.rb b/test/unit/token_test.rb index dcf3fc4..51c8d8e 100644 --- a/test/unit/token_test.rb +++ b/test/unit/token_test.rb @@ -4,7 +4,7 @@ class TokenTest < ActiveSupport::TestCase include StubRecordHelper setup do - InviteCodeValidator.any_instance.stubs(:not_existent?).returns(false) + InviteCodeValidator.any_instance.stubs(:validate) @user = find_record :user end -- cgit v1.2.3 From b3762da8d8a06e164524a20d26293a8d5a9770d8 Mon Sep 17 00:00:00 2001 From: ankonym Date: Wed, 2 Sep 2015 16:03:48 +0200 Subject: Fix three unit tests by passing Factory Girl a valid invite code The tests were failing because of a hardcoded "testcode" string so during test setup we generate a valid code and pass it to Factory Girl --- test/unit/account_test.rb | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'test/unit') diff --git a/test/unit/account_test.rb b/test/unit/account_test.rb index b2bfe27..8c66853 100644 --- a/test/unit/account_test.rb +++ b/test/unit/account_test.rb @@ -2,12 +2,17 @@ require 'test_helper' class AccountTest < ActiveSupport::TestCase + setup do + @testcode = InviteCode.new + @testcode.save! + end + teardown do Identity.destroy_all_disabled end test "create a new account" do - user = Account.create(FactoryGirl.attributes_for(:user)) + user = Account.create(FactoryGirl.attributes_for(:user, :invite_code => @testcode.invite_code)) assert user.valid?, "unexpected errors: #{user.errors.inspect}" assert user.persisted? assert id = user.identity @@ -20,14 +25,14 @@ class AccountTest < ActiveSupport::TestCase # We keep an identity that will block the handle from being reused. assert_difference "Identity.count" do assert_no_difference "User.count" do - user = Account.create(FactoryGirl.attributes_for(:user)) + user = Account.create(FactoryGirl.attributes_for(:user, :invite_code => @testcode.invite_code)) user.account.destroy end end end test "change username and create alias" do - user = Account.create(FactoryGirl.attributes_for(:user)) + user = Account.create(FactoryGirl.attributes_for(:user, :invite_code => @testcode.invite_code)) old_id = user.identity old_email = user.email_address user.account.update(FactoryGirl.attributes_for(:user)) -- cgit v1.2.3 From 2ce3d14cfa77f985b6849dd4431db65e9abd0226 Mon Sep 17 00:00:00 2001 From: Aya Jaff Date: Fri, 4 Sep 2015 14:01:49 +0200 Subject: Fixed the signup bug that wrongly consumes the invite code. --- test/unit/account_test.rb | 20 ++++++++++++++++++++ test/unit/invite_code_test.rb | 16 ---------------- test/unit/invite_code_validator_test.rb | 2 +- 3 files changed, 21 insertions(+), 17 deletions(-) (limited to 'test/unit') 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? -- cgit v1.2.3 From c1cd099089c09b79cb7945be4c3283afed9ab3b3 Mon Sep 17 00:00:00 2001 From: ankonym Date: Thu, 10 Sep 2015 18:09:49 +0200 Subject: Removed the view_by__id from invite code test --- test/unit/invite_code_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test/unit') diff --git a/test/unit/invite_code_test.rb b/test/unit/invite_code_test.rb index 1b6a002..b17d1bc 100644 --- a/test/unit/invite_code_test.rb +++ b/test/unit/invite_code_test.rb @@ -10,7 +10,7 @@ class InviteCodeTest < ActiveSupport::TestCase test "the invite code can be read from couch db correctly" do code1 = InviteCode.new code1.save - code2 = InviteCode.find_by__id code1.id + code2 = InviteCode.find_by_invite_code code1.invite_code assert_equal code1.invite_code, code2.invite_code end -- cgit v1.2.3 From 9adbde13619de8b2c300056b062d12f0961cb710 Mon Sep 17 00:00:00 2001 From: ankonym Date: Mon, 21 Sep 2015 18:34:04 +0200 Subject: Make invite code configurable Through the config param 'invite_required', providers can decide whether users need to provide an invite code upon signup. The default setting is false. --- test/unit/account_test.rb | 27 +++++++++++++++++++-------- test/unit/invite_code_validator_test.rb | 4 ++++ 2 files changed, 23 insertions(+), 8 deletions(-) (limited to 'test/unit') diff --git a/test/unit/account_test.rb b/test/unit/account_test.rb index 0882c43..6b814b6 100644 --- a/test/unit/account_test.rb +++ b/test/unit/account_test.rb @@ -11,7 +11,7 @@ class AccountTest < ActiveSupport::TestCase Identity.destroy_all_disabled end - test "create a new account" do + test "create a new account when invited" do user = Account.create(FactoryGirl.attributes_for(:user, :invite_code => @testcode.invite_code)) assert user.valid?, "unexpected errors: #{user.errors.inspect}" assert user.persisted? @@ -21,6 +21,16 @@ class AccountTest < ActiveSupport::TestCase user.account.destroy end + test "create a new account" do + with_config invite_required: false do + user = Account.create(FactoryGirl.attributes_for(:user)) + assert user.valid?, "unexpected errors: #{user.errors.inspect}" + assert user.persisted? + user.account.destroy + end + end + + test "create and remove a user account" do # We keep an identity that will block the handle from being reused. assert_difference "Identity.count" do @@ -50,13 +60,14 @@ class AccountTest < ActiveSupport::TestCase 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 + with_config invite_required: true 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 end diff --git a/test/unit/invite_code_validator_test.rb b/test/unit/invite_code_validator_test.rb index 75e691d..ee8f1b3 100644 --- a/test/unit/invite_code_validator_test.rb +++ b/test/unit/invite_code_validator_test.rb @@ -2,9 +2,11 @@ require 'test_helper' class InviteCodeValidatorTest < ActiveSupport::TestCase test "user should not be created with invalid invite code" do + with_config invite_required: true do invalid_user = FactoryGirl.build(:user) assert !invalid_user.valid? + end end test "user should be created with valid invite code" do @@ -16,11 +18,13 @@ class InviteCodeValidatorTest < ActiveSupport::TestCase end test "trying to create a user with invalid invite code should add error" do + with_config invite_required: true do invalid_user = FactoryGirl.build(:user, :invite_code => "a non-existent code") invalid_user.valid? errors = {invite_code: ["This is not a valid code"]} assert_equal errors, invalid_user.errors.messages + end end end \ No newline at end of file -- cgit v1.2.3