mirror of
https://github.com/SecurityBrewery/catalyst.git
synced 2025-12-06 15:22:47 +01:00
@@ -25,6 +25,8 @@ import (
|
||||
)
|
||||
|
||||
func TestBackupAndRestore(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
log.SetFlags(log.LstdFlags | log.Lshortfile)
|
||||
|
||||
if runtime.GOARCH == "arm64" {
|
||||
@@ -41,7 +43,10 @@ func TestBackupAndRestore(t *testing.T) {
|
||||
{name: "Backup", want: want{status: http.StatusOK}},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ctx, _, server, err := Catalyst(t)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -75,6 +80,8 @@ func TestBackupAndRestore(t *testing.T) {
|
||||
}
|
||||
|
||||
func assertBackup(t *testing.T, server *catalyst.Server) []byte {
|
||||
t.Helper()
|
||||
|
||||
// setup request
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/backup/create", nil)
|
||||
req.Header.Set("PRIVATE-TOKEN", "test")
|
||||
@@ -97,6 +104,8 @@ func assertBackup(t *testing.T, server *catalyst.Server) []byte {
|
||||
}
|
||||
|
||||
func assertZipFile(t *testing.T, r *zip.Reader) {
|
||||
t.Helper()
|
||||
|
||||
var names []string
|
||||
for _, f := range r.File {
|
||||
names = append(names, f.Name)
|
||||
@@ -120,9 +129,11 @@ func clearAllDatabases(server *catalyst.Server) {
|
||||
}
|
||||
|
||||
func deleteAllBuckets(t *testing.T, server *catalyst.Server) {
|
||||
t.Helper()
|
||||
|
||||
buckets, err := server.Storage.S3().ListBuckets(&s3.ListBucketsInput{})
|
||||
for _, bucket := range buckets.Buckets {
|
||||
server.Storage.S3().DeleteBucket(&s3.DeleteBucketInput{
|
||||
_, _ = server.Storage.S3().DeleteBucket(&s3.DeleteBucketInput{
|
||||
Bucket: bucket.Name,
|
||||
})
|
||||
}
|
||||
@@ -133,6 +144,8 @@ func deleteAllBuckets(t *testing.T, server *catalyst.Server) {
|
||||
}
|
||||
|
||||
func assertRestore(t *testing.T, zipB []byte, server *catalyst.Server) {
|
||||
t.Helper()
|
||||
|
||||
bodyBuf := &bytes.Buffer{}
|
||||
bodyWriter := multipart.NewWriter(bodyBuf)
|
||||
fileWriter, err := bodyWriter.CreateFormFile("backup", "backup.zip")
|
||||
@@ -166,7 +179,7 @@ func assertRestore(t *testing.T, zipB []byte, server *catalyst.Server) {
|
||||
func createFile(ctx context.Context, server *catalyst.Server) {
|
||||
buf := bytes.NewBufferString("test text")
|
||||
|
||||
server.Storage.S3().CreateBucket(&s3.CreateBucketInput{Bucket: pointer.String("catalyst-8125")})
|
||||
_, _ = server.Storage.S3().CreateBucket(&s3.CreateBucketInput{Bucket: pointer.String("catalyst-8125")})
|
||||
|
||||
if _, err := server.Storage.Uploader().Upload(&s3manager.UploadInput{Body: buf, Bucket: pointer.String("catalyst-8125"), Key: pointer.String("test.txt")}); err != nil {
|
||||
log.Fatal(err)
|
||||
@@ -178,6 +191,8 @@ func createFile(ctx context.Context, server *catalyst.Server) {
|
||||
}
|
||||
|
||||
func assertTicketExists(t *testing.T, server *catalyst.Server) {
|
||||
t.Helper()
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/tickets/8125", nil)
|
||||
req.Header.Set("PRIVATE-TOKEN", "test")
|
||||
|
||||
@@ -202,6 +217,8 @@ func assertTicketExists(t *testing.T, server *catalyst.Server) {
|
||||
}
|
||||
|
||||
func assertFileExists(t *testing.T, server *catalyst.Server) {
|
||||
t.Helper()
|
||||
|
||||
obj, err := server.Storage.S3().GetObject(&s3.GetObjectInput{
|
||||
Bucket: aws.String("catalyst-8125"),
|
||||
Key: aws.String("test.txt"),
|
||||
@@ -215,6 +232,8 @@ func assertFileExists(t *testing.T, server *catalyst.Server) {
|
||||
}
|
||||
|
||||
func includes(t *testing.T, names []string, s string) bool {
|
||||
t.Helper()
|
||||
|
||||
for _, name := range names {
|
||||
match, err := regexp.MatchString(s, name)
|
||||
if err != nil {
|
||||
@@ -225,10 +244,13 @@ func includes(t *testing.T, names []string, s string) bool {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func readZipFile(t *testing.T, b []byte) *zip.Reader {
|
||||
t.Helper()
|
||||
|
||||
buf := bytes.NewReader(b)
|
||||
|
||||
zr, err := zip.NewReader(buf, int64(buf.Len()))
|
||||
|
||||
@@ -10,9 +10,11 @@ import (
|
||||
"github.com/SecurityBrewery/catalyst/generated/pointer"
|
||||
)
|
||||
|
||||
var bobSetting = &model.UserData{Email: pointer.String("bob@example.org"), Name: pointer.String("Bob Bad")}
|
||||
var bobForm = &model.UserForm{ID: "bob", Blocked: false, Roles: []string{"admin"}}
|
||||
var Bob = &model.UserResponse{ID: "bob", Blocked: false, Roles: []string{"admin"}}
|
||||
var (
|
||||
bobSetting = &model.UserData{Email: pointer.String("bob@example.org"), Name: pointer.String("Bob Bad")}
|
||||
bobForm = &model.UserForm{ID: "bob", Blocked: false, Roles: []string{"admin"}}
|
||||
Bob = &model.UserResponse{ID: "bob", Blocked: false, Roles: []string{"admin"}}
|
||||
)
|
||||
|
||||
func SetupTestData(ctx context.Context, db *database.Database) error {
|
||||
if err := db.UserDataCreate(ctx, "bob", bobSetting); err != nil {
|
||||
@@ -109,5 +111,6 @@ func parse(s string) *time.Time {
|
||||
}
|
||||
|
||||
utc := modified.UTC()
|
||||
|
||||
return &utc
|
||||
}
|
||||
|
||||
@@ -18,6 +18,8 @@ import (
|
||||
)
|
||||
|
||||
func TestJob(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
log.SetFlags(log.LstdFlags | log.Lshortfile)
|
||||
|
||||
_, _, server, err := Catalyst(t)
|
||||
@@ -27,7 +29,7 @@ func TestJob(t *testing.T) {
|
||||
|
||||
b, err := json.Marshal(model.JobForm{
|
||||
Automation: "hash.sha1",
|
||||
Payload: map[string]interface{}{"default": "test"},
|
||||
Payload: map[string]any{"default": "test"},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -52,11 +54,14 @@ func TestJob(t *testing.T) {
|
||||
|
||||
output := gjson.GetBytes(job, "output.hash").String()
|
||||
assert.Equal(t, "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3", output)
|
||||
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
func request(t *testing.T, server chi.Router, method, url string, data io.Reader) []byte {
|
||||
t.Helper()
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
// setup request
|
||||
|
||||
@@ -25,10 +25,15 @@ func (testClock) Now() time.Time {
|
||||
}
|
||||
|
||||
func TestServer(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ctime.DefaultClock = testClock{}
|
||||
|
||||
for _, tt := range api.Tests {
|
||||
tt := tt
|
||||
t.Run(tt.Name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ctx, _, _, _, _, db, _, server, cleanup, err := Server(t)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -73,8 +78,10 @@ func TestServer(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func jsonEqual(t *testing.T, name string, got io.Reader, want interface{}) {
|
||||
var gotObject, wantObject interface{}
|
||||
func jsonEqual(t *testing.T, name string, got io.Reader, want any) {
|
||||
t.Helper()
|
||||
|
||||
var gotObject, wantObject any
|
||||
|
||||
// load bytes
|
||||
wantBytes, err := json.Marshal(want)
|
||||
|
||||
18
test/test.go
18
test/test.go
@@ -75,6 +75,8 @@ func Config(ctx context.Context) (*catalyst.Config, error) {
|
||||
}
|
||||
|
||||
func Index(t *testing.T) (*index.Index, func(), error) {
|
||||
t.Helper()
|
||||
|
||||
dir, err := os.MkdirTemp("", "catalyst-test-"+cleanName(t))
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
@@ -84,10 +86,13 @@ func Index(t *testing.T) (*index.Index, func(), error) {
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
return catalystIndex, func() { catalystIndex.Close(); os.RemoveAll(dir) }, nil
|
||||
}
|
||||
|
||||
func Bus(t *testing.T) (context.Context, *catalyst.Config, *bus.Bus, error) {
|
||||
t.Helper()
|
||||
|
||||
ctx := Context()
|
||||
|
||||
config, err := Config(ctx)
|
||||
@@ -99,10 +104,13 @@ func Bus(t *testing.T) (context.Context, *catalyst.Config, *bus.Bus, error) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
return ctx, config, catalystBus, err
|
||||
}
|
||||
|
||||
func DB(t *testing.T) (context.Context, *catalyst.Config, *bus.Bus, *index.Index, *storage.Storage, *database.Database, func(), error) {
|
||||
t.Helper()
|
||||
|
||||
ctx, config, rbus, err := Bus(t)
|
||||
if err != nil {
|
||||
return nil, nil, nil, nil, nil, nil, nil, err
|
||||
@@ -146,6 +154,8 @@ func DB(t *testing.T) (context.Context, *catalyst.Config, *bus.Bus, *index.Index
|
||||
}
|
||||
|
||||
func Service(t *testing.T) (context.Context, *catalyst.Config, *bus.Bus, *index.Index, *storage.Storage, *database.Database, *service.Service, func(), error) {
|
||||
t.Helper()
|
||||
|
||||
ctx, config, rbus, catalystIndex, catalystStorage, db, cleanup, err := DB(t)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -160,6 +170,8 @@ func Service(t *testing.T) (context.Context, *catalyst.Config, *bus.Bus, *index.
|
||||
}
|
||||
|
||||
func Server(t *testing.T) (context.Context, *catalyst.Config, *bus.Bus, *index.Index, *storage.Storage, *database.Database, *service.Service, chi.Router, func(), error) {
|
||||
t.Helper()
|
||||
|
||||
ctx, config, rbus, catalystIndex, catalystStorage, db, catalystService, cleanup, err := Service(t)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -177,6 +189,8 @@ func Server(t *testing.T) (context.Context, *catalyst.Config, *bus.Bus, *index.I
|
||||
}
|
||||
|
||||
func Catalyst(t *testing.T) (context.Context, *catalyst.Config, *catalyst.Server, error) {
|
||||
t.Helper()
|
||||
|
||||
ctx := Context()
|
||||
|
||||
config, err := Config(ctx)
|
||||
@@ -189,13 +203,17 @@ func Catalyst(t *testing.T) (context.Context, *catalyst.Config, *catalyst.Server
|
||||
c, err := catalyst.New(&hooks.Hooks{
|
||||
DatabaseAfterConnectFuncs: []func(ctx context.Context, client driver.Client, name string){Clear},
|
||||
}, config)
|
||||
|
||||
return ctx, config, c, err
|
||||
}
|
||||
|
||||
func cleanName(t *testing.T) string {
|
||||
t.Helper()
|
||||
|
||||
name := t.Name()
|
||||
name = strings.ReplaceAll(name, " ", "")
|
||||
name = strings.ReplaceAll(name, "/", "_")
|
||||
|
||||
return strings.ReplaceAll(name, "#", "_")
|
||||
}
|
||||
|
||||
|
||||
@@ -9,14 +9,16 @@ import (
|
||||
)
|
||||
|
||||
func TestUser(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
type args struct {
|
||||
method string
|
||||
url string
|
||||
data interface{}
|
||||
data any
|
||||
}
|
||||
type want struct {
|
||||
status int
|
||||
body interface{}
|
||||
body any
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
@@ -27,7 +29,10 @@ func TestUser(t *testing.T) {
|
||||
{name: "ListUsers", args: args{method: http.MethodGet, url: "/users"}, want: want{status: http.StatusOK}},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
_, _, _, _, _, _, _, server, cleanup, err := Server(t)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
|
||||
Reference in New Issue
Block a user