mirror of
https://tangled.org/evan.jarrett.net/at-container-registry
synced 2026-09-22 10:14:15 +00:00
81 lines
2.2 KiB
Go
81 lines
2.2 KiB
Go
package auth
|
|
|
|
import (
|
|
"context"
|
|
|
|
"atcr.io/pkg/atproto"
|
|
)
|
|
|
|
// MockHoldAuthorizer is a test double for HoldAuthorizer.
|
|
// It allows tests to control the return values of authorization checks
|
|
// without making network calls or querying a real PDS.
|
|
type MockHoldAuthorizer struct {
|
|
// Direct result control
|
|
CanReadResult bool
|
|
CanWriteResult bool
|
|
CanAdminResult bool
|
|
Error error
|
|
|
|
// Captain record to return (optional, for GetCaptainRecord)
|
|
CaptainRecord *atproto.CaptainRecord
|
|
|
|
// Crew membership (optional, for IsCrewMember)
|
|
IsCrewResult bool
|
|
}
|
|
|
|
// NewMockHoldAuthorizer creates a MockHoldAuthorizer with sensible defaults.
|
|
// By default, it allows all access (public hold, user is owner).
|
|
func NewMockHoldAuthorizer() *MockHoldAuthorizer {
|
|
return &MockHoldAuthorizer{
|
|
CanReadResult: true,
|
|
CanWriteResult: true,
|
|
CanAdminResult: false,
|
|
IsCrewResult: false,
|
|
CaptainRecord: &atproto.CaptainRecord{
|
|
Type: "io.atcr.hold.captain",
|
|
Owner: "did:plc:mock-owner",
|
|
Public: true,
|
|
},
|
|
}
|
|
}
|
|
|
|
// CheckReadAccess returns the configured CanReadResult.
|
|
func (m *MockHoldAuthorizer) CheckReadAccess(ctx context.Context, holdDID, userDID string) (bool, error) {
|
|
if m.Error != nil {
|
|
return false, m.Error
|
|
}
|
|
return m.CanReadResult, nil
|
|
}
|
|
|
|
// CheckWriteAccess returns the configured CanWriteResult.
|
|
func (m *MockHoldAuthorizer) CheckWriteAccess(ctx context.Context, holdDID, userDID string) (bool, error) {
|
|
if m.Error != nil {
|
|
return false, m.Error
|
|
}
|
|
return m.CanWriteResult, nil
|
|
}
|
|
|
|
// GetCaptainRecord returns the configured CaptainRecord or a default.
|
|
func (m *MockHoldAuthorizer) GetCaptainRecord(ctx context.Context, holdDID string) (*atproto.CaptainRecord, error) {
|
|
if m.Error != nil {
|
|
return nil, m.Error
|
|
}
|
|
if m.CaptainRecord != nil {
|
|
return m.CaptainRecord, nil
|
|
}
|
|
// Return a default captain record
|
|
return &atproto.CaptainRecord{
|
|
Type: "io.atcr.hold.captain",
|
|
Owner: "did:plc:mock-owner",
|
|
Public: true,
|
|
}, nil
|
|
}
|
|
|
|
// IsCrewMember returns the configured IsCrewResult.
|
|
func (m *MockHoldAuthorizer) IsCrewMember(ctx context.Context, holdDID, userDID string) (bool, error) {
|
|
if m.Error != nil {
|
|
return false, m.Error
|
|
}
|
|
return m.IsCrewResult, nil
|
|
}
|