cmd_users_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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 console
  11. import (
  12. "bytes"
  13. "testing"
  14. "devt.de/krotik/eliasdb/config"
  15. )
  16. func TestUsersCommands(t *testing.T) {
  17. var out bytes.Buffer
  18. var pass = "!El1as9845"
  19. ResetDB()
  20. credGiver.Reset()
  21. // Enable access control
  22. config.Config[config.EnableAccessControl] = true
  23. defer func() {
  24. config.Config[config.EnableAccessControl] = false
  25. }()
  26. c := NewConsole("http://localhost"+TESTPORT, &out, credGiver.GetCredentials,
  27. func() string { return pass },
  28. func(args []string, e *bytes.Buffer) error {
  29. return nil
  30. })
  31. credGiver.UserQueue = []string{"elias"}
  32. credGiver.PassQueue = []string{"elias"}
  33. if ok, err := c.Run("users"); !ok || err != nil {
  34. t.Error(ok, err)
  35. return
  36. }
  37. if res := out.String(); res != `
  38. Login as user elias
  39. ┌─────────┬─────────────┐
  40. │Username │Groups │
  41. ├─────────┼─────────────┤
  42. │elias │admin/public │
  43. │johndoe │public │
  44. └─────────┴─────────────┘
  45. `[1:] {
  46. t.Error("Unexpected result:", res)
  47. return
  48. }
  49. out.Reset()
  50. if ok, err := c.Run("useradd"); ok || err == nil || err.Error() != "Please specify a username" {
  51. t.Error(ok, err)
  52. return
  53. }
  54. out.Reset()
  55. if ok, err := c.Run("useradd ml"); !ok || err != nil {
  56. t.Error(ok, err)
  57. return
  58. }
  59. if res := out.String(); res != `
  60. User ml was created
  61. `[1:] {
  62. t.Error("Unexpected result:", res)
  63. return
  64. }
  65. out.Reset()
  66. if ok, err := c.Run("users"); !ok || err != nil {
  67. t.Error(ok, err)
  68. return
  69. }
  70. if res := out.String(); res != `
  71. ┌─────────┬─────────────┐
  72. │Username │Groups │
  73. ├─────────┼─────────────┤
  74. │elias │admin/public │
  75. │johndoe │public │
  76. │ml │ │
  77. └─────────┴─────────────┘
  78. `[1:] {
  79. t.Error("Unexpected result:", res)
  80. return
  81. }
  82. out.Reset()
  83. if ok, err := c.Run("newpass"); ok || err == nil || err.Error() != "Please specify a username" {
  84. t.Error(ok, err)
  85. return
  86. }
  87. out.Reset()
  88. pass = "!El1as9846"
  89. if ok, err := c.Run("newpass ml"); !ok || err != nil {
  90. t.Error(ok, err)
  91. return
  92. }
  93. if res := out.String(); res != `
  94. Password for user ml was changed
  95. `[1:] {
  96. t.Error("Unexpected result:", res)
  97. return
  98. }
  99. out.Reset()
  100. if ok, err := c.Run("joingroup"); ok || err == nil || err.Error() != "Please specify a username and a group" {
  101. t.Error(ok, err)
  102. return
  103. }
  104. out.Reset()
  105. if ok, err := c.Run("joingroup ml public"); !ok || err != nil {
  106. t.Error(ok, err)
  107. return
  108. }
  109. if res := out.String(); res != `
  110. User ml has joined group public
  111. `[1:] {
  112. t.Error("Unexpected result:", res)
  113. return
  114. }
  115. out.Reset()
  116. if ok, err := c.Run("joingroup ml public"); ok || err == nil || err.Error() != "User ml is already member of group public" {
  117. t.Error(ok, err)
  118. return
  119. }
  120. out.Reset()
  121. if ok, err := c.Run("users"); !ok || err != nil {
  122. t.Error(ok, err)
  123. return
  124. }
  125. if res := out.String(); res != `
  126. ┌─────────┬─────────────┐
  127. │Username │Groups │
  128. ├─────────┼─────────────┤
  129. │elias │admin/public │
  130. │johndoe │public │
  131. │ml │public │
  132. └─────────┴─────────────┘
  133. `[1:] {
  134. t.Error("Unexpected result:", res)
  135. return
  136. }
  137. out.Reset()
  138. if ok, err := c.Run("groups"); !ok || err != nil {
  139. t.Error(ok, err)
  140. return
  141. }
  142. if res := out.String(); res != `
  143. ┌───────┬──────────┬────────────┐
  144. │Group │Path │Permissions │
  145. ├───────┼──────────┼────────────┤
  146. │admin │/db/* │CRUD │
  147. │public │/ │-R-- │
  148. │ │/css/* │-R-- │
  149. │ │/db/* │-R-- │
  150. │ │/img/* │-R-- │
  151. │ │/js/* │-R-- │
  152. │ │/vendor/* │-R-- │
  153. └───────┴──────────┴────────────┘
  154. `[1:] {
  155. t.Error("Unexpected result:", res)
  156. return
  157. }
  158. // Creating special group
  159. out.Reset()
  160. if ok, err := c.Run("groupadd"); ok || err == nil || err.Error() != "Please specify a groupname" {
  161. t.Error(ok, err)
  162. return
  163. }
  164. if ok, err := c.Run("groupadd ml"); !ok || err != nil {
  165. t.Error(ok, err)
  166. return
  167. }
  168. if res := out.String(); res != `
  169. Group ml was created
  170. `[1:] {
  171. t.Error("Unexpected result:", res)
  172. return
  173. }
  174. out.Reset()
  175. if ok, err := c.Run("groups"); !ok || err != nil {
  176. t.Error(ok, err)
  177. return
  178. }
  179. if res := out.String(); res != `
  180. ┌───────┬──────────┬────────────┐
  181. │Group │Path │Permissions │
  182. ├───────┼──────────┼────────────┤
  183. │admin │/db/* │CRUD │
  184. │ml │ │ │
  185. │public │/ │-R-- │
  186. │ │/css/* │-R-- │
  187. │ │/db/* │-R-- │
  188. │ │/img/* │-R-- │
  189. │ │/js/* │-R-- │
  190. │ │/vendor/* │-R-- │
  191. └───────┴──────────┴────────────┘
  192. `[1:] {
  193. t.Error("Unexpected result:", res)
  194. return
  195. }
  196. out.Reset()
  197. // Test grant / revoke
  198. if ok, err := c.Run("grantperm"); ok || err == nil || err.Error() != "Please specify a permission, a resource path and a groupname" {
  199. t.Error(ok, err)
  200. return
  201. }
  202. if ok, err := c.Run("revokeperm"); ok || err == nil || err.Error() != "Please specify a resource path and a groupname" {
  203. t.Error(ok, err)
  204. return
  205. }
  206. out.Reset()
  207. if ok, err := c.Run("grantperm -r-- /styles/* ml"); !ok || err != nil {
  208. t.Error(ok, err)
  209. return
  210. }
  211. if res := out.String(); res != `
  212. Permission -r-- on /styles/* was granted to ml
  213. `[1:] {
  214. t.Error("Unexpected result:", res)
  215. return
  216. }
  217. out.Reset()
  218. if ok, err := c.Run("grantperm -r-- /styles2/* ml"); !ok || err != nil {
  219. t.Error(ok, err)
  220. return
  221. }
  222. if res := out.String(); res != `
  223. Permission -r-- on /styles2/* was granted to ml
  224. `[1:] {
  225. t.Error("Unexpected result:", res)
  226. return
  227. }
  228. out.Reset()
  229. if ok, err := c.Run("groups"); !ok || err != nil {
  230. t.Error(ok, err)
  231. return
  232. }
  233. if res := out.String(); res != `
  234. ┌───────┬───────────┬────────────┐
  235. │Group │Path │Permissions │
  236. ├───────┼───────────┼────────────┤
  237. │admin │/db/* │CRUD │
  238. │ml │/styles/* │-R-- │
  239. │ │/styles2/* │-R-- │
  240. │public │/ │-R-- │
  241. │ │/css/* │-R-- │
  242. │ │/db/* │-R-- │
  243. │ │/img/* │-R-- │
  244. │ │/js/* │-R-- │
  245. │ │/vendor/* │-R-- │
  246. └───────┴───────────┴────────────┘
  247. `[1:] {
  248. t.Error("Unexpected result:", res)
  249. return
  250. }
  251. out.Reset()
  252. if ok, err := c.Run("revokeperm /styles2/* ml"); !ok || err != nil {
  253. t.Error(ok, err)
  254. return
  255. }
  256. if res := out.String(); res != `
  257. All permissions on /styles2/* were revoked for ml
  258. `[1:] {
  259. t.Error("Unexpected result:", res)
  260. return
  261. }
  262. out.Reset()
  263. if ok, err := c.Run("groups"); !ok || err != nil {
  264. t.Error(ok, err)
  265. return
  266. }
  267. if res := out.String(); res != `
  268. ┌───────┬──────────┬────────────┐
  269. │Group │Path │Permissions │
  270. ├───────┼──────────┼────────────┤
  271. │admin │/db/* │CRUD │
  272. │ml │/styles/* │-R-- │
  273. │public │/ │-R-- │
  274. │ │/css/* │-R-- │
  275. │ │/db/* │-R-- │
  276. │ │/img/* │-R-- │
  277. │ │/js/* │-R-- │
  278. │ │/vendor/* │-R-- │
  279. └───────┴──────────┴────────────┘
  280. `[1:] {
  281. t.Error("Unexpected result:", res)
  282. return
  283. }
  284. out.Reset()
  285. if ok, err := c.Run("groupdel"); ok || err == nil || err.Error() != "Please specify a groupname" {
  286. t.Error(ok, err)
  287. return
  288. }
  289. out.Reset()
  290. if ok, err := c.Run("groupdel ml"); !ok || err != nil {
  291. t.Error(ok, err)
  292. return
  293. }
  294. if res := out.String(); res != `
  295. Group ml was deleted
  296. `[1:] {
  297. t.Error("Unexpected result:", res)
  298. return
  299. }
  300. out.Reset()
  301. if ok, err := c.Run("leavegroup"); ok || err == nil || err.Error() != "Please specify a username and a group" {
  302. t.Error(ok, err)
  303. return
  304. }
  305. out.Reset()
  306. if ok, err := c.Run("leavegroup ml foo"); ok || err == nil || err.Error() != "User ml is not in group foo" {
  307. t.Error(ok, err)
  308. return
  309. }
  310. out.Reset()
  311. if ok, err := c.Run("leavegroup ml public"); !ok || err != nil {
  312. t.Error(ok, err)
  313. return
  314. }
  315. if res := out.String(); res != `
  316. User ml has left group public
  317. `[1:] {
  318. t.Error("Unexpected result:", res)
  319. return
  320. }
  321. out.Reset()
  322. if ok, err := c.Run("users"); !ok || err != nil {
  323. t.Error(ok, err)
  324. return
  325. }
  326. if res := out.String(); res != `
  327. ┌─────────┬─────────────┐
  328. │Username │Groups │
  329. ├─────────┼─────────────┤
  330. │elias │admin/public │
  331. │johndoe │public │
  332. │ml │ │
  333. └─────────┴─────────────┘
  334. `[1:] {
  335. t.Error("Unexpected result:", res)
  336. return
  337. }
  338. out.Reset()
  339. if ok, err := c.Run("userdel"); ok || err == nil || err.Error() != "Please specify a username" {
  340. t.Error(ok, err)
  341. return
  342. }
  343. out.Reset()
  344. if ok, err := c.Run("userdel ml"); !ok || err != nil {
  345. t.Error(ok, err)
  346. return
  347. }
  348. if res := out.String(); res != `
  349. User ml was deleted
  350. `[1:] {
  351. t.Error("Unexpected result:", res)
  352. return
  353. }
  354. out.Reset()
  355. if ok, err := c.Run("users"); !ok || err != nil {
  356. t.Error(ok, err)
  357. return
  358. }
  359. if res := out.String(); res != `
  360. ┌─────────┬─────────────┐
  361. │Username │Groups │
  362. ├─────────┼─────────────┤
  363. │elias │admin/public │
  364. │johndoe │public │
  365. └─────────┴─────────────┘
  366. `[1:] {
  367. t.Error("Unexpected result:", res)
  368. return
  369. }
  370. out.Reset()
  371. }