From 33437cd0115fc2d106b839ce90c7bf5a624800b9 Mon Sep 17 00:00:00 2001 From: Rowan Seymour Date: Thu, 14 Sep 2023 12:21:20 -0500 Subject: [PATCH] More tests --- assets/static/source_test.go | 30 +++++++++++- flows/optin.go | 14 ++++-- flows/optin_test.go | 75 ++++++++++++++++++++++++++++++ flows/triggers/optin.go | 4 +- flows/triggers/testdata/optin.json | 61 ++++++++++++++++++++++++ 5 files changed, 176 insertions(+), 8 deletions(-) create mode 100644 flows/optin_test.go diff --git a/assets/static/source_test.go b/assets/static/source_test.go index 39a00a637..71d357f55 100644 --- a/assets/static/source_test.go +++ b/assets/static/source_test.go @@ -9,6 +9,20 @@ import ( ) var assetsJSON = `{ + "channels": [ + { + "uuid": "58e9b092-fe42-4173-876c-ff45a14a24fe", + "name": "Facebook", + "address": "457547478475", + "schemes": [ + "facebook" + ], + "roles": [ + "send", + "receive" + ] + } + ], "flows": [ { "uuid": "76f0a02f-3b75-4b86-9064-e9195e1b3a02", @@ -35,6 +49,16 @@ var assetsJSON = `{ "name": "Spam" } ], + "optins": [ + { + "uuid": "248be71d-78e9-4d71-a6c4-9981d369e5cb", + "name": "Joke Of The Day", + "channel": { + "uuid": "58e9b092-fe42-4173-876c-ff45a14a24fe", + "name": "Facebook" + } + } + ], "resthooks": [ { "slug": "new-registration", @@ -59,7 +83,7 @@ func TestSource(t *testing.T) { channels, err = src.Channels() assert.NoError(t, err) - assert.Len(t, channels, 0) + assert.Len(t, channels, 1) classifiers, err := src.Classifiers() assert.NoError(t, err) @@ -93,6 +117,10 @@ func TestSource(t *testing.T) { assert.NoError(t, err) assert.Len(t, locations, 0) + optIns, err := src.OptIns() + assert.NoError(t, err) + assert.Len(t, optIns, 1) + resthooks, err := src.Resthooks() assert.NoError(t, err) assert.Len(t, resthooks, 1) diff --git a/flows/optin.go b/flows/optin.go index 13fabe42e..816a05265 100644 --- a/flows/optin.go +++ b/flows/optin.go @@ -11,7 +11,7 @@ import ( type OptIn struct { assets.OptIn - Channel *Channel + channel *Channel } // NewOptIn returns a new optin object from the given optin asset @@ -21,12 +21,15 @@ func NewOptIn(channels *ChannelAssets, asset assets.OptIn) (*OptIn, error) { return nil, errors.Errorf("no such channel with UUID %s", asset.Channel().UUID) } - return &OptIn{OptIn: asset, Channel: ch}, nil + return &OptIn{OptIn: asset, channel: ch}, nil } // Asset returns the underlying asset func (o *OptIn) Asset() assets.OptIn { return o.OptIn } +// Channel returns the associated channel +func (o *OptIn) Channel() *Channel { return o.channel } + // Reference returns a reference to this optin func (o *OptIn) Reference() *assets.OptInReference { if o == nil { @@ -44,9 +47,10 @@ func (o *OptIn) Reference() *assets.OptInReference { // @context ticket func (o *OptIn) Context(env envs.Environment) map[string]types.XValue { return map[string]types.XValue{ - "uuid": types.NewXText(string(o.UUID())), - "name": types.NewXText(string(o.Name())), - "channel": Context(env, o.Channel), + "__default__": types.NewXText(string(o.Name())), + "uuid": types.NewXText(string(o.UUID())), + "name": types.NewXText(string(o.Name())), + "channel": Context(env, o.channel), } } diff --git a/flows/optin_test.go b/flows/optin_test.go new file mode 100644 index 000000000..c3f8fb8d1 --- /dev/null +++ b/flows/optin_test.go @@ -0,0 +1,75 @@ +package flows_test + +import ( + "testing" + + "github.com/nyaruka/gocommon/uuids" + "github.com/nyaruka/goflow/assets" + "github.com/nyaruka/goflow/assets/static" + "github.com/nyaruka/goflow/envs" + "github.com/nyaruka/goflow/excellent/types" + "github.com/nyaruka/goflow/flows" + "github.com/nyaruka/goflow/flows/engine" + "github.com/nyaruka/goflow/test" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestOptIns(t *testing.T) { + defer uuids.SetGenerator(uuids.DefaultGenerator) + uuids.SetGenerator(uuids.NewSeededGenerator(12345)) + + env := envs.NewBuilder().Build() + + source, err := static.NewSource([]byte(`{ + "channels": [ + { + "uuid": "58e9b092-fe42-4173-876c-ff45a14a24fe", + "name": "Facebook", + "address": "457547478475", + "schemes": [ + "facebook" + ], + "roles": [ + "send", + "receive" + ] + } + ], + "optins": [ + { + "uuid": "248be71d-78e9-4d71-a6c4-9981d369e5cb", + "name": "Joke Of The Day", + "channel": { + "uuid": "58e9b092-fe42-4173-876c-ff45a14a24fe", + "name": "Facebook" + } + } + ] + }`)) + require.NoError(t, err) + + sa, err := engine.NewSessionAssets(env, source, nil) + require.NoError(t, err) + + jotd := sa.OptIns().Get("248be71d-78e9-4d71-a6c4-9981d369e5cb") + assert.Equal(t, assets.OptInUUID("248be71d-78e9-4d71-a6c4-9981d369e5cb"), jotd.UUID()) + assert.Equal(t, "Joke Of The Day", jotd.Name()) + assert.Equal(t, "Facebook", jotd.Channel().Name()) + assert.Equal(t, assets.NewOptInReference("248be71d-78e9-4d71-a6c4-9981d369e5cb", "Joke Of The Day"), jotd.Reference()) + + // check use in expressions + test.AssertXEqual(t, types.NewXObject(map[string]types.XValue{ + "__default__": types.NewXText("Joke Of The Day"), + "uuid": types.NewXText("248be71d-78e9-4d71-a6c4-9981d369e5cb"), + "name": types.NewXText("Joke Of The Day"), + "channel": types.NewXObject(map[string]types.XValue{ + "__default__": types.NewXText("Facebook"), + "uuid": types.NewXText("58e9b092-fe42-4173-876c-ff45a14a24fe"), + "name": types.NewXText("Facebook"), + "address": types.NewXText("457547478475"), + }), + }), flows.Context(env, jotd)) + + assert.Nil(t, (*flows.OptIn)(nil).Reference()) +} diff --git a/flows/triggers/optin.go b/flows/triggers/optin.go index 4b1d2c15e..7afe54d2b 100644 --- a/flows/triggers/optin.go +++ b/flows/triggers/optin.go @@ -103,7 +103,7 @@ type optInEventEnvelope struct { type optInTriggerEnvelope struct { baseTriggerEnvelope - Event optInEventEnvelope `json:"event" validate:"required,dive"` + Event *optInEventEnvelope `json:"event" validate:"required,dive"` } func readOptInTrigger(sa flows.SessionAssets, data json.RawMessage, missing assets.MissingCallback) (flows.Trigger, error) { @@ -133,7 +133,7 @@ func readOptInTrigger(sa flows.SessionAssets, data json.RawMessage, missing asse // MarshalJSON marshals this trigger into JSON func (t *OptInTrigger) MarshalJSON() ([]byte, error) { e := &optInTriggerEnvelope{ - Event: optInEventEnvelope{ + Event: &optInEventEnvelope{ Type: t.event.type_, OptIn: t.event.optIn.Reference(), }, diff --git a/flows/triggers/testdata/optin.json b/flows/triggers/testdata/optin.json index 116e4299f..714f75132 100644 --- a/flows/triggers/testdata/optin.json +++ b/flows/triggers/testdata/optin.json @@ -13,5 +13,66 @@ "triggered_on": "2000-01-01T00:00:00Z" }, "read_error": "field 'flow' is required" + }, + { + "description": "event is required", + "trigger": { + "type": "optin", + "flow": { + "uuid": "bead76f5-dac4-4c9d-996c-c62b326e8c0a", + "name": "Trigger Tester" + }, + "contact": { + "uuid": "9f7ede93-4b16-4692-80ad-b7dc54a1cd81", + "name": "Bob", + "status": "active", + "created_on": "2018-01-01T12:00:00Z" + }, + "triggered_on": "2000-01-01T00:00:00Z" + }, + "read_error": "field 'event' is required" + }, + { + "description": "with all required fields", + "trigger": { + "type": "optin", + "flow": { + "uuid": "bead76f5-dac4-4c9d-996c-c62b326e8c0a", + "name": "Trigger Tester" + }, + "contact": { + "uuid": "9f7ede93-4b16-4692-80ad-b7dc54a1cd81", + "name": "Bob", + "status": "active", + "created_on": "2018-01-01T12:00:00Z" + }, + "event": { + "type": "started", + "optin": { + "uuid": "248be71d-78e9-4d71-a6c4-9981d369e5cb", + "name": "Joke Of The Day" + } + }, + "triggered_on": "2000-01-01T00:00:00Z" + }, + "events": [], + "context": { + "campaign": null, + "keyword": "", + "optin": { + "uuid": "248be71d-78e9-4d71-a6c4-9981d369e5cb", + "name": "Joke Of The Day", + "channel": { + "uuid": "58e9b092-fe42-4173-876c-ff45a14a24fe", + "name": "Facebook", + "address": "457547478475" + } + }, + "origin": "", + "params": {}, + "ticket": null, + "type": "optin", + "user": null + } } ] \ No newline at end of file