authtest

package
v5.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package authtest provides an in-memory implementation of auth.AuthenticatorStore for use in consumer tests. It is not intended for production use.

Every read returns a fresh copy and every write stores a fresh copy, so a consumer test can mutate what it receives without reaching the store's state and vice versa. That isolation is what [TestMemStoreIsolatesStoredValues] pins: it holds today because auth.User, auth.Session and auth.Key contain no pointer, slice or map field, so a struct copy is a complete one. A reference-typed field added to any of the three would silently break it, which is why the guarantee is asserted rather than assumed.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MemStore

type MemStore struct {
	// contains filtered or unexported fields
}

MemStore is an in-memory implementation of auth interfaces for testing. A MemStore must be constructed with NewMemStore: the zero value has nil maps and panics on the first Add/Create call.

func NewMemStore

func NewMemStore() *MemStore

NewMemStore returns a new empty MemStore.

func (*MemStore) APIKeyByHash

func (m *MemStore) APIKeyByHash(_ context.Context, hash string) (key *auth.Key, found bool, err error)

APIKeyByHash returns a copy of the API key stored under hash. found is false when the key is absent; err is always nil.

func (*MemStore) AddAPIKey

func (m *MemStore) AddAPIKey(k *auth.Key)

AddAPIKey stores a copy of k, keyed by its KeyHash, assigning an auto-incremented ID when k carries none. Unlike MemStore.AddUser it does not write the ID back: k.ID == 0 is how a caller asks for one, so writing it back would change the meaning of a second Add of the same value.

func (*MemStore) AddSession

func (m *MemStore) AddSession(s *auth.Session)

AddSession stores a copy of s, keyed by its TokenHash.

func (*MemStore) AddUser

func (m *MemStore) AddUser(u *auth.User)

AddUser stores a copy of u and assigns it an auto-incremented ID, which it also writes back to u so the caller knows the ID its user was given.

func (*MemStore) CreateSession

func (m *MemStore) CreateSession(_ context.Context, s *auth.Session) error

CreateSession stores a copy of s, keyed by its TokenHash.

func (*MemStore) DeleteSession

func (m *MemStore) DeleteSession(_ context.Context, tokenHash string) error

DeleteSession removes the session stored under tokenHash, if any.

func (*MemStore) DeleteUserSessions

func (m *MemStore) DeleteUserSessions(_ context.Context, userID int64, exceptHash string) error

DeleteUserSessions removes every session belonging to userID except the one whose token hash equals exceptHash.

func (*MemStore) SessionByHash

func (m *MemStore) SessionByHash(_ context.Context, tokenHash string) (sess *auth.Session, found bool, err error)

SessionByHash returns a copy of the session stored under tokenHash. found is false when no such session exists; err is always nil.

func (*MemStore) UpdateSessionActivity

func (m *MemStore) UpdateSessionActivity(_ context.Context, tokenHash string, now time.Time) error

UpdateSessionActivity sets LastActivity to now for the session identified by tokenHash. It is a no-op if no such session exists.

func (*MemStore) UserByID

func (m *MemStore) UserByID(_ context.Context, id int64) (user *auth.User, found bool, err error)

UserByID returns a copy of the user with the given id. found is false when the user is absent; err is always nil.

Jump to

Keyboard shortcuts

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