-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathaudit_internal_test.go
More file actions
226 lines (216 loc) · 6.51 KB
/
Copy pathaudit_internal_test.go
File metadata and controls
226 lines (216 loc) · 6.51 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package coderd
import (
"context"
"database/sql"
"encoding/json"
"testing"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
"cdr.dev/slog/v3/sloggers/slogtest"
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/database/dbauthz"
"github.com/coder/coder/v2/coderd/database/dbmock"
"github.com/coder/coder/v2/codersdk"
)
func TestAuditLogIsResourceDeleted(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
name string
err error
wantDeleted bool
}{
{name: "AnError", err: assert.AnError, wantDeleted: false},
{name: "NotAuthorized", err: dbauthz.NotAuthorizedError{}, wantDeleted: false},
{name: "NoError", err: nil, wantDeleted: false},
{name: "NoRows", err: sql.ErrNoRows, wantDeleted: true},
} {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
ctrl := gomock.NewController(t)
db := dbmock.NewMockStore(ctrl)
chatID := uuid.New()
db.EXPECT().GetChatByID(gomock.Any(), chatID).Return(database.Chat{}, tc.err)
api := &API{
Options: &Options{Database: db, Logger: slogtest.Make(t, &slogtest.Options{IgnoreErrors: true})},
}
deleted := api.auditLogIsResourceDeleted(context.Background(), database.GetAuditLogsOffsetRow{
AuditLog: database.AuditLog{ResourceType: database.ResourceTypeChat, ResourceID: chatID},
})
require.Equal(t, tc.wantDeleted, deleted)
})
}
}
func TestAuditLogDescription(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
alog database.GetAuditLogsOffsetRow
want string
}{
{
name: "mainline",
alog: database.GetAuditLogsOffsetRow{
AuditLog: database.AuditLog{
Action: database.AuditActionCreate,
StatusCode: 200,
ResourceType: database.ResourceTypeWorkspace,
},
},
want: "{user} created workspace {target}",
},
{
name: "unsuccessful",
alog: database.GetAuditLogsOffsetRow{
AuditLog: database.AuditLog{
Action: database.AuditActionCreate,
StatusCode: 400,
ResourceType: database.ResourceTypeWorkspace,
},
},
want: "{user} unsuccessfully attempted to create workspace {target}",
},
{
name: "login",
alog: database.GetAuditLogsOffsetRow{
AuditLog: database.AuditLog{
Action: database.AuditActionLogin,
StatusCode: 200,
ResourceType: database.ResourceTypeApiKey,
},
},
want: "{user} logged in",
},
{
name: "unsuccessful_login",
alog: database.GetAuditLogsOffsetRow{
AuditLog: database.AuditLog{
Action: database.AuditActionLogin,
StatusCode: 401,
ResourceType: database.ResourceTypeApiKey,
},
},
want: "{user} unsuccessfully attempted to login",
},
{
name: "gitsshkey",
alog: database.GetAuditLogsOffsetRow{
AuditLog: database.AuditLog{
Action: database.AuditActionDelete,
StatusCode: 200,
ResourceType: database.ResourceTypeGitSshKey,
},
},
want: "{user} deleted the git ssh key",
},
{
name: "chat_archived",
alog: chatAuditLogRow(t, codersdk.AuditDiff{
"archived": {Old: false, New: true},
}),
want: "{user} archived chat {target}",
},
{
name: "chat_unarchived",
alog: chatAuditLogRow(t, codersdk.AuditDiff{
"archived": {Old: true, New: false},
}),
want: "{user} unarchived chat {target}",
},
{
name: "chat_sharing_user_acl",
alog: chatAuditLogRow(t, codersdk.AuditDiff{
"user_acl": {Old: map[string]any{}, New: map[string]any{"user-1": map[string]any{"permissions": []string{"read"}}}},
}),
want: "{user} updated sharing for chat {target}",
},
{
name: "chat_sharing_group_acl",
alog: chatAuditLogRow(t, codersdk.AuditDiff{
"group_acl": {Old: map[string]any{}, New: map[string]any{"group-1": map[string]any{"permissions": []string{"read"}}}},
}),
want: "{user} updated sharing for chat {target}",
},
{
name: "chat_sharing_both_acls",
alog: chatAuditLogRow(t, codersdk.AuditDiff{
"user_acl": {Old: map[string]any{}, New: map[string]any{"user-1": map[string]any{"permissions": []string{"read"}}}},
"group_acl": {Old: map[string]any{}, New: map[string]any{"group-1": map[string]any{"permissions": []string{"read"}}}},
}),
want: "{user} updated sharing for chat {target}",
},
{
name: "chat_mixed_diff_falls_through",
alog: chatAuditLogRow(t, codersdk.AuditDiff{
"archived": {Old: false, New: true},
"pin_order": {Old: 1, New: 0},
}),
want: "{user} updated chat {target}",
},
{
name: "chat_acl_with_extra_field_falls_through",
alog: chatAuditLogRow(t, codersdk.AuditDiff{
"user_acl": {Old: map[string]any{}, New: map[string]any{}},
"pin_order": {Old: 1, New: 0},
}),
want: "{user} updated chat {target}",
},
{
name: "chat_failed_write_no_override",
alog: func() database.GetAuditLogsOffsetRow {
row := chatAuditLogRow(t, codersdk.AuditDiff{
"archived": {Old: false, New: true},
})
row.AuditLog.StatusCode = 400
return row
}(),
want: "{user} unsuccessfully attempted to write chat {target}",
},
{
name: "chat_redirect_no_override",
alog: func() database.GetAuditLogsOffsetRow {
row := chatAuditLogRow(t, codersdk.AuditDiff{
"archived": {Old: false, New: true},
})
row.AuditLog.StatusCode = 303
return row
}(),
want: "{user} was redirected attempting to write chat {target}",
},
{
name: "chat_non_write_action_no_override",
alog: func() database.GetAuditLogsOffsetRow {
row := chatAuditLogRow(t, codersdk.AuditDiff{
"user_acl": {Old: map[string]any{}, New: map[string]any{"user-1": map[string]any{"permissions": []string{"read"}}}},
})
row.AuditLog.Action = database.AuditActionCreate
return row
}(),
want: "{user} created chat {target}",
},
}
// nolint: paralleltest // no longer need to reinitialize loop vars in go 1.22
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
got := auditLogDescription(tc.alog)
require.Equal(t, tc.want, got)
})
}
}
// chatAuditLogRow builds a GetAuditLogsOffsetRow for a successful chat write
// with the given diff, suitable for testing auditLogDescription.
func chatAuditLogRow(t *testing.T, diff codersdk.AuditDiff) database.GetAuditLogsOffsetRow {
t.Helper()
rawDiff, err := json.Marshal(diff)
require.NoError(t, err)
return database.GetAuditLogsOffsetRow{
AuditLog: database.AuditLog{
Action: database.AuditActionWrite,
StatusCode: 200,
ResourceType: database.ResourceTypeChat,
Diff: rawDiff,
},
}
}