mirror of
https://github.com/SecurityBrewery/catalyst.git
synced 2026-02-07 22:03:32 +01:00
feat: add reactions (#1074)
This commit is contained in:
@@ -6,7 +6,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/brianvoe/gofakeit/v7"
|
||||
"github.com/pocketbase/pocketbase"
|
||||
"github.com/pocketbase/pocketbase/core"
|
||||
"github.com/pocketbase/pocketbase/daos"
|
||||
"github.com/pocketbase/pocketbase/models"
|
||||
"github.com/pocketbase/pocketbase/tools/security"
|
||||
@@ -19,7 +19,7 @@ const (
|
||||
minimumTicketCount = 1
|
||||
)
|
||||
|
||||
func Generate(app *pocketbase.PocketBase, userCount, ticketCount int) error {
|
||||
func Generate(app core.App, userCount, ticketCount int) error {
|
||||
if userCount < minimumUserCount {
|
||||
userCount = minimumUserCount
|
||||
}
|
||||
@@ -28,24 +28,39 @@ func Generate(app *pocketbase.PocketBase, userCount, ticketCount int) error {
|
||||
ticketCount = minimumTicketCount
|
||||
}
|
||||
|
||||
types, err := app.Dao().FindRecordsByExpr(migrations.TypeCollectionName)
|
||||
records, err := Records(app, userCount, ticketCount)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, record := range records {
|
||||
if err := app.Dao().SaveRecord(record); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func Records(app core.App, userCount int, ticketCount int) ([]*models.Record, error) {
|
||||
types, err := app.Dao().FindRecordsByExpr(migrations.TypeCollectionName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
users := userRecords(app.Dao(), userCount)
|
||||
tickets := ticketRecords(app.Dao(), users, types, ticketCount)
|
||||
webhooks := webhookRecords(app.Dao())
|
||||
reactions := reactionRecords(app.Dao())
|
||||
|
||||
for _, records := range [][]*models.Record{users, tickets, webhooks} {
|
||||
for _, record := range records {
|
||||
if err := app.Dao().SaveRecord(record); err != nil {
|
||||
app.Logger().Error(err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
var records []*models.Record
|
||||
records = append(records, users...)
|
||||
records = append(records, types...)
|
||||
records = append(records, tickets...)
|
||||
records = append(records, webhooks...)
|
||||
records = append(records, reactions...)
|
||||
|
||||
return nil
|
||||
return records, nil
|
||||
}
|
||||
|
||||
func userRecords(dao *daos.Dao, count int) []*models.Record {
|
||||
@@ -222,3 +237,41 @@ func webhookRecords(dao *daos.Dao) []*models.Record {
|
||||
|
||||
return []*models.Record{record}
|
||||
}
|
||||
|
||||
const (
|
||||
triggerWebhook = `{"token":"1234567890","path":"webhook"}`
|
||||
reactionPython = `{"requirements":"requests","script":"import sys\n\nprint(sys.argv[1])"}`
|
||||
triggerHook = `{"collections":["tickets","comments"],"events":["create","update","delete"]}`
|
||||
reactionWebhook = `{"headers":["Content-Type: application/json"],"url":"http://localhost:8080/webhook"}`
|
||||
)
|
||||
|
||||
func reactionRecords(dao *daos.Dao) []*models.Record {
|
||||
var records []*models.Record
|
||||
|
||||
collection, err := dao.FindCollectionByNameOrId(migrations.ReactionCollectionName)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
record := models.NewRecord(collection)
|
||||
record.SetId("w_" + security.PseudorandomString(10))
|
||||
record.Set("name", "Test Reaction")
|
||||
record.Set("trigger", "webhook")
|
||||
record.Set("triggerdata", triggerWebhook)
|
||||
record.Set("action", "python")
|
||||
record.Set("actiondata", reactionPython)
|
||||
|
||||
records = append(records, record)
|
||||
|
||||
record = models.NewRecord(collection)
|
||||
record.SetId("w_" + security.PseudorandomString(10))
|
||||
record.Set("name", "Test Reaction 2")
|
||||
record.Set("trigger", "hook")
|
||||
record.Set("triggerdata", triggerHook)
|
||||
record.Set("action", "webhook")
|
||||
record.Set("actiondata", reactionWebhook)
|
||||
|
||||
records = append(records, record)
|
||||
|
||||
return records
|
||||
}
|
||||
|
||||
29
fakedata/records_test.go
Normal file
29
fakedata/records_test.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package fakedata_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/SecurityBrewery/catalyst/fakedata"
|
||||
catalystTesting "github.com/SecurityBrewery/catalyst/testing"
|
||||
)
|
||||
|
||||
func Test_records(t *testing.T) {
|
||||
app, cleanup := catalystTesting.App(t)
|
||||
defer cleanup()
|
||||
|
||||
got, err := fakedata.Records(app, 2, 2)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Greater(t, len(got), 2)
|
||||
}
|
||||
|
||||
func TestGenerate(t *testing.T) {
|
||||
app, cleanup := catalystTesting.App(t)
|
||||
defer cleanup()
|
||||
|
||||
err := fakedata.Generate(app, 0, 0)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
48
fakedata/text_test.go
Normal file
48
fakedata/text_test.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package fakedata
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func Test_fakeTicketComment(t *testing.T) {
|
||||
assert.NotEmpty(t, fakeTicketComment())
|
||||
}
|
||||
|
||||
func Test_fakeTicketDescription(t *testing.T) {
|
||||
assert.NotEmpty(t, fakeTicketDescription())
|
||||
}
|
||||
|
||||
func Test_fakeTicketTask(t *testing.T) {
|
||||
assert.NotEmpty(t, fakeTicketTask())
|
||||
}
|
||||
|
||||
func Test_fakeTicketTimelineMessage(t *testing.T) {
|
||||
assert.NotEmpty(t, fakeTicketTimelineMessage())
|
||||
}
|
||||
|
||||
func Test_random(t *testing.T) {
|
||||
type args[T any] struct {
|
||||
e []T
|
||||
}
|
||||
|
||||
type testCase[T any] struct {
|
||||
name string
|
||||
args args[T]
|
||||
}
|
||||
|
||||
tests := []testCase[int]{
|
||||
{
|
||||
name: "Test random",
|
||||
args: args[int]{e: []int{1, 2, 3, 4, 5}},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := random(tt.args.e)
|
||||
|
||||
assert.Contains(t, tt.args.e, got)
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user