testutils

package
v0.0.0-...-17b847e Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2025 License: AGPL-3.0 Imports: 11 Imported by: 0

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 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

Jump to

Keyboard shortcuts

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