testutils

package
v0.0.0-...-9cec0f7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 23, 2026 License: AGPL-3.0 Imports: 13 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const (
	ValidJWTToken     = "" /* 168-byte string literal not displayed */
	ExpiredJWTToken   = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ0ZXN0LXVzZXIiLCJleHAiOjEsInVzZXJfaWQiOiJ0ZXN0LXVzZXIifQ.test-signature"
	InvalidJWTToken   = "invalid.jwt.token"
	MalformedJWTToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.invalid-payload.signature"
)

Test JWT tokens for different scenarios

Variables

View Source
var LogtoMockResponses = struct {
	UserInfo      string
	Roles         string
	Organizations string
}{
	UserInfo: `{
		"sub": "test-user-id",
		"username": "test-user",
		"email": "test@example.com",
		"name": "Test User",
		"custom_data": {
			"organizationId": "test-org-id"
		}
	}`,
	Roles: `{
		"data": [
			{
				"id": "admin-role-id",
				"name": "Admin",
				"description": "Administrator role"
			},
			{
				"id": "support-role-id",
				"name": "Support",
				"description": "Support role"
			}
		]
	}`,
	Organizations: `{
		"data": [
			{
				"id": "test-org-id",
				"name": "Test Organization",
				"custom_data": {
					"role": "Customer"
				}
			}
		]
	}`,
}

LogtoMockResponses contains mock responses for Logto API

Functions

func AssertJSONResponse

func AssertJSONResponse(t *testing.T, w *httptest.ResponseRecorder, expectedStatus int) map[string]interface{}

AssertJSONResponse checks if response is valid JSON and matches expected status

func AuthHeaders

func AuthHeaders(token string) map[string]string

AuthHeaders creates authorization headers for testing

func MakeRequest

func MakeRequest(t *testing.T, router *gin.Engine, method, url string, body interface{}, headers map[string]string) *httptest.ResponseRecorder

MakeRequest performs an HTTP request for testing

func MockCreateUserRequest

func MockCreateUserRequest() map[string]interface{}

MockCreateUserRequest creates a test user creation request

func MockCustomerAdmin

func MockCustomerAdmin() *models.User

MockCustomerAdmin creates a customer admin user

func MockDistributorAdmin

func MockDistributorAdmin() *models.User

MockDistributorAdmin creates a distributor admin user

func MockEnvironment

func MockEnvironment(t *testing.T)

MockEnvironment sets up test environment variables

func MockHTTPServer

func MockHTTPServer(t *testing.T, handler http.HandlerFunc) *httptest.Server

MockHTTPServer creates a test HTTP server for external API mocking

func MockJWTToken

func MockJWTToken(userID, orgID string, exp int64) string

MockJWTToken generates a simple mock JWT for testing (not cryptographically valid)

func MockLogtoHTTPServer

func MockLogtoHTTPServer(t *testing.T) *httptest.Server

MockLogtoHTTPServer creates a mock HTTP server that simulates Logto API responses

func MockOwnerUser

func MockOwnerUser() *models.User

MockOwnerUser creates a user with Owner role and all permissions

func MockResellerAdmin

func MockResellerAdmin() *models.User

MockResellerAdmin creates a reseller admin user

func MockSupportUser

func MockSupportUser() *models.User

MockSupportUser creates a support user with limited permissions

func MockUser

func MockUser(userID, orgID string, userRoles, orgRoles []string, userPerms, orgPerms []string) *models.User

MockUser creates a test user with specified roles and permissions

func SetupLogger

func SetupLogger()

SetupLogger initializes logger for testing

func SetupTestGin

func SetupTestGin() *gin.Engine

SetupTestGin configures Gin for testing

Types

type MockLogtoService

type MockLogtoService struct {
	mock.Mock
}

MockLogtoService is a mock implementation of Logto service functions

func (*MockLogtoService) EnrichUserWithRolesAndPermissions

func (m *MockLogtoService) EnrichUserWithRolesAndPermissions(user *models.User) error

MockEnrichUserWithRolesAndPermissions simulates enriching user with roles and permissions

func (*MockLogtoService) GetUserInfoFromLogto

func (m *MockLogtoService) GetUserInfoFromLogto(accessToken string) (*models.User, error)

MockGetUserInfoFromLogto simulates fetching user info from Logto

type MockRedisClient

type MockRedisClient struct {
	mock.Mock
}

MockRedisClient is a mock implementation of Redis client for testing

func (*MockRedisClient) Del

func (m *MockRedisClient) Del(keys ...string) (int64, error)

Del mocks Redis DEL operation

func (*MockRedisClient) Get

func (m *MockRedisClient) Get(key string) ([]byte, error)

Get mocks Redis GET operation

func (*MockRedisClient) Info

func (m *MockRedisClient) Info() (map[string]string, error)

Info mocks Redis INFO operation

func (*MockRedisClient) Set

func (m *MockRedisClient) Set(key string, value interface{}, expiration time.Duration) error

Set mocks Redis SET operation

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL