Permalink
Browse files

login and loginasUser.

  • Loading branch information...
1 parent 9c4d15b commit 833f6c2e6163abda352dc421b07009edd3c24bff @robey committed Apr 12, 2013
Showing with 43 additions and 3 deletions.
  1. +8 −2 src/fauna/fauna_client.coffee
  2. +35 −1 test/test_fauna_client.coffee
@@ -140,8 +140,6 @@ class FaunaClient
deleteEverything: requireOwner asObject -> @rest("delete", "everything")
- # tell fauna to create classes & event sets for us.
- # return: { class, event_sets[] }[]
installSchema: ->
###
Create (or update) all the custom classes & event sets registered in the
@@ -167,6 +165,14 @@ class FaunaClient
for set in x.eventSets then rv[set._fauna.id] = set
rv
+ # login as a normal user, and receive a token object on success.
+ login: requireClientOrPublisher asObject (email, password) ->
+ @rest("post", "tokens", email: email, password: password)
+
+ # the publisher may login as another user, using the publisher key as auth.
+ loginAsUser: requirePublisher (email) ->
+ @login(@publisherKey, email)
+
class _PublisherKeys
get: requireOwner asEventArray -> @rest("get", "keys/publisher")
create: requireOwner asObject -> @rest("post", "keys/publisher")
@@ -126,7 +126,7 @@ describe "FaunaClient", ->
f = new fauna.FaunaClient()
f.setOwnerAuth("u", "p")
r = -> f.deleteEverything()
- withSuccessfulRequest(JSON.parse(data3), r).then ([ resp, requests ]) ->
+ withSuccessfulRequest(data3, r).then ([ resp, requests ]) ->
requests.length.should.eql(1)
requests[0].url.should.match(/everything/)
resp.name.should.eql("Sierra Online")
@@ -160,6 +160,27 @@ describe "FaunaClient", ->
resp["classes/fishs/config"].name.should.eql("fishs")
resp["classes/fishs/sets/scales/config"].name.should.eql("scales")
+ it "can login", futureTest ->
+ f = new fauna.FaunaClient()
+ f.setPublisherKey("winner")
+ r = -> f.login("test@example.com", "nothing")
+ withSuccessfulRequest(data4, r).then ([ resp, requests ]) ->
+ requests.length.should.equal(1)
+ requests[0].method.should.eql("POST")
+ requests[0].url.should.match(/\/tokens/)
+ requests[0].body.should.match(/"email":"test@example.com"/)
+ resp.token.should.eql("AQIAayWqL0AAAQBrJan6kAABAGslqZywAAEBAGslqfewAAHrRxQyyxxJ_FgqRBbkmjVjAAAAAAAAAA")
+
+ it "can login as a user", futureTest ->
+ f = new fauna.FaunaClient()
+ f.setPublisherKey("winner")
+ r = -> f.loginAsUser("test@example.com")
+ withSuccessfulRequest(data4, r).then ([ resp, requests ]) ->
+ requests.length.should.equal(1)
+ requests[0].method.should.eql("POST")
+ requests[0].url.should.match(/\/tokens/)
+ requests[0].body.should.match(/"email":"winner"/)
+ resp.token.should.eql("AQIAayWqL0AAAQBrJan6kAABAGslqZywAAEBAGslqfewAAHrRxQyyxxJ_FgqRBbkmjVjAAAAAAAAAA")
# test data from the fauna documentation for GET keys/publisher
data1 = """
@@ -289,3 +310,16 @@ data_bird = """
}
}
"""
+
+data4 = """
+{
+ "resource" : {
+ "ref" : "tokens/30159235375431681",
+ "class" : "tokens",
+ "ts" : 1365020938182000,
+ "user" : "users/30159234491482113",
+ "token" : "AQIAayWqL0AAAQBrJan6kAABAGslqZywAAEBAGslqfewAAHrRxQyyxxJ_FgqRBbkmjVjAAAAAAAAAA",
+ "deleted" : false
+ }
+}
+"""

0 comments on commit 833f6c2

Please sign in to comment.