Restructure the projec and start adding projects
Signed-off-by: Nikolai Rodionov <iam@allanger.xyz>
This commit is contained in:
71
internal/services/authorization_test.go
Normal file
71
internal/services/authorization_test.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package services_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"gitea.badhouseplants.net/softplayer/softplayer-backend/internal/services"
|
||||
"github.com/google/uuid"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
var (
|
||||
testAccessTTL = time.Second * 5
|
||||
testRefreshTTL = time.Second * 20
|
||||
testUserID = uuid.New().String()
|
||||
)
|
||||
|
||||
func TestGenerateInvalidTokenType(t *testing.T) {
|
||||
data := &services.JWTData{
|
||||
UserID: testUserID,
|
||||
TokenType: "invalid_type",
|
||||
}
|
||||
|
||||
authCtrl := services.NewAuthController([]byte("test"), testAccessTTL, testRefreshTTL, nil)
|
||||
|
||||
token, _, err := authCtrl.GenerateToken(data)
|
||||
assert.Equal(t, "", token)
|
||||
assert.ErrorIs(t, services.ErrUnknownTokenType, err)
|
||||
}
|
||||
|
||||
func TestGenerateValidateAccessToken(t *testing.T) {
|
||||
data := &services.JWTData{
|
||||
UserID: testUserID,
|
||||
TokenType: services.TokenTypeAccess,
|
||||
}
|
||||
authCtrl := services.NewAuthController([]byte("test"), testAccessTTL, testRefreshTTL, nil)
|
||||
now := time.Now()
|
||||
token, _, err := authCtrl.GenerateToken(data)
|
||||
assert.NoError(t, err)
|
||||
assert.NotEmpty(t, token)
|
||||
|
||||
claims, err := authCtrl.ParseToken(token)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, testUserID, claims.UserID)
|
||||
assert.NotEmpty(t, claims.TokenID)
|
||||
assert.Equal(t, services.TokenTypeAccess, claims.TokenType)
|
||||
assert.Equal(t, now.Add(testAccessTTL).Unix(), claims.ExpiresAt.Unix())
|
||||
assert.Equal(t, now.Unix(), claims.IssuedAt.Unix())
|
||||
assert.Equal(t, now.Unix(), claims.NotBefore.Unix())
|
||||
}
|
||||
|
||||
func TestGenerateValidateRefreshToken(t *testing.T) {
|
||||
data := &services.JWTData{
|
||||
UserID: testUserID,
|
||||
TokenType: services.TokenTypeRefresh,
|
||||
}
|
||||
authCtrl := services.NewAuthController([]byte("test"), testAccessTTL, testRefreshTTL, nil)
|
||||
now := time.Now()
|
||||
token, _, err := authCtrl.GenerateToken(data)
|
||||
assert.NoError(t, err)
|
||||
assert.NotEmpty(t, token)
|
||||
|
||||
claims, err := authCtrl.ParseToken(token)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, testUserID, claims.UserID)
|
||||
assert.NotEmpty(t, claims.TokenID)
|
||||
assert.Equal(t, services.TokenTypeRefresh, claims.TokenType)
|
||||
assert.Equal(t, now.Add(testRefreshTTL).Unix(), claims.ExpiresAt.Unix())
|
||||
assert.Equal(t, now.Unix(), claims.IssuedAt.Unix())
|
||||
assert.Equal(t, now.Unix(), claims.NotBefore.Unix())
|
||||
}
|
||||
Reference in New Issue
Block a user