user_test.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  1. /*
  2. * EliasDB
  3. *
  4. * Copyright 2016 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the Mozilla Public
  7. * License, v. 2.0. If a copy of the MPL was not distributed with this
  8. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. */
  10. package ac
  11. import (
  12. "net/http"
  13. "strings"
  14. "testing"
  15. )
  16. func TestUserEndpoint(t *testing.T) {
  17. queryURL := "http://localhost" + TESTPORT
  18. res, resp := sendTestRequestResponse("application/json", queryURL+EndpointWhoAmI, "GET", nil, nil)
  19. if res != `{
  20. "logged_in": false,
  21. "username": ""
  22. }` {
  23. t.Error("Unexpected response:", res, resp)
  24. }
  25. authCookie := doAuth("johndoe", "doe")
  26. res, resp = sendTestRequestResponse("application/json", queryURL+EndpointWhoAmI, "GET", nil,
  27. func(req *http.Request) {
  28. req.AddCookie(authCookie)
  29. })
  30. if res != `{
  31. "logged_in": true,
  32. "username": "johndoe"
  33. }` {
  34. t.Error("Unexpected response:", res, resp)
  35. }
  36. // Send request with auth cookie to the user endpoint
  37. res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"u/", "GET", nil,
  38. func(req *http.Request) {
  39. req.AddCookie(authCookie)
  40. })
  41. if res != `[
  42. {
  43. "data": null,
  44. "groups": [
  45. "admin",
  46. "public"
  47. ],
  48. "username": "elias"
  49. },
  50. {
  51. "data": null,
  52. "groups": [],
  53. "username": "guest"
  54. },
  55. {
  56. "data": null,
  57. "groups": [
  58. "public"
  59. ],
  60. "username": "johndoe"
  61. }
  62. ]` {
  63. t.Error("Unexpected response:", res, resp)
  64. return
  65. }
  66. sendTestRequestResponse("application/json", queryURL+EndpointUser+"u/elias", "GET", nil,
  67. func(req *http.Request) {
  68. req.AddCookie(authCookie)
  69. })
  70. res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"g/", "GET", nil,
  71. func(req *http.Request) {
  72. req.AddCookie(authCookie)
  73. })
  74. if res != `{
  75. "admin": {
  76. "/db/*": "CRUD"
  77. },
  78. "public": {
  79. "/": "-R--",
  80. "/css/*": "-R--",
  81. "/db/*": "-R--",
  82. "/img/*": "-R--",
  83. "/js/*": "-R--",
  84. "/vendor/*": "-R--"
  85. }
  86. }` {
  87. t.Error("Unexpected response:", res, resp)
  88. return
  89. }
  90. res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"g/public", "GET", nil,
  91. func(req *http.Request) {
  92. req.AddCookie(authCookie)
  93. })
  94. if res != `{
  95. "/": "-R--",
  96. "/css/*": "-R--",
  97. "/db/*": "-R--",
  98. "/img/*": "-R--",
  99. "/js/*": "-R--",
  100. "/vendor/*": "-R--"
  101. }` {
  102. t.Error("Unexpected response:", res, resp)
  103. return
  104. }
  105. res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"g/publi", "GET", nil,
  106. func(req *http.Request) {
  107. req.AddCookie(authCookie)
  108. })
  109. if res != `{}` {
  110. t.Error("Unexpected response:", res, resp)
  111. return
  112. }
  113. res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"u/elias", "GET", nil,
  114. func(req *http.Request) {
  115. req.AddCookie(authCookie)
  116. })
  117. if res != `{
  118. "data": null,
  119. "groups": [
  120. "admin",
  121. "public"
  122. ],
  123. "username": "elias"
  124. }` {
  125. t.Error("Unexpected response:", res, resp)
  126. return
  127. }
  128. res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser, "GET", nil,
  129. func(req *http.Request) {
  130. req.AddCookie(authCookie)
  131. })
  132. if res != "Need u or g (user/group) and optionally a name" {
  133. t.Error("Unexpected response:", res, resp)
  134. return
  135. }
  136. res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"u/foobar", "GET", nil,
  137. func(req *http.Request) {
  138. req.AddCookie(authCookie)
  139. })
  140. if res != "User foobar does not exist" {
  141. t.Error("Unexpected response:", res, resp)
  142. return
  143. }
  144. // Create another account
  145. res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"u/hans", "POST",
  146. []byte(`{
  147. "password": "123",
  148. "user_data": {
  149. "hobby": "fishing",
  150. "age": 35
  151. }
  152. }`),
  153. func(req *http.Request) {
  154. req.AddCookie(authCookie)
  155. })
  156. if res != "Requested create access to /db/user/u/hans was denied" {
  157. t.Error("Unexpected result:", res)
  158. return
  159. }
  160. authCookie = doAuth("elias", "elias")
  161. res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"x", "POST", nil,
  162. func(req *http.Request) {
  163. req.AddCookie(authCookie)
  164. })
  165. if res != "Need u or g (user/group) and a name" {
  166. t.Error("Unexpected result:", res)
  167. return
  168. }
  169. res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"x/bla", "POST", nil,
  170. func(req *http.Request) {
  171. req.AddCookie(authCookie)
  172. })
  173. if res != "Need u or g (user/group) as first path element" {
  174. t.Error("Unexpected result:", res)
  175. return
  176. }
  177. res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"x/bla/xxx", "POST", nil,
  178. func(req *http.Request) {
  179. req.AddCookie(authCookie)
  180. })
  181. if res != "Invalid resource specification: bla/xxx" {
  182. t.Error("Unexpected result:", res)
  183. return
  184. }
  185. res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"u/hans", "POST",
  186. []byte(`{
  187. "password": "123"xxx
  188. "user_data": {
  189. "hobby": "fishing",
  190. "age": 35
  191. }
  192. }`),
  193. func(req *http.Request) {
  194. req.AddCookie(authCookie)
  195. })
  196. if res != "Could not decode request body as object: invalid character 'x' after object key:value pair" {
  197. t.Error("Unexpected result:", res)
  198. return
  199. }
  200. res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"u/hans", "POST",
  201. []byte(`{
  202. "password": "123",
  203. "user_data": 123
  204. }`),
  205. func(req *http.Request) {
  206. req.AddCookie(authCookie)
  207. })
  208. if res != "User data is not an object" {
  209. t.Error("Unexpected result:", res)
  210. return
  211. }
  212. res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"u/hans", "POST",
  213. []byte(`{
  214. "password": "123",
  215. "user_data": {}
  216. }`),
  217. func(req *http.Request) {
  218. req.AddCookie(authCookie)
  219. })
  220. if !strings.HasPrefix(res, "Could not add user hans: Password matches a common dictionary password") {
  221. t.Error("Unexpected result:", res)
  222. return
  223. }
  224. res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"u/hans", "POST",
  225. []byte(`{
  226. "user_data": {}
  227. }`),
  228. func(req *http.Request) {
  229. req.AddCookie(authCookie)
  230. })
  231. if !strings.HasPrefix(res, "Password is missing in body object") {
  232. t.Error("Unexpected result:", res)
  233. return
  234. }
  235. res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"u/hans", "POST",
  236. []byte(`{
  237. "password" : "SolidFoundat!0n",
  238. "user_data" : {
  239. "hobby" : "fishing",
  240. "age" : 35
  241. },
  242. "group_list" : [ "public" ]
  243. }`),
  244. func(req *http.Request) {
  245. req.AddCookie(authCookie)
  246. })
  247. if resp.StatusCode != 200 {
  248. t.Error("Unexpected result:", res)
  249. return
  250. }
  251. res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"u/", "GET", nil,
  252. func(req *http.Request) {
  253. req.AddCookie(authCookie)
  254. })
  255. if res != `[
  256. {
  257. "data": null,
  258. "groups": [
  259. "admin",
  260. "public"
  261. ],
  262. "username": "elias"
  263. },
  264. {
  265. "data": null,
  266. "groups": [],
  267. "username": "guest"
  268. },
  269. {
  270. "data": {
  271. "age": 35,
  272. "hobby": "fishing"
  273. },
  274. "groups": [
  275. "public"
  276. ],
  277. "username": "hans"
  278. },
  279. {
  280. "data": null,
  281. "groups": [
  282. "public"
  283. ],
  284. "username": "johndoe"
  285. }
  286. ]` {
  287. t.Error("Unexpected response:", res, resp)
  288. return
  289. }
  290. res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"u/hans", "POST",
  291. []byte(`{
  292. "password" : "SolidFoundat!0n",
  293. "user_data" : {
  294. "hobby" : "fishing",
  295. "age" : 35
  296. },
  297. "group_list" : [ "public" ]
  298. }`),
  299. func(req *http.Request) {
  300. req.AddCookie(authCookie)
  301. })
  302. if res != "Could not add user hans: User hans already exists" {
  303. t.Error("Unexpected result:", res)
  304. return
  305. }
  306. res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"u/hans2", "POST",
  307. []byte(`{
  308. "password" : "SolidFoundat!0n",
  309. "user_data" : {
  310. "hobby" : "fishing",
  311. "age" : 35
  312. },
  313. "group_list" : [ "public", "foo" ]
  314. }`),
  315. func(req *http.Request) {
  316. req.AddCookie(authCookie)
  317. })
  318. if res != "Group foo does not exist" {
  319. t.Error("Unexpected result:", res)
  320. return
  321. }
  322. res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"u/hans2", "POST",
  323. []byte(`{
  324. "password" : "SolidFoundat!0n",
  325. "user_data" : {
  326. "hobby" : "fishing",
  327. "age" : 35
  328. },
  329. "group_list" : 1
  330. }`),
  331. func(req *http.Request) {
  332. req.AddCookie(authCookie)
  333. })
  334. if res != "Group list is not a list" {
  335. t.Error("Unexpected result:", res)
  336. return
  337. }
  338. res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"g/meyer", "POST", nil,
  339. func(req *http.Request) {
  340. req.AddCookie(authCookie)
  341. })
  342. if resp.StatusCode != 200 {
  343. t.Error("Unexpected result:", res)
  344. return
  345. }
  346. res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"g/", "GET", nil,
  347. func(req *http.Request) {
  348. req.AddCookie(authCookie)
  349. })
  350. if res != `{
  351. "admin": {
  352. "/db/*": "CRUD"
  353. },
  354. "meyer": {},
  355. "public": {
  356. "/": "-R--",
  357. "/css/*": "-R--",
  358. "/db/*": "-R--",
  359. "/img/*": "-R--",
  360. "/js/*": "-R--",
  361. "/vendor/*": "-R--"
  362. }
  363. }` {
  364. t.Error("Unexpected response:", res, resp)
  365. return
  366. }
  367. res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"g/meyer", "POST",
  368. []byte(`{
  369. "password": "123",
  370. "user_data": 123
  371. }`),
  372. func(req *http.Request) {
  373. req.AddCookie(authCookie)
  374. })
  375. if res != "Could not add group meyer: Group meyer added twice" {
  376. t.Error("Unexpected result:", res)
  377. return
  378. }
  379. // Update an existing user
  380. res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"u/hans", "PUT",
  381. []byte(`{
  382. "password" : "xyzSolidFoundat!0n",
  383. "user_data" : {
  384. "hobby" : "riding",
  385. "age" : 36
  386. },
  387. "group_list" : [ "public", "admin", "meyer" ]
  388. }`),
  389. func(req *http.Request) {
  390. req.AddCookie(authCookie)
  391. })
  392. if resp.StatusCode != 200 {
  393. t.Error("Unexpected result:", res)
  394. return
  395. }
  396. res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"u/", "GET", nil,
  397. func(req *http.Request) {
  398. req.AddCookie(authCookie)
  399. })
  400. if res != `[
  401. {
  402. "data": null,
  403. "groups": [
  404. "admin",
  405. "public"
  406. ],
  407. "username": "elias"
  408. },
  409. {
  410. "data": null,
  411. "groups": [],
  412. "username": "guest"
  413. },
  414. {
  415. "data": {
  416. "age": 36,
  417. "hobby": "riding"
  418. },
  419. "groups": [
  420. "admin",
  421. "meyer",
  422. "public"
  423. ],
  424. "username": "hans"
  425. },
  426. {
  427. "data": null,
  428. "groups": [
  429. "public"
  430. ],
  431. "username": "johndoe"
  432. }
  433. ]` {
  434. t.Error("Unexpected response:", res, resp)
  435. return
  436. }
  437. res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"u/hans", "PUT",
  438. []byte(`{
  439. "password" : "xxx",
  440. "user_data" : {
  441. "hobby" : "riding",
  442. "age" : 36
  443. },
  444. "group_list" : [ "public", "admin" ]
  445. }`),
  446. func(req *http.Request) {
  447. req.AddCookie(authCookie)
  448. })
  449. if !strings.HasPrefix(res, "Password must") {
  450. t.Error("Unexpected result:", res)
  451. return
  452. }
  453. res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"u/hans", "PUT",
  454. []byte(`{
  455. "user_data" : 1,
  456. "group_list" : [ "public", "admin" ]
  457. }`),
  458. func(req *http.Request) {
  459. req.AddCookie(authCookie)
  460. })
  461. if res != "User data is not an object" {
  462. t.Error("Unexpected result:", res)
  463. return
  464. }
  465. res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"g/hans", "PUT",
  466. []byte(`{}`),
  467. func(req *http.Request) {
  468. req.AddCookie(authCookie)
  469. })
  470. if res != "Group hans does not exist" {
  471. t.Error("Unexpected result:", res)
  472. return
  473. }
  474. res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"g/admin", "PUT",
  475. []byte(`{xxx}`),
  476. func(req *http.Request) {
  477. req.AddCookie(authCookie)
  478. })
  479. if res != "Could not decode request body as object: invalid character 'x' looking for beginning of object key string" {
  480. t.Error("Unexpected result:", res)
  481. return
  482. }
  483. res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"g/", "PUT",
  484. []byte(`{}`),
  485. func(req *http.Request) {
  486. req.AddCookie(authCookie)
  487. })
  488. if res != "Need u or g (user/group) and a name" {
  489. t.Error("Unexpected result:", res)
  490. return
  491. }
  492. res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"x/xxx", "PUT",
  493. []byte(`{}`),
  494. func(req *http.Request) {
  495. req.AddCookie(authCookie)
  496. })
  497. if res != "Need u or g (user/group) as first path element" {
  498. t.Error("Unexpected result:", res)
  499. return
  500. }
  501. res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"u/foo", "PUT",
  502. []byte(`{}`),
  503. func(req *http.Request) {
  504. req.AddCookie(authCookie)
  505. })
  506. if res != "User foo does not exist" {
  507. t.Error("Unexpected result:", res)
  508. return
  509. }
  510. res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"u/hans", "PUT",
  511. []byte(`{
  512. "group_list" : 1
  513. }`),
  514. func(req *http.Request) {
  515. req.AddCookie(authCookie)
  516. })
  517. if res != "Group list is not a list" {
  518. t.Error("Unexpected result:", res)
  519. return
  520. }
  521. res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"u/hans", "PUT",
  522. []byte(`{
  523. "group_list" : [ "admin" ]
  524. `),
  525. func(req *http.Request) {
  526. req.AddCookie(authCookie)
  527. })
  528. if res != "Could not decode request body as object: unexpected EOF" {
  529. t.Error("Unexpected result:", res)
  530. return
  531. }
  532. res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"u/hans", "PUT",
  533. []byte(`{
  534. "password" : "66adm!nA",
  535. "user_data" : {
  536. "hobby" : "nothing"
  537. },
  538. "group_list" : [ "public", "admin", "foo" ]
  539. }`),
  540. func(req *http.Request) {
  541. req.AddCookie(authCookie)
  542. })
  543. if res != "Group foo does not exist" {
  544. t.Error("Unexpected result:", res)
  545. return
  546. }
  547. // Make sure non of the failed requests did a partial update
  548. res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"u/", "GET", nil,
  549. func(req *http.Request) {
  550. req.AddCookie(authCookie)
  551. })
  552. if res != `[
  553. {
  554. "data": null,
  555. "groups": [
  556. "admin",
  557. "public"
  558. ],
  559. "username": "elias"
  560. },
  561. {
  562. "data": null,
  563. "groups": [],
  564. "username": "guest"
  565. },
  566. {
  567. "data": {
  568. "age": 36,
  569. "hobby": "riding"
  570. },
  571. "groups": [
  572. "admin",
  573. "meyer",
  574. "public"
  575. ],
  576. "username": "hans"
  577. },
  578. {
  579. "data": null,
  580. "groups": [
  581. "public"
  582. ],
  583. "username": "johndoe"
  584. }
  585. ]` {
  586. t.Error("Unexpected response:", res, resp)
  587. return
  588. }
  589. // ########################
  590. res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"g/meyer", "GET", nil,
  591. func(req *http.Request) {
  592. req.AddCookie(authCookie)
  593. })
  594. if res != "{}" {
  595. t.Error("Unexpected response:", res, resp)
  596. return
  597. }
  598. res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"g/meyer", "PUT",
  599. []byte(`{
  600. "/": "-R--",
  601. "/css/*": "-RU-",
  602. "/styles/*": "-R--",
  603. "/db/*": "-R--",
  604. "/img/*": "-R--",
  605. "/js/*": "-R--",
  606. "/vendor/*": "-R--"
  607. }`),
  608. func(req *http.Request) {
  609. req.AddCookie(authCookie)
  610. })
  611. if resp.StatusCode != 200 {
  612. t.Error("Unexpected response:", res, resp)
  613. return
  614. }
  615. res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"g/meyer", "GET", nil,
  616. func(req *http.Request) {
  617. req.AddCookie(authCookie)
  618. })
  619. if res != `{
  620. "/": "-R--",
  621. "/css/*": "-RU-",
  622. "/db/*": "-R--",
  623. "/img/*": "-R--",
  624. "/js/*": "-R--",
  625. "/styles/*": "-R--",
  626. "/vendor/*": "-R--"
  627. }` {
  628. t.Error("Unexpected response:", res, resp)
  629. return
  630. }
  631. res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"g/meyer", "PUT",
  632. []byte(`{
  633. "/": "-R--",
  634. "/css/*": "-RU-",
  635. "/styles/*": "-W--",
  636. "/db/*": "-R--",
  637. "/img/*": "-R--",
  638. "/js/*": "-R--",
  639. "/vendor/*": "-R--"
  640. }`),
  641. func(req *http.Request) {
  642. req.AddCookie(authCookie)
  643. })
  644. if res != "Read permission in rights string must be either 'r' or '-'" {
  645. t.Error("Unexpected response:", res, resp)
  646. return
  647. }
  648. res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"g/meyer", "PUT",
  649. []byte(`{
  650. "/": "-R--"
  651. }`),
  652. func(req *http.Request) {
  653. req.AddCookie(authCookie)
  654. })
  655. if resp.StatusCode != 200 {
  656. t.Error("Unexpected response:", res, resp)
  657. return
  658. }
  659. res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"g/meyer", "GET", nil,
  660. func(req *http.Request) {
  661. req.AddCookie(authCookie)
  662. })
  663. if res != `{
  664. "/": "-R--"
  665. }` {
  666. t.Error("Unexpected response:", res, resp)
  667. return
  668. }
  669. // ########################
  670. // Delete things
  671. res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"g/meyer", "DELETE", nil,
  672. func(req *http.Request) {
  673. req.AddCookie(authCookie)
  674. })
  675. if resp.StatusCode != 200 {
  676. t.Error("Unexpected result:", res)
  677. return
  678. }
  679. res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"u/", "GET", nil,
  680. func(req *http.Request) {
  681. req.AddCookie(authCookie)
  682. })
  683. if res != `[
  684. {
  685. "data": null,
  686. "groups": [
  687. "admin",
  688. "public"
  689. ],
  690. "username": "elias"
  691. },
  692. {
  693. "data": null,
  694. "groups": [],
  695. "username": "guest"
  696. },
  697. {
  698. "data": {
  699. "age": 36,
  700. "hobby": "riding"
  701. },
  702. "groups": [
  703. "admin",
  704. "public"
  705. ],
  706. "username": "hans"
  707. },
  708. {
  709. "data": null,
  710. "groups": [
  711. "public"
  712. ],
  713. "username": "johndoe"
  714. }
  715. ]` {
  716. t.Error("Unexpected response:", res, resp)
  717. return
  718. }
  719. res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"u/hans", "DELETE", nil,
  720. func(req *http.Request) {
  721. req.AddCookie(authCookie)
  722. })
  723. if resp.StatusCode != 200 {
  724. t.Error("Unexpected result:", res)
  725. return
  726. }
  727. res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"u/", "GET", nil,
  728. func(req *http.Request) {
  729. req.AddCookie(authCookie)
  730. })
  731. if res != `[
  732. {
  733. "data": null,
  734. "groups": [
  735. "admin",
  736. "public"
  737. ],
  738. "username": "elias"
  739. },
  740. {
  741. "data": null,
  742. "groups": [],
  743. "username": "guest"
  744. },
  745. {
  746. "data": null,
  747. "groups": [
  748. "public"
  749. ],
  750. "username": "johndoe"
  751. }
  752. ]` {
  753. t.Error("Unexpected response:", res, resp)
  754. return
  755. }
  756. res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"g/meyer", "DELETE", nil,
  757. func(req *http.Request) {
  758. req.AddCookie(authCookie)
  759. })
  760. if res != "Could not remove group meyer: Group meyer does not exist" {
  761. t.Error("Unexpected result:", res)
  762. return
  763. }
  764. res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"u/foo", "DELETE", nil,
  765. func(req *http.Request) {
  766. req.AddCookie(authCookie)
  767. })
  768. if res != "Could not remove user foo: Unknown user foo" {
  769. t.Error("Unexpected result:", res)
  770. return
  771. }
  772. res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"x/meyer", "DELETE", nil,
  773. func(req *http.Request) {
  774. req.AddCookie(authCookie)
  775. })
  776. if res != "Need u or g (user/group) as first path element" {
  777. t.Error("Unexpected result:", res)
  778. return
  779. }
  780. res, resp = sendTestRequestResponse("application/json", queryURL+EndpointUser+"x", "DELETE", nil,
  781. func(req *http.Request) {
  782. req.AddCookie(authCookie)
  783. })
  784. if res != "Need u or g (user/group) and a name" {
  785. t.Error("Unexpected result:", res)
  786. return
  787. }
  788. }