Skip to content

Commit 36c2004

Browse files
committed
chore: more review comments
1 parent b60e546 commit 36c2004

6 files changed

Lines changed: 87 additions & 88 deletions

File tree

internal/controller/oidc_controller_test.go

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,24 @@ import (
44
"encoding/json"
55
"net/http/httptest"
66
"net/url"
7-
"os"
7+
"path"
88
"strings"
99
"testing"
1010

1111
"github.com/gin-gonic/gin"
12+
"github.com/google/go-querystring/query"
1213
"github.com/steveiliop56/tinyauth/internal/bootstrap"
1314
"github.com/steveiliop56/tinyauth/internal/config"
1415
"github.com/steveiliop56/tinyauth/internal/controller"
1516
"github.com/steveiliop56/tinyauth/internal/repository"
1617
"github.com/steveiliop56/tinyauth/internal/service"
1718
"github.com/stretchr/testify/assert"
19+
"github.com/stretchr/testify/require"
1820
)
1921

2022
func TestOIDCController(t *testing.T) {
23+
tempDir := t.TempDir()
24+
2125
oidcServiceCfg := service.OIDCServiceConfig{
2226
Clients: map[string]config.OIDCClientConfig{
2327
"test": {
@@ -27,8 +31,8 @@ func TestOIDCController(t *testing.T) {
2731
Name: "Test Client",
2832
},
2933
},
30-
PrivateKeyPath: "/tmp/tinyauth_testing_key.pem",
31-
PublicKeyPath: "/tmp/tinyauth_testing_key.pub",
34+
PrivateKeyPath: path.Join(tempDir, "key.pem"),
35+
PublicKeyPath: path.Join(tempDir, "key.pub"),
3236
Issuer: "https://tinyauth.example.com",
3337
SessionExpiry: 500,
3438
}
@@ -170,11 +174,11 @@ func TestOIDCController(t *testing.T) {
170174
Code: "",
171175
RedirectURI: "https://test.example.com/callback",
172176
}
173-
reqBodyBytes, err := json.Marshal(reqBody)
177+
reqBodyEncoded, err := query.Values(reqBody)
174178
assert.NoError(t, err)
175179

176-
req := httptest.NewRequest("POST", "/api/oidc/token", strings.NewReader(string(reqBodyBytes)))
177-
req.Header.Set("Content-Type", "application/json")
180+
req := httptest.NewRequest("POST", "/api/oidc/token", strings.NewReader(reqBodyEncoded.Encode()))
181+
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
178182
router.ServeHTTP(recorder, req)
179183

180184
var res map[string]any
@@ -193,11 +197,11 @@ func TestOIDCController(t *testing.T) {
193197
Code: "some-code",
194198
RedirectURI: "https://test.example.com/callback",
195199
}
196-
reqBodyBytes, err := json.Marshal(reqBody)
200+
reqBodyEncoded, err := query.Values(reqBody)
197201
assert.NoError(t, err)
198202

199-
req := httptest.NewRequest("POST", "/api/oidc/token", strings.NewReader(string(reqBodyBytes)))
200-
req.Header.Set("Content-Type", "application/json")
203+
req := httptest.NewRequest("POST", "/api/oidc/token", strings.NewReader(reqBodyEncoded.Encode()))
204+
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
201205
req.SetBasicAuth("some-client-id", "some-client-secret")
202206
router.ServeHTTP(recorder, req)
203207

@@ -231,11 +235,11 @@ func TestOIDCController(t *testing.T) {
231235
Code: "some-code",
232236
RedirectURI: "https://test.example.com/callback",
233237
}
234-
reqBodyBytes, err := json.Marshal(reqBody)
238+
reqBodyEncoded, err := query.Values(reqBody)
235239
assert.NoError(t, err)
236240

237-
req := httptest.NewRequest("POST", "/api/oidc/token", strings.NewReader(string(reqBodyBytes)))
238-
req.Header.Set("Content-Type", "application/json")
241+
req := httptest.NewRequest("POST", "/api/oidc/token", strings.NewReader(reqBodyEncoded.Encode()))
242+
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
239243
router.ServeHTTP(recorder, req)
240244

241245
authHeader := recorder.Header().Get("www-authenticate")
@@ -270,11 +274,11 @@ func TestOIDCController(t *testing.T) {
270274
Code: code,
271275
RedirectURI: "https://test.example.com/callback",
272276
}
273-
reqBodyBytes, err := json.Marshal(reqBody)
277+
reqBodyEncoded, err := query.Values(reqBody)
274278
assert.NoError(t, err)
275279

276-
req := httptest.NewRequest("POST", "/api/oidc/token", strings.NewReader(string(reqBodyBytes)))
277-
req.Header.Set("Content-Type", "application/json")
280+
req := httptest.NewRequest("POST", "/api/oidc/token", strings.NewReader(reqBodyEncoded.Encode()))
281+
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
278282
req.SetBasicAuth("some-client-id", "some-client-secret")
279283
router.ServeHTTP(recorder, req)
280284

@@ -307,11 +311,11 @@ func TestOIDCController(t *testing.T) {
307311
ClientID: "some-client-id",
308312
ClientSecret: "some-client-secret",
309313
}
310-
reqBodyBytes, err := json.Marshal(reqBody)
314+
reqBodyEncoded, err := query.Values(reqBody)
311315
assert.NoError(t, err)
312316

313-
req := httptest.NewRequest("POST", "/api/oidc/token", strings.NewReader(string(reqBodyBytes)))
314-
req.Header.Set("Content-Type", "application/json")
317+
req := httptest.NewRequest("POST", "/api/oidc/token", strings.NewReader(reqBodyEncoded.Encode()))
318+
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
315319
router.ServeHTTP(recorder, req)
316320

317321
assert.NotEmpty(t, recorder.Header().Get("cache-control"))
@@ -356,19 +360,19 @@ func TestOIDCController(t *testing.T) {
356360
Code: code,
357361
RedirectURI: "https://test.example.com/callback",
358362
}
359-
reqBodyBytes, err := json.Marshal(reqBody)
363+
reqBodyEncoded, err := query.Values(reqBody)
360364
assert.NoError(t, err)
361365

362-
req := httptest.NewRequest("POST", "/api/oidc/token", strings.NewReader(string(reqBodyBytes)))
363-
req.Header.Set("Content-Type", "application/json")
366+
req := httptest.NewRequest("POST", "/api/oidc/token", strings.NewReader(reqBodyEncoded.Encode()))
367+
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
364368
req.SetBasicAuth("some-client-id", "some-client-secret")
365369
router.ServeHTTP(recorder, req)
366370

367371
assert.Equal(t, 200, recorder.Code)
368372

369373
// Try to use the same code again
370-
secondReq := httptest.NewRequest("POST", "/api/oidc/token", strings.NewReader(string(reqBodyBytes)))
371-
secondReq.Header.Set("Content-Type", "application/json")
374+
secondReq := httptest.NewRequest("POST", "/api/oidc/token", strings.NewReader(reqBodyEncoded.Encode()))
375+
secondReq.Header.Set("Content-Type", "application/x-www-form-urlencoded")
372376
secondReq.SetBasicAuth("some-client-id", "some-client-secret")
373377
secondRecorder := httptest.NewRecorder()
374378
router.ServeHTTP(secondRecorder, secondReq)
@@ -431,13 +435,13 @@ func TestOIDCController(t *testing.T) {
431435

432436
app := bootstrap.NewBootstrapApp(config.Config{})
433437

434-
db, err := app.SetupDatabase("/tmp/tinyauth_test.db")
435-
assert.NoError(t, err)
438+
db, err := app.SetupDatabase(path.Join(tempDir, "tinyauth.db"))
439+
require.NoError(t, err)
436440

437441
queries := repository.New(db)
438442
oidcService := service.NewOIDCService(oidcServiceCfg, queries)
439443
err = oidcService.Init()
440-
assert.NoError(t, err)
444+
require.NoError(t, err)
441445

442446
for _, test := range tests {
443447
t.Run(test.description, func(t *testing.T) {
@@ -459,15 +463,8 @@ func TestOIDCController(t *testing.T) {
459463
})
460464
}
461465

462-
err = db.Close()
463-
assert.NoError(t, err)
464-
465-
err = os.Remove("/tmp/tinyauth_test.db")
466-
assert.NoError(t, err)
467-
468-
err = os.Remove(oidcServiceCfg.PrivateKeyPath)
469-
assert.NoError(t, err)
470-
471-
err = os.Remove(oidcServiceCfg.PublicKeyPath)
472-
assert.NoError(t, err)
466+
t.Cleanup(func() {
467+
err = db.Close()
468+
require.NoError(t, err)
469+
})
473470
}

internal/controller/proxy_controller_test.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package controller_test
22

33
import (
44
"net/http/httptest"
5-
"os"
5+
"path"
66
"testing"
77

88
"github.com/gin-gonic/gin"
@@ -13,9 +13,12 @@ import (
1313
"github.com/steveiliop56/tinyauth/internal/service"
1414
"github.com/steveiliop56/tinyauth/internal/utils/tlog"
1515
"github.com/stretchr/testify/assert"
16+
"github.com/stretchr/testify/require"
1617
)
1718

1819
func TestProxyController(t *testing.T) {
20+
tempDir := t.TempDir()
21+
1922
authServiceCfg := service.AuthServiceConfig{
2023
Users: []config.User{
2124
{
@@ -320,26 +323,26 @@ func TestProxyController(t *testing.T) {
320323

321324
app := bootstrap.NewBootstrapApp(config.Config{})
322325

323-
db, err := app.SetupDatabase("/tmp/tinyauth_test.db")
324-
assert.NoError(t, err)
326+
db, err := app.SetupDatabase(path.Join(tempDir, "tinyauth.db"))
327+
require.NoError(t, err)
325328

326329
queries := repository.New(db)
327330

328331
docker := service.NewDockerService()
329332
err = docker.Init()
330-
assert.NoError(t, err)
333+
require.NoError(t, err)
331334

332335
ldap := service.NewLdapService(service.LdapServiceConfig{})
333336
err = ldap.Init()
334-
assert.NoError(t, err)
337+
require.NoError(t, err)
335338

336339
broker := service.NewOAuthBrokerService(oauthBrokerCfgs)
337340
err = broker.Init()
338-
assert.NoError(t, err)
341+
require.NoError(t, err)
339342

340343
authService := service.NewAuthService(authServiceCfg, docker, ldap, queries, broker)
341344
err = authService.Init()
342-
assert.NoError(t, err)
345+
require.NoError(t, err)
343346

344347
aclsService := service.NewAccessControlsService(docker, acls)
345348

@@ -363,9 +366,8 @@ func TestProxyController(t *testing.T) {
363366
})
364367
}
365368

366-
err = db.Close()
367-
assert.NoError(t, err)
368-
369-
err = os.Remove("/tmp/tinyauth_test.db")
370-
assert.NoError(t, err)
369+
t.Cleanup(func() {
370+
err = db.Close()
371+
require.NoError(t, err)
372+
})
371373
}

internal/controller/resources_controller_test.go

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,26 @@ package controller_test
33
import (
44
"net/http/httptest"
55
"os"
6+
"path"
67
"testing"
78

89
"github.com/gin-gonic/gin"
910
"github.com/steveiliop56/tinyauth/internal/controller"
1011
"github.com/stretchr/testify/assert"
12+
"github.com/stretchr/testify/require"
1113
)
1214

1315
func TestResourcesController(t *testing.T) {
16+
tempDir := t.TempDir()
17+
1418
resourcesControllerCfg := controller.ResourcesControllerConfig{
15-
Path: "/tmp/testfiles",
19+
Path: path.Join(tempDir, "resources"),
1620
Enabled: true,
1721
}
1822

23+
err := os.Mkdir(resourcesControllerCfg.Path, 0777)
24+
require.NoError(t, err)
25+
1926
type testCase struct {
2027
description string
2128
run func(t *testing.T, router *gin.Engine, recorder *httptest.ResponseRecorder)
@@ -52,16 +59,13 @@ func TestResourcesController(t *testing.T) {
5259
},
5360
}
5461

55-
err := os.MkdirAll(resourcesControllerCfg.Path, 0777)
56-
assert.NoError(t, err)
57-
5862
testFilePath := resourcesControllerCfg.Path + "/testfile.txt"
5963
err = os.WriteFile(testFilePath, []byte("This is a test file."), 0777)
60-
assert.NoError(t, err)
64+
require.NoError(t, err)
6165

62-
testFilePathParent := resourcesControllerCfg.Path + "/../somefile.txt"
66+
testFilePathParent := tempDir + "/somefile.txt"
6367
err = os.WriteFile(testFilePathParent, []byte("This file should not be accessible."), 0777)
64-
assert.NoError(t, err)
68+
require.NoError(t, err)
6569

6670
for _, test := range tests {
6771
t.Run(test.description, func(t *testing.T) {
@@ -76,13 +80,4 @@ func TestResourcesController(t *testing.T) {
7680
test.run(t, router, recorder)
7781
})
7882
}
79-
80-
err = os.Remove(testFilePath)
81-
assert.NoError(t, err)
82-
83-
err = os.Remove(testFilePathParent)
84-
assert.NoError(t, err)
85-
86-
err = os.Remove(resourcesControllerCfg.Path)
87-
assert.NoError(t, err)
8883
}

internal/controller/user_controller_test.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package controller_test
33
import (
44
"encoding/json"
55
"net/http/httptest"
6-
"os"
6+
"path"
77
"slices"
88
"strings"
99
"testing"
@@ -18,9 +18,12 @@ import (
1818
"github.com/steveiliop56/tinyauth/internal/service"
1919
"github.com/steveiliop56/tinyauth/internal/utils/tlog"
2020
"github.com/stretchr/testify/assert"
21+
"github.com/stretchr/testify/require"
2122
)
2223

2324
func TestUserController(t *testing.T) {
25+
tempDir := t.TempDir()
26+
2427
authServiceCfg := service.AuthServiceConfig{
2528
Users: []config.User{
2629
{
@@ -277,26 +280,26 @@ func TestUserController(t *testing.T) {
277280

278281
app := bootstrap.NewBootstrapApp(config.Config{})
279282

280-
db, err := app.SetupDatabase("/tmp/tinyauth_test.db")
281-
assert.NoError(t, err)
283+
db, err := app.SetupDatabase(path.Join(tempDir, "tinyauth.db"))
284+
require.NoError(t, err)
282285

283286
queries := repository.New(db)
284287

285288
docker := service.NewDockerService()
286289
err = docker.Init()
287-
assert.NoError(t, err)
290+
require.NoError(t, err)
288291

289292
ldap := service.NewLdapService(service.LdapServiceConfig{})
290293
err = ldap.Init()
291-
assert.NoError(t, err)
294+
require.NoError(t, err)
292295

293296
broker := service.NewOAuthBrokerService(oauthBrokerCfgs)
294297
err = broker.Init()
295-
assert.NoError(t, err)
298+
require.NoError(t, err)
296299

297300
authService := service.NewAuthService(authServiceCfg, docker, ldap, queries, broker)
298301
err = authService.Init()
299-
assert.NoError(t, err)
302+
require.NoError(t, err)
300303

301304
beforeEach := func() {
302305
// Clear failed login attempts before each test
@@ -346,9 +349,8 @@ func TestUserController(t *testing.T) {
346349
})
347350
}
348351

349-
err = db.Close()
350-
assert.NoError(t, err)
351-
352-
err = os.Remove("/tmp/tinyauth_test.db")
353-
assert.NoError(t, err)
352+
t.Cleanup(func() {
353+
err = db.Close()
354+
require.NoError(t, err)
355+
})
354356
}

0 commit comments

Comments
 (0)