Skip to content

Commit

Permalink
Output is now session, more tests, fix index offsets for jsonparser
Browse files Browse the repository at this point in the history
  • Loading branch information
nicpottier committed May 30, 2017
1 parent 98f4c72 commit 6dce6be
Show file tree
Hide file tree
Showing 17 changed files with 529 additions and 172 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Compiled Object files, Static and Dynamic libs (Shared Objects)
.vscode
*~
*.o
*.a
Expand Down
2 changes: 1 addition & 1 deletion cmd/flowrunner/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func main() {
inputs = append(inputs, event)

// rebuild our output
output, err = runs.ReadRunOutput(outJSON)
output, err = runs.ReadSession(outJSON)
if err != nil {
log.Fatalf("Error unmarshalling output: %s", err)
}
Expand Down
55 changes: 52 additions & 3 deletions cmd/flowrunner/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ import (
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"net/http/httptest"
"strings"
"testing"

"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/flows/actions"
"github.com/nyaruka/goflow/flows/definition"
"github.com/nyaruka/goflow/flows/engine"
"github.com/nyaruka/goflow/flows/events"
Expand All @@ -26,9 +30,11 @@ var flowTests = []struct {
{"two_questions.json", "", "", "two_questions_test.json"},
{"subflow.json", "", "", "subflow_test.json"},
{"brochure.json", "", "", "brochure_test.json"},
{"all_actions.json", "", "", "all_actions_test.json"},
}

var writeOutput bool
var serverURL = ""

func init() {
flag.BoolVar(&writeOutput, "write", false, "whether to rewrite TestFlow output")
Expand Down Expand Up @@ -64,6 +70,18 @@ func runFlow(env utils.Environment, flowFilename string, contactFilename string,
return nil, fmt.Errorf("Error unmarshalling flows '%s': %s", flowFilename, err)
}

// rewrite the URL on any webhook actions
for _, flow := range runnerFlows {
for _, n := range flow.Nodes() {
for _, a := range n.Actions() {
webhook, isWebhook := a.(*actions.WebhookAction)
if isWebhook {
webhook.URL = strings.Replace(webhook.URL, "http://localhost", serverURL, 1)
}
}
}
}

contactJSON, err := readFile("contacts/", contactFilename)
if err != nil {
return nil, err
Expand Down Expand Up @@ -99,7 +117,7 @@ func runFlow(env utils.Environment, flowFilename string, contactFilename string,
}
outputs = append(outputs, outJSON)

output, err = runs.ReadRunOutput(outJSON)
output, err = runs.ReadSession(outJSON)
if err != nil {
return nil, fmt.Errorf("Error marshalling output: %s", err)
}
Expand Down Expand Up @@ -134,8 +152,39 @@ func runFlow(env utils.Environment, flowFilename string, contactFilename string,
return outputs, nil
}

// set up a mock server for webhook actions
func startTestHTTPServer() {
server := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
cmd := r.URL.Query().Get("cmd")
defer r.Body.Close()
w.Header().Set("Date", "")

switch cmd {
case "success":
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{ "ok": "true" }`))
case "unavailable":
w.WriteHeader(http.StatusServiceUnavailable)
w.Write([]byte(`{ "errors": ["service unavailable"] }`))
default:
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(`{ "errors": ["bad_request"] }`))
}
}))
// manually create a listener for our test server so that our output is predictable
l, err := net.Listen("tcp", "127.0.0.1:49999")
if err != nil {
log.Fatal(err)
}
server.Listener = l
server.Start()
defer server.Close()
serverURL = server.URL
}

func TestFlows(t *testing.T) {
env := utils.NewDefaultEnvironment()
startTestHTTPServer()

for _, test := range flowTests {
testJSON, err := readFile("flows/", test.output)
Expand Down Expand Up @@ -195,11 +244,11 @@ func TestFlows(t *testing.T) {
}

for i := range outputs {
o, err := runs.ReadRunOutput(outputs[i])
o, err := runs.ReadSession(outputs[i])
if err != nil {
t.Errorf("Error unmarshalling output: %s\n", err)
}
expectedO, err := runs.ReadRunOutput(flowTest.Outputs[i])
expectedO, err := runs.ReadSession(flowTest.Outputs[i])
if err != nil {
t.Errorf("Error unmarshalling output: %s\n", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/flowrunner/testdata/flows/all_actions.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
"uuid": "06153fbd-3e2c-413a-b0df-ed15d631835a",
"type": "webhook",
"method": "get",
"url": "http://echo.jsontest.com/key/value/one/two"
"url": "http://localhost/?cmd=success"
}
]
}
Expand Down
238 changes: 238 additions & 0 deletions cmd/flowrunner/testdata/flows/all_actions_test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@
{
"resume_events": [],
"outputs": [
{
"runs": [
{
"uuid": "",
"flow": "flow1",
"channel": "",
"contact": "contact1-test-4b7f-a34b-e37e31e86451",
"path": [
{
"uuid": "",
"node": "node1",
"arrived_on": "2000-01-01T00:00:00.000000000-00:00",
"left_on": "2000-01-01T00:00:00.000000000-00:00",
"events": [
{
"type": "add_to_group",
"created_on": "2000-01-01T00:00:00.000000000-00:00",
"step": "",
"groups": [
{
"uuid": "",
"name": "Survey Audience"
}
]
},
{
"type": "save_to_contact",
"created_on": "2000-01-01T00:00:00.000000000-00:00",
"step": "",
"field": "f06c5b73-eb0d-4417-b7e0-4f650ed30dc8",
"name": "activation_token",
"value": "XXX-YYY-ZZZ"
},
{
"type": "email",
"created_on": "2000-01-01T00:00:00.000000000-00:00",
"step": "",
"email": "",
"subject": "Here is your activation token",
"body": "Your activation token is XXX-YYY-ZZZ"
},
{
"type": "flow_enter",
"created_on": "2000-01-01T00:00:00.000000000-00:00",
"step": "",
"flow": "b7cf0d83-f1c9-411c-96fd-c511a4cfa86d",
"contact": "contact1-test-4b7f-a34b-e37e31e86451"
},
{
"type": "flow_exit",
"created_on": "2000-01-01T00:00:00.000000000-00:00",
"step": "",
"flow": "b7cf0d83-f1c9-411c-96fd-c511a4cfa86d",
"contact": "contact1-test-4b7f-a34b-e37e31e86451",
"status": "C",
"exited_on": "2000-01-01T00:00:00.000000000-00:00"
},
{
"type": "msg_out",
"created_on": "2000-01-01T00:00:00.000000000-00:00",
"step": "",
"urn": "tel:+12065551212",
"text": "Hi Ben Haggerty, are you ready?"
},
{
"type": "msg_out",
"created_on": "2000-01-01T00:00:00.000000000-00:00",
"step": "",
"contact": "contact1-test-4b7f-a34b-e37e31e86451",
"text": "Hi Ben Haggerty, are you ready to complete today's survey?"
},
{
"type": "save_result",
"created_on": "2000-01-01T00:00:00.000000000-00:00",
"step": "",
"node": "node1",
"name": "gender",
"value": "m",
"category": "Male"
},
{
"type": "save_to_contact",
"created_on": "2000-01-01T00:00:00.000000000-00:00",
"step": "",
"field": "0cb17b2a-3bfe-4a19-8c99-98ab9561045d",
"name": "Gender",
"value": "Male"
},
{
"type": "error",
"created_on": "2000-01-01T00:00:00.000000000-00:00",
"step": "",
"text": "Get http://127.0.0.1:49999/?cmd=success: dial tcp 127.0.0.1:49999: getsockopt: connection refused"
},
{
"type": "webhook",
"created_on": "2000-01-01T00:00:00.000000000-00:00",
"step": "",
"url": "http://127.0.0.1:49999/?cmd=success",
"status": "F",
"status_code": 0,
"request": "GET /?cmd=success HTTP/1.1\r\nHost: 127.0.0.1:49999\r\nUser-Agent: Go-http-client/1.1\r\nAccept-Encoding: gzip\r\n\r\n",
"response": ""
}
]
}
],
"status": "C",
"child": "c458a838-efa8-42d7-a655-3e4784bcbf5b",
"results": {
"gender": {
"node": "node1",
"name": "gender",
"value": "m",
"category": "Male",
"created_on": "2000-01-01T00:00:00.000000000-00:00"
}
},
"created_on": "2000-01-01T00:00:00.000000000-00:00",
"modified_on": "2000-01-01T00:00:00.000000000-00:00",
"expires_on": "2000-01-01T00:00:00.000000000-00:00",
"timesout_on": "2000-01-01T00:00:00.000000000-00:00",
"exited_on": "2000-01-01T00:00:00.000000000-00:00"
},
{
"uuid": "",
"flow": "b7cf0d83-f1c9-411c-96fd-c511a4cfa86d",
"channel": "",
"contact": "contact1-test-4b7f-a34b-e37e31e86451",
"path": [],
"status": "C",
"parent": "2d17c317-93c1-497d-879d-3ed56cf1b0d4",
"results": {},
"created_on": "2000-01-01T00:00:00.000000000-00:00",
"modified_on": "2000-01-01T00:00:00.000000000-00:00",
"expires_on": "2000-01-01T00:00:00.000000000-00:00",
"timesout_on": "2000-01-01T00:00:00.000000000-00:00",
"exited_on": "2000-01-01T00:00:00.000000000-00:00"
}
],
"events": [
{
"type": "add_to_group",
"created_on": "2000-01-01T00:00:00.000000000-00:00",
"step": "",
"groups": [
{
"uuid": "",
"name": "Survey Audience"
}
]
},
{
"type": "save_to_contact",
"created_on": "2000-01-01T00:00:00.000000000-00:00",
"step": "",
"field": "f06c5b73-eb0d-4417-b7e0-4f650ed30dc8",
"name": "activation_token",
"value": "XXX-YYY-ZZZ"
},
{
"type": "email",
"created_on": "2000-01-01T00:00:00.000000000-00:00",
"step": "",
"email": "",
"subject": "Here is your activation token",
"body": "Your activation token is XXX-YYY-ZZZ"
},
{
"type": "flow_enter",
"created_on": "2000-01-01T00:00:00.000000000-00:00",
"step": "",
"flow": "b7cf0d83-f1c9-411c-96fd-c511a4cfa86d",
"contact": "contact1-test-4b7f-a34b-e37e31e86451"
},
{
"type": "flow_exit",
"created_on": "2000-01-01T00:00:00.000000000-00:00",
"step": "",
"flow": "b7cf0d83-f1c9-411c-96fd-c511a4cfa86d",
"contact": "contact1-test-4b7f-a34b-e37e31e86451",
"status": "C",
"exited_on": "2000-01-01T00:00:00.000000000-00:00"
},
{
"type": "msg_out",
"created_on": "2000-01-01T00:00:00.000000000-00:00",
"step": "",
"urn": "tel:+12065551212",
"text": "Hi Ben Haggerty, are you ready?"
},
{
"type": "msg_out",
"created_on": "2000-01-01T00:00:00.000000000-00:00",
"step": "",
"contact": "contact1-test-4b7f-a34b-e37e31e86451",
"text": "Hi Ben Haggerty, are you ready to complete today's survey?"
},
{
"type": "save_result",
"created_on": "2000-01-01T00:00:00.000000000-00:00",
"step": "",
"node": "node1",
"name": "gender",
"value": "m",
"category": "Male"
},
{
"type": "save_to_contact",
"created_on": "2000-01-01T00:00:00.000000000-00:00",
"step": "",
"field": "0cb17b2a-3bfe-4a19-8c99-98ab9561045d",
"name": "Gender",
"value": "Male"
},
{
"type": "error",
"created_on": "2000-01-01T00:00:00.000000000-00:00",
"step": "",
"text": "Get http://127.0.0.1:49999/?cmd=success: dial tcp 127.0.0.1:49999: getsockopt: connection refused"
},
{
"type": "webhook",
"created_on": "2000-01-01T00:00:00.000000000-00:00",
"step": "",
"url": "http://127.0.0.1:49999/?cmd=success",
"status": "F",
"status_code": 0,
"request": "GET /?cmd=success HTTP/1.1\r\nHost: 127.0.0.1:49999\r\nUser-Agent: Go-http-client/1.1\r\nAccept-Encoding: gzip\r\n\r\n",
"response": ""
}
]
}
]
}
Loading

0 comments on commit 6dce6be

Please sign in to comment.