summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--users/app/models/email.rb4
-rw-r--r--users/app/models/identity.rb12
-rw-r--r--users/app/models/local_email.rb4
-rw-r--r--users/app/models/login_format_validation.rb8
-rw-r--r--users/test/unit/identity_test.rb15
5 files changed, 36 insertions, 7 deletions
diff --git a/users/app/models/email.rb b/users/app/models/email.rb
index 1bcff1c..89c31bb 100644
--- a/users/app/models/email.rb
+++ b/users/app/models/email.rb
@@ -19,4 +19,8 @@ class Email < String
self
end
+ def handle
+ self.split('@').first
+ end
+
end
diff --git a/users/app/models/identity.rb b/users/app/models/identity.rb
index 355f67a..91345a0 100644
--- a/users/app/models/identity.rb
+++ b/users/app/models/identity.rb
@@ -1,4 +1,5 @@
class Identity < CouchRest::Model::Base
+ include LoginFormatValidation
use_database :identities
@@ -10,6 +11,7 @@ class Identity < CouchRest::Model::Base
validate :unique_forward
validate :alias_available
+ validate :address_local_email
design do
view :by_user_id
@@ -63,6 +65,11 @@ class Identity < CouchRest::Model::Base
write_attribute('keys', keys.merge(type => value))
end
+ # for LoginFormatValidation
+ def login
+ self.address.handle
+ end
+
protected
def unique_forward
@@ -79,4 +86,9 @@ class Identity < CouchRest::Model::Base
end
end
+ def address_local_email
+ return if address.valid? #this ensures it is LocalEmail
+ self.errors.add(:address, address.errors.messages[:email].first) #assumes only one error
+ end
+
end
diff --git a/users/app/models/local_email.rb b/users/app/models/local_email.rb
index c1f7c11..6303bb6 100644
--- a/users/app/models/local_email.rb
+++ b/users/app/models/local_email.rb
@@ -20,10 +20,6 @@ class LocalEmail < Email
[handle]
end
- def handle
- gsub(/@#{domain}/i, '')
- end
-
def domain
LocalEmail.domain
end
diff --git a/users/app/models/login_format_validation.rb b/users/app/models/login_format_validation.rb
index 1d02bd1..c1fcf70 100644
--- a/users/app/models/login_format_validation.rb
+++ b/users/app/models/login_format_validation.rb
@@ -1,19 +1,21 @@
module LoginFormatValidation
extend ActiveSupport::Concern
+ #TODO: Probably will replace this. Playing with using it for aliases too, but won't want it connected to login field.
+
included do
# Have multiple regular expression validations so we can get specific error messages:
validates :login,
:format => { :with => /\A.{2,}\z/,
- :message => "Login must have at least two characters"}
+ :message => "Must have at least two characters"}
validates :login,
:format => { :with => /\A[a-z\d_\.-]+\z/,
:message => "Only lowercase letters, digits, . - and _ allowed."}
validates :login,
:format => { :with => /\A[a-z].*\z/,
- :message => "Login must begin with a lowercase letter"}
+ :message => "Must begin with a lowercase letter"}
validates :login,
:format => { :with => /\A.*[a-z\d]\z/,
- :message => "Login must end with a letter or digit"}
+ :message => "Must end with a letter or digit"}
end
end
diff --git a/users/test/unit/identity_test.rb b/users/test/unit/identity_test.rb
index fa88315..b3918f1 100644
--- a/users/test/unit/identity_test.rb
+++ b/users/test/unit/identity_test.rb
@@ -70,6 +70,21 @@ class IdentityTest < ActiveSupport::TestCase
id.destroy
end
+ test "fail to end non-local email address as identity address" do
+ id = Identity.for @user, address: 'blah@sdlfksjdfljk.com'
+ assert !id.valid?
+ assert_match /needs to end in/, id.errors[:address].first
+ end
+
+ test "only lowercase alias" do
+ id = Identity.create_for @user, address: alias_name.capitalize
+ assert !id.valid?
+ #hacky way to do this, but okay for now:
+ assert id.errors.messages.flatten(2).include? "Must begin with a lowercase letter"
+ assert id.errors.messages.flatten(2).include? "Only lowercase letters, digits, . - and _ allowed."
+ end
+
+
def alias_name
@alias_name ||= Faker::Internet.user_name
end