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 ab7ab25
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 7 deletions.
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 ab7ab25

Please sign in to comment.