Permalink
Browse files

ClientKeys.getById, deleteById. Users.create, getByEmail, getByUniqueId

  • Loading branch information...
1 parent 36808dc commit 95d2328abf3cfbcab7fcc339eac1a142643e30da @robey committed Apr 12, 2013
Showing with 114 additions and 53 deletions.
  1. +0 −51 src/fauna.coffee
  2. +25 −0 src/fauna/fauna_client.coffee
  3. +20 −0 test/test_client_keys.coffee
  4. +15 −0 test/test_fauna_client.coffee
  5. +52 −1 test/test_users.coffee
  6. +2 −1 test/test_util.coffee
View
@@ -29,41 +29,13 @@ dump = (x) -> util.inspect(x, false, null, true)
class Fauna
- setKey: (key) ->
- @debug "Auth as #{key}"
- @username = key
- @password = ""
- # tell fauna to create classes & event sets for us.
- # return: { class, event_sets[] }[]
- setupSchemas: ->
- Q.all(
- @schema.foreachClass (name, metadata) =>
- @createClass(name).then (klass) =>
- Q.all(for setName in metadata.eventSets then @createEventSet(name, setName)).then (sets) =>
- { "class": klass, "event_sets": sets }
- )
- # ----- publisher keys
-
- deletePublisherKeys: (refs) ->
- if refs.length == 0 then return
- ref = refs.shift()
- @deletePublisherKey(ref).then (_) => @deletePublisherKeys(refs)
-
- deleteAllPublisherKeys: ->
- @getPublisherKeys().then (keys) =>
- @deletePublisherKeys keys.map((key) -> key.ref)
-
# ----- client keys
- # auth: publisher email
- createClientKey: ->
- @post("keys/client", {}).then (data) => data.resource
-
getOrCreateClientKey: ->
@getClientKeys().then (keys) =>
if keys.length > 0 then return keys[0].key
@@ -85,34 +57,14 @@ class Fauna
## ----- classes
- # auth: publisher
- # returns: new class object
- createClass: (name, data) ->
- @put("classes/#{name}/config", data: data).then (data) =>
- @debug "Created class #{data.resource.ref}"
- data.resource
-
# auth: token or publisher
listInstancesOfClass: (className, params) ->
@getEventSet("classes/#{className}", params)
## ----- event sets
- # auth: publisher
- createEventSet: (className, name, config) ->
- @put("classes/#{className}/sets/#{name}/config", data: config).then (data) =>
- @debug "Created event set #{data.resource.ref}"
- data.resource
- # auth: token or publisher
- # returns: { before, after, items }
- getEventSet: (setName, params={}) ->
- list = []
- if params.count? then list.push "count=#{params.count}"
- if params.before? then list.push "before=#{params.before}"
- if params.after? then list.push "after=#{params.after}"
- @get("#{setName}?#{list.join("&")}").then (data) => @collapseEvents(data)
# auth: token or publisher
# returns: list containing the new object
@@ -128,9 +80,6 @@ class Fauna
## ----- users
- # auth: publisher
- listUsers: ->
- @listEventSet("users")
# auth: publisher
getUserByEmail: (email) ->
@@ -38,6 +38,16 @@ requireTokenOrPublisher = (f) ->
@setAuth @publisherKey, ""
f.bind(@)(x...)
+requireClientOrPublisher = (f) ->
+ (x...) ->
+ if not (@publisherKey? or @clientKey?)
+ return Q.reject(new FaunaError(error: "Requires authentication as client or publisher"))
+ if @clientKey?
+ @setAuth @clientKey, ""
+ else
+ @setAuth @publisherKey, ""
+ f.bind(@)(x...)
+
assertNamespace = (namespace) ->
(f) ->
(id, x...) ->
@@ -114,6 +124,12 @@ class FaunaClient
###
@publisherKey = key
+ setClientKey: (key) ->
+ ###
+ Set the client key to use for requests that require one.
+ ###
+ @clientKey = key
+
setUserToken: (key) ->
###
Set the user login token to use for requests. (This is tantamount to
@@ -169,6 +185,8 @@ class FaunaClient
class _ClientKeys
get: requireOwner asEventArray -> @rest("get", "keys/client")
create: requireOwner asObject -> @rest("post", "keys/client")
+ getById: requireOwner asObject assertNamespace("keys/client") (id) -> @rest("get", "#{id}")
+ deleteById: requireOwner assertNamespace("keys/client") (id) -> @rest("delete", "#{id}")
class _EventSets
create: requirePublisher asObject (className, setName, data) -> @rest("put", "#{className}/sets/#{setName}/config", data)
@@ -187,13 +205,20 @@ class FaunaClient
getStats: requirePublisher asObject (name) -> @rest("get", "#{name}/stats")
class _Users
+ create: requireClientOrPublisher asObject (user) ->
+ data = @schema.deflate(user)
+ for key in [ "email", "unique_id", "password" ] then if user[key]? then data[key] = user[key]
+ @rest("post", "users", data)
+ getByEmail: requirePublisher asObject (email) -> @rest("get", "users/email/#{escape(email)}")
+ getByUniqueId: requirePublisher asObject (uniqueId) -> @rest("get", "users/unique_id/#{uniqueId}")
list: requirePublisher (params) -> @eventSets.get("users", params)
# wrapper to allow easy testing of the decorators
class _Testing
requireOwner: requireOwner (x) -> Q(x)
requirePublisher: requirePublisher (x) -> Q(x)
requireTokenOrPublisher: requireTokenOrPublisher (x) -> Q(x)
+ requireClientOrPublisher: requireClientOrPublisher (x) -> Q(x)
# ----- internals
@@ -31,6 +31,26 @@ describe "FaunaClient.clientKeys", ->
requests[0].url.should.match(/keys\/client/)
resp.key.should.eql "AQEAayWp97AAAQBrJamcsAABR0HPBQQdosjqwCGI7q1cMg"
+ it "getById", futureTest ->
+ f = new fauna.FaunaClient()
+ f.setOwnerAuth("u", "p")
+ r = -> f.clientKeys.getById("keys/client/30159234443247617")
+ withSuccessfulRequest(JSON.parse(data2), r).then ([ resp, requests ]) ->
+ requests.length.should.eql(1)
+ requests[0].method.should.eql("GET")
+ requests[0].url.should.match(/keys\/client\/30159234443247617/)
+ resp.key.should.eql "AQEAayWp97AAAQBrJamcsAABR0HPBQQdosjqwCGI7q1cMg"
+
+ it "deleteById", futureTest ->
+ f = new fauna.FaunaClient()
+ f.setOwnerAuth("u", "p")
+ r = -> f.clientKeys.deleteById("keys/client/30159234443247617")
+ handler = (options) -> [ null, 204, null ]
+ fauna.Rest.withRequestHandler(handler, r).then ([ resp, requests ]) ->
+ requests.length.should.eql(1)
+ requests[0].method.should.eql("DELETE")
+ requests[0].url.should.match(/keys\/client\/30159234443247617/)
+
data1 = """
{
@@ -99,6 +99,21 @@ describe "FaunaClient", ->
.then (x) ->
x.should.eql("hello")
+ it "client or publisher", futureTest ->
+ f = new fauna.FaunaClient()
+ f._testing.requireClientOrPublisher("hello").fail (error) ->
+ error.message.should.eql("Requires authentication as client or publisher")
+ .then ->
+ f.setPublisherKey("qqq")
+ f._testing.requireClientOrPublisher("hello")
+ .then (x) ->
+ x.should.eql("hello")
+ f.setPublisherKey(null)
+ f.setClientKey("xxx")
+ f._testing.requireClientOrPublisher("hello")
+ .then (x) ->
+ x.should.eql("hello")
+
it "uses owner auth", futureTest ->
f = new fauna.FaunaClient()
f.setOwnerAuth("u", "p")
View
@@ -8,11 +8,48 @@ withSuccessfulRequest = test_util.withSuccessfulRequest
futureTest = test_util.futureTest
describe "FaunaClient.users", ->
+ it "create", futureTest ->
+ class User extends fauna.Class
+ @native()
+ @field "zipcode"
+ f = new fauna.FaunaClient()
+ f.addPrototypes User
+ f.setClientKey("c")
+ user = new User(email: "robey@example.com", password: "wut", zipcode: 37604)
+ r = -> f.users.create(user)
+ withSuccessfulRequest(data2, r).then ([ resp, requests ]) ->
+ requests.length.should.eql(1)
+ requests[0].method.should.eql "POST"
+ requests[0].url.should.match(/\/users/)
+ requests[0].body.should.match(/"email":"robey@example.com"/)
+ requests[0].body.should.match(/"zipcode":37604/)
+ resp._fauna.id.should.eql("users/30159234491482113")
+
+ it "getByEmail", futureTest ->
+ f = new fauna.FaunaClient()
+ f.setPublisherKey("c")
+ r = -> f.users.getByEmail("foo@example.com")
+ withSuccessfulRequest(data2, r).then ([ resp, requests ]) ->
+ requests.length.should.eql(1)
+ requests[0].method.should.eql "GET"
+ requests[0].url.should.match(/\/users\/email\/foo@example.com/)
+ resp._fauna.id.should.eql("users/30159234491482113")
+
+ it "getByUniqueId", futureTest ->
+ f = new fauna.FaunaClient()
+ f.setPublisherKey("c")
+ r = -> f.users.getByUniqueId("taran186")
+ withSuccessfulRequest(data2, r).then ([ resp, requests ]) ->
+ requests.length.should.eql(1)
+ requests[0].method.should.eql "GET"
+ requests[0].url.should.match(/\/users\/unique_id\/taran186/)
+ resp._fauna.id.should.eql("users/30159234491482113")
+
it "list", futureTest ->
f = new fauna.FaunaClient()
f.setPublisherKey("qqq")
r = -> f.users.list(after: 3)
- withSuccessfulRequest(JSON.parse(data1), r).then ([ resp, requests ]) ->
+ withSuccessfulRequest(data1, r).then ([ resp, requests ]) ->
requests.length.should.eql(1)
requests[0].method.should.eql "GET"
requests[0].url.should.match(/\/users\?after=3/)
@@ -51,3 +88,17 @@ data1 = """
}
}
"""
+
+data2 = """
+{
+ "resource" : {
+ "ref" : "users/30159234491482113",
+ "class" : "users",
+ "ts" : 1365020937346000,
+ "unique_id" : "taran186",
+ "references" : { },
+ "data" : { },
+ "deleted" : false
+ }
+}
+"""
View
@@ -3,7 +3,8 @@ fauna = require("../lib/fauna")
# simplified version of 'withRequestHandler'
exports.withSuccessfulRequest = (json, f) ->
- fauna.Rest.withRequestHandler(((options) -> [ null, 200, JSON.stringify(json) ]), f)
+ if typeof json != "string" then json = JSON.stringify(json)
+ fauna.Rest.withRequestHandler(((options) -> [ null, 200, json ]), f)
# run a test as a future, and call mocha's 'done' method at the end of the chain.
exports.futureTest = (f) ->

0 comments on commit 95d2328

Please sign in to comment.