Repository navigation
Expand file tree
/
Copy pathlist_test.go
More file actions
147 lines (125 loc) · 3.87 KB
/
Copy pathlist_test.go
File metadata and controls
147 lines (125 loc) · 3.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// SPDX-License-Identifier: Apache-2.0
package auth_test
import (
"strings"
"testing"
"github.com/gopherium/gouncer"
"github.com/gopherium/framework/gonsole"
"github.com/gopherium/framework/gonsole/auth"
"github.com/gopherium/framework/gonsole/testkit"
)
// listed returns the address of a seeded world whose editor is disabled and that holds one account under no role.
func listed(t *testing.T) string {
t.Helper()
address := seeded(t)
store := storeAt(t, address)
roleless, err := gouncer.NewUser("roleless@example.com", "account without a role", demoPassword)
if err != nil {
t.Fatalf("NewUser() = %v", err)
}
if err := store.CreateUser(t.Context(), roleless); err != nil {
t.Fatalf("CreateUser() = %v", err)
}
if err := store.SetUserDisabled(t.Context(), account(t, store, "editor@example.com").ID, true); err != nil {
t.Fatalf("SetUserDisabled() = %v", err)
}
return address
}
func TestListPrintsEveryAccountWithItsRoleAndStanding(t *testing.T) {
t.Parallel()
var reads rolesRead
got := testkit.Run(t, program(listed(t), auth.List(config(&reads, "manage_accounts"))), "", "account:list")
want := `roleless@example.com - enabled
admin@example.com admin enabled
author@example.com author enabled
editor@example.com editor disabled
`
if got != (testkit.Result{Code: gonsole.ExitDone, Stdout: want}) {
t.Errorf("Run() = %+v, want %q", got, want)
}
if reads.count != 0 {
t.Errorf("roles read %d times, want none for a listing", reads.count)
}
}
func TestListAnswersOneDocument(t *testing.T) {
t.Parallel()
address := listed(t)
var reads rolesRead
got := testkit.Run(t, program(address, auth.List(config(&reads, ""))), "", "account:list", "-json")
store := storeAt(t, address)
for _, email := range []string{"admin@example.com", "author@example.com", "editor@example.com",
"roleless@example.com"} {
got.Stdout = strings.ReplaceAll(got.Stdout, account(t, store, email).ID.String(), "id of "+email)
}
want := `{
"accounts": [
{
"id": "id of roleless@example.com",
"email": "roleless@example.com",
"name": "account without a role",
"role": "",
"disabled": false
},
{
"id": "id of admin@example.com",
"email": "admin@example.com",
"name": "admin account",
"role": "admin",
"disabled": false
},
{
"id": "id of author@example.com",
"email": "author@example.com",
"name": "author account",
"role": "author",
"disabled": false
},
{
"id": "id of editor@example.com",
"email": "editor@example.com",
"name": "editor account",
"role": "editor",
"disabled": true
}
]
}
`
if got != (testkit.Result{Code: gonsole.ExitDone, Stdout: want}) {
t.Errorf("Run() = %+v with the stored ids named, want the document %q", got, want)
}
}
func TestListOfNoAccount(t *testing.T) {
t.Parallel()
var reads rolesRead
p := program(migrated(t), auth.List(config(&reads, "")))
text := testkit.Run(t, p, "", "account:list")
document := testkit.Run(t, p, "", "account:list", "-json")
if text != (testkit.Result{Code: gonsole.ExitDone}) {
t.Errorf("Run() = %+v, want nothing printed", text)
}
if want := "{\n \"accounts\": []\n}\n"; document != (testkit.Result{Code: gonsole.ExitDone, Stdout: want}) {
t.Errorf("Run() with -json = %+v, want %q", document, want)
}
}
func TestListFailsWithoutAUsableDatabase(t *testing.T) {
t.Parallel()
cases := []struct {
name string
failure error
}{
{"a database never migrated", errListing},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
var reads rolesRead
address := fresh(t)
storeAt(t, address).ListUsersErr = tc.failure
p := program(address, auth.List(config(&reads, "")))
got := testkit.Run(t, p, "", "account:list")
if want := failed(tc.failure); got != want {
t.Errorf("Run() = %+v, want %+v", got, want)
}
})
}
}