summaryrefslogtreecommitdiff
path: root/users
diff options
context:
space:
mode:
authorAzul <azul@leap.se>2013-11-26 11:22:47 +0100
committerAzul <azul@leap.se>2013-11-26 11:41:01 +0100
commit7de12c71ce7eb4eeb6e0795275434ed4a4120c25 (patch)
tree9916fcd11ccf289a9dfcf0f183f7655789464092 /users
parent21cc6313320efa2992a58b7434468678ca6cf3b6 (diff)
ignore attempts to empty public_key, refactor
refactor: prepare validations of the uploaded pgp keys
Diffstat (limited to 'users')
-rw-r--r--users/app/models/account.rb11
-rw-r--r--users/app/models/identity.rb6
-rw-r--r--users/app/models/pgp_key.rb25
-rw-r--r--users/test/integration/api/account_flow_test.rb6
4 files changed, 39 insertions, 9 deletions
diff --git a/users/app/models/account.rb b/users/app/models/account.rb
index 5c943bb..cf998e4 100644
--- a/users/app/models/account.rb
+++ b/users/app/models/account.rb
@@ -27,7 +27,8 @@ class Account
@user.update_attributes attrs.slice(:password_verifier, :password_salt)
end
# TODO: move into identity controller
- update_pgp_key(attrs[:public_key]) if attrs.has_key? :public_key
+ key = update_pgp_key(attrs[:public_key])
+ @user.errors.set :public_key, key.errors.full_messages
@user.save && save_identities
@user.refresh_identity
end
@@ -49,8 +50,12 @@ class Account
end
def update_pgp_key(key)
- @new_identity ||= Identity.for(@user)
- @new_identity.set_key(:pgp, key)
+ PgpKey.new(key).tap do |key|
+ if key.present? && key.valid?
+ @new_identity ||= Identity.for(@user)
+ @new_identity.set_key(:pgp, key)
+ end
+ end
end
def save_identities
diff --git a/users/app/models/identity.rb b/users/app/models/identity.rb
index 97966d0..cbb540e 100644
--- a/users/app/models/identity.rb
+++ b/users/app/models/identity.rb
@@ -94,9 +94,9 @@ class Identity < CouchRest::Model::Base
read_attribute('keys') || HashWithIndifferentAccess.new
end
- def set_key(type, value)
- return if keys[type] == value
- write_attribute('keys', keys.merge(type => value))
+ def set_key(type, key)
+ return if keys[type] == key.to_s
+ write_attribute('keys', keys.merge(type => key.to_s))
end
# for LoginFormatValidation
diff --git a/users/app/models/pgp_key.rb b/users/app/models/pgp_key.rb
new file mode 100644
index 0000000..fddec1e
--- /dev/null
+++ b/users/app/models/pgp_key.rb
@@ -0,0 +1,25 @@
+class PgpKey
+ include ActiveModel::Validations
+
+ # mostly for testing.
+ attr_accessor :key_block
+
+ def initialize(key_block = nil)
+ @key_block = key_block
+ end
+
+ def to_s
+ @key_block
+ end
+
+ def present?
+ @key_block.present?
+ end
+
+ # let's allow comparison with plain key_block strings.
+ def ==(other)
+ self.equal?(other) or
+ self.to_s == other
+ end
+
+end
diff --git a/users/test/integration/api/account_flow_test.rb b/users/test/integration/api/account_flow_test.rb
index e41befa..90f2a97 100644
--- a/users/test/integration/api/account_flow_test.rb
+++ b/users/test/integration/api/account_flow_test.rb
@@ -114,9 +114,9 @@ class AccountFlowTest < RackTest
# should not overwrite public key:
put "http://api.lvh.me:3000/1/users/" + @user.id + '.json', :user => {:blee => :blah}, :format => :json
assert_equal test_public_key, Identity.for(@user).keys[:pgp]
- # should overwrite public key:
- put "http://api.lvh.me:3000/1/users/" + @user.id + '.json', :user => {:public_key => nil}, :format => :json
- assert_nil Identity.for(@user).keys[:pgp]
+ # should not empty public key:
+ put "http://api.lvh.me:3000/1/users/" + @user.id + '.json', :user => {:public_key => ""}, :format => :json
+ assert_equal test_public_key, Identity.for(@user).keys[:pgp]
end
end