Skip to content

Commit

Permalink
Test updates and style fixes for Go 1.22 for internal/
Browse files Browse the repository at this point in the history
- All test variations pass.
  • Loading branch information
bconway committed Feb 27, 2024
1 parent f5ce1cf commit 239046d
Show file tree
Hide file tree
Showing 20 changed files with 163 additions and 211 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Copyright (c) 2021-2023 Brian Conway <[email protected]>
Copyright (c) 2021-2024 Brian Conway <[email protected]>

All rights reserved.
24 changes: 11 additions & 13 deletions internal/hermes-api/interceptor/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,39 +125,37 @@ func TestAuth(t *testing.T) {
}

for _, test := range tests {
lTest := test

t.Run(fmt.Sprintf("Can log %+v", lTest), func(t *testing.T) {
t.Run(fmt.Sprintf("Can log %+v", test), func(t *testing.T) {
t.Parallel()

ctrl := gomock.NewController(t)
cacher := cache.NewMockCacher(ctrl)
cacher.EXPECT().Get(gomock.Any(), gomock.Any()).
Return(lTest.inpCache, "", lTest.inpCacheErr).
Times(lTest.inpCacheTimes)
Return(test.inpCache, "", test.inpCacheErr).
Times(test.inpCacheTimes)
orger := service.NewMockOrger(ctrl)
orger.EXPECT().Read(gomock.Any(), lTest.inpOrg.GetId()).
Return(lTest.inpOrg, lTest.inpOrgErr).Times(lTest.inpOrgTimes)
orger.EXPECT().Read(gomock.Any(), test.inpOrg.GetId()).
Return(test.inpOrg, test.inpOrgErr).Times(test.inpOrgTimes)

ctx, cancel := context.WithTimeout(context.Background(),
testTimeout)
defer cancel()
if lTest.inpMD != nil {
if test.inpMD != nil {
ctx = metadata.NewIncomingContext(ctx,
metadata.Pairs(lTest.inpMD...))
metadata.Pairs(test.inpMD...))
}

handler := func(_ context.Context, req interface{}) (
interface{}, error,
) {
return req, lTest.inpHandlerErr
return req, test.inpHandlerErr
}

res, err := Auth(lTest.inpSkipPaths, key, cacher, orger)(ctx, nil,
lTest.inpInfo, handler)
res, err := Auth(test.inpSkipPaths, key, cacher, orger)(ctx, nil,
test.inpInfo, handler)
t.Logf("res, err: %v, %v", res, err)
require.Nil(t, res)
require.Equal(t, lTest.err, err)
require.Equal(t, test.err, err)
})
}
}
16 changes: 7 additions & 9 deletions internal/hermes-api/interceptor/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,26 +55,24 @@ func TestLog(t *testing.T) {
}

for _, test := range tests {
lTest := test

t.Run(fmt.Sprintf("Can log %+v", lTest), func(t *testing.T) {
t.Run(fmt.Sprintf("Can log %+v", test), func(t *testing.T) {
t.Parallel()

ctx, cancel := context.WithTimeout(context.Background(),
testTimeout)
defer cancel()
ctx = metadata.NewIncomingContext(ctx,
metadata.Pairs(lTest.inpMD...))
metadata.Pairs(test.inpMD...))

handler := func(_ context.Context, req any) (any, error) {
return req, lTest.inpHandlerErr
return req, test.inpHandlerErr
}

res, err := Log(lTest.inpSkipPaths)(ctx, lTest.inpReq,
lTest.inpInfo, handler)
res, err := Log(test.inpSkipPaths)(ctx, test.inpReq,
test.inpInfo, handler)
t.Logf("res, err: %v, %v", res, err)
require.Equal(t, lTest.inpReq, res)
require.Equal(t, lTest.inpHandlerErr, err)
require.Equal(t, test.inpReq, res)
require.Equal(t, test.inpHandlerErr, err)
})
}
}
12 changes: 5 additions & 7 deletions internal/hermes-api/interceptor/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,24 +54,22 @@ func TestValidate(t *testing.T) {
}

for _, test := range tests {
lTest := test

t.Run(fmt.Sprintf("Can log %+v", lTest), func(t *testing.T) {
t.Run(fmt.Sprintf("Can log %+v", test), func(t *testing.T) {
t.Parallel()

ctx, cancel := context.WithTimeout(context.Background(),
testTimeout)
defer cancel()

handler := func(_ context.Context, _ any) (any, error) {
return nil, lTest.err
return nil, test.err
}

res, err := Validate(lTest.inpSkipPaths)(ctx, lTest.inpReq,
lTest.inpInfo, handler)
res, err := Validate(test.inpSkipPaths)(ctx, test.inpReq,
test.inpInfo, handler)
t.Logf("res, err: %v, %v", res, err)
require.Nil(t, res)
require.Equal(t, lTest.err, err)
require.Equal(t, test.err, err)
})
}
}
24 changes: 8 additions & 16 deletions internal/hermes-api/key/key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@ import (
func TestDisabled(t *testing.T) {
t.Parallel()

for i := 0; i < 5; i++ {
lTest := i

t.Run(fmt.Sprintf("Can key %v", lTest), func(t *testing.T) {
for i := range 5 {
t.Run(fmt.Sprintf("Can key %v", i), func(t *testing.T) {
t.Parallel()

orgID := uuid.NewString()
Expand All @@ -36,10 +34,8 @@ func TestDisabled(t *testing.T) {
func TestTOTPOffset(t *testing.T) {
t.Parallel()

for i := 0; i < 5; i++ {
lTest := i

t.Run(fmt.Sprintf("Can key %v", lTest), func(t *testing.T) {
for i := range 5 {
t.Run(fmt.Sprintf("Can key %v", i), func(t *testing.T) {
t.Parallel()

orgID := uuid.NewString()
Expand All @@ -59,10 +55,8 @@ func TestTOTPOffset(t *testing.T) {
func TestReuse(t *testing.T) {
t.Parallel()

for i := 0; i < 5; i++ {
lTest := i

t.Run(fmt.Sprintf("Can key %v", lTest), func(t *testing.T) {
for i := range 5 {
t.Run(fmt.Sprintf("Can key %v", i), func(t *testing.T) {
t.Parallel()

orgID := uuid.NewString()
Expand All @@ -83,10 +77,8 @@ func TestReuse(t *testing.T) {
func TestChallenge(t *testing.T) {
t.Parallel()

for i := 0; i < 5; i++ {
lTest := i

t.Run(fmt.Sprintf("Can key %v", lTest), func(t *testing.T) {
for i := range 5 {
t.Run(fmt.Sprintf("Can key %v", i), func(t *testing.T) {
t.Parallel()

orgID := uuid.NewString()
Expand Down
22 changes: 8 additions & 14 deletions internal/hermes-api/service/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,17 @@ func TestErrToStatus(t *testing.T) {
}

for _, test := range tests {
lTest := test

t.Run(fmt.Sprintf("Can map %+v", lTest), func(t *testing.T) {
t.Run(fmt.Sprintf("Can map %+v", test), func(t *testing.T) {
t.Parallel()

res := errToStatus(lTest.inp)
res := errToStatus(test.inp)
t.Logf("res: %#v", res)
// Comparison of gRPC status errors does not play well with
// require.Equal.
if lTest.res == "" {
if test.res == "" {
require.NoError(t, res)
} else {
require.EqualError(t, res, lTest.res)
require.EqualError(t, res, test.res)
}
})
}
Expand All @@ -64,10 +62,8 @@ func TestErrToStatus(t *testing.T) {
func TestErrPerm(t *testing.T) {
t.Parallel()

for i := 0; i < 5; i++ {
lTest := i

t.Run(fmt.Sprintf("Can generate %v", lTest), func(t *testing.T) {
for i := range 5 {
t.Run(fmt.Sprintf("Can generate %v", i), func(t *testing.T) {
t.Parallel()

role := []api.Role{
Expand All @@ -90,10 +86,8 @@ func TestErrPerm(t *testing.T) {
func TestErrPlan(t *testing.T) {
t.Parallel()

for i := 0; i < 5; i++ {
lTest := i

t.Run(fmt.Sprintf("Can generate %v", lTest), func(t *testing.T) {
for i := range 5 {
t.Run(fmt.Sprintf("Can generate %v", i), func(t *testing.T) {
t.Parallel()

plan := []api.Plan{
Expand Down
2 changes: 1 addition & 1 deletion internal/hermes-api/service/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func (ai *AppIdentity) CreateIdentity(

// Populate pregenerated backup codes.
if m, ok := identity.GetMethodOneof().(*api.Identity_BackupCodesMethod); ok {
for i := 0; i < int(m.BackupCodesMethod.GetPasscodes()); i++ {
for i := range m.BackupCodesMethod.GetPasscodes() {
passcode, err := otp.HOTP(int64(i + 10))
if err != nil {
return nil, errToStatus(err)
Expand Down
10 changes: 4 additions & 6 deletions internal/hermes-api/service/identity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,22 +438,20 @@ func TestCreateIdentity(t *testing.T) {
}

for _, test := range tests {
lTest := test

t.Run(fmt.Sprintf("Can create %v", lTest), func(t *testing.T) {
t.Run(fmt.Sprintf("Can create %v", test), func(t *testing.T) {
t.Parallel()

ctx, cancel := context.WithTimeout(session.NewContext(
context.Background(), &session.Session{
OrgID: lTest.GetOrgId(), OrgPlan: api.Plan_STARTER,
OrgID: test.GetOrgId(), OrgPlan: api.Plan_STARTER,
Role: api.Role_ADMIN,
}), testTimeout)
defer cancel()

aiSvc := NewAppIdentity(nil, nil, nil, nil, nil, nil, "")
createIdentity, err := aiSvc.CreateIdentity(ctx,
&api.CreateIdentityRequest{Identity: lTest})
t.Logf("lTest, createIdentity, err: %+v, %+v, %v", lTest,
&api.CreateIdentityRequest{Identity: test})
t.Logf("test, createIdentity, err: %+v, %+v, %v", test,
createIdentity, err)
require.Nil(t, createIdentity)
require.Equal(t, errPlan(api.Plan_PRO), err)
Expand Down
30 changes: 13 additions & 17 deletions internal/hermes-api/session/page_token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,16 @@ func TestGeneratePageToken(t *testing.T) {
}

for _, test := range tests {
lTest := test

t.Run(fmt.Sprintf("Can generate %+v", lTest), func(t *testing.T) {
t.Run(fmt.Sprintf("Can generate %+v", test), func(t *testing.T) {
t.Parallel()

res, err := GeneratePageToken(lTest.inpTS, lTest.inpID)
res, err := GeneratePageToken(test.inpTS, test.inpID)
t.Logf("res, err: %v, %#v", res, err)
require.GreaterOrEqual(t, len(res), lTest.resMinLen)
if lTest.err == "" {
require.GreaterOrEqual(t, len(res), test.resMinLen)
if test.err == "" {
require.NoError(t, err)
} else {
require.EqualError(t, err, lTest.err)
require.EqualError(t, err, test.err)
}
})
}
Expand Down Expand Up @@ -93,31 +91,29 @@ func TestParsePageToken(t *testing.T) {
}

for _, test := range tests {
lTest := test

t.Run(fmt.Sprintf("Can generate %+v", lTest), func(t *testing.T) {
t.Run(fmt.Sprintf("Can generate %+v", test), func(t *testing.T) {
t.Parallel()

resGen, err := GeneratePageToken(lTest.inpTS, lTest.inpID)
resGen, err := GeneratePageToken(test.inpTS, test.inpID)
t.Logf("resGen, err: %v, %#v", resGen, err)
require.NoError(t, err)

var resTS time.Time
var resID string
if lTest.inpPT == "res" {
if test.inpPT == "res" {
resTS, resID, err = ParsePageToken(resGen)
} else {
resTS, resID, err = ParsePageToken(lTest.inpPT)
resTS, resID, err = ParsePageToken(test.inpPT)
}
t.Logf("resTS, resID, err: %v, %v, %#v", resTS, resID, err)
if resID != "" {
require.Equal(t, lTest.inpID, resID)
require.Equal(t, lTest.inpTS, resTS)
require.Equal(t, test.inpID, resID)
require.Equal(t, test.inpTS, resTS)
}
if lTest.err == "" {
if test.err == "" {
require.NoError(t, err)
} else {
require.Contains(t, err.Error(), lTest.err)
require.Contains(t, err.Error(), test.err)
}
})
}
Expand Down
Loading

0 comments on commit 239046d

Please sign in to comment.