-
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
// SPDX-FileCopyrightText: 2025 Shota FUJI <pockawoooh@gmail.com>
// SPDX-License-Identifier: AGPL-3.0-only
package projection_test
import (
"database/sql"
"io"
"log/slog"
"testing"
eventV1 "pocka.jp/x/yamori/proto/go/backend/events/v1"
"pocka.jp/x/yamori/proto/go/backend/workspace/v1/types"
"pocka.jp/x/yamori/backend/core"
"pocka.jp/x/yamori/backend/core/event"
"pocka.jp/x/yamori/backend/core/projection"
workspaceEvent "pocka.jp/x/yamori/backend/events/workspace"
_ "modernc.org/sqlite"
)
// Permission メッセージのフィールド番号の最も大きい数字
const PERMISSION_MAX_NUMBER = 12
func TestUserPermissions(t *testing.T) {
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
db, err := sql.Open("sqlite", ":memory:")
if err != nil {
t.Fatal(err)
}
core, err := core.New(db, logger)
if err != nil {
t.Fatal(err)
}
tx, err := core.DB.Begin()
if err != nil {
t.Fatal(err)
}
err = event.AppendEvents(tx, []*eventV1.Event{
workspaceEvent.CreateUser("foo", "foo", "Foo", []byte{}),
workspaceEvent.GrantPermission("foo", []types.Permission{
types.Permission_PERMISSION_DELETE_REGULAR_USER,
types.Permission_PERMISSION_ADD_REGULAR_USER,
}),
workspaceEvent.RevokePermission("foo", []types.Permission{
types.Permission_PERMISSION_DELETE_REGULAR_USER,
types.Permission_PERMISSION_EDIT_WORKSPACE_PROFILE,
}),
})
if err != nil {
t.Fatal(err)
}
p, err := projection.GetUsers(tx)
if err != nil {
t.Fatal(err)
}
if err := event.UpdateProjections(tx, p); err != nil {
t.Fatal(err)
}
for _, u := range p.Projection.Users {
if u.GetId() == "foo" {
if len(u.Permissions) != 1 {
t.Errorf("Expected a slice of length of 1, got length of %d", len(u.Permissions))
}
if u.Permissions[0] != types.Permission_PERMISSION_ADD_REGULAR_USER {
t.Errorf("Expected ADD_REGULAR_USER, got %v", u.Permissions[0])
}
return
}
}
t.Errorf("User foo not created")
}
func TestAdminHaveFullPermissions(t *testing.T) {
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
db, err := sql.Open("sqlite", ":memory:")
if err != nil {
t.Fatal(err)
}
core, err := core.New(db, logger)
if err != nil {
t.Fatal(err)
}
tx, err := core.DB.Begin()
if err != nil {
t.Fatal(err)
}
err = event.AppendEvents(tx, []*eventV1.Event{
workspaceEvent.CreateUser("foo", "foo", "Foo", []byte{}),
workspaceEvent.GrantAdminAccess("foo"),
workspaceEvent.GrantPermission("foo", []types.Permission{
types.Permission_PERMISSION_ADD_REGULAR_USER,
}),
workspaceEvent.RevokePermission("foo", []types.Permission{
types.Permission_PERMISSION_EDIT_WORKSPACE_PROFILE,
}),
})
if err != nil {
t.Fatal(err)
}
p, err := projection.GetUsers(tx)
if err != nil {
t.Fatal(err)
}
if err := event.UpdateProjections(tx, p); err != nil {
t.Fatal(err)
}
for _, u := range p.Projection.Users {
if u.GetId() == "foo" {
if len(u.Permissions) != PERMISSION_MAX_NUMBER {
t.Errorf(
"Expected a slice of length of %d, got length of %d",
PERMISSION_MAX_NUMBER,
len(u.Permissions),
)
}
return
}
}
t.Errorf("User foo not created")
}