diff --git a/shared/util_test.go b/shared/util_test.go new file mode 100644 index 0000000..4a8e914 --- /dev/null +++ b/shared/util_test.go @@ -0,0 +1,45 @@ +package shared + +import ( + "encoding/base64" + "gopkg.in/mgo.v2/bson" + "testing" +) + +func TestObjectIdToString(t *testing.T) { + b := bson.ObjectIdHex("5596d9b7f6fb5e195e000004") + s := ObjectIdToPublicId(b) + key, err := base64.URLEncoding.DecodeString(s) + if err != nil { + t.Error(err) + return + } + if len(key) != 16 { + t.Error("key must be of length 16 bytes to be compatible with siphash.", len(key)) + } +} + +func TestStringToObjectId(t *testing.T) { + s := "O725hlWW2bf2-14ZXgAABA==" + oid, err := PublicIdToObjectId(s) + if err != nil { + t.Error(err) + return + } else if oid != bson.ObjectIdHex("5596d9b7f6fb5e195e000004") { + t.Error(oid) + } +} + +func TestValidateId(t *testing.T) { + if id, err := base64.URLEncoding.DecodeString("cGOZFFWW2bf2-14ZXgAABA=="); err != nil { + t.Error(err) + } else if err = validateId(id); err == nil { + t.Error() + } + + if id, err := base64.URLEncoding.DecodeString("O725hlWW2bf2-14ZXgAA"); err != nil { + t.Error(err) + } else if err = validateId(id); err == nil { + t.Error() + } +} diff --git a/static/coffee/controllers/doormanCtrl.coffee b/static/coffee/controllers/doormanCtrl.coffee index b4472e8..5d9f23b 100644 --- a/static/coffee/controllers/doormanCtrl.coffee +++ b/static/coffee/controllers/doormanCtrl.coffee @@ -1,10 +1,11 @@ define ["app", "lodash", "directives/ratinput"], (app, _, ratinput) -> - app.controller "DoormanCtrl", ["$scope", "$http","$routeParams","$location", ($scope, $http, $routeParams, $location) -> + app.controller "DoormanCtrl", ["$scope", "$http","$routeParams","$location", "$timeout", ($scope, $http, $routeParams, $location, $timeout) -> $scope.doorman = {values: []} $scope.somevalue = "2/6" $scope.apiurl = "/api/doormen/#{$routeParams.id}" $scope.user = localStorage.getItem("email") + $scope.doormanChanged = false $http.get($scope.apiurl).then((res) -> $scope.doorman = res.data @@ -61,6 +62,11 @@ define ["app", "lodash", "directives/ratinput"], (app, _, ratinput) -> params = {params: {user: $scope.user}} $http.put($scope.apiurl, $scope.doorman, params).then((ret)-> console.log "update" + ).then((res) -> + $scope.doormanChanged = true + $timeout(() -> + $scope.doormanChanged = false + , 3000) ).catch((res) -> $scope.error = message: res.data diff --git a/static/partials/apispec.html b/static/partials/apispec.html index 28937eb..c92098c 100644 --- a/static/partials/apispec.html +++ b/static/partials/apispec.html @@ -4,6 +4,11 @@ +
+ All calls that change an asset requires authentication.
+ Authentication is done by adding a
+ user={user_name} parameter to the endpoint. For example, to
+ authenticate to /api/doormen/zST8_FWcPDT2-14wDQAAAQ==
+ with a user called "bob", the url becomes
+ /api/doormen/zST8_FWcPDT2-14wDQAAAQ==?user=bob
+