123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905 |
- /*
- * EliasDB
- *
- * Copyright 2016 Matthias Ladkau. All rights reserved.
- *
- * This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0. If a copy of the MPL was not distributed with this
- * file, You can obtain one at http://mozilla.org/MPL/2.0/.
- */
- package ac
- import (
- "net/http"
- "strings"
- "testing"
- )
- func TestUserEndpoint(t *testing.T) {
- queryURL := "http://localhost" + TESTPORT
- res, resp := sendTestRequestResponse("application/json", queryURL+EndpointWhoAmI, "GET", nil, nil)
- if res != `{
- "logged_in": false,
- "username": ""
- }` {
- t.Error("Unexpected response:", res, resp)
- }
- authCookie := doAuth("johndoe", "doe")
- res, resp = sendTestRequestResponse("application/json", queryURL+EndpointWhoAmI, "GET", nil,
- func(req *http.Request) {
- req.AddCookie(authCookie)
- })
- if res != `{
- "logged_in": true,
- "username": "johndoe"
- }` {
- t.Error("Unexpected response:", res, resp)
- }
- // Send request with auth cookie to the user endpoint
- res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"u/", "GET", nil,
- func(req *http.Request) {
- req.AddCookie(authCookie)
- })
- if res != `[
- {
- "data": null,
- "groups": [
- "admin",
- "public"
- ],
- "username": "elias"
- },
- {
- "data": null,
- "groups": [],
- "username": "guest"
- },
- {
- "data": null,
- "groups": [
- "public"
- ],
- "username": "johndoe"
- }
- ]` {
- t.Error("Unexpected response:", res, resp)
- return
- }
- sendTestRequestResponse("application/json", queryURL+EndpointUser+"u/elias", "GET", nil,
- func(req *http.Request) {
- req.AddCookie(authCookie)
- })
- res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"g/", "GET", nil,
- func(req *http.Request) {
- req.AddCookie(authCookie)
- })
- if res != `{
- "admin": {
- "/db/*": "CRUD"
- },
- "public": {
- "/": "-R--",
- "/css/*": "-R--",
- "/db/*": "-R--",
- "/img/*": "-R--",
- "/js/*": "-R--",
- "/vendor/*": "-R--"
- }
- }` {
- t.Error("Unexpected response:", res, resp)
- return
- }
- res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"g/public", "GET", nil,
- func(req *http.Request) {
- req.AddCookie(authCookie)
- })
- if res != `{
- "/": "-R--",
- "/css/*": "-R--",
- "/db/*": "-R--",
- "/img/*": "-R--",
- "/js/*": "-R--",
- "/vendor/*": "-R--"
- }` {
- t.Error("Unexpected response:", res, resp)
- return
- }
- res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"g/publi", "GET", nil,
- func(req *http.Request) {
- req.AddCookie(authCookie)
- })
- if res != `{}` {
- t.Error("Unexpected response:", res, resp)
- return
- }
- res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"u/elias", "GET", nil,
- func(req *http.Request) {
- req.AddCookie(authCookie)
- })
- if res != `{
- "data": null,
- "groups": [
- "admin",
- "public"
- ],
- "username": "elias"
- }` {
- t.Error("Unexpected response:", res, resp)
- return
- }
- res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser, "GET", nil,
- func(req *http.Request) {
- req.AddCookie(authCookie)
- })
- if res != "Need u or g (user/group) and optionally a name" {
- t.Error("Unexpected response:", res, resp)
- return
- }
- res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"u/foobar", "GET", nil,
- func(req *http.Request) {
- req.AddCookie(authCookie)
- })
- if res != "User foobar does not exist" {
- t.Error("Unexpected response:", res, resp)
- return
- }
- // Create another account
- res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"u/hans", "POST",
- []byte(`{
- "password": "123",
- "user_data": {
- "hobby": "fishing",
- "age": 35
- }
- }`),
- func(req *http.Request) {
- req.AddCookie(authCookie)
- })
- if res != "Requested create access to /db/user/u/hans was denied" {
- t.Error("Unexpected result:", res)
- return
- }
- authCookie = doAuth("elias", "elias")
- res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"x", "POST", nil,
- func(req *http.Request) {
- req.AddCookie(authCookie)
- })
- if res != "Need u or g (user/group) and a name" {
- t.Error("Unexpected result:", res)
- return
- }
- res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"x/bla", "POST", nil,
- func(req *http.Request) {
- req.AddCookie(authCookie)
- })
- if res != "Need u or g (user/group) as first path element" {
- t.Error("Unexpected result:", res)
- return
- }
- res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"x/bla/xxx", "POST", nil,
- func(req *http.Request) {
- req.AddCookie(authCookie)
- })
- if res != "Invalid resource specification: bla/xxx" {
- t.Error("Unexpected result:", res)
- return
- }
- res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"u/hans", "POST",
- []byte(`{
- "password": "123"xxx
- "user_data": {
- "hobby": "fishing",
- "age": 35
- }
- }`),
- func(req *http.Request) {
- req.AddCookie(authCookie)
- })
- if res != "Could not decode request body as object: invalid character 'x' after object key:value pair" {
- t.Error("Unexpected result:", res)
- return
- }
- res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"u/hans", "POST",
- []byte(`{
- "password": "123",
- "user_data": 123
- }`),
- func(req *http.Request) {
- req.AddCookie(authCookie)
- })
- if res != "User data is not an object" {
- t.Error("Unexpected result:", res)
- return
- }
- res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"u/hans", "POST",
- []byte(`{
- "password": "123",
- "user_data": {}
- }`),
- func(req *http.Request) {
- req.AddCookie(authCookie)
- })
- if !strings.HasPrefix(res, "Could not add user hans: Password matches a common dictionary password") {
- t.Error("Unexpected result:", res)
- return
- }
- res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"u/hans", "POST",
- []byte(`{
- "user_data": {}
- }`),
- func(req *http.Request) {
- req.AddCookie(authCookie)
- })
- if !strings.HasPrefix(res, "Password is missing in body object") {
- t.Error("Unexpected result:", res)
- return
- }
- res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"u/hans", "POST",
- []byte(`{
- "password" : "SolidFoundat!0n",
- "user_data" : {
- "hobby" : "fishing",
- "age" : 35
- },
- "group_list" : [ "public" ]
- }`),
- func(req *http.Request) {
- req.AddCookie(authCookie)
- })
- if resp.StatusCode != 200 {
- t.Error("Unexpected result:", res)
- return
- }
- res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"u/", "GET", nil,
- func(req *http.Request) {
- req.AddCookie(authCookie)
- })
- if res != `[
- {
- "data": null,
- "groups": [
- "admin",
- "public"
- ],
- "username": "elias"
- },
- {
- "data": null,
- "groups": [],
- "username": "guest"
- },
- {
- "data": {
- "age": 35,
- "hobby": "fishing"
- },
- "groups": [
- "public"
- ],
- "username": "hans"
- },
- {
- "data": null,
- "groups": [
- "public"
- ],
- "username": "johndoe"
- }
- ]` {
- t.Error("Unexpected response:", res, resp)
- return
- }
- res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"u/hans", "POST",
- []byte(`{
- "password" : "SolidFoundat!0n",
- "user_data" : {
- "hobby" : "fishing",
- "age" : 35
- },
- "group_list" : [ "public" ]
- }`),
- func(req *http.Request) {
- req.AddCookie(authCookie)
- })
- if res != "Could not add user hans: User hans already exists" {
- t.Error("Unexpected result:", res)
- return
- }
- res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"u/hans2", "POST",
- []byte(`{
- "password" : "SolidFoundat!0n",
- "user_data" : {
- "hobby" : "fishing",
- "age" : 35
- },
- "group_list" : [ "public", "foo" ]
- }`),
- func(req *http.Request) {
- req.AddCookie(authCookie)
- })
- if res != "Group foo does not exist" {
- t.Error("Unexpected result:", res)
- return
- }
- res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"u/hans2", "POST",
- []byte(`{
- "password" : "SolidFoundat!0n",
- "user_data" : {
- "hobby" : "fishing",
- "age" : 35
- },
- "group_list" : 1
- }`),
- func(req *http.Request) {
- req.AddCookie(authCookie)
- })
- if res != "Group list is not a list" {
- t.Error("Unexpected result:", res)
- return
- }
- res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"g/meyer", "POST", nil,
- func(req *http.Request) {
- req.AddCookie(authCookie)
- })
- if resp.StatusCode != 200 {
- t.Error("Unexpected result:", res)
- return
- }
- res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"g/", "GET", nil,
- func(req *http.Request) {
- req.AddCookie(authCookie)
- })
- if res != `{
- "admin": {
- "/db/*": "CRUD"
- },
- "meyer": {},
- "public": {
- "/": "-R--",
- "/css/*": "-R--",
- "/db/*": "-R--",
- "/img/*": "-R--",
- "/js/*": "-R--",
- "/vendor/*": "-R--"
- }
- }` {
- t.Error("Unexpected response:", res, resp)
- return
- }
- res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"g/meyer", "POST",
- []byte(`{
- "password": "123",
- "user_data": 123
- }`),
- func(req *http.Request) {
- req.AddCookie(authCookie)
- })
- if res != "Could not add group meyer: Group meyer added twice" {
- t.Error("Unexpected result:", res)
- return
- }
- // Update an existing user
- res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"u/hans", "PUT",
- []byte(`{
- "password" : "xyzSolidFoundat!0n",
- "user_data" : {
- "hobby" : "riding",
- "age" : 36
- },
- "group_list" : [ "public", "admin", "meyer" ]
- }`),
- func(req *http.Request) {
- req.AddCookie(authCookie)
- })
- if resp.StatusCode != 200 {
- t.Error("Unexpected result:", res)
- return
- }
- res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"u/", "GET", nil,
- func(req *http.Request) {
- req.AddCookie(authCookie)
- })
- if res != `[
- {
- "data": null,
- "groups": [
- "admin",
- "public"
- ],
- "username": "elias"
- },
- {
- "data": null,
- "groups": [],
- "username": "guest"
- },
- {
- "data": {
- "age": 36,
- "hobby": "riding"
- },
- "groups": [
- "admin",
- "meyer",
- "public"
- ],
- "username": "hans"
- },
- {
- "data": null,
- "groups": [
- "public"
- ],
- "username": "johndoe"
- }
- ]` {
- t.Error("Unexpected response:", res, resp)
- return
- }
- res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"u/hans", "PUT",
- []byte(`{
- "password" : "xxx",
- "user_data" : {
- "hobby" : "riding",
- "age" : 36
- },
- "group_list" : [ "public", "admin" ]
- }`),
- func(req *http.Request) {
- req.AddCookie(authCookie)
- })
- if !strings.HasPrefix(res, "Password must") {
- t.Error("Unexpected result:", res)
- return
- }
- res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"u/hans", "PUT",
- []byte(`{
- "user_data" : 1,
- "group_list" : [ "public", "admin" ]
- }`),
- func(req *http.Request) {
- req.AddCookie(authCookie)
- })
- if res != "User data is not an object" {
- t.Error("Unexpected result:", res)
- return
- }
- res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"g/hans", "PUT",
- []byte(`{}`),
- func(req *http.Request) {
- req.AddCookie(authCookie)
- })
- if res != "Group hans does not exist" {
- t.Error("Unexpected result:", res)
- return
- }
- res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"g/admin", "PUT",
- []byte(`{xxx}`),
- func(req *http.Request) {
- req.AddCookie(authCookie)
- })
- if res != "Could not decode request body as object: invalid character 'x' looking for beginning of object key string" {
- t.Error("Unexpected result:", res)
- return
- }
- res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"g/", "PUT",
- []byte(`{}`),
- func(req *http.Request) {
- req.AddCookie(authCookie)
- })
- if res != "Need u or g (user/group) and a name" {
- t.Error("Unexpected result:", res)
- return
- }
- res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"x/xxx", "PUT",
- []byte(`{}`),
- func(req *http.Request) {
- req.AddCookie(authCookie)
- })
- if res != "Need u or g (user/group) as first path element" {
- t.Error("Unexpected result:", res)
- return
- }
- res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"u/foo", "PUT",
- []byte(`{}`),
- func(req *http.Request) {
- req.AddCookie(authCookie)
- })
- if res != "User foo does not exist" {
- t.Error("Unexpected result:", res)
- return
- }
- res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"u/hans", "PUT",
- []byte(`{
- "group_list" : 1
- }`),
- func(req *http.Request) {
- req.AddCookie(authCookie)
- })
- if res != "Group list is not a list" {
- t.Error("Unexpected result:", res)
- return
- }
- res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"u/hans", "PUT",
- []byte(`{
- "group_list" : [ "admin" ]
- `),
- func(req *http.Request) {
- req.AddCookie(authCookie)
- })
- if res != "Could not decode request body as object: unexpected EOF" {
- t.Error("Unexpected result:", res)
- return
- }
- res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"u/hans", "PUT",
- []byte(`{
- "password" : "66adm!nA",
- "user_data" : {
- "hobby" : "nothing"
- },
- "group_list" : [ "public", "admin", "foo" ]
- }`),
- func(req *http.Request) {
- req.AddCookie(authCookie)
- })
- if res != "Group foo does not exist" {
- t.Error("Unexpected result:", res)
- return
- }
- // Make sure non of the failed requests did a partial update
- res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"u/", "GET", nil,
- func(req *http.Request) {
- req.AddCookie(authCookie)
- })
- if res != `[
- {
- "data": null,
- "groups": [
- "admin",
- "public"
- ],
- "username": "elias"
- },
- {
- "data": null,
- "groups": [],
- "username": "guest"
- },
- {
- "data": {
- "age": 36,
- "hobby": "riding"
- },
- "groups": [
- "admin",
- "meyer",
- "public"
- ],
- "username": "hans"
- },
- {
- "data": null,
- "groups": [
- "public"
- ],
- "username": "johndoe"
- }
- ]` {
- t.Error("Unexpected response:", res, resp)
- return
- }
- // ########################
- res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"g/meyer", "GET", nil,
- func(req *http.Request) {
- req.AddCookie(authCookie)
- })
- if res != "{}" {
- t.Error("Unexpected response:", res, resp)
- return
- }
- res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"g/meyer", "PUT",
- []byte(`{
- "/": "-R--",
- "/css/*": "-RU-",
- "/styles/*": "-R--",
- "/db/*": "-R--",
- "/img/*": "-R--",
- "/js/*": "-R--",
- "/vendor/*": "-R--"
- }`),
- func(req *http.Request) {
- req.AddCookie(authCookie)
- })
- if resp.StatusCode != 200 {
- t.Error("Unexpected response:", res, resp)
- return
- }
- res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"g/meyer", "GET", nil,
- func(req *http.Request) {
- req.AddCookie(authCookie)
- })
- if res != `{
- "/": "-R--",
- "/css/*": "-RU-",
- "/db/*": "-R--",
- "/img/*": "-R--",
- "/js/*": "-R--",
- "/styles/*": "-R--",
- "/vendor/*": "-R--"
- }` {
- t.Error("Unexpected response:", res, resp)
- return
- }
- res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"g/meyer", "PUT",
- []byte(`{
- "/": "-R--",
- "/css/*": "-RU-",
- "/styles/*": "-W--",
- "/db/*": "-R--",
- "/img/*": "-R--",
- "/js/*": "-R--",
- "/vendor/*": "-R--"
- }`),
- func(req *http.Request) {
- req.AddCookie(authCookie)
- })
- if res != "Read permission in rights string must be either 'r' or '-'" {
- t.Error("Unexpected response:", res, resp)
- return
- }
- res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"g/meyer", "PUT",
- []byte(`{
- "/": "-R--"
- }`),
- func(req *http.Request) {
- req.AddCookie(authCookie)
- })
- if resp.StatusCode != 200 {
- t.Error("Unexpected response:", res, resp)
- return
- }
- res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"g/meyer", "GET", nil,
- func(req *http.Request) {
- req.AddCookie(authCookie)
- })
- if res != `{
- "/": "-R--"
- }` {
- t.Error("Unexpected response:", res, resp)
- return
- }
- // ########################
- // Delete things
- res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"g/meyer", "DELETE", nil,
- func(req *http.Request) {
- req.AddCookie(authCookie)
- })
- if resp.StatusCode != 200 {
- t.Error("Unexpected result:", res)
- return
- }
- res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"u/", "GET", nil,
- func(req *http.Request) {
- req.AddCookie(authCookie)
- })
- if res != `[
- {
- "data": null,
- "groups": [
- "admin",
- "public"
- ],
- "username": "elias"
- },
- {
- "data": null,
- "groups": [],
- "username": "guest"
- },
- {
- "data": {
- "age": 36,
- "hobby": "riding"
- },
- "groups": [
- "admin",
- "public"
- ],
- "username": "hans"
- },
- {
- "data": null,
- "groups": [
- "public"
- ],
- "username": "johndoe"
- }
- ]` {
- t.Error("Unexpected response:", res, resp)
- return
- }
- res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"u/hans", "DELETE", nil,
- func(req *http.Request) {
- req.AddCookie(authCookie)
- })
- if resp.StatusCode != 200 {
- t.Error("Unexpected result:", res)
- return
- }
- res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"u/", "GET", nil,
- func(req *http.Request) {
- req.AddCookie(authCookie)
- })
- if res != `[
- {
- "data": null,
- "groups": [
- "admin",
- "public"
- ],
- "username": "elias"
- },
- {
- "data": null,
- "groups": [],
- "username": "guest"
- },
- {
- "data": null,
- "groups": [
- "public"
- ],
- "username": "johndoe"
- }
- ]` {
- t.Error("Unexpected response:", res, resp)
- return
- }
- res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"g/meyer", "DELETE", nil,
- func(req *http.Request) {
- req.AddCookie(authCookie)
- })
- if res != "Could not remove group meyer: Group meyer does not exist" {
- t.Error("Unexpected result:", res)
- return
- }
- res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"u/foo", "DELETE", nil,
- func(req *http.Request) {
- req.AddCookie(authCookie)
- })
- if res != "Could not remove user foo: Unknown user foo" {
- t.Error("Unexpected result:", res)
- return
- }
- res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"x/meyer", "DELETE", nil,
- func(req *http.Request) {
- req.AddCookie(authCookie)
- })
- if res != "Need u or g (user/group) as first path element" {
- t.Error("Unexpected result:", res)
- return
- }
- res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"x", "DELETE", nil,
- func(req *http.Request) {
- req.AddCookie(authCookie)
- })
- if res != "Need u or g (user/group) and a name" {
- t.Error("Unexpected result:", res)
- return
- }
- }
|