authtest

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package authtest provides a conformance suite for auth.UserDriver implementations, modeled on testing/fstest.

RunUserDriver seeds a driver with known User fixtures and runs one subtest per documented behavior: a found user with its version and claims, a missing user, a disabled user and invalid subjects. Fixture subjects are decimal digits, so drivers keyed by integers, strings or text types can all pass.

Example:

func TestDriver(t *testing.T) {
	authtest.RunUserDriver(t, func(t *testing.T, users []authtest.User) auth.UserDriver {
		driver := memoryauth.New()
		// Seed driver from users.
		return driver
	})
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func RunUserDriver

func RunUserDriver(t *testing.T, newDriver func(t *testing.T, users []User) auth.UserDriver, opts ...Option)

RunUserDriver executes the conformance suite against drivers returned by newDriver. newDriver is called once per subtest with the fixtures the driver must contain (one enabled user with a version and claims, one disabled user) and registers any cleanup on t. The suite reports every deviation with t.Errorf or t.Fatalf in a subtest named after the behavior.

Example:

func TestMemoryDriver(t *testing.T) {
	authtest.RunUserDriver(t, func(t *testing.T, users []authtest.User) auth.UserDriver {
		driver := memoryauth.New()
		for _, user := range users {
			_ = driver.Put(auth.UserRecord{Subject: user.Subject, Version: user.Version, Claims: user.Claims})
			if user.Disabled {
				_ = driver.Disable(user.Subject)
			}
		}
		return driver
	})
}
Example
package main

import (
	"fmt"
	"testing"

	"arkoren.dev/core/auth"
	"arkoren.dev/core/auth/authtest"
	"arkoren.dev/core/auth/memoryauth"
)

// seededDriver returns a memory driver containing users.
func seededDriver(users []authtest.User) auth.UserDriver {
	driver := memoryauth.New()

	for _, user := range users {
		_ = driver.Put(auth.UserRecord{Subject: user.Subject, Version: user.Version, Claims: user.Claims})

		if user.Disabled {
			_ = driver.Disable(user.Subject)
		}
	}

	return driver
}

func main() {
	// In a real package this is a Test function receiving its own *testing.T.
	_ = func(t *testing.T) {
		authtest.RunUserDriver(t, func(t *testing.T, users []authtest.User) auth.UserDriver {
			return seededDriver(users)
		})
	}

	fmt.Println("run the suite from a Test function")
}
Output:
run the suite from a Test function

Types

type Option

type Option func(*settings)

Option configures RunUserDriver; create one with WithTimeout.

Example:

option := authtest.WithTimeout(time.Minute)
authtest.RunUserDriver(t, newDriver, option)
Example
package main

import (
	"fmt"

	"arkoren.dev/core/auth/authtest"
)

func main() {
	option := authtest.Option(authtest.WithTimeout(1))

	fmt.Println(option != nil)
}
Output:
true

func WithTimeout

func WithTimeout(timeout time.Duration) Option

WithTimeout bounds every subtest's context, for slow remote drivers. A zero or negative timeout keeps the default of 30 seconds.

Example:

authtest.RunUserDriver(t, newDriver, authtest.WithTimeout(time.Minute))
Example
package main

import (
	"fmt"

	"arkoren.dev/core/auth/authtest"
)

func main() {
	option := authtest.WithTimeout(0) // Zero keeps the 30 second default.

	fmt.Println(option != nil)
}
Output:
true

type User

type User struct {
	// Subject identifies the user. Fixture subjects are decimal digits.
	Subject string

	// Version must come back as [auth.UserRecord.Version].
	Version string

	// Claims must come back as [auth.UserRecord.Claims].
	Claims map[string]any

	// Disabled users must make Find return [auth.ErrUserDisabled].
	Disabled bool
}

User is a fixture the suite asks a driver under test to contain.

Example:

authtest.User{Subject: "1", Version: "v1", Claims: map[string]any{"role": "admin"}}
Example
package main

import (
	"fmt"

	"arkoren.dev/core/auth/authtest"
)

func main() {
	user := authtest.User{Subject: "1", Version: "v7", Claims: map[string]any{"role": "admin"}}

	fmt.Println(user.Subject, user.Version, user.Disabled)
}
Output:
1 v7 false

Jump to

Keyboard shortcuts

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