summaryrefslogtreecommitdiff
path: root/test/unit
diff options
context:
space:
mode:
authorazul <azul@leap.se>2015-09-30 10:51:21 +0200
committerazul <azul@leap.se>2015-09-30 10:51:21 +0200
commitd45f6c61f6a13be06f1977b857e0cb31e79c5317 (patch)
treed3089b334c2663ea2fd76cf62dea853bdd57b047 /test/unit
parenta894966e425f27c31e7da196658c6ddee3fc3714 (diff)
parentd4f10a8d47572bcab4c44878b952146732d64d2e (diff)
Merge pull request #194 from Alster-Hamburgers/feature/invite_code
Request for feedback on invite code feature
Diffstat (limited to 'test/unit')
-rw-r--r--test/unit/account_test.rb44
-rw-r--r--test/unit/invite_code_test.rb67
-rw-r--r--test/unit/invite_code_validator_test.rb30
-rw-r--r--test/unit/tmp_user_test.rb4
-rw-r--r--test/unit/token_test.rb1
-rw-r--r--test/unit/user_test.rb3
6 files changed, 145 insertions, 4 deletions
diff --git a/test/unit/account_test.rb b/test/unit/account_test.rb
index b2bfe27..6b814b6 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))
+ 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?
assert id = user.identity
@@ -16,18 +21,28 @@ 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
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))
@@ -44,4 +59,25 @@ class AccountTest < ActiveSupport::TestCase
user.account.destroy
end
+ test "Invite code count goes up by 1 when the invite code is entered" do
+ 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
+
+ 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
new file mode 100644
index 0000000..b17d1bc
--- /dev/null
+++ b/test/unit/invite_code_test.rb
@@ -0,0 +1,67 @@
+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_invite_code code1.invite_code
+ 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
+
+
+ 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
+
+ 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
new file mode 100644
index 0000000..ee8f1b3
--- /dev/null
+++ b/test/unit/invite_code_validator_test.rb
@@ -0,0 +1,30 @@
+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
+ 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
+ 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
diff --git a/test/unit/tmp_user_test.rb b/test/unit/tmp_user_test.rb
index 55b117f..9494377 100644
--- a/test/unit/tmp_user_test.rb
+++ b/test/unit/tmp_user_test.rb
@@ -2,6 +2,10 @@ require 'test_helper'
class TmpUserTest < ActiveSupport::TestCase
+ setup do
+ InviteCodeValidator.any_instance.stubs(:validate)
+ end
+
test "test_user saved to tmp_users" do
begin
assert User.ancestors.include?(TemporaryUser)
diff --git a/test/unit/token_test.rb b/test/unit/token_test.rb
index 5468650..51c8d8e 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(:validate)
@user = find_record :user
end
diff --git a/test/unit/user_test.rb b/test/unit/user_test.rb
index c301923..9501d34 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(:validate)
@user = FactoryGirl.build(:user)
end
@@ -70,6 +71,8 @@ class UserTest < ActiveSupport::TestCase
assert_equal key, @user.public_key
end
+
+
#
## Regression tests
#