Permalink
Browse files

users.list

  • Loading branch information...
1 parent 8c63ef6 commit 36808dcbb54c9196bf44209b6f7b3f8d81661063 @robey committed Apr 12, 2013
Showing with 141 additions and 2 deletions.
  1. +11 −0 src/fauna/fauna_client.coffee
  2. +4 −2 src/fauna/schema.coffee
  3. +23 −0 test/test_client_keys.coffee
  4. +50 −0 test/test_event_sets.coffee
  5. +53 −0 test/test_users.coffee
@@ -82,6 +82,7 @@ class FaunaClient
@clientKeys = plumb _ClientKeys
@eventSets = plumb _EventSets
@classes = plumb _Classes
+ @users = plumb _Users
@_testing = plumb _Testing
debug: (message) -> Rest.debug(message)
@@ -167,9 +168,16 @@ class FaunaClient
class _ClientKeys
get: requireOwner asEventArray -> @rest("get", "keys/client")
+ create: requireOwner asObject -> @rest("post", "keys/client")
class _EventSets
create: requirePublisher asObject (className, setName, data) -> @rest("put", "#{className}/sets/#{setName}/config", data)
+ get: asObject (setName, params={}) ->
+ list = []
+ if params.size? then list.push "size=#{params.size}"
+ if params.before? then list.push "before=#{params.before}"
+ if params.after? then list.push "after=#{params.after}"
+ @rest("get", "#{setName}?#{list.join("&")}")
class _Classes
get: requirePublisher asEventArray -> @rest("get", "classes")
@@ -178,6 +186,9 @@ class FaunaClient
deleteByName: requirePublisher assertNamespace("classes") (name) -> @rest("delete", "#{name}/config")
getStats: requirePublisher asObject (name) -> @rest("get", "#{name}/stats")
+ class _Users
+ list: requirePublisher (params) -> @eventSets.get("users", params)
+
# wrapper to allow easy testing of the decorators
class _Testing
requireOwner: requireOwner (x) -> Q(x)
@@ -95,9 +95,11 @@ class EventSet extends Class
objects are ordered newest to oldest.
###
rv = []
- for event in @events then if event.action == "create" then rv.push(event.resource)
+ for event in @events then if event.action == "create"
+ # don't push raw resource ids that we weren't able to inflate.
+ if typeof event.resource == "object" then rv.push(event.resource)
for event in @events then if event.action == "delete"
- rv = (x for x in rv when x._fauna.id != event.resource._fauna.id)
+ rv = (x for x in rv when x._fauna?.id != event.resource)
rv
exports.EventSet = EventSet
@@ -14,12 +14,23 @@ describe "FaunaClient.clientKeys", ->
r = -> f.clientKeys.get()
withSuccessfulRequest(JSON.parse(data1), r).then ([ resp, requests ]) ->
requests.length.should.eql(1)
+ requests[0].method.should.eql("GET")
requests[0].url.should.match(/keys\/client/)
resp.map((item) -> item.key).should.eql [
"AQEAayWp-xAAAQBrJamcsAABodTOVpMJU4EvMU6paEgrHA"
"AQEAayWp97AAAQBrJamcsAABR0HPBQQdosjqwCGI7q1cMg"
]
+ it "create", futureTest ->
+ f = new fauna.FaunaClient()
+ f.setOwnerAuth("u", "p")
+ r = -> f.clientKeys.create()
+ withSuccessfulRequest(JSON.parse(data2), r).then ([ resp, requests ]) ->
+ requests.length.should.eql(1)
+ requests[0].method.should.eql("POST")
+ requests[0].url.should.match(/keys\/client/)
+ resp.key.should.eql "AQEAayWp97AAAQBrJamcsAABR0HPBQQdosjqwCGI7q1cMg"
+
data1 = """
{
@@ -63,3 +74,15 @@ data1 = """
}
}
"""
+
+data2 = """
+{
+ "resource" : {
+ "ref" : "keys/client/30159234443247617",
+ "class" : "keys/client",
+ "ts" : 1365020937292003,
+ "key" : "AQEAayWp97AAAQBrJamcsAABR0HPBQQdosjqwCGI7q1cMg",
+ "deleted" : false
+ }
+}
+"""
@@ -14,9 +14,27 @@ describe "FaunaClient.eventSets", ->
r = -> f.eventSets.create("users", "spellbook")
withSuccessfulRequest(JSON.parse(data1), r).then ([ resp, requests ]) ->
requests.length.should.eql(1)
+ requests[0].method.should.eql "PUT"
requests[0].url.should.match(/\/users\/sets\/spellbook\/config/)
resp.name.should.eql("spellbook")
+ it "get", futureTest ->
+ f = new fauna.FaunaClient()
+ f.setPublisherKey("qqq")
+ r = -> f.eventSets.get("users/9000/changes", size: 3)
+ withSuccessfulRequest(JSON.parse(data2), r).then ([ resp, requests ]) ->
+ requests.length.should.eql(1)
+ requests[0].method.should.eql "GET"
+ requests[0].url.should.match(/\/users\/9000\/changes\?size=3/)
+ resp.after.should.eql 9223372036854775
+ resp.creates.should.eql 1
+ resp.events.length.should.eql 1
+ resp.events[0].set.should.eql("users/30159234491482113/changes")
+ resp.events[0].resource.unique_id.should.eql("taran186")
+ users = resp.toArray()
+ users.length.should.eql(1)
+ users[0].unique_id.should.eql("taran186")
+
data1 = """
{
@@ -29,3 +47,35 @@ data1 = """
}
}
"""
+
+data2 = """
+{
+ "resource" : {
+ "ref" : "users/30159234491482113/changes",
+ "class" : "sets",
+ "after" : 9223372036854775,
+ "creates" : 1,
+ "updates" : 0,
+ "deletes" : 0,
+ "events" : [
+ {
+ "ts" : 1365020937346000,
+ "action" : "create",
+ "resource" : "users/30159234491482113",
+ "set" : "users/30159234491482113/changes"
+ }
+ ]
+ },
+ "references" : {
+ "users/30159234491482113" : {
+ "ref" : "users/30159234491482113",
+ "class" : "users",
+ "ts" : 1365020937346000,
+ "unique_id" : "taran186",
+ "references" : { },
+ "data" : { },
+ "deleted" : false
+ }
+ }
+}
+"""
@@ -0,0 +1,53 @@
+should = require 'should'
+Q = require 'q'
+util = require 'util'
+
+fauna = require("../lib/fauna")
+test_util = require("./test_util")
+withSuccessfulRequest = test_util.withSuccessfulRequest
+futureTest = test_util.futureTest
+
+describe "FaunaClient.users", ->
+ it "list", futureTest ->
+ f = new fauna.FaunaClient()
+ f.setPublisherKey("qqq")
+ r = -> f.users.list(after: 3)
+ withSuccessfulRequest(JSON.parse(data1), r).then ([ resp, requests ]) ->
+ requests.length.should.eql(1)
+ requests[0].method.should.eql "GET"
+ requests[0].url.should.match(/\/users\?after=3/)
+ users = resp.toArray()
+ users.length.should.eql(1)
+ users[0].unique_id.should.eql("taran186")
+
+data1 = """
+{
+ "resource" : {
+ "ref" : "users/30159234491482113/changes",
+ "class" : "sets",
+ "after" : 9223372036854775,
+ "creates" : 1,
+ "updates" : 0,
+ "deletes" : 0,
+ "events" : [
+ {
+ "ts" : 1365020937346000,
+ "action" : "create",
+ "resource" : "users/30159234491482113",
+ "set" : "users/30159234491482113/changes"
+ }
+ ]
+ },
+ "references" : {
+ "users/30159234491482113" : {
+ "ref" : "users/30159234491482113",
+ "class" : "users",
+ "ts" : 1365020937346000,
+ "unique_id" : "taran186",
+ "references" : { },
+ "data" : { },
+ "deleted" : false
+ }
+ }
+}
+"""

0 comments on commit 36808dc

Please sign in to comment.