summaryrefslogtreecommitdiff
path: root/users/test/unit
diff options
context:
space:
mode:
Diffstat (limited to 'users/test/unit')
-rw-r--r--users/test/unit/email_aliases_test.rb66
-rw-r--r--users/test/unit/email_test.rb19
-rw-r--r--users/test/unit/identity_test.rb86
-rw-r--r--users/test/unit/local_email_test.rb34
-rw-r--r--users/test/unit/user_test.rb23
5 files changed, 154 insertions, 74 deletions
diff --git a/users/test/unit/email_aliases_test.rb b/users/test/unit/email_aliases_test.rb
deleted file mode 100644
index 86d14aa..0000000
--- a/users/test/unit/email_aliases_test.rb
+++ /dev/null
@@ -1,66 +0,0 @@
-require 'test_helper'
-
-class EmailAliasTest < ActiveSupport::TestCase
-
- setup do
- @user = FactoryGirl.build :user
- @alias = "valid_alias"
- # make sure no existing records are in the way...
- User.find_by_login_or_alias(@alias).try(:destroy)
- end
-
- test "no email aliases set in the beginning" do
- assert_equal [], @user.email_aliases
- end
-
- test "adding email alias through params" do
- @user.attributes = {:email_aliases_attributes => {"0" => {:email => @alias}}}
- assert @user.changed?
- assert @user.save
- assert_equal @alias, @user.email_aliases.first.username
- end
-
- test "adding email alias directly" do
- @user.email_aliases.build :email => @alias
- assert @user.save
- assert_equal @alias, @user.email_aliases.first.username
- end
-
- test "duplicated email aliases are invalid" do
- @user.email_aliases.build :email => @alias
- @user.save
- assert_invalid_alias @alias
- end
-
- test "email alias needs to be different from other peoples login" do
- other_user = FactoryGirl.create :user, :login => @alias
- assert_invalid_alias @alias
- other_user.destroy
- end
-
- test "email needs to be different from other peoples email aliases" do
- other_user = FactoryGirl.create :user, :email_aliases_attributes => {'1' => @alias}
- assert_invalid_alias @alias
- other_user.destroy
- end
-
- test "login is invalid as email alias" do
- @user.login = @alias
- assert_invalid_alias @alias
- end
-
- test "find user by email alias" do
- @user.email_aliases.build :email => @alias
- assert @user.save
- assert_equal @user, User.find_by_login_or_alias(@alias)
- assert_equal @user, User.find_by_alias(@alias)
- assert_nil User.find_by_login(@alias)
- end
-
- def assert_invalid_alias(string)
- email_alias = @user.email_aliases.build :email => string
- assert !email_alias.valid?
- assert !@user.valid?
- end
-
-end
diff --git a/users/test/unit/email_test.rb b/users/test/unit/email_test.rb
new file mode 100644
index 0000000..7cfbc84
--- /dev/null
+++ b/users/test/unit/email_test.rb
@@ -0,0 +1,19 @@
+require 'test_helper'
+
+class EmailTest < ActiveSupport::TestCase
+
+ test "valid format" do
+ email = Email.new(email_string)
+ assert email.valid?
+ end
+
+ test "validates format" do
+ email = Email.new("email")
+ assert !email.valid?
+ assert_equal ["needs to be a valid email address"], email.errors[:email]
+ end
+
+ def email_string
+ @email_string ||= Faker::Internet.email
+ end
+end
diff --git a/users/test/unit/identity_test.rb b/users/test/unit/identity_test.rb
new file mode 100644
index 0000000..bf24f02
--- /dev/null
+++ b/users/test/unit/identity_test.rb
@@ -0,0 +1,86 @@
+require 'test_helper'
+
+class IdentityTest < ActiveSupport::TestCase
+
+ setup do
+ @user = FactoryGirl.create(:user)
+ end
+
+ teardown do
+ @user.destroy
+ end
+
+ test "initial identity for a user" do
+ id = Identity.for(@user)
+ assert_equal @user.email_address, id.address
+ assert_equal @user.email_address, id.destination
+ assert_equal @user, id.user
+ end
+
+ test "add alias" do
+ id = Identity.for @user, address: alias_name
+ assert_equal LocalEmail.new(alias_name), id.address
+ assert_equal @user.email_address, id.destination
+ assert_equal @user, id.user
+ end
+
+ test "add forward" do
+ id = Identity.for @user, destination: forward_address
+ assert_equal @user.email_address, id.address
+ assert_equal Email.new(forward_address), id.destination
+ assert_equal @user, id.user
+ end
+
+ test "forward alias" do
+ id = Identity.for @user, address: alias_name, destination: forward_address
+ assert_equal LocalEmail.new(alias_name), id.address
+ assert_equal Email.new(forward_address), id.destination
+ assert_equal @user, id.user
+ id.save
+ end
+
+ test "prevents duplicates" do
+ id = Identity.create_for @user, address: alias_name, destination: forward_address
+ dup = Identity.build_for @user, address: alias_name, destination: forward_address
+ assert !dup.valid?
+ assert_equal ["This alias already exists"], dup.errors[:base]
+ end
+
+ test "validates availability" do
+ other_user = FactoryGirl.create(:user)
+ id = Identity.create_for @user, address: alias_name, destination: forward_address
+ taken = Identity.build_for other_user, address: alias_name
+ assert !taken.valid?
+ assert_equal ["This email has already been taken"], taken.errors[:base]
+ other_user.destroy
+ end
+
+ test "setting and getting pgp key" do
+ id = Identity.for(@user)
+ id.set_key(:pgp, pgp_key_string)
+ assert_equal pgp_key_string, id.keys[:pgp]
+ end
+
+ test "querying pgp key via couch" do
+ id = Identity.for(@user)
+ id.set_key(:pgp, pgp_key_string)
+ id.save
+ view = Identity.pgp_key_by_email.key(id.address)
+ assert_equal 1, view.rows.count
+ assert result = view.rows.first
+ assert_equal id.address, result["key"]
+ assert_equal id.keys[:pgp], result["value"]
+ end
+
+ def alias_name
+ @alias_name ||= Faker::Internet.user_name
+ end
+
+ def forward_address
+ @forward_address ||= Faker::Internet.email
+ end
+
+ def pgp_key_string
+ @pgp_key ||= "DUMMY PGP KEY ... "+SecureRandom.base64(4096)
+ end
+end
diff --git a/users/test/unit/local_email_test.rb b/users/test/unit/local_email_test.rb
new file mode 100644
index 0000000..b25f46f
--- /dev/null
+++ b/users/test/unit/local_email_test.rb
@@ -0,0 +1,34 @@
+require 'test_helper'
+
+class LocalEmailTest < ActiveSupport::TestCase
+
+ test "appends domain" do
+ local = LocalEmail.new(handle)
+ assert_equal LocalEmail.new(email), local
+ assert local.valid?
+ end
+
+ test "returns handle" do
+ local = LocalEmail.new(email)
+ assert_equal handle, local.handle
+ end
+
+ test "prints full email" do
+ local = LocalEmail.new(handle)
+ assert_equal email, "#{local}"
+ end
+
+ test "validates domain" do
+ local = LocalEmail.new(Faker::Internet.email)
+ assert !local.valid?
+ assert_equal ["needs to end in @#{LocalEmail.domain}"], local.errors[:email]
+ end
+
+ def handle
+ @handle ||= Faker::Internet.user_name
+ end
+
+ def email
+ handle + "@" + APP_CONFIG[:domain]
+ end
+end
diff --git a/users/test/unit/user_test.rb b/users/test/unit/user_test.rb
index c8c837b..89ee749 100644
--- a/users/test/unit/user_test.rb
+++ b/users/test/unit/user_test.rb
@@ -56,23 +56,30 @@ class UserTest < ActiveSupport::TestCase
other_user.destroy
end
- test "login needs to be different from other peoples email aliases" do
+ test "login needs to be unique amongst aliases" do
other_user = FactoryGirl.create :user
- other_user.email_aliases.build :email => @user.login
- other_user.save
+ Identity.create_for other_user, address: @user.login
assert !@user.valid?
other_user.destroy
end
+ test "deprecated public key api still works" do
+ key = SecureRandom.base64(4096)
+ @user.public_key = key
+ assert_equal key, @user.public_key
+ end
+
test "pgp key view" do
- @user.public_key = SecureRandom.base64(4096)
- @user.save
+ key = SecureRandom.base64(4096)
+ identity = Identity.create_for @user
+ identity.set_key('pgp', key)
+ identity.save
- view = User.pgp_key_by_handle.key(@user.login)
+ view = Identity.pgp_key_by_email.key(@user.email_address)
assert_equal 1, view.rows.count
assert result = view.rows.first
- assert_equal @user.login, result["key"]
- assert_equal @user.public_key, result["value"]
+ assert_equal @user.email_address, result["key"]
+ assert_equal key, result["value"]
end
end