-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
276 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
//nolint:lll // jwt tokens in test cases | ||
package auth | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/golang-jwt/jwt/v5" | ||
"github.com/stretchr/testify/require" | ||
"google.golang.org/grpc/metadata" | ||
) | ||
|
||
func TestAuth_GRPCAuthFunc(t *testing.T) { | ||
type args struct { | ||
ctxFunc func() context.Context | ||
} | ||
type want struct { | ||
claims *Claims | ||
errMsg string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want want | ||
}{ | ||
{ | ||
name: "valid token", | ||
args: args{ | ||
ctxFunc: func() context.Context { | ||
return metadata.NewIncomingContext( | ||
context.Background(), | ||
metadata.Pairs("authorization", "bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOiJ0ZXN0dXNlcmlkIiwibmFtZSI6InRlc3RVc2VyIiwiZXhwIjo0NzE1ODM5MzY0fQ.QpWCWosIauu56J4LsEvpxBImwvl_mUoUMVyEeSRKL-M")) | ||
}, | ||
}, | ||
want: want{ | ||
claims: &Claims{ | ||
UserID: "testuserid", | ||
Name: "testUser", | ||
RegisteredClaims: jwt.RegisteredClaims{ | ||
ExpiresAt: &jwt.NumericDate{Time: time.Unix(4715839364, 0)}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "invalid token", | ||
args: args{ | ||
ctxFunc: func() context.Context { | ||
return metadata.NewIncomingContext( | ||
context.Background(), | ||
metadata.Pairs("authorization", "bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQ")) | ||
}, | ||
}, | ||
want: want{ | ||
errMsg: "invalid token", | ||
}, | ||
}, | ||
{ | ||
name: "missing token", | ||
args: args{ | ||
ctxFunc: func() context.Context { | ||
return metadata.NewIncomingContext( | ||
context.Background(), | ||
metadata.Pairs("qwe", "bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQ")) | ||
}, | ||
}, | ||
want: want{ | ||
errMsg: "missing token", | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
a, err := NewAuth(&Config{Secret: "superSecret"}) | ||
require.NoError(t, err) | ||
require.NotNil(t, a) | ||
|
||
outCtx, err := a.GRPCAuthFunc(tt.args.ctxFunc()) | ||
if tt.want.errMsg == "" { | ||
require.NoError(t, err) | ||
claims, ok := GetClaimsFromContext(outCtx) | ||
require.True(t, ok) | ||
require.Equal(t, tt.want.claims, claims) | ||
} else { | ||
require.Error(t, err) | ||
require.Nil(t, outCtx) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package auth | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/golang-jwt/jwt/v5" | ||
"github.com/labstack/echo/v4" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGetClaimFromEchoContext(t *testing.T) { | ||
type args struct { | ||
ctxFunc func() echo.Context | ||
} | ||
type want struct { | ||
errMsg string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want want | ||
}{ | ||
{ | ||
name: "success", | ||
args: args{ | ||
ctxFunc: func() echo.Context { | ||
e := echo.New() | ||
ctx := e.NewContext(nil, nil) | ||
ctx.Set(sessionContextKey, &jwt.Token{Claims: &Claims{}}) | ||
return ctx | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "no token", | ||
args: args{ | ||
ctxFunc: func() echo.Context { | ||
e := echo.New() | ||
ctx := e.NewContext(nil, nil) | ||
ctx.Set(sessionContextKey, "tuytu") | ||
return ctx | ||
}, | ||
}, | ||
want: want{errMsg: "cannot get jwt token from session context"}, | ||
}, | ||
{ | ||
name: "no claims", | ||
args: args{ | ||
ctxFunc: func() echo.Context { | ||
e := echo.New() | ||
ctx := e.NewContext(nil, nil) | ||
ctx.Set(sessionContextKey, &jwt.Token{Claims: Claims{}}) | ||
return ctx | ||
}, | ||
}, | ||
want: want{errMsg: "cannot get claims from jwt token"}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
claims, err := GetClaimFromEchoContext(tt.args.ctxFunc()) | ||
if tt.want.errMsg != "" { | ||
assert.EqualError(t, err, tt.want.errMsg) | ||
assert.Nil(t, claims) | ||
} else { | ||
assert.NoError(t, err) | ||
assert.NotNil(t, claims) | ||
} | ||
}) | ||
} | ||
} |