1
1
package audit_test
2
2
3
3
import (
4
+ "database/sql"
5
+ "reflect"
4
6
"testing"
7
+ "time"
5
8
9
+ "github.com/google/uuid"
6
10
"github.com/stretchr/testify/require"
7
11
8
12
"github.com/coder/coder/coderd/audit"
@@ -12,30 +16,193 @@ import (
12
16
func TestDiff (t * testing.T ) {
13
17
t .Parallel ()
14
18
15
- t .Run ("Normal" , func (t * testing.T ) {
16
- t .Parallel ()
17
-
18
- runDiffTests (t , []diffTest [database.User ]{
19
- {
20
- name : "LeftEmpty" ,
21
- left : audit .Empty [database.User ](), right : database.User {Username : "colin" , Email : "colin@coder.com" },
22
- exp : audit.Map {
23
- "email" : "colin@coder.com" ,
24
- },
25
- },
26
- {
27
- name : "RightEmpty" ,
28
- left : database.User {Username : "colin" , Email : "colin@coder.com" }, right : audit .Empty [database.User ](),
29
- exp : audit.Map {
30
- "email" : "" ,
31
- },
32
- },
33
- {
34
- name : "NoChange" ,
35
- left : audit .Empty [database.User ](), right : audit .Empty [database.User ](),
36
- exp : audit.Map {},
19
+ runDiffTests (t , []diffTest [database.GitSSHKey ]{
20
+ {
21
+ name : "Create" ,
22
+ left : audit .Empty [database.GitSSHKey ](),
23
+ right : database.GitSSHKey {
24
+ UserID : uuid.UUID {1 },
25
+ CreatedAt : time .Now (),
26
+ UpdatedAt : time .Now (),
27
+ PrivateKey : "a very secret private key" ,
28
+ PublicKey : "a very public public key" ,
37
29
},
38
- })
30
+ exp : audit.Map {
31
+ "user_id" : uuid.UUID {1 }.String (),
32
+ "private_key" : "" ,
33
+ "public_key" : "a very public public key" ,
34
+ },
35
+ },
36
+ })
37
+
38
+ runDiffTests (t , []diffTest [database.OrganizationMember ]{
39
+ {
40
+ name : "Create" ,
41
+ left : audit .Empty [database.OrganizationMember ](),
42
+ right : database.OrganizationMember {
43
+ UserID : uuid.UUID {1 },
44
+ OrganizationID : uuid.UUID {2 },
45
+ CreatedAt : time .Now (),
46
+ UpdatedAt : time .Now (),
47
+ Roles : []string {"auditor" },
48
+ },
49
+ exp : audit.Map {
50
+ "user_id" : uuid.UUID {1 }.String (),
51
+ "organization_id" : uuid.UUID {2 }.String (),
52
+ "roles" : []string {"auditor" },
53
+ },
54
+ },
55
+ })
56
+
57
+ runDiffTests (t , []diffTest [database.Organization ]{
58
+ {
59
+ name : "Create" ,
60
+ left : audit .Empty [database.Organization ](),
61
+ right : database.Organization {
62
+ ID : uuid.UUID {1 },
63
+ Name : "rust developers" ,
64
+ Description : "an organization for rust developers" ,
65
+ CreatedAt : time .Now (),
66
+ UpdatedAt : time .Now (),
67
+ },
68
+ exp : audit.Map {
69
+ "id" : uuid.UUID {1 }.String (),
70
+ "name" : "rust developers" ,
71
+ "description" : "an organization for rust developers" ,
72
+ },
73
+ },
74
+ })
75
+
76
+ runDiffTests (t , []diffTest [database.Template ]{
77
+ {
78
+ name : "Create" ,
79
+ left : audit .Empty [database.Template ](),
80
+ right : database.Template {
81
+ ID : uuid.UUID {1 },
82
+ CreatedAt : time .Now (),
83
+ UpdatedAt : time .Now (),
84
+ OrganizationID : uuid.UUID {2 },
85
+ Deleted : false ,
86
+ Name : "rust" ,
87
+ Provisioner : database .ProvisionerTypeTerraform ,
88
+ ActiveVersionID : uuid.UUID {3 },
89
+ },
90
+ exp : audit.Map {
91
+ "id" : uuid.UUID {1 }.String (),
92
+ "organization_id" : uuid.UUID {2 }.String (),
93
+ "name" : "rust" ,
94
+ "provisioner" : database .ProvisionerTypeTerraform ,
95
+ "active_version_id" : uuid.UUID {3 }.String (),
96
+ },
97
+ },
98
+ })
99
+
100
+ runDiffTests (t , []diffTest [database.TemplateVersion ]{
101
+ {
102
+ name : "Create" ,
103
+ left : audit .Empty [database.TemplateVersion ](),
104
+ right : database.TemplateVersion {
105
+ ID : uuid.UUID {1 },
106
+ TemplateID : uuid.NullUUID {UUID : uuid.UUID {2 }, Valid : true },
107
+ CreatedAt : time .Now (),
108
+ UpdatedAt : time .Now (),
109
+ OrganizationID : uuid.UUID {3 },
110
+ Name : "rust" ,
111
+ },
112
+ exp : audit.Map {
113
+ "id" : uuid.UUID {1 }.String (),
114
+ "template_id" : uuid.UUID {2 }.String (),
115
+ "organization_id" : uuid.UUID {3 }.String (),
116
+ "name" : "rust" ,
117
+ },
118
+ },
119
+ {
120
+ name : "CreateNullTemplateID" ,
121
+ left : audit .Empty [database.TemplateVersion ](),
122
+ right : database.TemplateVersion {
123
+ ID : uuid.UUID {1 },
124
+ TemplateID : uuid.NullUUID {},
125
+ CreatedAt : time .Now (),
126
+ UpdatedAt : time .Now (),
127
+ OrganizationID : uuid.UUID {3 },
128
+ Name : "rust" ,
129
+ },
130
+ exp : audit.Map {
131
+ "id" : uuid.UUID {1 }.String (),
132
+ "organization_id" : uuid.UUID {3 }.String (),
133
+ "name" : "rust" ,
134
+ },
135
+ },
136
+ })
137
+
138
+ runDiffTests (t , []diffTest [database.User ]{
139
+ {
140
+ name : "Create" ,
141
+ left : audit .Empty [database.User ](),
142
+ right : database.User {
143
+ ID : uuid.UUID {1 },
144
+ Email : "colin@coder.com" ,
145
+ Username : "colin" ,
146
+ HashedPassword : []byte ("hunter2ButHashed" ),
147
+ CreatedAt : time .Now (),
148
+ UpdatedAt : time .Now (),
149
+ Status : database .UserStatusActive ,
150
+ RBACRoles : []string {"omega admin" },
151
+ },
152
+ exp : audit.Map {
153
+ "id" : uuid.UUID {1 }.String (),
154
+ "email" : "colin@coder.com" ,
155
+ "username" : "colin" ,
156
+ "hashed_password" : ([]byte )(nil ),
157
+ "status" : database .UserStatusActive ,
158
+ "rbac_roles" : []string {"omega admin" },
159
+ },
160
+ },
161
+ })
162
+
163
+ runDiffTests (t , []diffTest [database.Workspace ]{
164
+ {
165
+ name : "Create" ,
166
+ left : audit .Empty [database.Workspace ](),
167
+ right : database.Workspace {
168
+ ID : uuid.UUID {1 },
169
+ CreatedAt : time .Now (),
170
+ UpdatedAt : time .Now (),
171
+ OwnerID : uuid.UUID {2 },
172
+ TemplateID : uuid.UUID {3 },
173
+ Name : "rust workspace" ,
174
+ AutostartSchedule : sql.NullString {String : "0 12 * * 1-5" , Valid : true },
175
+ AutostopSchedule : sql.NullString {String : "0 2 * * 2-6" , Valid : true },
176
+ },
177
+ exp : audit.Map {
178
+ "id" : uuid.UUID {1 }.String (),
179
+ "owner_id" : uuid.UUID {2 }.String (),
180
+ "template_id" : uuid.UUID {3 }.String (),
181
+ "name" : "rust workspace" ,
182
+ "autostart_schedule" : "0 12 * * 1-5" ,
183
+ "autostop_schedule" : "0 2 * * 2-6" ,
184
+ },
185
+ },
186
+ {
187
+ name : "NullSchedules" ,
188
+ left : audit .Empty [database.Workspace ](),
189
+ right : database.Workspace {
190
+ ID : uuid.UUID {1 },
191
+ CreatedAt : time .Now (),
192
+ UpdatedAt : time .Now (),
193
+ OwnerID : uuid.UUID {2 },
194
+ TemplateID : uuid.UUID {3 },
195
+ Name : "rust workspace" ,
196
+ AutostartSchedule : sql.NullString {},
197
+ AutostopSchedule : sql.NullString {},
198
+ },
199
+ exp : audit.Map {
200
+ "id" : uuid.UUID {1 }.String (),
201
+ "owner_id" : uuid.UUID {2 }.String (),
202
+ "template_id" : uuid.UUID {3 }.String (),
203
+ "name" : "rust workspace" ,
204
+ },
205
+ },
39
206
})
40
207
}
41
208
@@ -48,8 +215,11 @@ type diffTest[T audit.Auditable] struct {
48
215
func runDiffTests [T audit.Auditable ](t * testing.T , tests []diffTest [T ]) {
49
216
t .Helper ()
50
217
218
+ var typ T
219
+ typName := reflect .TypeOf (typ ).Name ()
220
+
51
221
for _ , test := range tests {
52
- t .Run (test .name , func (t * testing.T ) {
222
+ t .Run (typName + "/" + test .name , func (t * testing.T ) {
53
223
require .Equal (t ,
54
224
test .exp ,
55
225
audit .Diff (test .left , test .right ),
0 commit comments