Skip to content

Commit bfa88b7

Browse files
committed
Refactor tests to remove direct database setup
Refactored various tests to eliminate the need for manually setting up a database connection and generating crypto keys. This change simplifies the test setup and makes it less dependent on database-specific configurations, aligning with recent structural refactoring.
1 parent a3020fc commit bfa88b7

File tree

6 files changed

+5
-46
lines changed

6 files changed

+5
-46
lines changed

coderd/workspaceapps/db_test.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ import (
2020

2121
"github.com/coder/coder/v2/agent/agenttest"
2222
"github.com/coder/coder/v2/coderd/coderdtest"
23-
"github.com/coder/coder/v2/coderd/database"
24-
"github.com/coder/coder/v2/coderd/database/dbgen"
25-
"github.com/coder/coder/v2/coderd/database/dbtestutil"
2623
"github.com/coder/coder/v2/coderd/httpmw"
2724
"github.com/coder/coder/v2/coderd/jwtutils"
2825
"github.com/coder/coder/v2/coderd/workspaceapps"
@@ -79,7 +76,6 @@ func Test_ResolveRequest(t *testing.T) {
7976
deploymentValues.Dangerous.AllowPathAppSharing = true
8077
deploymentValues.Dangerous.AllowPathAppSiteOwnerAccess = true
8178

82-
db, pubsub := dbtestutil.NewDB(t)
8379
client, closer, api := coderdtest.NewWithAPI(t, &coderdtest.Options{
8480
AppHostname: "*.test.coder.com",
8581
DeploymentValues: deploymentValues,
@@ -95,17 +91,11 @@ func Test_ResolveRequest(t *testing.T) {
9591
"CF-Connecting-IP",
9692
},
9793
},
98-
Database: db,
99-
Pubsub: pubsub,
10094
})
10195
t.Cleanup(func() {
10296
_ = closer.Close()
10397
})
10498

105-
_ = dbgen.CryptoKey(t, db, database.CryptoKey{
106-
Feature: database.CryptoKeyFeatureWorkspaceAppsToken,
107-
})
108-
10999
ctx := testutil.Context(t, testutil.WaitMedium)
110100

111101
firstUser := coderdtest.CreateFirstUser(t, client)

coderd/workspaceapps/token_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ func Test_FromRequest(t *testing.T) {
301301
signer := newSigner(t)
302302

303303
token := workspaceapps.SignedToken{
304-
Claims: jwt.Claims{
304+
RegisteredClaims: jwtutils.RegisteredClaims{
305305
Expiry: jwt.NewNumericDate(time.Now().Add(time.Hour)),
306306
},
307307
Request: workspaceapps.Request{
@@ -321,7 +321,7 @@ func Test_FromRequest(t *testing.T) {
321321

322322
// Add an expired cookie
323323
expired := token
324-
expired.Claims.Expiry = jwt.NewNumericDate(time.Now().Add(time.Hour * -1))
324+
expired.RegisteredClaims.Expiry = jwt.NewNumericDate(time.Now().Add(time.Hour * -1))
325325
expiredStr, err := jwtutils.Sign(ctx, signer, expired)
326326
require.NoError(t, err)
327327
r.AddCookie(&http.Cookie{

enterprise/workspaceapps_test.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import (
55
"testing"
66

77
"github.com/coder/coder/v2/coderd/coderdtest"
8-
"github.com/coder/coder/v2/coderd/database"
9-
"github.com/coder/coder/v2/coderd/database/dbgen"
108
"github.com/coder/coder/v2/coderd/database/dbtestutil"
119
"github.com/coder/coder/v2/coderd/httpmw"
1210
"github.com/coder/coder/v2/coderd/workspaceapps/apptest"
@@ -67,13 +65,6 @@ func TestWorkspaceApps(t *testing.T) {
6765
},
6866
})
6967

70-
_ = dbgen.CryptoKey(t, db, database.CryptoKey{
71-
Feature: database.CryptoKeyFeatureWorkspaceAppsToken,
72-
})
73-
_ = dbgen.CryptoKey(t, db, database.CryptoKey{
74-
Feature: database.CryptoKeyFeatureWorkspaceAppsAPIKey,
75-
})
76-
7768
return &apptest.Deployment{
7869
Options: opts,
7970
SDKClient: client,

scaletest/createworkspaces/run_test.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ import (
1515
"cdr.dev/slog/sloggers/slogtest"
1616
"github.com/coder/coder/v2/agent"
1717
"github.com/coder/coder/v2/coderd/coderdtest"
18-
"github.com/coder/coder/v2/coderd/database"
19-
"github.com/coder/coder/v2/coderd/database/dbgen"
2018
"github.com/coder/coder/v2/coderd/httpapi"
2119
"github.com/coder/coder/v2/coderd/util/ptr"
2220
"github.com/coder/coder/v2/codersdk"
@@ -57,12 +55,9 @@ func Test_Runner(t *testing.T) {
5755
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
5856
defer cancel()
5957

60-
client, db := coderdtest.NewWithDatabase(t, &coderdtest.Options{
58+
client := coderdtest.New(t, &coderdtest.Options{
6159
IncludeProvisionerDaemon: true,
6260
})
63-
_ = dbgen.CryptoKey(t, db, database.CryptoKey{
64-
Feature: database.CryptoKeyFeatureWorkspaceAppsToken,
65-
})
6661
user := coderdtest.CreateFirstUser(t, client)
6762

6863
authToken := uuid.NewString()
@@ -348,12 +343,9 @@ func Test_Runner(t *testing.T) {
348343
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
349344
defer cancel()
350345

351-
client, db := coderdtest.NewWithDatabase(t, &coderdtest.Options{
346+
client := coderdtest.New(t, &coderdtest.Options{
352347
IncludeProvisionerDaemon: true,
353348
})
354-
_ = dbgen.CryptoKey(t, db, database.CryptoKey{
355-
Feature: database.CryptoKeyFeatureWorkspaceAppsToken,
356-
})
357349
user := coderdtest.CreateFirstUser(t, client)
358350

359351
authToken := uuid.NewString()

scaletest/reconnectingpty/run_test.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ import (
1212

1313
"github.com/coder/coder/v2/agent/agenttest"
1414
"github.com/coder/coder/v2/coderd/coderdtest"
15-
"github.com/coder/coder/v2/coderd/database"
16-
"github.com/coder/coder/v2/coderd/database/dbgen"
17-
"github.com/coder/coder/v2/coderd/database/dbtestutil"
1815
"github.com/coder/coder/v2/coderd/httpapi"
1916
"github.com/coder/coder/v2/codersdk"
2017
"github.com/coder/coder/v2/codersdk/workspacesdk"
@@ -251,15 +248,9 @@ func Test_Runner(t *testing.T) {
251248
func setupRunnerTest(t *testing.T) (client *codersdk.Client, agentID uuid.UUID) {
252249
t.Helper()
253250

254-
db, pubsub := dbtestutil.NewDB(t)
255251
client, _, api := coderdtest.NewWithAPI(t, &coderdtest.Options{
256-
Database: db,
257-
Pubsub: pubsub,
258252
IncludeProvisionerDaemon: true,
259253
})
260-
_ = dbgen.CryptoKey(t, db, database.CryptoKey{
261-
Feature: database.CryptoKeyFeatureWorkspaceAppsToken,
262-
})
263254
user := coderdtest.CreateFirstUser(t, client)
264255

265256
authToken := uuid.NewString()

scaletest/workspacetraffic/run_test.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ import (
1717

1818
"github.com/coder/coder/v2/agent/agenttest"
1919
"github.com/coder/coder/v2/coderd/coderdtest"
20-
"github.com/coder/coder/v2/coderd/database"
21-
"github.com/coder/coder/v2/coderd/database/dbgen"
2220
"github.com/coder/coder/v2/codersdk"
2321
"github.com/coder/coder/v2/provisioner/echo"
2422
"github.com/coder/coder/v2/provisionersdk/proto"
@@ -44,10 +42,7 @@ func TestRun(t *testing.T) {
4442
t.Parallel()
4543
// We need to stand up an in-memory coderd and run a fake workspace.
4644
var (
47-
client, db = coderdtest.NewWithDatabase(t, &coderdtest.Options{IncludeProvisionerDaemon: true})
48-
_ = dbgen.CryptoKey(t, db, database.CryptoKey{
49-
Feature: database.CryptoKeyFeatureWorkspaceAppsToken,
50-
})
45+
client = coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true})
5146
firstUser = coderdtest.CreateFirstUser(t, client)
5247
authToken = uuid.NewString()
5348
agentName = "agent"

0 commit comments

Comments
 (0)