Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions tavern/internal/c2/c2test/ent.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,14 @@ func NewRandomAssignedTask(ctx context.Context, graph *ent.Client, beaconIdentif
SetName(namegen.NewComplex()).
SetEldritch(fmt.Sprintf(`print("%s")`, namegen.NewComplex())).
SetDescription(string(newRandomBytes(120))).
SetParamDefs(`{"test":"string"}`).
SetParamDefs(`[{"name":"test-param","label":"Test","type":"string","placeholder":"Enter text..."}]`).
AddFiles(files...).
SaveX(ctx)
quest := graph.Quest.Create().
SetName(namegen.NewComplex()).
SetBundle(bundle).
SetTome(tome).
SetParameters(fmt.Sprintf(`{"test":"%v"}`, namegen.NewComplex())).
SetParameters(fmt.Sprintf(`{"test-param":"%v"}`, namegen.NewComplex())).
SaveX(ctx)

return graph.Task.Create().
Expand Down
2 changes: 2 additions & 0 deletions tavern/internal/ent/quest/quest.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions tavern/internal/ent/quest_create.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions tavern/internal/ent/quest_update.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions tavern/internal/ent/runtime/runtime.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions tavern/internal/ent/schema/quest.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"entgo.io/ent/schema"
"entgo.io/ent/schema/edge"
"entgo.io/ent/schema/field"
"realm.pub/tavern/internal/ent/schema/validators"
)

// Quest holds the schema definition for the Quest entity.
Expand All @@ -24,6 +25,7 @@ func (Quest) Fields() []ent.Field {
).
Comment("Name of the quest"),
field.String("parameters").
Validate(validators.NewJSONStringString()).
SchemaType(map[string]string{
dialect.MySQL: "LONGTEXT", // Override MySQL, improve length maximum
}).
Expand Down
4 changes: 3 additions & 1 deletion tavern/internal/ent/schema/tome.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"entgo.io/ent/schema/field"
"golang.org/x/crypto/sha3"
"realm.pub/tavern/internal/ent/hook"
"realm.pub/tavern/internal/ent/schema/validators"
)

// Tome holds the schema definition for the Tome entity.
Expand All @@ -32,11 +33,12 @@ func (Tome) Fields() []ent.Field {
field.String("description").
Comment("Information about the tome"),
field.String("param_defs").
Validate(validators.NewTomeParameterDefinitions()).
Optional().
SchemaType(map[string]string{
dialect.MySQL: "LONGTEXT", // Override MySQL, improve length maximum
}).
Comment("JSON string describing what parameters are used with the tome"),
Comment("JSON string describing what parameters are used with the tome. Requires a list of JSON objects, one for each parameter."),
field.String("hash").
MaxLen(100).
Annotations(
Expand Down
40 changes: 40 additions & 0 deletions tavern/internal/ent/schema/validators/json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package validators

import (
"encoding/json"

"realm.pub/tavern/tomes"
)

// NewJSONStringString returns a validator that errors if the string field has a value that cannot be JSON unmarshalled to a map[string]string.
func NewJSONStringString() func(string) error {
return func(data string) error {
if data == "" {
return nil
}
var dataMap map[string]string
return json.Unmarshal([]byte(data), &dataMap)
}
}

// NewTomeParameterDefinitions returns a validator that errors if the string field has a value that cannot be JSON unmarshalled to a []tomes.TomeParamDefinition.
func NewTomeParameterDefinitions() func(string) error {
return func(data string) error {
if data == "" {
return nil
}
var paramDefs []tomes.ParamDefinition
if err := json.Unmarshal([]byte(data), &paramDefs); err != nil {
return err
}

// Validate parameters
for _, paramDef := range paramDefs {
if err := paramDef.Validate(); err != nil {
return err
}
}

return nil
}
}
97 changes: 97 additions & 0 deletions tavern/internal/ent/schema/validators/json_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package validators_test

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
"realm.pub/tavern/internal/ent/schema/validators"
)

func TestNewJSONStringString(t *testing.T) {
tests := []struct {
name string
data string
wantErr error
}{
{
name: "Empty",
data: ``,
wantErr: nil,
},
{
name: "Valid",
data: `{"data":"stuff"}`,
wantErr: nil,
},
{
name: "Invalid",
data: `blah`,
wantErr: &json.SyntaxError{},
},
{
name: "Partial",
data: `{"blah":"stuff"`,
wantErr: &json.SyntaxError{},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
err := validators.NewJSONStringString()(tc.data)
if tc.wantErr == nil {
assert.NoError(t, err)
return
}
assert.ErrorAs(t, err, &tc.wantErr)
})
}
}

func TestNewTomeParameterDefinitions(t *testing.T) {
tests := []struct {
name string
data string
wantErr error
}{
{
name: "Empty",
data: ``,
wantErr: nil,
},
{
name: "Int32",
data: `[{"name":"an-int","type": "int32"}]`,
wantErr: nil,
},
{
name: "Multiple",
data: `[{"name":"an-int","type":"int32"},{"name":"a-str","type": "string"}]`,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we actually support int type in params?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No not yet

wantErr: nil,
},
{
name: "Valid",
data: `[{"name":"stuff","type":"string"}]`,
wantErr: nil,
},
{
name: "Invalid",
data: `blah`,
wantErr: &json.SyntaxError{},
},
{
name: "Partial",
data: `{"blah":"stuff"`,
wantErr: &json.SyntaxError{},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
err := validators.NewTomeParameterDefinitions()(tc.data)
if tc.wantErr == nil {
assert.NoError(t, err)
return
}
assert.ErrorAs(t, err, &tc.wantErr)
})
}
}
2 changes: 1 addition & 1 deletion tavern/internal/ent/tome.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions tavern/internal/ent/tome/tome.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions tavern/internal/ent/tome_create.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions tavern/internal/ent/tome_update.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions tavern/internal/graphql/generated/root_.generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions tavern/internal/graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ input CreateTomeInput {
name: String!
"""Information about the tome"""
description: String!
"""JSON string describing what parameters are used with the tome"""
"""JSON string describing what parameters are used with the tome. Requires a list of JSON objects, one for each parameter."""
paramDefs: String
"""Eldritch script that will be executed when the tome is run"""
eldritch: String!
Expand Down Expand Up @@ -804,7 +804,7 @@ type Tome implements Node {
name: String!
"""Information about the tome"""
description: String!
"""JSON string describing what parameters are used with the tome"""
"""JSON string describing what parameters are used with the tome. Requires a list of JSON objects, one for each parameter."""
paramDefs: String
"""Eldritch script that will be executed when the tome is run"""
eldritch: String!
Expand Down
4 changes: 2 additions & 2 deletions tavern/internal/graphql/schema/ent.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ input CreateTomeInput {
name: String!
"""Information about the tome"""
description: String!
"""JSON string describing what parameters are used with the tome"""
"""JSON string describing what parameters are used with the tome. Requires a list of JSON objects, one for each parameter."""
paramDefs: String
"""Eldritch script that will be executed when the tome is run"""
eldritch: String!
Expand Down Expand Up @@ -799,7 +799,7 @@ type Tome implements Node {
name: String!
"""Information about the tome"""
description: String!
"""JSON string describing what parameters are used with the tome"""
"""JSON string describing what parameters are used with the tome. Requires a list of JSON objects, one for each parameter."""
paramDefs: String
"""Eldritch script that will be executed when the tome is run"""
eldritch: String!
Expand Down
4 changes: 2 additions & 2 deletions tavern/internal/www/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ input CreateTomeInput {
name: String!
"""Information about the tome"""
description: String!
"""JSON string describing what parameters are used with the tome"""
"""JSON string describing what parameters are used with the tome. Requires a list of JSON objects, one for each parameter."""
paramDefs: String
"""Eldritch script that will be executed when the tome is run"""
eldritch: String!
Expand Down Expand Up @@ -804,7 +804,7 @@ type Tome implements Node {
name: String!
"""Information about the tome"""
description: String!
"""JSON string describing what parameters are used with the tome"""
"""JSON string describing what parameters are used with the tome. Requires a list of JSON objects, one for each parameter."""
paramDefs: String
"""Eldritch script that will be executed when the tome is run"""
eldritch: String!
Expand Down
Loading