Skip to content

Commit 2a25c13

Browse files
feat: fix tests
1 parent 66ac24e commit 2a25c13

File tree

12 files changed

+62
-36
lines changed

12 files changed

+62
-36
lines changed

.github/workflows/test.yaml

+2-15
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
runs-on: ubuntu-latest
1111
strategy:
1212
matrix:
13-
go-version: [ '1.22.x' ]
13+
go-version: ["1.22.x"]
1414

1515
services:
1616
postgres:
@@ -44,18 +44,5 @@ jobs:
4444
restore-keys: |
4545
${{ runner.os }}-go-
4646
47-
- name: Run Tests
47+
- name: Run Tests
4848
run: go test -v ./...
49-
50-
lint:
51-
name: lint
52-
runs-on: ubuntu-latest
53-
steps:
54-
- uses: actions/checkout@v4
55-
- uses: actions/setup-go@v5
56-
with:
57-
go-version: stable
58-
- name: golangci-lint
59-
uses: golangci/golangci-lint-action@v6
60-
with:
61-
version: v1.58

flake.nix

+4-3
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
flake-utils.url = "github:numtide/flake-utils";
77
};
88

9-
outputs = { self, nixpkgs, flake-utils }:
10-
flake-utils.lib.eachDefaultSystem (system:
9+
outputs =
10+
{ nixpkgs, flake-utils }:
11+
flake-utils.lib.eachDefaultSystem (
12+
system:
1113
let
1214
pkgs = import nixpkgs { inherit system; };
1315
in
@@ -17,7 +19,6 @@
1719
nodejs
1820
go_1_23
1921
sqlc
20-
golangci-lint
2122
gofumpt
2223
];
2324
};

go.mod

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ require (
1313
github.com/joho/godotenv v1.5.1
1414
github.com/stretchr/testify v1.9.0
1515
go.inout.gg/conduit v0.0.0-20241011174716-52bc52d23ab9
16-
go.inout.gg/foundations v0.0.0-20240922153347-0be999149d7c
16+
go.inout.gg/foundations v0.0.0-20241021195437-f07677a425bd
1717
golang.org/x/crypto v0.28.0
1818
golang.org/x/oauth2 v0.23.0
1919
golang.org/x/text v0.19.0
@@ -40,7 +40,6 @@ require (
4040
github.com/mitchellh/mapstructure v1.5.0 // indirect
4141
github.com/pmezard/go-difflib v1.0.0 // indirect
4242
github.com/russross/blackfriday/v2 v2.1.0 // indirect
43-
github.com/samber/lo v1.47.0 // indirect
4443
github.com/segmentio/go-camelcase v0.0.0-20160726192923-7085f1e3c734 // indirect
4544
github.com/segmentio/go-snakecase v1.2.0 // indirect
4645
github.com/urfave/cli/v2 v2.27.4 // indirect

go.sum

+2-4
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,6 @@ github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU
6565
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
6666
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
6767
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
68-
github.com/samber/lo v1.47.0 h1:z7RynLwP5nbyRscyvcD043DWYoOcYRv3mV8lBeqOCLc=
69-
github.com/samber/lo v1.47.0/go.mod h1:RmDH9Ct32Qy3gduHQuKJ3gW1fMHAnE/fAzQuf6He5cU=
7068
github.com/segmentio/go-camelcase v0.0.0-20160726192923-7085f1e3c734 h1:Cpx2WLIv6fuPvaJAHNhYOgYzk/8RcJXu/8+mOrxf2KM=
7169
github.com/segmentio/go-camelcase v0.0.0-20160726192923-7085f1e3c734/go.mod h1:hqVOMAwu+ekffC3Tvq5N1ljnXRrFKcaSjbCmQ8JgYaI=
7270
github.com/segmentio/go-snakecase v1.2.0 h1:4cTmEjPGi03WmyAHWBjX53viTpBkn/z+4DO++fqYvpw=
@@ -84,8 +82,8 @@ github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 h1:gEOO8jv9F4OT7lGC
8482
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1/go.mod h1:Ohn+xnUBiLI6FVj/9LpzZWtj1/D6lUovWYBkxHVV3aM=
8583
go.inout.gg/conduit v0.0.0-20241011174716-52bc52d23ab9 h1:nYmfjoOrKiCUGAIDWLiyu+RrdN9TQ9WRwK5KUAH0n5Y=
8684
go.inout.gg/conduit v0.0.0-20241011174716-52bc52d23ab9/go.mod h1:qEwpFTi4ynxp2ad6/bFfWdyiZmWcO7+m+XVm+hO/+oI=
87-
go.inout.gg/foundations v0.0.0-20240922153347-0be999149d7c h1:xkhJcJgpXchg5mL4xxzxlmMtFepJLewCUVa81bv03Q4=
88-
go.inout.gg/foundations v0.0.0-20240922153347-0be999149d7c/go.mod h1:/6QZOnj4l6OJz8iogY0uArlkPeUsxz1PTA2EHWotD7Q=
85+
go.inout.gg/foundations v0.0.0-20241021195437-f07677a425bd h1:QhKw4ULy1+HX27eA/Qiyd8W3sxHb3UOYhc9lJWX7Xxk=
86+
go.inout.gg/foundations v0.0.0-20241021195437-f07677a425bd/go.mod h1:iPlNgVnVKcN3nVoi6bNiDrbBeS3I9zNxZHzTCy2w994=
8987
golang.org/x/crypto v0.28.0 h1:GBDwsMXVQi34v5CCYUm2jkJvu4cbtru2U4TN2PSyQnw=
9088
golang.org/x/crypto v0.28.0/go.mod h1:rmgy+3RHxRZMyY0jjAJShp2zgEdOqj2AO7U0pYmeQ7U=
9189
golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE=

internal/random/random.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package random
2+
3+
import (
4+
"crypto/rand"
5+
"encoding/hex"
6+
"fmt"
7+
)
8+
9+
// secureBytes returns a securely random byte slice of length l.
10+
func secureBytes(l int) ([]byte, error) {
11+
bytes := make([]byte, l)
12+
_, err := rand.Read(bytes)
13+
if err != nil {
14+
return bytes, fmt.Errorf("random: error reading random bytes: %w", err)
15+
}
16+
return bytes, nil
17+
}
18+
19+
// SecureHexString returns a securely random hex string of length 2*l.
20+
func SecureHexString(l int) (string, error) {
21+
bytes, err := secureBytes(l)
22+
if err != nil {
23+
return "", err
24+
}
25+
return hex.EncodeToString(bytes), nil
26+
}

shieldcsrf/token.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"net/http"
1111
"strings"
1212

13-
"go.inout.gg/foundations/random"
13+
"go.inout.gg/shield/internal/random"
1414
)
1515

1616
var (

shieldmigrate/migrator.go

+5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ import (
88
"go.inout.gg/shield/internal/migrations"
99
)
1010

11+
var (
12+
DefaultUpStep = conduit.DefaultUpStep
13+
DefaultDownStep = conduit.DefaultDownStep
14+
)
15+
1116
// Migrator is a database migration utility to roll up and down shield's
1217
// migrations in order.
1318
type Migrator struct {

shieldpassword/handler.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"github.com/google/uuid"
99
"github.com/jackc/pgx/v5"
1010
"go.inout.gg/foundations/debug"
11-
"go.inout.gg/foundations/sql/db/dbutil"
11+
"go.inout.gg/foundations/sqldb"
1212
"go.inout.gg/shield"
1313
"go.inout.gg/shield/db/driver"
1414
"go.inout.gg/shield/internal/dbsqlc"
@@ -145,7 +145,7 @@ func (h *Handler[T]) handleUserRegistrationTx(
145145
ID: uid,
146146
Email: email,
147147
}); err != nil {
148-
if dbutil.IsUniqueViolationError(err) {
148+
if sqldb.IsUniqueViolationError(err) {
149149
return uid, ErrEmailAlreadyTaken
150150
}
151151

@@ -185,7 +185,7 @@ func (h *Handler[T]) HandleUserLogin(
185185
email,
186186
)
187187
if err != nil {
188-
if dbutil.IsNotFoundError(err) {
188+
if sqldb.IsNotFoundError(err) {
189189
return nil, shield.ErrUserNotFound
190190
}
191191

shieldpassword/handler_test.go

+14-4
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,25 @@ import (
66
"testing"
77
"time"
88

9+
"github.com/jackc/pgx/v5"
910
"github.com/stretchr/testify/assert"
10-
"go.inout.gg/shield/db/driverpgxv5"
1111
"go.inout.gg/foundations/must"
12-
"go.inout.gg/foundations/sql/db/dbtest"
12+
"go.inout.gg/foundations/sqldb/sqldbtest"
13+
"go.inout.gg/shield/db/driverpgxv5"
14+
"go.inout.gg/shield/shieldmigrate"
1315
)
1416

17+
var migrator = shieldmigrate.New()
18+
19+
func makeDB(ctx context.Context, t *testing.T) *sqldbtest.DB {
20+
return sqldbtest.Must(ctx, t, sqldbtest.WithUp(func(ctx context.Context, conn *pgx.Conn) error {
21+
return migrator.Up(ctx, conn, nil)
22+
}))
23+
}
24+
1525
func TestUserRegistration(t *testing.T) {
1626
ctx := context.Background()
17-
db := dbtest.Must(ctx, t)
27+
db := makeDB(ctx, t)
1828
logger := slog.Default()
1929
config := &Config[any]{
2030
PasswordHasher: DefaultPasswordHasher,
@@ -76,7 +86,7 @@ func TestUserRegistration(t *testing.T) {
7686

7787
func TestUserLogin(t *testing.T) {
7888
ctx := context.Background()
79-
db := dbtest.Must(ctx, t)
89+
db := makeDB(ctx, t)
8090

8191
t.Run("user not found", func(t *testing.T) {
8292
must.Must1(db.Reset(ctx))

shieldpasswordreset/handler.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"github.com/jackc/pgx/v5/pgtype"
1111
"go.inout.gg/foundations/debug"
1212
"go.inout.gg/foundations/must"
13-
"go.inout.gg/foundations/random"
13+
"go.inout.gg/shield/internal/random"
1414
"go.inout.gg/shield"
1515
"go.inout.gg/shield/db/driver"
1616
"go.inout.gg/shield/internal/dbsqlc"

shieldsso/shieldsso.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"net/url"
99

1010
"go.inout.gg/foundations/must"
11-
"go.inout.gg/foundations/random"
11+
"go.inout.gg/shield/internal/random"
1212
"golang.org/x/oauth2"
1313
)
1414

shieldstrategy/session/session.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
"github.com/jackc/pgx/v5/pgtype"
1010
"go.inout.gg/foundations/http/cookie"
11-
"go.inout.gg/foundations/sql/db/dbutil"
11+
"go.inout.gg/foundations/sqldb"
1212
"go.inout.gg/shield"
1313
"go.inout.gg/shield/db/driver"
1414
"go.inout.gg/shield/internal/dbsqlc"
@@ -111,7 +111,7 @@ func (s *sessionStrategy[T]) Authenticate(
111111

112112
session, err := q.FindUserSessionByID(ctx, sessionID)
113113
if err != nil {
114-
if dbutil.IsNotFoundError(err) {
114+
if sqldb.IsNotFoundError(err) {
115115
s.config.Logger.Error(
116116
"No sessions found with given ID",
117117
slog.String("session_id", sessionID.String()),

0 commit comments

Comments
 (0)