123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459 |
- /*
- * 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 console
- import (
- "bytes"
- "testing"
- "devt.de/krotik/eliasdb/config"
- )
- func TestUsersCommands(t *testing.T) {
- var out bytes.Buffer
- var pass = "!El1as9845"
- ResetDB()
- credGiver.Reset()
- // Enable access control
- config.Config[config.EnableAccessControl] = true
- defer func() {
- config.Config[config.EnableAccessControl] = false
- }()
- c := NewConsole("http://localhost"+TESTPORT, &out, credGiver.GetCredentials,
- func() string { return pass },
- func(args []string, e *bytes.Buffer) error {
- return nil
- })
- credGiver.UserQueue = []string{"elias"}
- credGiver.PassQueue = []string{"elias"}
- if ok, err := c.Run("users"); !ok || err != nil {
- t.Error(ok, err)
- return
- }
- if res := out.String(); res != `
- Login as user elias
- ┌─────────┬─────────────┐
- │Username │Groups │
- ├─────────┼─────────────┤
- │elias │admin/public │
- │johndoe │public │
- └─────────┴─────────────┘
- `[1:] {
- t.Error("Unexpected result:", res)
- return
- }
- out.Reset()
- if ok, err := c.Run("useradd"); ok || err == nil || err.Error() != "Please specify a username" {
- t.Error(ok, err)
- return
- }
- out.Reset()
- if ok, err := c.Run("useradd ml"); !ok || err != nil {
- t.Error(ok, err)
- return
- }
- if res := out.String(); res != `
- User ml was created
- `[1:] {
- t.Error("Unexpected result:", res)
- return
- }
- out.Reset()
- if ok, err := c.Run("users"); !ok || err != nil {
- t.Error(ok, err)
- return
- }
- if res := out.String(); res != `
- ┌─────────┬─────────────┐
- │Username │Groups │
- ├─────────┼─────────────┤
- │elias │admin/public │
- │johndoe │public │
- │ml │ │
- └─────────┴─────────────┘
- `[1:] {
- t.Error("Unexpected result:", res)
- return
- }
- out.Reset()
- if ok, err := c.Run("newpass"); ok || err == nil || err.Error() != "Please specify a username" {
- t.Error(ok, err)
- return
- }
- out.Reset()
- pass = "!El1as9846"
- if ok, err := c.Run("newpass ml"); !ok || err != nil {
- t.Error(ok, err)
- return
- }
- if res := out.String(); res != `
- Password for user ml was changed
- `[1:] {
- t.Error("Unexpected result:", res)
- return
- }
- out.Reset()
- if ok, err := c.Run("joingroup"); ok || err == nil || err.Error() != "Please specify a username and a group" {
- t.Error(ok, err)
- return
- }
- out.Reset()
- if ok, err := c.Run("joingroup ml public"); !ok || err != nil {
- t.Error(ok, err)
- return
- }
- if res := out.String(); res != `
- User ml has joined group public
- `[1:] {
- t.Error("Unexpected result:", res)
- return
- }
- out.Reset()
- if ok, err := c.Run("joingroup ml public"); ok || err == nil || err.Error() != "User ml is already member of group public" {
- t.Error(ok, err)
- return
- }
- out.Reset()
- if ok, err := c.Run("users"); !ok || err != nil {
- t.Error(ok, err)
- return
- }
- if res := out.String(); res != `
- ┌─────────┬─────────────┐
- │Username │Groups │
- ├─────────┼─────────────┤
- │elias │admin/public │
- │johndoe │public │
- │ml │public │
- └─────────┴─────────────┘
- `[1:] {
- t.Error("Unexpected result:", res)
- return
- }
- out.Reset()
- if ok, err := c.Run("groups"); !ok || err != nil {
- t.Error(ok, err)
- return
- }
- if res := out.String(); res != `
- ┌───────┬──────────┬────────────┐
- │Group │Path │Permissions │
- ├───────┼──────────┼────────────┤
- │admin │/db/* │CRUD │
- │public │/ │-R-- │
- │ │/css/* │-R-- │
- │ │/db/* │-R-- │
- │ │/img/* │-R-- │
- │ │/js/* │-R-- │
- │ │/vendor/* │-R-- │
- └───────┴──────────┴────────────┘
- `[1:] {
- t.Error("Unexpected result:", res)
- return
- }
- // Creating special group
- out.Reset()
- if ok, err := c.Run("groupadd"); ok || err == nil || err.Error() != "Please specify a groupname" {
- t.Error(ok, err)
- return
- }
- if ok, err := c.Run("groupadd ml"); !ok || err != nil {
- t.Error(ok, err)
- return
- }
- if res := out.String(); res != `
- Group ml was created
- `[1:] {
- t.Error("Unexpected result:", res)
- return
- }
- out.Reset()
- if ok, err := c.Run("groups"); !ok || err != nil {
- t.Error(ok, err)
- return
- }
- if res := out.String(); res != `
- ┌───────┬──────────┬────────────┐
- │Group │Path │Permissions │
- ├───────┼──────────┼────────────┤
- │admin │/db/* │CRUD │
- │ml │ │ │
- │public │/ │-R-- │
- │ │/css/* │-R-- │
- │ │/db/* │-R-- │
- │ │/img/* │-R-- │
- │ │/js/* │-R-- │
- │ │/vendor/* │-R-- │
- └───────┴──────────┴────────────┘
- `[1:] {
- t.Error("Unexpected result:", res)
- return
- }
- out.Reset()
- // Test grant / revoke
- if ok, err := c.Run("grantperm"); ok || err == nil || err.Error() != "Please specify a permission, a resource path and a groupname" {
- t.Error(ok, err)
- return
- }
- if ok, err := c.Run("revokeperm"); ok || err == nil || err.Error() != "Please specify a resource path and a groupname" {
- t.Error(ok, err)
- return
- }
- out.Reset()
- if ok, err := c.Run("grantperm -r-- /styles/* ml"); !ok || err != nil {
- t.Error(ok, err)
- return
- }
- if res := out.String(); res != `
- Permission -r-- on /styles/* was granted to ml
- `[1:] {
- t.Error("Unexpected result:", res)
- return
- }
- out.Reset()
- if ok, err := c.Run("grantperm -r-- /styles2/* ml"); !ok || err != nil {
- t.Error(ok, err)
- return
- }
- if res := out.String(); res != `
- Permission -r-- on /styles2/* was granted to ml
- `[1:] {
- t.Error("Unexpected result:", res)
- return
- }
- out.Reset()
- if ok, err := c.Run("groups"); !ok || err != nil {
- t.Error(ok, err)
- return
- }
- if res := out.String(); res != `
- ┌───────┬───────────┬────────────┐
- │Group │Path │Permissions │
- ├───────┼───────────┼────────────┤
- │admin │/db/* │CRUD │
- │ml │/styles/* │-R-- │
- │ │/styles2/* │-R-- │
- │public │/ │-R-- │
- │ │/css/* │-R-- │
- │ │/db/* │-R-- │
- │ │/img/* │-R-- │
- │ │/js/* │-R-- │
- │ │/vendor/* │-R-- │
- └───────┴───────────┴────────────┘
- `[1:] {
- t.Error("Unexpected result:", res)
- return
- }
- out.Reset()
- if ok, err := c.Run("revokeperm /styles2/* ml"); !ok || err != nil {
- t.Error(ok, err)
- return
- }
- if res := out.String(); res != `
- All permissions on /styles2/* were revoked for ml
- `[1:] {
- t.Error("Unexpected result:", res)
- return
- }
- out.Reset()
- if ok, err := c.Run("groups"); !ok || err != nil {
- t.Error(ok, err)
- return
- }
- if res := out.String(); res != `
- ┌───────┬──────────┬────────────┐
- │Group │Path │Permissions │
- ├───────┼──────────┼────────────┤
- │admin │/db/* │CRUD │
- │ml │/styles/* │-R-- │
- │public │/ │-R-- │
- │ │/css/* │-R-- │
- │ │/db/* │-R-- │
- │ │/img/* │-R-- │
- │ │/js/* │-R-- │
- │ │/vendor/* │-R-- │
- └───────┴──────────┴────────────┘
- `[1:] {
- t.Error("Unexpected result:", res)
- return
- }
- out.Reset()
- if ok, err := c.Run("groupdel"); ok || err == nil || err.Error() != "Please specify a groupname" {
- t.Error(ok, err)
- return
- }
- out.Reset()
- if ok, err := c.Run("groupdel ml"); !ok || err != nil {
- t.Error(ok, err)
- return
- }
- if res := out.String(); res != `
- Group ml was deleted
- `[1:] {
- t.Error("Unexpected result:", res)
- return
- }
- out.Reset()
- if ok, err := c.Run("leavegroup"); ok || err == nil || err.Error() != "Please specify a username and a group" {
- t.Error(ok, err)
- return
- }
- out.Reset()
- if ok, err := c.Run("leavegroup ml foo"); ok || err == nil || err.Error() != "User ml is not in group foo" {
- t.Error(ok, err)
- return
- }
- out.Reset()
- if ok, err := c.Run("leavegroup ml public"); !ok || err != nil {
- t.Error(ok, err)
- return
- }
- if res := out.String(); res != `
- User ml has left group public
- `[1:] {
- t.Error("Unexpected result:", res)
- return
- }
- out.Reset()
- if ok, err := c.Run("users"); !ok || err != nil {
- t.Error(ok, err)
- return
- }
- if res := out.String(); res != `
- ┌─────────┬─────────────┐
- │Username │Groups │
- ├─────────┼─────────────┤
- │elias │admin/public │
- │johndoe │public │
- │ml │ │
- └─────────┴─────────────┘
- `[1:] {
- t.Error("Unexpected result:", res)
- return
- }
- out.Reset()
- if ok, err := c.Run("userdel"); ok || err == nil || err.Error() != "Please specify a username" {
- t.Error(ok, err)
- return
- }
- out.Reset()
- if ok, err := c.Run("userdel ml"); !ok || err != nil {
- t.Error(ok, err)
- return
- }
- if res := out.String(); res != `
- User ml was deleted
- `[1:] {
- t.Error("Unexpected result:", res)
- return
- }
- out.Reset()
- if ok, err := c.Run("users"); !ok || err != nil {
- t.Error(ok, err)
- return
- }
- if res := out.String(); res != `
- ┌─────────┬─────────────┐
- │Username │Groups │
- ├─────────┼─────────────┤
- │elias │admin/public │
- │johndoe │public │
- └─────────┴─────────────┘
- `[1:] {
- t.Error("Unexpected result:", res)
- return
- }
- out.Reset()
- }
|