@@ -2,10 +2,16 @@ package databasefake
2
2
3
3
import (
4
4
"context"
5
+ "crypto/sha256"
5
6
"database/sql"
7
+ "fmt"
6
8
"testing"
7
9
"time"
8
10
11
+ "github.com/coder/coder/cryptorand"
12
+
13
+ "github.com/tabbed/pqtype"
14
+
9
15
"github.com/coder/coder/coderd/database"
10
16
"github.com/google/uuid"
11
17
"github.com/moby/moby/pkg/namesgenerator"
@@ -52,10 +58,42 @@ func (g *Generator) PrimaryOrg(ctx context.Context) database.Organization {
52
58
}
53
59
54
60
func populate [DBType any ](ctx context.Context , g * Generator , name string , seed DBType ) DBType {
61
+ if name == "" {
62
+ name = g .RandomName ()
63
+ }
64
+
55
65
out := g .Populate (ctx , map [string ]interface {}{
56
66
name : seed ,
57
67
})
58
- return out [name ].(DBType )
68
+ v , ok := out [name ].(DBType )
69
+ if ! ok {
70
+ panic ("developer error, type mismatch" )
71
+ }
72
+ return v
73
+ }
74
+
75
+ func (g * Generator ) RandomName () string {
76
+ for {
77
+ name := namesgenerator .GetRandomName (0 )
78
+ if _ , ok := g .names [name ]; ! ok {
79
+ return name
80
+ }
81
+ }
82
+ }
83
+
84
+ func (g * Generator ) APIKey (ctx context.Context , name string , seed database.APIKey ) (key database.APIKey , token string ) {
85
+ if name == "" {
86
+ name = g .RandomName ()
87
+ }
88
+
89
+ out := g .Populate (ctx , map [string ]interface {}{
90
+ name : seed ,
91
+ })
92
+ key , keyOk := out [name ].(database.APIKey )
93
+ secret , secOk := out [name + "_secret" ].(string )
94
+ require .True (g .testT , keyOk && secOk , "APIKey & secret must be populated with the right type" )
95
+
96
+ return key , fmt .Sprintf ("%s-%s" , key .ID , secret )
59
97
}
60
98
61
99
func (g * Generator ) WorkspaceResource (ctx context.Context , name string , seed database.WorkspaceResource ) database.WorkspaceResource {
@@ -100,11 +138,34 @@ func (g *Generator) Populate(ctx context.Context, seed map[string]interface{}) m
100
138
101
139
for name , v := range seed {
102
140
switch orig := v .(type ) {
141
+ case database.APIKey :
142
+ id , _ := cryptorand .String (10 )
143
+ secret , _ := cryptorand .String (22 )
144
+ hashed := sha256 .Sum256 ([]byte (secret ))
145
+
146
+ key , err := db .InsertAPIKey (ctx , database.InsertAPIKeyParams {
147
+ ID : takeFirst (orig .ID , id ),
148
+ LifetimeSeconds : takeFirst (orig .LifetimeSeconds , 3600 ),
149
+ HashedSecret : takeFirstBytes (orig .HashedSecret , hashed [:]),
150
+ IPAddress : pqtype.Inet {},
151
+ UserID : takeFirst (orig .UserID , uuid .New ()),
152
+ LastUsed : takeFirstTime (orig .LastUsed , time .Now ()),
153
+ ExpiresAt : takeFirstTime (orig .ExpiresAt , time .Now ().Add (time .Hour )),
154
+ CreatedAt : takeFirstTime (orig .CreatedAt , time .Now ()),
155
+ UpdatedAt : takeFirstTime (orig .UpdatedAt , time .Now ()),
156
+ LoginType : takeFirst (orig .LoginType , database .LoginTypePassword ),
157
+ Scope : takeFirst (orig .Scope , database .APIKeyScopeAll ),
158
+ })
159
+ require .NoError (t , err , "insert api key" )
160
+
161
+ seed [name ] = key
162
+ // Need to also save the secret
163
+ seed [name + "_secret" ] = secret
103
164
case database.Template :
104
165
template , err := db .InsertTemplate (ctx , database.InsertTemplateParams {
105
166
ID : g .Lookup (name ),
106
167
CreatedAt : takeFirstTime (orig .CreatedAt , time .Now ()),
107
- UpdatedAt : takeFirstTime (orig .CreatedAt , time .Now ()),
168
+ UpdatedAt : takeFirstTime (orig .UpdatedAt , time .Now ()),
108
169
OrganizationID : takeFirst (orig .OrganizationID , g .PrimaryOrg (ctx ).ID ),
109
170
Name : takeFirst (orig .Name , namesgenerator .GetRandomName (1 )),
110
171
Provisioner : takeFirst (orig .Provisioner , database .ProvisionerTypeEcho ),
@@ -125,7 +186,7 @@ func (g *Generator) Populate(ctx context.Context, seed map[string]interface{}) m
125
186
workspace , err := db .InsertWorkspace (ctx , database.InsertWorkspaceParams {
126
187
ID : g .Lookup (name ),
127
188
CreatedAt : takeFirstTime (orig .CreatedAt , time .Now ()),
128
- UpdatedAt : takeFirstTime (orig .CreatedAt , time .Now ()),
189
+ UpdatedAt : takeFirstTime (orig .UpdatedAt , time .Now ()),
129
190
OrganizationID : takeFirst (orig .OrganizationID , g .PrimaryOrg (ctx ).ID ),
130
191
TemplateID : takeFirst (orig .TemplateID , uuid .New ()),
131
192
Name : takeFirst (orig .Name , namesgenerator .GetRandomName (1 )),
@@ -139,15 +200,15 @@ func (g *Generator) Populate(ctx context.Context, seed map[string]interface{}) m
139
200
build , err := db .InsertWorkspaceBuild (ctx , database.InsertWorkspaceBuildParams {
140
201
ID : g .Lookup (name ),
141
202
CreatedAt : takeFirstTime (orig .CreatedAt , time .Now ()),
142
- UpdatedAt : takeFirstTime (orig .CreatedAt , time .Now ()),
203
+ UpdatedAt : takeFirstTime (orig .UpdatedAt , time .Now ()),
143
204
WorkspaceID : takeFirst (orig .WorkspaceID , uuid .New ()),
144
205
TemplateVersionID : takeFirst (orig .TemplateVersionID , uuid .New ()),
145
206
BuildNumber : takeFirst (orig .BuildNumber , 0 ),
146
207
Transition : takeFirst (orig .Transition , database .WorkspaceTransitionStart ),
147
208
InitiatorID : takeFirst (orig .InitiatorID , uuid .New ()),
148
209
JobID : takeFirst (orig .JobID , uuid .New ()),
149
210
ProvisionerState : takeFirstBytes (orig .ProvisionerState , []byte {}),
150
- Deadline : takeFirstTime (orig .CreatedAt , time .Now ().Add (time .Hour )),
211
+ Deadline : takeFirstTime (orig .Deadline , time .Now ().Add (time .Hour )),
151
212
Reason : takeFirst (orig .Reason , database .BuildReasonInitiator ),
152
213
})
153
214
require .NoError (t , err , "insert workspace build" )
@@ -160,7 +221,7 @@ func (g *Generator) Populate(ctx context.Context, seed map[string]interface{}) m
160
221
Username : takeFirst (orig .Username , namesgenerator .GetRandomName (1 )),
161
222
HashedPassword : takeFirstBytes (orig .HashedPassword , []byte {}),
162
223
CreatedAt : takeFirstTime (orig .CreatedAt , time .Now ()),
163
- UpdatedAt : takeFirstTime (orig .CreatedAt , time .Now ()),
224
+ UpdatedAt : takeFirstTime (orig .UpdatedAt , time .Now ()),
164
225
RBACRoles : []string {},
165
226
LoginType : takeFirst (orig .LoginType , database .LoginTypePassword ),
166
227
})
@@ -172,9 +233,9 @@ func (g *Generator) Populate(ctx context.Context, seed map[string]interface{}) m
172
233
org , err := db .InsertOrganization (ctx , database.InsertOrganizationParams {
173
234
ID : g .Lookup (name ),
174
235
Name : takeFirst (orig .Name , namesgenerator .GetRandomName (1 )),
175
- Description : takeFirst (orig .Name , namesgenerator .GetRandomName (1 )),
236
+ Description : takeFirst (orig .Description , namesgenerator .GetRandomName (1 )),
176
237
CreatedAt : takeFirstTime (orig .CreatedAt , time .Now ()),
177
- UpdatedAt : takeFirstTime (orig .CreatedAt , time .Now ()),
238
+ UpdatedAt : takeFirstTime (orig .UpdatedAt , time .Now ()),
178
239
})
179
240
require .NoError (t , err , "insert organization" )
180
241
@@ -185,7 +246,7 @@ func (g *Generator) Populate(ctx context.Context, seed map[string]interface{}) m
185
246
ID : g .Lookup (name ),
186
247
Name : takeFirst (orig .Name , namesgenerator .GetRandomName (1 )),
187
248
OrganizationID : takeFirst (orig .OrganizationID , g .PrimaryOrg (ctx ).ID ),
188
- AvatarURL : takeFirst (orig .Name , "https://logo.example.com" ),
249
+ AvatarURL : takeFirst (orig .AvatarURL , "https://logo.example.com" ),
189
250
QuotaAllowance : takeFirst (orig .QuotaAllowance , 0 ),
190
251
})
191
252
require .NoError (t , err , "insert organization" )
@@ -196,7 +257,7 @@ func (g *Generator) Populate(ctx context.Context, seed map[string]interface{}) m
196
257
job , err := db .InsertProvisionerJob (ctx , database.InsertProvisionerJobParams {
197
258
ID : g .Lookup (name ),
198
259
CreatedAt : takeFirstTime (orig .CreatedAt , time .Now ()),
199
- UpdatedAt : takeFirstTime (orig .CreatedAt , time .Now ()),
260
+ UpdatedAt : takeFirstTime (orig .UpdatedAt , time .Now ()),
200
261
OrganizationID : takeFirst (orig .OrganizationID , g .PrimaryOrg (ctx ).ID ),
201
262
InitiatorID : takeFirst (orig .InitiatorID , uuid .New ()),
202
263
Provisioner : takeFirst (orig .Provisioner , database .ProvisionerTypeEcho ),
@@ -220,7 +281,7 @@ func (g *Generator) Populate(ctx context.Context, seed map[string]interface{}) m
220
281
Type : takeFirst (orig .Type , "" ),
221
282
Name : takeFirst (orig .Name , namesgenerator .GetRandomName (1 )),
222
283
Hide : takeFirst (orig .Hide , false ),
223
- Icon : takeFirst (orig .Name , "" ),
284
+ Icon : takeFirst (orig .Icon , "" ),
224
285
InstanceType : sql.NullString {
225
286
String : takeFirst (orig .InstanceType .String , "" ),
226
287
Valid : takeFirst (orig .InstanceType .Valid , false ),
0 commit comments