authtest

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package authtest helps test apps using auth: requests and live sessions signed in as anyone, without a login, and Users kept in memory.

Index

Constants

This section is empty.

Variables

View Source
var Cheap = auth.Argon2{Memory: 64, Time: 1, Threads: 1}

Cheap is a hashing cost for tests: fast, and far too weak for anything else. Users hashes with it; pass it as auth.Config.Passwords so logins don't re-hash.

Functions

func As

func As(id *auth.Identity) web.Option

As signs every request of the app in as id — pages, APIs, live pages and their events — whatever credentials they carry:

app := web.New(auth.Module(cfg), authtest.As(&auth.Identity{ID: "7", Perms: []string{"orders.*"}}))

A nil id makes every request anonymous. Outside a test binary it panics: an app shipped with it would let everyone in.

func Context

func Context(ctx context.Context, id *auth.Identity) context.Context

Context is ctx signed in as id: for a live session a test builds itself (live.NewSession, livetest), whose Mount and event handlers then see id in auth.Current, and for services called directly.

func Request

func Request(r *http.Request, id *auth.Identity) *http.Request

Request is r signed in as id, for an app's handler called directly (app.Handler().ServeHTTP with an httptest.ResponseRecorder).

Types

type Users

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

Users is an auth.Users kept in memory, with SetPassword.

func NewUsers

func NewUsers() *Users

NewUsers is an empty Users.

func (*Users) Add

func (u *Users) Add(login, password string, id auth.Identity) *Users

Add adds a user who signs in with login and password; id.ID names it.

func (*Users) AddEncoded

func (u *Users) AddEncoded(login, encoded string, id auth.Identity) *Users

AddEncoded adds a user whose password is already encoded (a bcrypt hash from an older system, say).

func (*Users) Encoded

func (u *Users) Encoded(id string) string

Encoded is the stored password of user id, to see a re-hash happen.

func (*Users) FindLogin

func (u *Users) FindLogin(_ context.Context, login string) (*auth.Identity, string, error)

func (*Users) Load

func (u *Users) Load(_ context.Context, id string) (*auth.Identity, error)

func (*Users) Remove

func (u *Users) Remove(id string)

Remove deletes a user: its sessions stop working at their next request.

func (*Users) SetPassword

func (u *Users) SetPassword(_ context.Context, id, encoded string) error

Jump to

Keyboard shortcuts

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