Skip to content

Commit 9891daa

Browse files
authored
refactor: simplify tests by using slog.DiscardHandler (#4058)
Signed-off-by: Oleksandr Redko <oleksandr.red+github@gmail.com>
1 parent f93a648 commit 9891daa

File tree

22 files changed

+36
-57
lines changed

22 files changed

+36
-57
lines changed

connector/atlassiancrowd/atlassiancrowd_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"crypto/tls"
77
"encoding/json"
88
"fmt"
9-
"io"
109
"log/slog"
1110
"net/http"
1211
"net/http/httptest"
@@ -150,7 +149,7 @@ type TestServerResponse struct {
150149
func newTestCrowdConnector(baseURL string) crowdConnector {
151150
connector := crowdConnector{}
152151
connector.BaseURL = baseURL
153-
connector.logger = slog.New(slog.NewTextHandler(io.Discard, &slog.HandlerOptions{}))
152+
connector.logger = slog.New(slog.DiscardHandler)
154153
return connector
155154
}
156155

connector/authproxy/authproxy_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package authproxy
22

33
import (
4-
"io"
54
"log/slog"
65
"net/http"
76
"reflect"
@@ -23,7 +22,7 @@ const (
2322
testUserID = "1234567890"
2423
)
2524

26-
var logger = slog.New(slog.NewTextHandler(io.Discard, &slog.HandlerOptions{}))
25+
var logger = slog.New(slog.DiscardHandler)
2726

2827
func TestUser(t *testing.T) {
2928
config := Config{

connector/github/github_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"encoding/json"
77
"errors"
88
"fmt"
9-
"io"
109
"log/slog"
1110
"net/http"
1211
"net/http/httptest"
@@ -451,7 +450,7 @@ func Test_isPreferredEmailDomain(t *testing.T) {
451450
}
452451

453452
func Test_Open_PreferredDomainConfig(t *testing.T) {
454-
log := slog.New(slog.NewTextHandler(io.Discard, &slog.HandlerOptions{}))
453+
log := slog.New(slog.DiscardHandler)
455454
tests := []struct {
456455
preferredEmailDomain string
457456
email string

connector/google/google_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7-
"io"
87
"log/slog"
98
"net/http"
109
"net/http/httptest"
@@ -52,7 +51,7 @@ func testSetup() *httptest.Server {
5251
}
5352

5453
func newConnector(config *Config) (*googleConnector, error) {
55-
log := slog.New(slog.NewTextHandler(io.Discard, &slog.HandlerOptions{}))
54+
log := slog.New(slog.DiscardHandler)
5655
conn, err := config.Open("id", log)
5756
if err != nil {
5857
return nil, err

connector/ldap/ldap_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package ldap
33
import (
44
"context"
55
"fmt"
6-
"io"
76
"log/slog"
87
"os"
98
"testing"
@@ -567,7 +566,7 @@ func runTests(t *testing.T, connMethod connectionMethod, config *Config, tests [
567566
c.BindDN = "cn=admin,dc=example,dc=org"
568567
c.BindPW = "admin"
569568

570-
l := slog.New(slog.NewTextHandler(io.Discard, &slog.HandlerOptions{}))
569+
l := slog.New(slog.DiscardHandler)
571570

572571
conn, err := c.openConnector(l)
573572
if err != nil {

connector/oauth/oauth_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"encoding/json"
77
"errors"
88
"fmt"
9-
"io"
109
"log/slog"
1110
"net/http"
1211
"net/http/httptest"
@@ -271,7 +270,7 @@ func newConnector(t *testing.T, serverURL string) *oauthConnector {
271270
testConfig.ClaimMapping.EmailKey = "mail"
272271
testConfig.ClaimMapping.EmailVerifiedKey = "has_verified_email"
273272

274-
log := slog.New(slog.NewTextHandler(io.Discard, &slog.HandlerOptions{}))
273+
log := slog.New(slog.DiscardHandler)
275274

276275
conn, err := testConfig.Open("id", log)
277276
if err != nil {

connector/oidc/oidc_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"encoding/json"
1111
"errors"
1212
"fmt"
13-
"io"
1413
"log/slog"
1514
"net/http"
1615
"net/http/httptest"
@@ -834,7 +833,7 @@ func newToken(key *jose.JSONWebKey, claims map[string]interface{}) (string, erro
834833
}
835834

836835
func newConnector(config Config) (*oidcConnector, error) {
837-
logger := slog.New(slog.NewTextHandler(io.Discard, &slog.HandlerOptions{}))
836+
logger := slog.New(slog.DiscardHandler)
838837
conn, err := config.Open("id", logger)
839838
if err != nil {
840839
return nil, fmt.Errorf("unable to open: %v", err)

connector/openshift/openshift_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7-
"io"
87
"log/slog"
98
"net/http"
109
"net/http/httptest"
@@ -38,7 +37,7 @@ func TestOpen(t *testing.T) {
3837
InsecureCA: true,
3938
}
4039

41-
logger := slog.New(slog.NewTextHandler(io.Discard, &slog.HandlerOptions{}))
40+
logger := slog.New(slog.DiscardHandler)
4241

4342
oconfig, err := c.Open("id", logger)
4443

connector/saml/saml_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"encoding/base64"
66
"encoding/pem"
77
"errors"
8-
"io"
98
"log/slog"
109
"os"
1110
"sort"
@@ -421,7 +420,7 @@ func (r responseTest) run(t *testing.T) {
421420
t.Fatalf("parse test time: %v", err)
422421
}
423422

424-
conn, err := c.openConnector(slog.New(slog.NewTextHandler(io.Discard, &slog.HandlerOptions{})))
423+
conn, err := c.openConnector(slog.New(slog.DiscardHandler))
425424
if err != nil {
426425
t.Fatal(err)
427426
}
@@ -455,7 +454,7 @@ func (r responseTest) run(t *testing.T) {
455454
}
456455

457456
func TestConfigCAData(t *testing.T) {
458-
logger := slog.New(slog.NewTextHandler(io.Discard, &slog.HandlerOptions{}))
457+
logger := slog.New(slog.DiscardHandler)
459458
validPEM, err := os.ReadFile("testdata/ca.crt")
460459
if err != nil {
461460
t.Fatal(err)

server/api_test.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package server
22

33
import (
44
"context"
5-
"io"
65
"log/slog"
76
"net"
87
"os"
@@ -60,7 +59,7 @@ func newAPI(s storage.Storage, logger *slog.Logger, t *testing.T) *apiClient {
6059

6160
// Attempts to create, update and delete a test Password
6261
func TestPassword(t *testing.T) {
63-
logger := slog.New(slog.NewTextHandler(io.Discard, &slog.HandlerOptions{}))
62+
logger := slog.New(slog.DiscardHandler)
6463

6564
s := memory.New(logger)
6665
client := newAPI(s, logger, t)
@@ -169,7 +168,7 @@ func TestPassword(t *testing.T) {
169168

170169
// Ensures checkCost returns expected values
171170
func TestCheckCost(t *testing.T) {
172-
logger := slog.New(slog.NewTextHandler(io.Discard, &slog.HandlerOptions{}))
171+
logger := slog.New(slog.DiscardHandler)
173172

174173
s := memory.New(logger)
175174
client := newAPI(s, logger, t)
@@ -222,7 +221,7 @@ func TestCheckCost(t *testing.T) {
222221

223222
// Attempts to list and revoke an existing refresh token.
224223
func TestRefreshToken(t *testing.T) {
225-
logger := slog.New(slog.NewTextHandler(io.Discard, &slog.HandlerOptions{}))
224+
logger := slog.New(slog.DiscardHandler)
226225

227226
s := memory.New(logger)
228227
client := newAPI(s, logger, t)
@@ -331,7 +330,7 @@ func TestRefreshToken(t *testing.T) {
331330
}
332331

333332
func TestUpdateClient(t *testing.T) {
334-
logger := slog.New(slog.NewTextHandler(io.Discard, &slog.HandlerOptions{}))
333+
logger := slog.New(slog.DiscardHandler)
335334

336335
s := memory.New(logger)
337336
client := newAPI(s, logger, t)
@@ -497,7 +496,7 @@ func TestCreateConnector(t *testing.T) {
497496
os.Setenv("DEX_API_CONNECTORS_CRUD", "true")
498497
defer os.Unsetenv("DEX_API_CONNECTORS_CRUD")
499498

500-
logger := slog.New(slog.NewTextHandler(io.Discard, &slog.HandlerOptions{}))
499+
logger := slog.New(slog.DiscardHandler)
501500

502501
s := memory.New(logger)
503502
client := newAPI(s, logger, t)
@@ -547,7 +546,7 @@ func TestUpdateConnector(t *testing.T) {
547546
os.Setenv("DEX_API_CONNECTORS_CRUD", "true")
548547
defer os.Unsetenv("DEX_API_CONNECTORS_CRUD")
549548

550-
logger := slog.New(slog.NewTextHandler(io.Discard, &slog.HandlerOptions{}))
549+
logger := slog.New(slog.DiscardHandler)
551550

552551
s := memory.New(logger)
553552
client := newAPI(s, logger, t)
@@ -615,7 +614,7 @@ func TestDeleteConnector(t *testing.T) {
615614
os.Setenv("DEX_API_CONNECTORS_CRUD", "true")
616615
defer os.Unsetenv("DEX_API_CONNECTORS_CRUD")
617616

618-
logger := slog.New(slog.NewTextHandler(io.Discard, &slog.HandlerOptions{}))
617+
logger := slog.New(slog.DiscardHandler)
619618

620619
s := memory.New(logger)
621620
client := newAPI(s, logger, t)
@@ -659,7 +658,7 @@ func TestListConnectors(t *testing.T) {
659658
os.Setenv("DEX_API_CONNECTORS_CRUD", "true")
660659
defer os.Unsetenv("DEX_API_CONNECTORS_CRUD")
661660

662-
logger := slog.New(slog.NewTextHandler(io.Discard, &slog.HandlerOptions{}))
661+
logger := slog.New(slog.DiscardHandler)
663662

664663
s := memory.New(logger)
665664
client := newAPI(s, logger, t)
@@ -699,7 +698,7 @@ func TestListConnectors(t *testing.T) {
699698
}
700699

701700
func TestMissingConnectorsCRUDFeatureFlag(t *testing.T) {
702-
logger := slog.New(slog.NewTextHandler(io.Discard, &slog.HandlerOptions{}))
701+
logger := slog.New(slog.DiscardHandler)
703702

704703
s := memory.New(logger)
705704
client := newAPI(s, logger, t)

0 commit comments

Comments
 (0)