testing

package
v1.23.0 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2026 License: MIT Imports: 36 Imported by: 0

Documentation

Overview

Package testing is what the API key packages' tests share: a database of a test package's own, on the server TEST_DATABASE_URL names, with the library's tables in it; accounts in it; and session tokens for them, signed by a key the tests hold, as a browser or a minted token would carry them.

It is the one package of the library that reaches a database through a driver, and nothing but tests imports it, so a service importing the library is not made to build pgx unless it already does.

Index

Constants

View Source
const (
	// Issuer is what the sessions here are issued by, and what Authorizer expects.
	Issuer = "https://login.example.test"
	// Audience is what the sessions here are for: the cookie domain of the session manager.
	Audience = "example.test"
)
View Source
const DatabaseUrlVariable = "TEST_DATABASE_URL"

DatabaseUrlVariable names the server the tests run against, as a postgres:// URL of a role that may create databases. Without it, the tests that need a database skip.

View Source
const UnrecordedAuthenticationId = "00000000-0000-0000-0000-000000000000"

UnrecordedAuthenticationId is the sign-in a session names when nothing about it is in the database: enough for an endpoint that judges the token alone, and refused by one that looks the sign-in up.

Variables

View Source
var (
	// SessionMethod signs the sessions made here and verifies them in the authorizers.
	SessionMethod *altshiftCryptoEddsa.Method

	// Authorizer is an authorizer as an endpoint for a signed-in person has one: the session in a
	// cookie, and the browser's ways of signing in and no other.
	Authorizer *authorizer_request_parser.Parser
)

Functions

func ApiKeyAuthorizer

func ApiKeyAuthorizer() (*authorizer_request_parser.Parser, error)

ApiKeyAuthorizer is an authorizer as an endpoint for API clients has one: the token as a bearer token, minted against a key and in no other way.

func Db

func Db(t testing.TB) *sql.DB

Db is the database Main made for the test package, and skips the test when it made none.

func InsertCustomer

func InsertCustomer(ctx context.Context, db *sql.DB, name string) (string, error)

InsertCustomer makes a customer, which an account belongs to when it is given one.

func Main

func Main(m *testing.M, name string) int

* Main is a test package's TestMain, as os.Exit(Main(m, "package_name")). * * Where DatabaseUrlVariable names a server, the package's tests run with a database of their own, * which Db hands out; where it does not, they run without one, and Db skips the tests that would * have used it. The tests that need no database run either way.

func Schema

func Schema() (string, error)

Schema is the SQL the library's tables generate: an account with its customer, its sign-ins, and the keys it registers with what they mint.

func SessionManager

func SessionManager(db *sql.DB) (*session_manager.Manager, error)

SessionManager mints sessions signed by SessionMethod, issued by Issuer for Audience.

Types

type Account

type Account struct {
	Id           string
	EmailAddress string
	CustomerId   string
	Roles        []string
}

Account is an account as the tests make one.

func InsertAccount

func InsertAccount(
	ctx context.Context,
	db *sql.DB,
	emailAddress string,
	customerId string,
	roles ...string,
) (*Account, error)

InsertAccount makes an account with the roles, belonging to the customer where one is named.

func (*Account) ActiveSession

func (account *Account) ActiveSession(ctx context.Context, db *sql.DB) (*Session, error)

ActiveSession is a session for the account whose sign-in is in the database and running, which is what an endpoint that checks a session against the database finds standing behind it.

func (*Account) Session

func (account *Account) Session() *Session

Session is a session for the account, naming a sign-in that is not in the database.

type Database

type Database struct {
	*sql.DB
	// contains filtered or unexported fields
}

Database is a database made for one test package, dropped again when it is closed.

func NewDatabase

func NewDatabase(ctx context.Context, databaseUrl string, name string) (*Database, error)

* NewDatabase makes a database of its own on the server databaseUrl reaches and applies Schema to it. * * It is named after name with a random suffix, so that two runs side by side -- the packages of one * `go test ./...`, or two of those -- never share one. The connection databaseUrl makes is kept for * dropping it again.

func (*Database) Close

func (database *Database) Close() error

Close closes the database's connections and drops it. It is not given a context: it is what a test package does once its tests are done, by when theirs have ended.

type Session

type Session struct {
	AccountId        string
	EmailAddress     string
	AuthenticationId string
	Roles            []string

	// AuthenticationMethods is the token's "amr"; a browser's sign-in where it is left empty.
	AuthenticationMethods []string
}

Session is a session token's subject and the sign-in it names.

func (*Session) Cookie

func (session *Session) Cookie() (string, error)

Cookie is the session as the value of a Cookie header, which is how a browser presents it.

func (*Session) Token

func (session *Session) Token() (string, error)

Token is the session as a signed session token, good for an hour.

Jump to

Keyboard shortcuts

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