Documentation
¶
Overview ¶
Package testutil provides a ready-to-use HTTP test server backed by a real AuthSome engine with in-memory stores and all standard plugins. It creates an httptest.Server that mirrors the production API surface so the generated SDK client can be exercised end-to-end over HTTP.
Index ¶
- Constants
- type ServerOption
- type TestServer
- func (ts *TestServer) AddMember(t *testing.T, orgID, userID string, role organization.MemberRole) *organization.Member
- func (ts *TestServer) Close()
- func (ts *TestServer) CreateOrg(t *testing.T, ownerUserID, name, slug string) *organization.Organization
- func (ts *TestServer) CreateUser(t *testing.T, email, pass string) (resp *authclient.AuthResponse, token string)
- func (ts *TestServer) CreateUserClient(t *testing.T, email, pass string) *authclient.Client
- func (ts *TestServer) NewTestUserFactory() *TestUserFactory
- func (ts *TestServer) SessionByToken(t *testing.T, token string) *session.Session
- func (ts *TestServer) SetMemberRole(t *testing.T, memberID string, role organization.MemberRole) *organization.Member
- func (ts *TestServer) SwitchOrg(t *testing.T, sessionID, orgID string) *session.Session
- type TestUserFactory
Constants ¶
const DefaultAppID = "aapp_01jf0000000000000000000000"
DefaultAppID is the TypeID used for the test application.
const DefaultPublishableKey = "pk_test_authsome_testutil_default"
DefaultPublishableKey is the publishable key seeded onto the default test app and pre-configured on the returned authclient so that SDK tests routing through /v1/signup et al. resolve to the seeded app without each test having to wire up a key. Stable across test runs; not a secret.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ServerOption ¶
type ServerOption func(*serverConfig)
ServerOption configures the test server.
func WithAppID ¶
func WithAppID(appID string) ServerOption
WithAppID overrides the default test app ID.
func WithPlugins ¶
func WithPlugins(plugins ...plugin.Plugin) ServerOption
WithPlugins adds extra plugins beyond the default set (password, apikey, organization).
type TestServer ¶
type TestServer struct {
Engine *authsome.Engine
Server *httptest.Server
Client *authclient.Client
AppID string
Store *memory.Store
Warden *warden.Engine
OrgPlugin *orgplugin.Plugin
Logger log.Logger
}
TestServer wraps a full AuthSome engine with an httptest.Server and a pre-configured SDK client. All stores are in-memory.
func NewTestServer ¶
func NewTestServer(t *testing.T, opts ...ServerOption) *TestServer
NewTestServer creates a fully wired AuthSome test server with in-memory stores, the standard plugin set, and an httptest.Server serving the API. The returned Client is pointed at the test server's URL.
t.Cleanup automatically stops the server and engine.
func (*TestServer) AddMember ¶
func (ts *TestServer) AddMember(t *testing.T, orgID, userID string, role organization.MemberRole) *organization.Member
AddMember adds a user as a member of an organization (server-side).
func (*TestServer) Close ¶
func (ts *TestServer) Close()
Close shuts down the test server and engine.
func (*TestServer) CreateOrg ¶
func (ts *TestServer) CreateOrg(t *testing.T, ownerUserID, name, slug string) *organization.Organization
CreateOrg creates an Authsome organization owned by the given user. It uses the org plugin directly (server-side) and returns the created org.
func (*TestServer) CreateUser ¶
func (ts *TestServer) CreateUser(t *testing.T, email, pass string) (resp *authclient.AuthResponse, token string)
CreateUser signs up a user via the engine (server-side) and returns the user data plus the session token. Does NOT set the token on ts.Client — call CreateUserClient for that.
func (*TestServer) CreateUserClient ¶
func (ts *TestServer) CreateUserClient(t *testing.T, email, pass string) *authclient.Client
CreateUserClient creates a user and returns a new authclient.Client with the user's session token pre-set. Each call creates an independent client.
func (*TestServer) NewTestUserFactory ¶
func (ts *TestServer) NewTestUserFactory() *TestUserFactory
NewTestUserFactory creates a factory bound to the given test server.
func (*TestServer) SessionByToken ¶
SessionByToken fetches a session by its bearer token. Useful for asserting session state (e.g. OrgID after a switch-org call) in tests that drive the SDK and only have the token in hand.
func (*TestServer) SetMemberRole ¶
func (ts *TestServer) SetMemberRole(t *testing.T, memberID string, role organization.MemberRole) *organization.Member
SetMemberRole mutates the role of an existing org member. Works off the member ID returned by AddMember. Panics via t.Fatal when the member doesn't exist or the org plugin rejects the change.
Use this to set up role-gate tests (owner vs admin vs member) on the same user without recreating the org.
type TestUserFactory ¶
type TestUserFactory struct {
// contains filtered or unexported fields
}
TestUserFactory generates test users with sequential emails.
func (*TestUserFactory) Next ¶
func (f *TestUserFactory) Next(t *testing.T) (resp *authclient.AuthResponse, token string)
Next creates the next test user and returns the auth response and token.
func (*TestUserFactory) NextClient ¶
func (f *TestUserFactory) NextClient(t *testing.T) *authclient.Client
NextClient creates the next test user and returns a pre-authed client.