155 lines
4.2 KiB
Go
155 lines
4.2 KiB
Go
package iop
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"git.toki-labs.com/toki/ariadne/internal/domain/errorrecord"
|
|
"git.toki-labs.com/toki/ariadne/internal/domain/organization"
|
|
"git.toki-labs.com/toki/ariadne/internal/domain/remediation"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
func TestRequestValidateRequiresReadOnlyRuntime(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
organizationID := organization.NewID()
|
|
projectID := uuid.NewString()
|
|
ingestionSourceID := uuid.NewString()
|
|
request := Request{
|
|
ExecutionID: uuid.NewString(),
|
|
OrganizationID: organizationID,
|
|
ProjectID: projectID,
|
|
IngestionSourceID: ingestionSourceID,
|
|
ErrorGroupID: uuid.NewString(),
|
|
Intent: IntentAnalyze,
|
|
Mode: remediation.ModeReportBeforeChange,
|
|
Error: errorrecord.Event{
|
|
SchemaVersion: errorrecord.SchemaVersionV1,
|
|
EventID: "event-1",
|
|
OrganizationID: organizationID,
|
|
ProjectID: projectID,
|
|
IngestionSourceID: ingestionSourceID,
|
|
OccurredAt: time.Now(),
|
|
Source: errorrecord.Source{
|
|
System: "payments",
|
|
Service: "checkout",
|
|
Environment: "production",
|
|
},
|
|
Error: errorrecord.ErrorDetail{
|
|
Type: "TimeoutError",
|
|
Message: "upstream request timed out",
|
|
},
|
|
},
|
|
Repository: RepositoryTarget{
|
|
RepositoryID: uuid.NewString(),
|
|
WorkspaceReference: "workspace://repository-1",
|
|
BaseRevision: "abc123",
|
|
},
|
|
Policy: Policy{
|
|
RepositoryAccess: RepositoryAccessReadOnly,
|
|
RuntimeAccess: "write",
|
|
},
|
|
Deadline: time.Now().Add(time.Minute),
|
|
}
|
|
|
|
if err := request.Validate(); err == nil {
|
|
t.Fatal("Validate() expected a runtime access error")
|
|
}
|
|
|
|
request.Policy.RuntimeAccess = RuntimeAccessReadOnly
|
|
if err := request.Validate(); err != nil {
|
|
t.Fatalf("Validate() error = %v", err)
|
|
}
|
|
}
|
|
|
|
func TestAnalysisEventRequiresProposalDigestWhenFixable(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
event := Event{
|
|
ExecutionID: uuid.NewString(),
|
|
Type: EventAnalysisResult,
|
|
OccurredAt: time.Now(),
|
|
Analysis: &AnalysisResult{
|
|
Fixability: remediation.FixabilityFixable,
|
|
Report: "A code change can resolve the error.",
|
|
},
|
|
}
|
|
|
|
if err := event.Validate(); err == nil {
|
|
t.Fatal("Validate() expected a proposal digest error")
|
|
}
|
|
event.Analysis.ProposalDigest = "sha256:proposal"
|
|
if err := event.Validate(); err != nil {
|
|
t.Fatalf("Validate() error = %v", err)
|
|
}
|
|
}
|
|
|
|
func TestChangeRequestRequiresAuthorizedProposalDigest(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
organizationID := organization.NewID()
|
|
projectID := uuid.NewString()
|
|
ingestionSourceID := uuid.NewString()
|
|
request := Request{
|
|
ExecutionID: uuid.NewString(),
|
|
OrganizationID: organizationID,
|
|
ProjectID: projectID,
|
|
IngestionSourceID: ingestionSourceID,
|
|
ErrorGroupID: uuid.NewString(),
|
|
Intent: IntentApplyChange,
|
|
Mode: remediation.ModeReportBeforeChange,
|
|
Error: errorrecord.Event{
|
|
SchemaVersion: errorrecord.SchemaVersionV1,
|
|
EventID: "event-1",
|
|
OrganizationID: organizationID,
|
|
ProjectID: projectID,
|
|
IngestionSourceID: ingestionSourceID,
|
|
OccurredAt: time.Now(),
|
|
Source: errorrecord.Source{
|
|
System: "payments",
|
|
Service: "checkout",
|
|
Environment: "production",
|
|
},
|
|
Error: errorrecord.ErrorDetail{
|
|
Type: "TimeoutError",
|
|
Message: "upstream request timed out",
|
|
},
|
|
},
|
|
Repository: RepositoryTarget{
|
|
RepositoryID: uuid.NewString(),
|
|
WorkspaceReference: "workspace://repository-1",
|
|
BaseRevision: "abc123",
|
|
},
|
|
Policy: Policy{
|
|
RepositoryAccess: RepositoryAccessIsolatedBranch,
|
|
RuntimeAccess: RuntimeAccessReadOnly,
|
|
},
|
|
Deadline: time.Now().Add(time.Minute),
|
|
}
|
|
|
|
if err := request.Validate(); err == nil {
|
|
t.Fatal("Validate() expected a proposal digest error")
|
|
}
|
|
request.ProposalDigest = "sha256:approved-proposal"
|
|
if err := request.Validate(); err != nil {
|
|
t.Fatalf("Validate() error = %v", err)
|
|
}
|
|
}
|
|
|
|
func TestExecutionEventRejectsUnexpectedPayload(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
event := Event{
|
|
ExecutionID: uuid.NewString(),
|
|
Type: EventCompleted,
|
|
OccurredAt: time.Now(),
|
|
Change: &ChangeResult{
|
|
Changed: false,
|
|
},
|
|
}
|
|
|
|
if err := event.Validate(); err == nil {
|
|
t.Fatal("Validate() expected an unexpected payload error")
|
|
}
|
|
}
|