Skip to content

Commit

Permalink
Merge pull request #11 from nyaruka/output-to-session
Browse files Browse the repository at this point in the history
Output is now session, more tests, fix index offsets for jsonparser
  • Loading branch information
nicpottier authored May 30, 2017
2 parents 98f4c72 + 0cfc908 commit c41ba17
Show file tree
Hide file tree
Showing 17 changed files with 521 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
59 changes: 56 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,9 +152,44 @@ func runFlow(env utils.Environment, flowFilename string, contactFilename string,
return outputs, nil
}

// set up a mock server for webhook actions
func newTestHTTPServer() *httptest.Server {
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
return server
}

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

server := newTestHTTPServer()
server.Start()
defer server.Close()

// save away our server URL so we can rewrite our URLs
serverURL = server.URL

for _, test := range flowTests {
testJSON, err := readFile("flows/", test.output)
if err != nil {
Expand Down Expand Up @@ -195,11 +248,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
226 changes: 226 additions & 0 deletions cmd/flowrunner/testdata/flows/all_actions_test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
{
"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": "webhook",
"created_on": "2000-01-01T00:00:00.000000000-00:00",
"step": "",
"url": "http://127.0.0.1:49999/?cmd=success",
"status": "S",
"status_code": 200,
"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": "HTTP/1.1 200 OK\r\nContent-Length: 16\r\nContent-Type: text/plain; charset=utf-8\r\nDate: \r\n\r\n{ \"ok\": \"true\" }"
}
]
}
],
"status": "C",
"child": "c9943827-b80b-4e22-8e46-6be598049f4e",
"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": "746ab857-7354-4aa9-8b16-a9b62d23c4aa",
"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": "webhook",
"created_on": "2000-01-01T00:00:00.000000000-00:00",
"step": "",
"url": "http://127.0.0.1:49999/?cmd=success",
"status": "S",
"status_code": 200,
"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": "HTTP/1.1 200 OK\r\nContent-Length: 16\r\nContent-Type: text/plain; charset=utf-8\r\nDate: \r\n\r\n{ \"ok\": \"true\" }"
}
]
}
]
}
Loading

0 comments on commit c41ba17

Please sign in to comment.