WIP: Adding tests to the token controller
Some checks failed
ci/woodpecker/push/build Pipeline failed
Some checks failed
ci/woodpecker/push/build Pipeline failed
Signed-off-by: Nikolai Rodionov <iam@allanger.xyz>
This commit is contained in:
@@ -22,7 +22,6 @@ import (
|
||||
|
||||
type TokenController struct {
|
||||
DB *sql.DB
|
||||
HashCost int16
|
||||
ServiceInfo map[string]grpc.ServiceInfo
|
||||
rules []rule
|
||||
Redis *redis.Client
|
||||
|
||||
67
internal/controllers/tokens_test.go
Normal file
67
internal/controllers/tokens_test.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package controllers_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"gitea.badhouseplants.net/softplayer/softplayer-backend/internal/controllers"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func newTestTokensController(ctx context.Context) *controllers.TokenController {
|
||||
return &controllers.TokenController{
|
||||
DB: newTestDbConnection(ctx),
|
||||
Redis: newTestRedisConnection(),
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateToken_Success(t *testing.T) {
|
||||
// Create a user for the token
|
||||
ctrlAccount := newTestAccountController(t.Context())
|
||||
accountData := &controllers.AccountData{
|
||||
Password: "qwertyu9",
|
||||
Email: newTestUniqueEmail("accounts"),
|
||||
}
|
||||
id, err := ctrlAccount.Create(t.Context(), accountData)
|
||||
assert.NoError(t, err)
|
||||
|
||||
tokenData := &controllers.TokenData{
|
||||
Name: "Test Token",
|
||||
UserID: id,
|
||||
ExpiresAt: time.Now().Add(time.Second * 5),
|
||||
Scopes: map[string][]string{
|
||||
"Test": {"test", "test2"},
|
||||
},
|
||||
}
|
||||
|
||||
ctrl := newTestTokensController(t.Context())
|
||||
tokenID, err := ctrl.Create(t.Context(), tokenData)
|
||||
assert.NoError(t, err)
|
||||
assert.NotEmpty(t, tokenID)
|
||||
}
|
||||
|
||||
func TestCreateToken_Success(t *testing.T) {
|
||||
// Create a user for the token
|
||||
ctrlAccount := newTestAccountController(t.Context())
|
||||
accountData := &controllers.AccountData{
|
||||
Password: "qwertyu9",
|
||||
Email: newTestUniqueEmail("accounts"),
|
||||
}
|
||||
id, err := ctrlAccount.Create(t.Context(), accountData)
|
||||
assert.NoError(t, err)
|
||||
|
||||
tokenData := &controllers.TokenData{
|
||||
Name: "Test Token",
|
||||
UserID: id,
|
||||
ExpiresAt: time.Now().Add(time.Second * 5),
|
||||
Scopes: map[string][]string{
|
||||
"Test": {"test", "test2"},
|
||||
},
|
||||
}
|
||||
|
||||
ctrl := newTestTokensController(t.Context())
|
||||
tokenID, err := ctrl.Create(t.Context(), tokenData)
|
||||
assert.NoError(t, err)
|
||||
assert.NotEmpty(t, tokenID)
|
||||
}
|
||||
Reference in New Issue
Block a user