Add linter and a little bit more tests
All checks were successful
ci/woodpecker/push/build Pipeline was successful

Signed-off-by: Nikolai Rodionov <iam@allanger.xyz>
This commit is contained in:
2026-05-18 11:02:10 +02:00
parent 732f70238a
commit 8c5f88cb46
8 changed files with 489 additions and 18 deletions

View File

@@ -16,10 +16,11 @@ import (
"github.com/stretchr/testify/assert"
)
func newTestDbConnection(ctx context.Context) *sql.DB {
func newTestDBConnection(ctx context.Context) *sql.DB {
connStr, ok := os.LookupEnv("SOFTPLAYER_DB_CONNECTION_STRING")
if !ok {
panic("set the db connection string env var")
// Default connection string
connStr = "postgres://softplayer:qwertyu9@localhost:30432/softplayer?sslmode=disable"
}
db, err := postgres.Open(ctx, connStr)
if err != nil {
@@ -31,7 +32,8 @@ func newTestDbConnection(ctx context.Context) *sql.DB {
func newTestRedisConnection() *redis.Client {
connStr, ok := os.LookupEnv("SOFTPLAYER_REDIS_HOST")
if !ok {
panic("set the redis connection string env var")
// Default redis host
connStr = "localhost:30379"
}
return redis.NewClient(&redis.Options{
Addr: connStr,
@@ -40,7 +42,7 @@ func newTestRedisConnection() *redis.Client {
func newTestAccountController(ctx context.Context) *controllers.AccountController {
return &controllers.AccountController{
DB: newTestDbConnection(ctx),
DB: newTestDBConnection(ctx),
Redis: newTestRedisConnection(),
DevMode: true,
HashCost: 3,

View File

@@ -1,3 +1,5 @@
// package controllers for token management
// This a token controller, that implements the logic around tokens
package controllers
import (
@@ -28,7 +30,8 @@ type TokenController struct {
Redis *redis.Client
}
// Services that are not available for tokens
// DisabledServicesRegex is a slice of regex to catch the services
// that are not available for tokens
var DisabledServicesRegex = []string{".*Accounts.*", ".*Tokens.*"}
// Errors
@@ -51,9 +54,7 @@ type TokenData struct {
Scopes map[string][]string
}
type Scopes struct{}
// Set the grpc info, must happen after all the service are initialized
// SetGRPCInfo must happen after all the service are initialized
func (ctrl *TokenController) SetGRPCInfo(info map[string]grpc.ServiceInfo) {
ctrl.ServiceInfo = info
}
@@ -67,8 +68,7 @@ func (ctrl *TokenController) SetRules() {
ctrl.rules = rules
}
// Each token operation must first verify that the current user
// is allowed to manipulate the token.
// VerifyTokenOwner is there to ensure that a user can't manipulate tokens of other users
func (ctrl *TokenController) VerifyTokenOwner(ctx context.Context, userID, tokenID string) error {
log := logger.FromContext(ctx).WithValues("uuid", tokenID, "user_id", userID)
log.V(2).Info("Verifying the token owner")
@@ -119,7 +119,7 @@ func (ctrl *TokenController) Create(ctx context.Context, data *TokenData) (strin
tokenHash := hashSHA256(tokenValue)
scopesJson, err := json.Marshal(data.Scopes)
scopesJSON, err := json.Marshal(data.Scopes)
if err != nil {
log.Error(err, "Couldn't marshal permissions into json")
return "", "", ErrServerError
@@ -133,7 +133,7 @@ func (ctrl *TokenController) Create(ctx context.Context, data *TokenData) (strin
CreatedAt: time.Now(),
GeneratedAt: time.Now(),
ExpiresAt: data.ExpiresAt,
Scope: string(scopesJson),
Scope: string(scopesJSON),
}
if err := repository.CreateToken(ctx, ctrl.DB, queryData); err != nil {
@@ -149,7 +149,7 @@ func (ctrl *TokenController) Update(ctx context.Context, data *TokenData) error
log := logger.FromContext(ctx).WithValues("uuid", data.UUID)
log.V(2).Info("Updating a token")
scopesJson, err := json.Marshal(data.Scopes)
scopesJSON, err := json.Marshal(data.Scopes)
if err != nil {
log.Error(err, "Couldn't marshal permissions into json")
return ErrServerError
@@ -157,7 +157,7 @@ func (ctrl *TokenController) Update(ctx context.Context, data *TokenData) error
queryData := &repository.TokenData{
UUID: data.UUID,
Scope: string(scopesJson),
Scope: string(scopesJSON),
Decsription: data.Name,
}
@@ -389,7 +389,6 @@ func hashSHA256(s string) string {
return hex.EncodeToString(hash[:])
}
// Unit Tests
func TestUnitHashPersistence(t *testing.T) {
password := "qwertyu9"
hash1 := hashSHA256(password)

View File

@@ -12,7 +12,7 @@ import (
func newTestTokensController(ctx context.Context) *controllers.TokenController {
return &controllers.TokenController{
DB: newTestDbConnection(ctx),
DB: newTestDBConnection(ctx),
Redis: newTestRedisConnection(),
}
}
@@ -104,6 +104,14 @@ func TestIntegrationGetToken_NotExists(t *testing.T) {
assert.Empty(t, token)
}
func TestIntegrationGetToken_WrongRequest(t *testing.T) {
ctrl := newTestTokensController(t.Context())
token, err := ctrl.Get(t.Context(), "test", "test")
assert.Error(t, err)
assert.ErrorIs(t, err, controllers.ErrServerError)
assert.Empty(t, token)
}
func TestIntegrationVerifyTokenOwner_Success(t *testing.T) {
// Create a user for the token
ctrlAccount := newTestAccountController(t.Context())

View File

@@ -6,7 +6,6 @@ import (
"github.com/jackc/pgx/v5/pgxpool"
"github.com/jackc/pgx/v5/stdlib"
_ "github.com/jackc/pgx/v5/stdlib"
)
func Open(ctx context.Context, dsn string) (*sql.DB, error) {