Documentation
¶
Overview ¶
Package testutils provides helpers for the integration test suite: the docker-compose-managed TheHive bootstrap, API/MCP test clients, and fixtures.
Index ¶
- Constants
- func CallTool(t *testing.T, c *client.Client, name string, args map[string]any) *mcp.CallToolResult
- func CallToolOK(t *testing.T, c *client.Client, name string, args map[string]any) *mcp.CallToolResult
- func CreateAuthContext(username, password string) context.Context
- func CreateCaseWithTLP(t *testing.T, hiveClient *thehive.APIClient, title string, tlp int32) *thehive.OutputCase
- func CreateOrgClient(t *testing.T, cfg *Config) *thehive.APIClient
- func DummyElicitationAccept(_ context.Context, _ mcp.ElicitationRequest) (*mcp.ElicitationResult, error)
- func DummySamplingHandlerCreateMessage(ctx context.Context, request mcp.CreateMessageRequest) (*mcp.CreateMessageResult, error)
- func GetAuthContext(t *testing.T) context.Context
- func GetMCPTestClient(t *testing.T, ...) *client.Client
- func GetMCPTestClientWithPermissions(t *testing.T, ...) *client.Client
- func LicensePresent() bool
- func MockInputAlert() *thehive.InputCreateAlert
- func MockInputAnalyzerJob() *thehive.InputJob
- func MockInputCase() *thehive.InputCreateCase
- func MockInputCaseTemplate() *thehive.InputCreateCaseTemplate
- func MockInputObservable() *thehive.InputCreateObservable
- func MockInputOrganisation() *thehive.InputCreateOrganisation
- func MockInputTask() *thehive.InputCreateTask
- func MockInputUser() *thehive.InputCreateUser
- func MockInputUserOrganisation() []thehive.InputUserOrganisation
- func NewMCPTestConfig() *types.TheHiveMcpDefaultOptions
- func NewTestClient(t *testing.T) *thehive.APIClient
- func Parallel(t *testing.T)
- func PermissionsFixture(t *testing.T, name string) string
- func RequirePermissionDenied(t *testing.T, result *mcp.CallToolResult)
- func RequireScopeDenied(t *testing.T, result *mcp.CallToolResult, wantSubstring string)
- func SamplingHandlerCreateMessageFromStringResponse(response string) ...
- func SetupTestWithCleanup(t *testing.T) *thehive.APIClient
- func StartTheHiveContainer(t *testing.T) (string, error)
- func StructuredData(t *testing.T, result *mcp.CallToolResult) map[string]any
- func TeardownContainers(_ context.Context)
- func TestUserLogin(t *testing.T) string
- func TheHiveTestImage() string
- func TheHiveTestURL() string
- func WritePermissionsFile(t *testing.T, content string) string
- type Config
- type HiveTestConfig
Constants ¶
const DefaultAdminUser = "admin@thehive.local"
DefaultAdminUser is the built-in TheHive superadmin login used across the integration suite for authentication and as a default assignee in fixtures.
const DefaultTheHiveTestImage = "strangebee/thehive:5.6.3"
DefaultTheHiveTestImage is used when THEHIVE_TEST_IMAGE is unset (CI sets it per version-matrix leg). Renovate tracks this tag via the custom Docker manager in renovate.json.
const TestMITREPatternID = "T1059"
TestMITREPatternID is the patternId available in the test TheHive instance after initHiveInstance
Variables ¶
This section is empty.
Functions ¶
func CallTool ¶ added in v1.0.0
CallTool asserts the transport call succeeded with a non-nil result. It does not assert result.IsError, so it serves both success and error cases.
func CallToolOK ¶ added in v1.0.0
func CallToolOK(t *testing.T, c *client.Client, name string, args map[string]any) *mcp.CallToolResult
CallToolOK is like CallTool but additionally asserts the tool did not report an error.
func CreateAuthContext ¶
CreateAuthContext creates an authentication context for API calls
func CreateCaseWithTLP ¶ added in v1.0.0
func CreateCaseWithTLP(t *testing.T, hiveClient *thehive.APIClient, title string, tlp int32) *thehive.OutputCase
CreateCaseWithTLP creates a case with the given title and TLP via the raw TheHive API and asserts it succeeded. Shared by the manage and execute_automation scope-enforcement tests.
func CreateOrgClient ¶
CreateOrgClient creates a client configured for a specific organisation
func DummyElicitationAccept ¶
func DummyElicitationAccept(_ context.Context, _ mcp.ElicitationRequest) (*mcp.ElicitationResult, error)
DummyElicitationAccept is an elicitation handler that always accepts with a fixed mock payload, for tests that only need elicitation to succeed.
func DummySamplingHandlerCreateMessage ¶
func DummySamplingHandlerCreateMessage(ctx context.Context, request mcp.CreateMessageRequest) (*mcp.CreateMessageResult, error)
DummySamplingHandlerCreateMessage is a sampling handler that replies with a fixed dummy response, for tests that only need sampling to succeed.
func GetAuthContext ¶
GetAuthContext returns a basic-auth context for this test's dedicated user, so data seeded through it lands in the test's own organisation. The org itself comes from the client's X-Organisation header, not this context.
func GetMCPTestClient ¶
func GetMCPTestClient( t *testing.T, samplingHandlerCreateMessage func(ctx context.Context, request mcp.CreateMessageRequest) (*mcp.CreateMessageResult, error), elicitationHandlerElicit func(ctx context.Context, request mcp.ElicitationRequest) (*mcp.ElicitationResult, error), ) *client.Client
GetMCPTestClient creates an in-process MCP test client with admin permissions and the given sampling and elicitation handlers.
func GetMCPTestClientWithPermissions ¶
func GetMCPTestClientWithPermissions( t *testing.T, samplingHandlerCreateMessage func(ctx context.Context, request mcp.CreateMessageRequest) (*mcp.CreateMessageResult, error), elicitationHandlerElicit func(ctx context.Context, request mcp.ElicitationRequest) (*mcp.ElicitationResult, error), permissionsConfigPath string, ) *client.Client
GetMCPTestClientWithPermissions creates a test client. permissionsConfigPath: - types.PermissionConfigAdmin — admin - types.PermissionConfigReadOnly — read-only - testutils.PermissionsFixture(t, "analyst.yaml") — file path - "" — default read-only
func LicensePresent ¶ added in v1.0.0
func LicensePresent() bool
LicensePresent reports whether the suite is running in license mode, keyed on THEHIVE_TEST_LICENSE being non-empty:
- present ⇒ per-test org + user, tests run in parallel (multi-org).
- absent ⇒ one shared main-org, tests run sequentially and purge their own data on cleanup (free-license path; the public-CI default).
The variable is NOT a user input: the harness (scripts/reset-integration-db.sh + the Makefile) decides the mode by whether the StrangeBee licensing image is pullable, mints a dev license on the fly when it is, and injects the minted token here for the go-test container. So this reads that harness-set signal.
func MockInputAlert ¶
func MockInputAlert() *thehive.InputCreateAlert
MockInputAlert returns a valid InputCreateAlert for testing.
func MockInputAnalyzerJob ¶
MockInputAnalyzerJob returns a valid InputJob for testing Cortex analyzer functionality.
func MockInputCase ¶
func MockInputCase() *thehive.InputCreateCase
MockInputCase returns a valid InputCreateCase (with a task and custom fields) for testing.
func MockInputCaseTemplate ¶
func MockInputCaseTemplate() *thehive.InputCreateCaseTemplate
MockInputCaseTemplate returns a valid InputCaseTemplate for testing.
func MockInputObservable ¶
func MockInputObservable() *thehive.InputCreateObservable
MockInputObservable returns a valid InputObservable for testing.
func MockInputOrganisation ¶
func MockInputOrganisation() *thehive.InputCreateOrganisation
MockInputOrganisation returns a valid InputCreateOrganisation for testing.
func MockInputTask ¶
func MockInputTask() *thehive.InputCreateTask
MockInputTask returns a valid InputTask for testing.
func MockInputUser ¶
func MockInputUser() *thehive.InputCreateUser
MockInputUser returns a valid InputCreateUser for testing.
func MockInputUserOrganisation ¶
func MockInputUserOrganisation() []thehive.InputUserOrganisation
MockInputUserOrganisation returns a valid slice of InputUserOrganisation for testing.
func NewMCPTestConfig ¶
func NewMCPTestConfig() *types.TheHiveMcpDefaultOptions
NewMCPTestConfig returns the default in-process MCP server options for tests. TheHiveOrganisation here is a placeholder default; GetMCPTestClient* overrides it with the per-test org from testOrgName (see orgs.go).
func NewTestClient ¶
NewTestClient starts (or reuses) the integration TheHive instance and returns an API client scoped to this test's dedicated organisation, authenticated as this test's dedicated user (see orgs.go).
func Parallel ¶ added in v1.0.0
Parallel marks a test as safe to run concurrently, but ONLY in license mode.
In license mode (THEHIVE_TEST_LICENSE set) each test gets its own org + user, so it calls t.Parallel() as usual. In free-license mode all tests share main-org and must run sequentially: Parallel is a no-op there, so the test runs in source order and its t.Cleanup (purgeOrg) completes before the next test starts — the ordering the shared-org data reset depends on.
Integration tests call testutils.Parallel(t) as their first statement in place of t.Parallel(). Pure unit tests (no live TheHive) keep t.Parallel().
func PermissionsFixture ¶ added in v1.0.0
PermissionsFixture returns the absolute path to a fixture under docs/reference/permissions-examples/. Anchored on this source file (not the working directory) so it survives test files moving between packages.
Example: PermissionsFixture(t, "analyst.yaml")
func RequirePermissionDenied ¶ added in v1.0.0
func RequirePermissionDenied(t *testing.T, result *mcp.CallToolResult)
RequirePermissionDenied asserts the tool reported an error whose text indicates the operation was not permitted.
func RequireScopeDenied ¶ added in v1.0.0
func RequireScopeDenied(t *testing.T, result *mcp.CallToolResult, wantSubstring string)
RequireScopeDenied asserts the tool reported an error whose text contains wantSubstring, used by the DL-6004 scope-enforcement tests.
func SamplingHandlerCreateMessageFromStringResponse ¶
func SamplingHandlerCreateMessageFromStringResponse(response string) func(ctx context.Context, request mcp.CreateMessageRequest) (*mcp.CreateMessageResult, error)
SamplingHandlerCreateMessageFromStringResponse returns a sampling handler that always replies with the given response text.
func SetupTestWithCleanup ¶
SetupTestWithCleanup returns a test client scoped to this test's organisation (see orgs.go). In license mode each test owns a throwaway org that is simply abandoned; in free-license mode the shared org is purged on t.Cleanup so the next sequential test starts clean. Either way the caller needs no teardown.
func StartTheHiveContainer ¶
StartTheHiveContainer returns the URL of the compose-managed TheHive instance (see docker-compose.test.yml, brought up by `make test`), after waiting for readiness and performing the one-time org/permission/ATT&CK bootstrap. Skips under `-short`, since these integration tests are slow and uncacheable.
func StructuredData ¶ added in v1.0.0
StructuredData returns result.StructuredContent as a map[string]any, asserting the type.
func TeardownContainers ¶ added in v0.3.3
TeardownContainers is a no-op kept so existing TestMain bodies compile: docker compose owns the stack lifecycle (`make test` runs `compose down`).
func TestUserLogin ¶ added in v1.0.0
TestUserLogin returns the login of this test's dedicated user. Use it where a test asserts on or assigns to the acting user's identity, instead of the former shared admin login (the admin is not a member of the per-test org).
func TheHiveTestImage ¶ added in v1.0.0
func TheHiveTestImage() string
TheHiveTestImage returns the integration-suite image: THEHIVE_TEST_IMAGE overrides the default (how the CI matrix selects 5.5 vs 5.6).
func TheHiveTestURL ¶ added in v1.0.0
func TheHiveTestURL() string
TheHiveTestURL returns the base URL of the compose-managed TheHive instance, overridable with THEHIVE_TEST_URL (e.g. when the stack runs on another host).
Types ¶
type Config ¶
Config holds the connection settings for a single-organisation TheHive API client used by the integration helpers.
type HiveTestConfig ¶
type HiveTestConfig struct {
ImageName string
ContainerName string
User string
Password string
MainOrg string
AdminOrg string
}
HiveTestConfig holds the credentials and organisation names used by the integration suite against the compose-managed TheHive instance.
func NewHiveTestConfig ¶
func NewHiveTestConfig() *HiveTestConfig
NewHiveTestConfig returns the default HiveTestConfig for the integration suite.
MainOrg / AdminOrg are the bootstrap seed orgs only. Per-test scoping no longer flows from here: each test gets its own freshly-created org via testOrgName (see orgs.go), which the client/MCP-creds constructors read directly. The org field here is kept for the one-time boot provisioning.