Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Sep 14, 2023
1 parent 828201c commit 233276a
Show file tree
Hide file tree
Showing 6 changed files with 204 additions and 9 deletions.
30 changes: 29 additions & 1 deletion assets/static/source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
29 changes: 28 additions & 1 deletion flows/engine/assets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,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",
Expand All @@ -41,6 +55,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",
Expand Down Expand Up @@ -74,6 +98,9 @@ func TestSessionAssets(t *testing.T) {

assert.Nil(t, sa.Groups().Get("xyz"))

optIn := sa.OptIns().Get("248be71d-78e9-4d71-a6c4-9981d369e5cb")
assert.Equal(t, "Joke Of The Day", optIn.Name())

resthook := sa.Resthooks().FindBySlug("new-registration")
assert.Equal(t, "new-registration", resthook.Slug())
assert.Equal(t, []string{"http://temba.io/"}, resthook.Subscribers())
Expand Down Expand Up @@ -116,7 +143,7 @@ func TestSessionAssetsWithSourceErrors(t *testing.T) {
_, err = sa.Flows().FindByName("Catch All")
assert.EqualError(t, err, "unable to load flow assets")

for _, errType := range []string{"channels", "classifiers", "fields", "globals", "groups", "labels", "locations", "resthooks", "templates", "users"} {
for _, errType := range []string{"channels", "classifiers", "fields", "globals", "groups", "labels", "locations", "optins", "resthooks", "templates", "users"} {
source.currentErrType = errType
_, err = engine.NewSessionAssets(env, source, nil)
assert.EqualError(t, err, fmt.Sprintf("unable to load %s assets", errType), "error mismatch for type %s", errType)
Expand Down
14 changes: 9 additions & 5 deletions flows/optin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Check warning on line 21 in flows/optin.go

View check run for this annotation

Codecov / codecov/patch

flows/optin.go#L21

Added line #L21 was not covered by tests
}

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 }

Check warning on line 28 in flows/optin.go

View check run for this annotation

Codecov / codecov/patch

flows/optin.go#L28

Added line #L28 was not covered by tests

// 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 {
Expand All @@ -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),
}
}

Expand Down
75 changes: 75 additions & 0 deletions flows/optin_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
4 changes: 2 additions & 2 deletions flows/triggers/optin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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(),
},
Expand Down
61 changes: 61 additions & 0 deletions flows/triggers/testdata/optin.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
]

0 comments on commit 233276a

Please sign in to comment.