Documentation
¶
Overview ¶
Package testing provides comprehensive mocking utilities for testing applications that integrate with the AuthSome authentication framework.
Version 2.0: Now with full multi-tenancy support, core/contexts integration, and Forge HTTP handler testing.
Overview ¶
This package is designed for external users who need to test their applications that depend on AuthSome without setting up a full AuthSome instance with database, Redis, and other infrastructure components.
Quick Start ¶
Import the package and create a mock instance:
import (
"testing"
authsometesting "github.com/xraph/authsome/testing"
)
func TestMyHandler(t *testing.T) {
mock := authsometesting.NewMock(t)
defer mock.Reset()
// Create authenticated context with full tenant hierarchy
ctx := mock.NewTestContext()
// Your test code here
}
Core Features ¶
- Multi-Tenancy: Full App → Environment → Organization hierarchy support
- Core Contexts: Uses AuthSome's actual context system from core/contexts
- Mock Users: Create test users with various states (verified, unverified, active, inactive)
- Mock Sessions: Create active or expired sessions for testing authentication flows
- Mock Organizations: Create organizations and manage memberships
- Mock Services: UserService, SessionService, OrganizationService with full CRUD operations
- Context Helpers: Easily add authentication data to context with typed keys
- Common Scenarios: Pre-configured scenarios for typical test cases
- Authorization Helpers: Test role-based access control
- Forge Mocking: Mock Forge contexts for HTTP handler testing
- Builder Pattern: Fluent API for creating complex test data
- Thread-Safe: Safe for concurrent use in tests
Creating Test Data ¶
Create users:
user := mock.CreateUser("user@example.com", "Test User")
adminUser := mock.CreateUserWithRole("admin@example.com", "Admin", "admin")
Create sessions:
session := mock.CreateSession(user.ID, org.ID) expiredSession := mock.CreateExpiredSession(user.ID, org.ID)
Create organizations:
org := mock.CreateOrganization("My Org", "my-org")
member := mock.AddUserToOrg(user.ID, org.ID, "member")
Working with Context ¶
Create authenticated contexts:
// Quick way - creates user, org, and session automatically
ctx := mock.NewTestContext()
// With specific user
user := mock.CreateUser("user@example.com", "Test User")
ctx := mock.NewTestContextWithUser(user)
// Manual setup with full control
ctx := context.Background()
ctx = mock.WithApp(ctx, app.ID)
ctx = mock.WithEnvironment(ctx, env.ID)
ctx = mock.WithOrganization(ctx, org.ID)
ctx = mock.WithSession(ctx, session.ID)
Retrieve from context (using core/contexts):
userID, ok := authsometesting.GetUserID(ctx) appID, ok := authsometesting.GetAppID(ctx) envID, ok := authsometesting.GetEnvironmentID(ctx) orgID, ok := authsometesting.GetOrganizationID(ctx) session, ok := authsometesting.GetSession(ctx) // Get full entities from context user, err := mock.GetUserFromContext(ctx) org, err := mock.GetOrganizationFromContext(ctx)
Authorization Testing ¶
Test authentication and authorization:
// Require authentication user, err := mock.RequireAuth(ctx) // Require organization membership member, err := mock.RequireOrgMember(ctx, orgID) // Require specific role member, err := mock.RequireOrgRole(ctx, orgID, "admin")
Common Scenarios ¶
Use pre-configured scenarios for typical test cases:
scenarios := mock.NewCommonScenarios() // Various scenarios available: authUser := scenarios.AuthenticatedUser() adminUser := scenarios.AdminUser() unverifiedUser := scenarios.UnverifiedUser() multiOrgUser := scenarios.MultiOrgUser() expiredSession := scenarios.ExpiredSession() unauthenticated := scenarios.UnauthenticatedUser() inactiveUser := scenarios.InactiveUser() // Use scenario in tests user := authUser.User ctx := authUser.Context
Service Methods ¶
Test with mock services that implement the same interfaces as real services:
// User service user, err := mock.UserService.GetByEmail(ctx, "user@example.com") user, err := mock.UserService.GetByID(ctx, userID) user, err := mock.UserService.Create(ctx, req) user, err := mock.UserService.Update(ctx, userID, req) // Session service session, err := mock.SessionService.GetByToken(ctx, token) session, err := mock.SessionService.Validate(ctx, token) err := mock.SessionService.Delete(ctx, sessionID) // Organization service org, err := mock.OrganizationService.GetByID(ctx, orgID) org, err := mock.OrganizationService.GetBySlug(ctx, "my-org") membersResp, err := mock.OrganizationService.GetMembers(ctx, orgID) orgs, err := mock.OrganizationService.GetUserOrganizations(ctx, userID)
Builder Pattern ¶
Create complex test data with fluent API:
app := mock.NewApp().
WithName("Production App").
WithSlug("prod-app").
Build()
user := mock.NewUser().
WithEmail("admin@example.com").
WithName("Admin User").
WithRole("admin").
Build()
HTTP Handler Testing ¶
Test HTTP handlers with mock Forge contexts:
// Quick authenticated context
forgeCtx := mock.QuickAuthenticatedForgeContext("GET", "/api/profile")
// With specific user
user := mock.CreateUser("user@example.com", "Test User")
forgeCtx := mock.QuickAuthenticatedForgeContextWithUser("POST", "/api/data", user)
// Full control
req := httptest.NewRequest("GET", "/api/profile", nil)
session := mock.CreateSession(user.ID, org.ID)
forgeCtx := mock.MockAuthenticatedForgeContext(req, user, app, env, org, session)
Complete Example ¶
Here's a complete example testing a handler that requires authentication:
func TestGetUserProfile(t *testing.T) {
mock := authsometesting.NewMock(t)
defer mock.Reset()
// Handler being tested
getUserProfile := func(ctx context.Context, mock *authsometesting.Mock) (map[string]string, error) {
userID, ok := authsometesting.GetUserID(ctx)
if !ok || userID.IsNil() {
return nil, authsometesting.ErrNotAuthenticated
}
user, err := mock.GetUserFromContext(ctx)
if err != nil {
return nil, err
}
return map[string]string{
"id": user.ID.String(),
"email": user.Email,
"name": user.Name,
}, nil
}
t.Run("authenticated", func(t *testing.T) {
ctx := mock.NewTestContext()
profile, err := getUserProfile(ctx, mock)
require.NoError(t, err)
assert.NotEmpty(t, profile["id"])
})
t.Run("unauthenticated", func(t *testing.T) {
ctx := context.Background()
_, err := getUserProfile(ctx, mock)
assert.Equal(t, authsometesting.ErrNotAuthenticated, err)
})
}
Migration from v1.x ¶
Key changes in v2.0:
- All ID parameters now use xid.ID instead of string
- Context helpers renamed to match core/contexts (GetUserID vs GetLoggedInUser)
- WithOrg renamed to WithOrganization
- Added App and Environment support
- Added builder pattern for test data creation
- Added Forge context mocking
- Member storage now uses OrganizationMember schema
Best Practices ¶
- Always call defer mock.Reset() to clean up between tests
- Use common scenarios for typical test cases
- Test both success and failure cases
- Use table-driven tests for multiple scenarios
- Verify context values before using them (check ok return values)
- Test full tenant hierarchy (App/Environment/Organization) for multi-tenant features
- Use builder pattern for complex test data
- Leverage Forge mocking for HTTP handler tests
Thread Safety ¶
The mock implementation is thread-safe and can be used across multiple goroutines in your tests.
Limitations ¶
This is a testing mock and does not:
- Connect to a real database
- Implement full RBAC policy evaluation
- Support all AuthSome plugins
- Provide rate limiting or caching
- Validate complex business logic
For integration testing with real services, use the full AuthSome setup.
See the README.md file in this package for more detailed documentation and examples.
Package testing provides mocked Authsome instances and utilities for testing external integrations with the authsome authentication framework.
This package is designed for developers building applications that integrate with authsome and need to test their code without setting up a full authsome instance with database, Redis, etc.
Example usage:
import (
"testing"
authsometesting "github.com/xraph/authsome/testing"
)
func TestMyHandler(t *testing.T) {
// Create a mock authsome with a test user
mock := authsometesting.NewMock(t)
user := mock.CreateUser("test@example.com", "Test User")
org := mock.GetDefaultOrg()
session := mock.CreateSession(user.ID.String(), org.ID.String())
// Set up authenticated context
ctx := mock.WithSession(context.Background(), session.ID.String())
// Your test code here
result, err := myService.DoSomething(ctx)
// ... assertions
}
Index ¶
- Variables
- func GetAppID(ctx context.Context) (xid.ID, bool)
- func GetEnvironmentID(ctx context.Context) (xid.ID, bool)
- func GetOrganizationID(ctx context.Context) (xid.ID, bool)
- func GetSession(ctx context.Context) (*schema.Session, bool)
- func GetUserID(ctx context.Context) (xid.ID, bool)
- type AppBuilder
- type CommonScenarios
- func (cs *CommonScenarios) AdminUser() *Scenario
- func (cs *CommonScenarios) AuthenticatedUser() *Scenario
- func (cs *CommonScenarios) ExpiredSession() *Scenario
- func (cs *CommonScenarios) InactiveUser() *Scenario
- func (cs *CommonScenarios) MultiOrgUser() *Scenario
- func (cs *CommonScenarios) UnauthenticatedUser() *Scenario
- func (cs *CommonScenarios) UnverifiedUser() *Scenario
- type EnvironmentBuilder
- type Mock
- func (m *Mock) AddUserToOrg(userID, orgID xid.ID, role string) *schema.OrganizationMember
- func (m *Mock) CreateExpiredSession(userID, orgID xid.ID) *schema.Session
- func (m *Mock) CreateOrganization(name, slug string) *schema.Organization
- func (m *Mock) CreateSession(userID, orgID xid.ID) *schema.Session
- func (m *Mock) CreateUser(email, name string) *schema.User
- func (m *Mock) CreateUserWithRole(email, name, role string) *schema.User
- func (m *Mock) GetApp(appID xid.ID) (*schema.App, error)
- func (m *Mock) GetAppFromContext(ctx context.Context) (*schema.App, error)
- func (m *Mock) GetDefaultApp() *schema.App
- func (m *Mock) GetDefaultEnvironment() *schema.Environment
- func (m *Mock) GetDefaultOrg() *schema.Organization
- func (m *Mock) GetEnvironment(envID xid.ID) (*schema.Environment, error)
- func (m *Mock) GetEnvironmentFromContext(ctx context.Context) (*schema.Environment, error)
- func (m *Mock) GetOrganization(orgID xid.ID) (*schema.Organization, error)
- func (m *Mock) GetOrganizationFromContext(ctx context.Context) (*schema.Organization, error)
- func (m *Mock) GetSession(sessionID xid.ID) (*schema.Session, error)
- func (m *Mock) GetUser(userID xid.ID) (*schema.User, error)
- func (m *Mock) GetUserFromContext(ctx context.Context) (*schema.User, error)
- func (m *Mock) GetUserOrgs(userID xid.ID) ([]*schema.Organization, error)
- func (m *Mock) MockAuthenticatedForgeContext(req *http.Request, user *schema.User, app *schema.App, env *schema.Environment, ...) *MockForgeContext
- func (m *Mock) NewApp() *AppBuilder
- func (m *Mock) NewCommonScenarios() *CommonScenarios
- func (m *Mock) NewEnvironment(appID xid.ID) *EnvironmentBuilder
- func (m *Mock) NewMockForgeContext(req *http.Request) *MockForgeContext
- func (m *Mock) NewOrganization() *OrganizationBuilder
- func (m *Mock) NewTestContext() context.Context
- func (m *Mock) NewTestContextWithUser(user *schema.User) context.Context
- func (m *Mock) NewUser() *UserBuilder
- func (m *Mock) QuickAuthenticatedForgeContext(method, path string) *MockForgeContext
- func (m *Mock) QuickAuthenticatedForgeContextWithUser(method, path string, user *schema.User) *MockForgeContext
- func (m *Mock) QuickForgeContext(method, path string) *MockForgeContext
- func (m *Mock) RequireAuth(ctx context.Context) (*schema.User, error)
- func (m *Mock) RequireOrgMember(ctx context.Context, orgID xid.ID) (*schema.OrganizationMember, error)
- func (m *Mock) RequireOrgRole(ctx context.Context, orgID xid.ID, requiredRole string) (*schema.OrganizationMember, error)
- func (m *Mock) Reset()
- func (m *Mock) WithApp(ctx context.Context, appID xid.ID) context.Context
- func (m *Mock) WithEnvironment(ctx context.Context, envID xid.ID) context.Context
- func (m *Mock) WithOrganization(ctx context.Context, orgID xid.ID) context.Context
- func (m *Mock) WithSession(ctx context.Context, sessionID xid.ID) context.Context
- func (m *Mock) WithUser(ctx context.Context, userID xid.ID) context.Context
- type MockForgeContext
- func (c *MockForgeContext) BindJSON(v interface{}) error
- func (c *MockForgeContext) Cookie(name string) (string, error)
- func (c *MockForgeContext) GetBody() interface{}
- func (c *MockForgeContext) GetStatus() int
- func (c *MockForgeContext) HTML(status int, html string) error
- func (c *MockForgeContext) Header() http.Header
- func (c *MockForgeContext) JSON(status int, data interface{}) error
- func (c *MockForgeContext) Param(name string) string
- func (c *MockForgeContext) Query(key string) string
- func (c *MockForgeContext) Redirect(status int, url string) error
- func (c *MockForgeContext) Request() *http.Request
- func (c *MockForgeContext) Response() http.ResponseWriter
- func (c *MockForgeContext) SetHeader(key, value string)
- func (c *MockForgeContext) SetParam(name, value string)
- func (c *MockForgeContext) String(status int, s string) error
- type MockOrganizationService
- func (s *MockOrganizationService) AddMember(ctx context.Context, orgID, userID xid.ID, role string) (*organization.Member, error)
- func (s *MockOrganizationService) CreateOrganization(ctx context.Context, req *organization.CreateOrganizationRequest, ...) (*organization.Organization, error)
- func (s *MockOrganizationService) FindOrganizationByID(ctx context.Context, id xid.ID) (*organization.Organization, error)
- func (s *MockOrganizationService) FindOrganizationBySlug(ctx context.Context, appID, environmentID xid.ID, slug string) (*organization.Organization, error)
- func (s *MockOrganizationService) GetByID(ctx context.Context, id xid.ID) (*organization.Organization, error)
- func (s *MockOrganizationService) GetBySlug(ctx context.Context, slug string) (*organization.Organization, error)
- func (s *MockOrganizationService) GetMembers(ctx context.Context, orgID xid.ID) (*pagination.PageResponse[*organization.Member], error)
- func (s *MockOrganizationService) GetUserMemberships(ctx context.Context, userID xid.ID, filter *pagination.PaginationParams) (*pagination.PageResponse[*organization.Member], error)
- func (s *MockOrganizationService) GetUserOrganizations(ctx context.Context, userID xid.ID) ([]*organization.Organization, error)
- func (s *MockOrganizationService) ListMembers(ctx context.Context, filter *organization.ListMembersFilter) (*pagination.PageResponse[*organization.Member], error)
- func (s *MockOrganizationService) ListOrganizations(ctx context.Context, filter *organization.ListOrganizationsFilter) (*pagination.PageResponse[*organization.Organization], error)
- type MockSessionService
- func (s *MockSessionService) Create(ctx context.Context, req *session.CreateSessionRequest) (*schema.Session, error)
- func (s *MockSessionService) Delete(ctx context.Context, sessionID xid.ID) error
- func (s *MockSessionService) GetByID(ctx context.Context, sessionID xid.ID) (*schema.Session, error)
- func (s *MockSessionService) GetByToken(ctx context.Context, token string) (*schema.Session, error)
- func (s *MockSessionService) Validate(ctx context.Context, token string) (*schema.Session, error)
- type MockUserService
- func (s *MockUserService) Create(ctx context.Context, req *user.CreateUserRequest) (*schema.User, error)
- func (s *MockUserService) Delete(ctx context.Context, userID xid.ID) error
- func (s *MockUserService) GetByEmail(ctx context.Context, email string) (*schema.User, error)
- func (s *MockUserService) GetByID(ctx context.Context, userID xid.ID) (*schema.User, error)
- func (s *MockUserService) Update(ctx context.Context, userID xid.ID, req *user.UpdateUserRequest) (*schema.User, error)
- type OrganizationBuilder
- func (b *OrganizationBuilder) Build() *schema.Organization
- func (b *OrganizationBuilder) WithApp(appID xid.ID) *OrganizationBuilder
- func (b *OrganizationBuilder) WithEnvironment(envID xid.ID) *OrganizationBuilder
- func (b *OrganizationBuilder) WithMetadata(metadata map[string]interface{}) *OrganizationBuilder
- func (b *OrganizationBuilder) WithName(name string) *OrganizationBuilder
- func (b *OrganizationBuilder) WithSlug(slug string) *OrganizationBuilder
- type Scenario
- type TestError
- type UserBuilder
Constants ¶
This section is empty.
Variables ¶
var ( ErrNotAuthenticated = &TestError{Code: "not_authenticated", Message: "user is not authenticated"} ErrInvalidSession = &TestError{Code: "invalid_session", Message: "session is invalid or expired"} ErrUserNotFound = &TestError{Code: "user_not_found", Message: "user not found"} ErrUserInactive = &TestError{Code: "user_inactive", Message: "user account is inactive"} ErrOrgNotFound = &TestError{Code: "org_not_found", Message: "organization not found"} ErrNotOrgMember = &TestError{Code: "not_org_member", Message: "user is not a member of this organization"} ErrInsufficientPermissions = &TestError{Code: "insufficient_permissions", Message: "user does not have required permissions"} )
Common test errors
Functions ¶
func GetEnvironmentID ¶
GetEnvironmentID retrieves the environment ID from context using core/contexts
func GetOrganizationID ¶
GetOrganizationID retrieves the organization ID from context using core/contexts
func GetSession ¶
GetSession retrieves the session object from context
Types ¶
type AppBuilder ¶
type AppBuilder struct {
// contains filtered or unexported fields
}
AppBuilder helps create test apps with fluent API
func (*AppBuilder) Build ¶
func (b *AppBuilder) Build() *schema.App
Build saves the app and returns it
func (*AppBuilder) WithName ¶
func (b *AppBuilder) WithName(name string) *AppBuilder
WithName sets the app name
func (*AppBuilder) WithSlug ¶
func (b *AppBuilder) WithSlug(slug string) *AppBuilder
WithSlug sets the app slug
type CommonScenarios ¶
type CommonScenarios struct {
// contains filtered or unexported fields
}
CommonScenarios provides pre-configured test scenarios.
func (*CommonScenarios) AdminUser ¶
func (cs *CommonScenarios) AdminUser() *Scenario
AdminUser returns a scenario with an admin user.
func (*CommonScenarios) AuthenticatedUser ¶
func (cs *CommonScenarios) AuthenticatedUser() *Scenario
AuthenticatedUser returns a scenario with a basic authenticated user.
func (*CommonScenarios) ExpiredSession ¶
func (cs *CommonScenarios) ExpiredSession() *Scenario
ExpiredSession returns a scenario with an expired session.
func (*CommonScenarios) InactiveUser ¶
func (cs *CommonScenarios) InactiveUser() *Scenario
InactiveUser returns a scenario with an inactive user account.
func (*CommonScenarios) MultiOrgUser ¶
func (cs *CommonScenarios) MultiOrgUser() *Scenario
MultiOrgUser returns a scenario with a user belonging to multiple organizations.
func (*CommonScenarios) UnauthenticatedUser ¶
func (cs *CommonScenarios) UnauthenticatedUser() *Scenario
UnauthenticatedUser returns a scenario with no authentication.
func (*CommonScenarios) UnverifiedUser ¶
func (cs *CommonScenarios) UnverifiedUser() *Scenario
UnverifiedUser returns a scenario with an unverified user.
type EnvironmentBuilder ¶
type EnvironmentBuilder struct {
// contains filtered or unexported fields
}
EnvironmentBuilder helps create test environments with fluent API
func (*EnvironmentBuilder) Build ¶
func (b *EnvironmentBuilder) Build() *schema.Environment
Build saves the environment and returns it
func (*EnvironmentBuilder) WithName ¶
func (b *EnvironmentBuilder) WithName(name string) *EnvironmentBuilder
WithName sets the environment name
func (*EnvironmentBuilder) WithSlug ¶
func (b *EnvironmentBuilder) WithSlug(slug string) *EnvironmentBuilder
WithSlug sets the environment slug
type Mock ¶
type Mock struct {
// Core services
UserService *MockUserService
SessionService *MockSessionService
OrganizationService *MockOrganizationService
// contains filtered or unexported fields
}
Mock provides a mocked Authsome instance for testing.
func NewMock ¶
NewMock creates a new mock Authsome instance for testing. It automatically creates default app, environment, and organization.
func (*Mock) AddUserToOrg ¶
AddUserToOrg adds a user to an organization with the specified role.
func (*Mock) CreateExpiredSession ¶
CreateExpiredSession creates an expired session for testing expiration scenarios.
func (*Mock) CreateOrganization ¶
func (m *Mock) CreateOrganization(name, slug string) *schema.Organization
CreateOrganization creates a test organization.
func (*Mock) CreateSession ¶
CreateSession creates a test session for the given user and organization.
func (*Mock) CreateUser ¶
CreateUser creates a test user with the given email and name. The user is automatically added to the default organization.
func (*Mock) CreateUserWithRole ¶
CreateUserWithRole creates a test user with a specific role in the default organization.
func (*Mock) GetAppFromContext ¶
GetAppFromContext retrieves the full app object from context using the Mock
func (*Mock) GetDefaultApp ¶
GetDefaultApp returns the default test app.
func (*Mock) GetDefaultEnvironment ¶
func (m *Mock) GetDefaultEnvironment() *schema.Environment
GetDefaultEnvironment returns the default test environment.
func (*Mock) GetDefaultOrg ¶
func (m *Mock) GetDefaultOrg() *schema.Organization
GetDefaultOrg returns the default test organization.
func (*Mock) GetEnvironment ¶
GetEnvironment retrieves an environment by ID.
func (*Mock) GetEnvironmentFromContext ¶
GetEnvironmentFromContext retrieves the full environment object from context using the Mock
func (*Mock) GetOrganization ¶
GetOrganization retrieves an organization by ID.
func (*Mock) GetOrganizationFromContext ¶
GetOrganizationFromContext retrieves the full organization object from context using the Mock
func (*Mock) GetSession ¶
GetSession retrieves a session by ID.
func (*Mock) GetUserFromContext ¶
GetUserFromContext retrieves the full user object from context using the Mock
func (*Mock) GetUserOrgs ¶
GetUserOrgs returns all organizations a user is a member of.
func (*Mock) MockAuthenticatedForgeContext ¶
func (m *Mock) MockAuthenticatedForgeContext( req *http.Request, user *schema.User, app *schema.App, env *schema.Environment, org *schema.Organization, session *schema.Session, ) *MockForgeContext
MockAuthenticatedForgeContext creates an authenticated mock Forge context This is useful for testing HTTP handlers that require authentication
func (*Mock) NewCommonScenarios ¶
func (m *Mock) NewCommonScenarios() *CommonScenarios
NewCommonScenarios creates a new set of common test scenarios.
func (*Mock) NewEnvironment ¶
func (m *Mock) NewEnvironment(appID xid.ID) *EnvironmentBuilder
NewEnvironment creates a new EnvironmentBuilder
func (*Mock) NewMockForgeContext ¶
func (m *Mock) NewMockForgeContext(req *http.Request) *MockForgeContext
NewMockForgeContext creates a basic mock forge context for testing
func (*Mock) NewOrganization ¶
func (m *Mock) NewOrganization() *OrganizationBuilder
NewOrganization creates a new OrganizationBuilder
func (*Mock) NewTestContext ¶
NewTestContext creates fully authenticated context with all tenancy levels
func (*Mock) NewTestContextWithUser ¶
NewTestContextWithUser creates authenticated context for specific user
func (*Mock) QuickAuthenticatedForgeContext ¶
func (m *Mock) QuickAuthenticatedForgeContext(method, path string) *MockForgeContext
QuickAuthenticatedForgeContext creates authenticated Forge context with defaults This is the simplest way to create an authenticated context for testing
func (*Mock) QuickAuthenticatedForgeContextWithUser ¶
func (m *Mock) QuickAuthenticatedForgeContextWithUser(method, path string, user *schema.User) *MockForgeContext
QuickAuthenticatedForgeContextWithUser creates authenticated Forge context for specific user
func (*Mock) QuickForgeContext ¶
func (m *Mock) QuickForgeContext(method, path string) *MockForgeContext
QuickForgeContext creates a basic unauthenticated Forge context
func (*Mock) RequireAuth ¶
RequireAuth is a helper that mimics authentication middleware behavior. It checks if the context has a valid session and returns the user.
func (*Mock) RequireOrgMember ¶
func (m *Mock) RequireOrgMember(ctx context.Context, orgID xid.ID) (*schema.OrganizationMember, error)
RequireOrgMember checks if the user is a member of the specified organization.
func (*Mock) RequireOrgRole ¶
func (m *Mock) RequireOrgRole(ctx context.Context, orgID xid.ID, requiredRole string) (*schema.OrganizationMember, error)
RequireOrgRole checks if the user has the specified role in the organization.
func (*Mock) Reset ¶
func (m *Mock) Reset()
Reset clears all test data. Useful for cleanup between tests.
func (*Mock) WithEnvironment ¶
WithEnvironment sets environment in context using core/contexts
func (*Mock) WithOrganization ¶
WithOrganization sets organization in context using core/contexts
func (*Mock) WithSession ¶
WithSession adds session and user to context
type MockForgeContext ¶
type MockForgeContext struct {
// contains filtered or unexported fields
}
MockForgeContext is a mock implementation of forge.Context for testing
func (*MockForgeContext) BindJSON ¶
func (c *MockForgeContext) BindJSON(v interface{}) error
func (*MockForgeContext) GetBody ¶
func (c *MockForgeContext) GetBody() interface{}
func (*MockForgeContext) GetStatus ¶
func (c *MockForgeContext) GetStatus() int
func (*MockForgeContext) Header ¶
func (c *MockForgeContext) Header() http.Header
func (*MockForgeContext) JSON ¶
func (c *MockForgeContext) JSON(status int, data interface{}) error
func (*MockForgeContext) Param ¶
func (c *MockForgeContext) Param(name string) string
func (*MockForgeContext) Query ¶
func (c *MockForgeContext) Query(key string) string
func (*MockForgeContext) Redirect ¶
func (c *MockForgeContext) Redirect(status int, url string) error
func (*MockForgeContext) Request ¶
func (c *MockForgeContext) Request() *http.Request
func (*MockForgeContext) Response ¶
func (c *MockForgeContext) Response() http.ResponseWriter
func (*MockForgeContext) SetHeader ¶
func (c *MockForgeContext) SetHeader(key, value string)
func (*MockForgeContext) SetParam ¶
func (c *MockForgeContext) SetParam(name, value string)
type MockOrganizationService ¶
type MockOrganizationService struct {
// contains filtered or unexported fields
}
MockOrganizationService implements organization service methods for testing.
func (*MockOrganizationService) AddMember ¶
func (s *MockOrganizationService) AddMember(ctx context.Context, orgID, userID xid.ID, role string) (*organization.Member, error)
AddMember adds a user as a member to an organization
func (*MockOrganizationService) CreateOrganization ¶
func (s *MockOrganizationService) CreateOrganization(ctx context.Context, req *organization.CreateOrganizationRequest, creatorUserID, appID, environmentID xid.ID) (*organization.Organization, error)
CreateOrganization creates a new organization
func (*MockOrganizationService) FindOrganizationByID ¶
func (s *MockOrganizationService) FindOrganizationByID(ctx context.Context, id xid.ID) (*organization.Organization, error)
FindOrganizationByID retrieves an organization by ID
func (*MockOrganizationService) FindOrganizationBySlug ¶
func (s *MockOrganizationService) FindOrganizationBySlug(ctx context.Context, appID, environmentID xid.ID, slug string) (*organization.Organization, error)
FindOrganizationBySlug retrieves an organization by slug
func (*MockOrganizationService) GetByID ¶
func (s *MockOrganizationService) GetByID(ctx context.Context, id xid.ID) (*organization.Organization, error)
GetByID is an alias for FindOrganizationByID for compatibility
func (*MockOrganizationService) GetBySlug ¶
func (s *MockOrganizationService) GetBySlug(ctx context.Context, slug string) (*organization.Organization, error)
GetBySlug is an alias for FindOrganizationBySlug for compatibility
func (*MockOrganizationService) GetMembers ¶
func (s *MockOrganizationService) GetMembers(ctx context.Context, orgID xid.ID) (*pagination.PageResponse[*organization.Member], error)
GetMembers is an alias for ListMembers for compatibility
func (*MockOrganizationService) GetUserMemberships ¶
func (s *MockOrganizationService) GetUserMemberships(ctx context.Context, userID xid.ID, filter *pagination.PaginationParams) (*pagination.PageResponse[*organization.Member], error)
GetUserMemberships retrieves all memberships for a user
func (*MockOrganizationService) GetUserOrganizations ¶
func (s *MockOrganizationService) GetUserOrganizations(ctx context.Context, userID xid.ID) ([]*organization.Organization, error)
GetUserOrganizations retrieves all organizations a user is a member of
func (*MockOrganizationService) ListMembers ¶
func (s *MockOrganizationService) ListMembers(ctx context.Context, filter *organization.ListMembersFilter) (*pagination.PageResponse[*organization.Member], error)
ListMembers lists members of an organization
func (*MockOrganizationService) ListOrganizations ¶
func (s *MockOrganizationService) ListOrganizations(ctx context.Context, filter *organization.ListOrganizationsFilter) (*pagination.PageResponse[*organization.Organization], error)
ListOrganizations lists organizations with pagination
type MockSessionService ¶
type MockSessionService struct {
// contains filtered or unexported fields
}
MockSessionService implements core session service methods for testing.
func (*MockSessionService) Create ¶
func (s *MockSessionService) Create(ctx context.Context, req *session.CreateSessionRequest) (*schema.Session, error)
func (*MockSessionService) GetByToken ¶
type MockUserService ¶
type MockUserService struct {
// contains filtered or unexported fields
}
MockUserService implements core user service methods for testing.
func (*MockUserService) Create ¶
func (s *MockUserService) Create(ctx context.Context, req *user.CreateUserRequest) (*schema.User, error)
func (*MockUserService) GetByEmail ¶
type OrganizationBuilder ¶
type OrganizationBuilder struct {
// contains filtered or unexported fields
}
OrganizationBuilder helps create test organizations with fluent API
func (*OrganizationBuilder) Build ¶
func (b *OrganizationBuilder) Build() *schema.Organization
Build saves the organization and returns it
func (*OrganizationBuilder) WithApp ¶
func (b *OrganizationBuilder) WithApp(appID xid.ID) *OrganizationBuilder
WithApp sets the app ID
func (*OrganizationBuilder) WithEnvironment ¶
func (b *OrganizationBuilder) WithEnvironment(envID xid.ID) *OrganizationBuilder
WithEnvironment sets the environment ID
func (*OrganizationBuilder) WithMetadata ¶
func (b *OrganizationBuilder) WithMetadata(metadata map[string]interface{}) *OrganizationBuilder
WithMetadata sets metadata
func (*OrganizationBuilder) WithName ¶
func (b *OrganizationBuilder) WithName(name string) *OrganizationBuilder
WithName sets the organization name
func (*OrganizationBuilder) WithSlug ¶
func (b *OrganizationBuilder) WithSlug(slug string) *OrganizationBuilder
WithSlug sets the organization slug
type Scenario ¶
type Scenario struct {
Name string
Description string
User *schema.User
App *schema.App
Environment *schema.Environment
Org *schema.Organization
Session *schema.Session
Context context.Context
}
Scenario represents a common test scenario with pre-configured data.
type UserBuilder ¶
type UserBuilder struct {
// contains filtered or unexported fields
}
UserBuilder helps create test users with fluent API
func (*UserBuilder) Build ¶
func (b *UserBuilder) Build() *schema.User
Build saves the user and returns it
func (*UserBuilder) WithEmail ¶
func (b *UserBuilder) WithEmail(email string) *UserBuilder
WithEmail sets the user email
func (*UserBuilder) WithEmailVerified ¶
func (b *UserBuilder) WithEmailVerified(verified bool) *UserBuilder
WithEmailVerified sets email verification status
func (*UserBuilder) WithName ¶
func (b *UserBuilder) WithName(name string) *UserBuilder
WithName sets the user name
func (*UserBuilder) WithRole ¶
func (b *UserBuilder) WithRole(role string) *UserBuilder
WithRole sets the role for the default organization