summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAzul <azul@leap.se>2014-07-07 10:12:53 +0200
committerAzul <azul@leap.se>2014-07-12 09:14:23 +0200
commitcc1666d9832415058bf0b22bb5912e432261af4f (patch)
tree9dddf4d76fd7a9b1e5b2ab1d64ec6b6e65ccb551
parent0e9c41a286b49b5ce52abcf0e014668d0167bbae (diff)
Identity view cert_fingerprints_by_expiry
Also move complex identity views into js designs. Includes test. Here's how you would query it from outside rails: ``` $ curl 'localhost:5984/identities/_design/Identity/_view/cert_fingerprints_by_expiry?startkey="2014-07-05"' {"total_rows":4,"offset":1,"rows":[ {"id":"6c9091d4f13eaeaa6062c9d0528fd34d","key":"2014-07-05","value":"fingerprint"}, {"id":"6f3aa93828b4f6978d551f2623b9d103","key":"2014-07-05","value":"fingerprint"}, {"id":"b6cafacfa65042679691cd5065fb19e3","key":"2014-07-07","value":"fp"} ]} ``` Note that the expiry will be used as the key. So you should use the current data (or yesterday) as the startkey to get all fingerprints that have not expired yet. The fingerprint itself is in the value. No need to include docs.
-rw-r--r--app/designs/identity/cert_fingerprints_by_expiry.js12
-rw-r--r--app/designs/identity/disabled.js8
-rw-r--r--app/designs/identity/pgp_key_by_email.js8
-rw-r--r--app/models/identity.rb27
-rw-r--r--lib/extensions/couchrest.rb12
-rw-r--r--test/unit/identity_test.rb16
6 files changed, 56 insertions, 27 deletions
diff --git a/app/designs/identity/cert_fingerprints_by_expiry.js b/app/designs/identity/cert_fingerprints_by_expiry.js
new file mode 100644
index 0000000..995219b
--- /dev/null
+++ b/app/designs/identity/cert_fingerprints_by_expiry.js
@@ -0,0 +1,12 @@
+function(doc) {
+ if (doc.type != 'Identity') {
+ return;
+ }
+ if (typeof doc.cert_fingerprints === "object") {
+ for (fp in doc.cert_fingerprints) {
+ if (doc.cert_fingerprints.hasOwnProperty(fp)) {
+ emit(doc.cert_fingerprints[fp], fp);
+ }
+ }
+ }
+}
diff --git a/app/designs/identity/disabled.js b/app/designs/identity/disabled.js
new file mode 100644
index 0000000..5509575
--- /dev/null
+++ b/app/designs/identity/disabled.js
@@ -0,0 +1,8 @@
+function(doc) {
+ if (doc.type != 'Identity') {
+ return;
+ }
+ if (typeof doc.user_id === "undefined") {
+ emit(doc._id, 1);
+ }
+}
diff --git a/app/designs/identity/pgp_key_by_email.js b/app/designs/identity/pgp_key_by_email.js
new file mode 100644
index 0000000..f783908
--- /dev/null
+++ b/app/designs/identity/pgp_key_by_email.js
@@ -0,0 +1,8 @@
+function(doc) {
+ if (doc.type != 'Identity') {
+ return;
+ }
+ if (typeof doc.keys === "object") {
+ emit(doc.address, doc.keys["pgp"]);
+ }
+}
diff --git a/app/models/identity.rb b/app/models/identity.rb
index 1d69437..9dc9c7a 100644
--- a/app/models/identity.rb
+++ b/app/models/identity.rb
@@ -18,32 +18,11 @@ class Identity < CouchRest::Model::Base
validate :destination_email
design do
+ own_path = Pathname.new(File.dirname(__FILE__))
+ load_views(own_path.join('..', 'designs', 'identity'), nil)
view :by_user_id
view :by_address_and_destination
view :by_address
- view :pgp_key_by_email,
- map: <<-EOJS
- function(doc) {
- if (doc.type != 'Identity') {
- return;
- }
- if (typeof doc.keys === "object") {
- emit(doc.address, doc.keys["pgp"]);
- }
- }
- EOJS
- view :disabled,
- map: <<-EOJS
- function(doc) {
- if (doc.type != 'Identity') {
- return;
- }
- if (typeof doc.user_id === "undefined") {
- emit(doc._id, 1);
- }
- }
- EOJS
-
end
def self.address_starts_with(query)
@@ -146,7 +125,7 @@ class Identity < CouchRest::Model::Base
end
def register_cert(cert)
- expiry = cert.expiry.to_data.to_s
+ expiry = cert.expiry.to_date.to_s
write_attribute 'cert_fingerprints',
cert_fingerprints.merge(cert.fingerprint => expiry)
end
diff --git a/lib/extensions/couchrest.rb b/lib/extensions/couchrest.rb
index df83c9f..290cd32 100644
--- a/lib/extensions/couchrest.rb
+++ b/lib/extensions/couchrest.rb
@@ -15,13 +15,19 @@ module CouchRest
end
class DesignMapper
- def load_views(dir)
+ DEFAULT_REDUCE = <<-EOJS
+ function(key, values, rereduce) {
+ return sum(values);
+ }
+ EOJS
+ def load_views(dir, reduce=DEFAULT_REDUCE)
Dir.glob("#{dir}/*.js") do |js|
name = File.basename(js, '.js')
file = File.open(js, 'r')
+ reduce =
view name.to_sym,
- :map => file.read,
- :reduce => "function(key, values, rereduce) { return sum(values); }"
+ map: file.read,
+ reduce: reduce
end
end
end
diff --git a/test/unit/identity_test.rb b/test/unit/identity_test.rb
index 49b2075..933b0ff 100644
--- a/test/unit/identity_test.rb
+++ b/test/unit/identity_test.rb
@@ -136,6 +136,22 @@ class IdentityTest < ActiveSupport::TestCase
assert_equal 0, Identity.disabled.count
end
+ test "store cert fingerprint" do
+ begin
+ id = Identity.for(@user)
+ cert = stub expiry: Time.now, fingerprint: "fp"
+ id.register_cert cert
+ id.save
+ entry = {cert.fingerprint => cert.expiry.to_date.to_s}
+ assert_equal entry, id.cert_fingerprints
+ row = Identity.cert_fingerprints_by_expiry.descending.rows.first
+ assert_equal row['key'], cert.expiry.to_date.to_s
+ assert_equal row['value'], cert.fingerprint
+ ensure
+ id.reload.destroy if id && id.persisted?
+ end
+ end
+
def alias_name
@alias_name ||= Faker::Internet.user_name
end