Skip to content

Commit

Permalink
[#175733306] Completed submission code (go-delve#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
paran01d authored Nov 19, 2020
1 parent 020d9f5 commit 98641be
Show file tree
Hide file tree
Showing 51 changed files with 7,603 additions and 46 deletions.
1 change: 1 addition & 0 deletions backend/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ require (
github.com/streadway/amqp v1.0.0
github.com/ttacon/builder v0.0.0-20170518171403-c099f663e1c2 // indirect
github.com/vmihailenco/msgpack/v4 v4.3.12
gotest.tools v2.2.0+incompatible
)
5 changes: 5 additions & 0 deletions backend/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.4.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.1 h1:JFrFEBb2xKufg6XkJsJr+WbKb4FQlURi5RUcBveYu9k=
github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-github v17.0.0+incompatible/go.mod h1:zLgOLi98H3fifZn+44m+umXrS52loVEgC2AApnigrVQ=
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
Expand Down Expand Up @@ -283,6 +284,7 @@ github.com/pborman/uuid v1.2.1/go.mod h1:X/NO0urCmaxf9VXbdlT7C2Yzkj2IKimNn4k+gtP
github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY=
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4/go.mod h1:4OwLy04Bl9Ef3GJJCoec+30X3LQs/0/m4HFRt/2LUSA=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
Expand Down Expand Up @@ -637,6 +639,9 @@ gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gotest.tools v1.4.0 h1:BjtEgfuw8Qyd+jPvQz8CfoxiO/UjFEidWinwEXZiWv0=
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
gotest.tools/v3 v3.0.2/go.mod h1:3SzNCllyD9/Y+b5r9JIKQ474KzkZyqLqEfYqMsX94Bk=
honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
107 changes: 81 additions & 26 deletions backend/tualet/submit.go
Original file line number Diff line number Diff line change
@@ -1,49 +1,104 @@
package tualet

import (
"fmt"
"log"
"net/http"
"net/url"

"github.com/burstsms/mtmo-tp/backend/logger"
)

func SubmitGET(r *Route) {
type submitParams struct {
username string
password string
command string
message string
dnis string
ani string
longMessageMode string
}

params := r.r.URL.Query()
username := params.Get("username")
log.Printf("username: %s", username)
if username == "" {
r.WriteError("not authorized (check login and password)", http.StatusUnauthorized)
return
func checkParams(params url.Values) (int, string, submitParams) {

status := http.StatusOK
response := "submission"

values := submitParams{}

values.username = params.Get("username")
if values.username == "" {
status = http.StatusUnauthorized
response = "not authorized (check login and password)"
return status, response, values
}
password := params.Get("password")
if password == "" {
r.WriteError("not authorized (check login and password)", http.StatusUnauthorized)
return

values.password = params.Get("password")
if values.password == "" {
status = http.StatusUnauthorized
response = "not authorized (check login and password)"
return status, response, values
}

command := params.Get("command")
if command != "submit" {
r.WriteError("invalid command", http.StatusBadRequest)
return
values.command = params.Get("command")
if values.command != "submit" {
status = http.StatusBadRequest
response = "invalid command"
return status, response, values
}

message := params.Get("message")
if message == "" {
r.WriteError("message missing", http.StatusBadRequest)
return
values.message = params.Get("message")
if values.message == "" {
status = http.StatusBadRequest
response = "message missing"
return status, response, values
}

dnis := params.Get("dnis")
if message == "" {
r.WriteError("destination missing", http.StatusBadRequest)
return
values.dnis = params.Get("dnis")
if values.dnis == "" {
status = http.StatusBadRequest
response = "destination missing"
return status, response, values
}

ani := params.Get("ani")
longMessageMode := params.Get("longMessageMode")
values.ani = params.Get("ani")
values.longMessageMode = params.Get("longMessageMode")

r.api.log.Fields(logger.Fields{"msgid": "xxx", "dnis": dnis, "ani": ani, "message": message, "longMessageMode": longMessageMode, "status": 200})
return status, response, values
}

func SubmitGET(r *Route) {

status, response, values := checkParams(r.r.URL.Query())

if values.dnis != "" {
// Check for special overrides on number suffix
number := values.dnis[len(values.dnis)-4:]
log.Printf("Number: %s", number)
// Check for spoofing a submission error
switch number {
case "1400":
status = http.StatusBadRequest
response = "NO ROUTES"
}
}

r.api.log.Fields(logger.Fields{
"msgid": "xxx",
"dnis": values.dnis,
"ani": values.ani,
"message": values.message,
"command": values.command,
"longMessageMode": values.longMessageMode,
"status": status,
}).Info(response)

if status != http.StatusOK {
r.w.Header().Set("Content-Type", "text/html")
r.w.WriteHeader(status)
fmt.Fprintf(r.w, response)
return
}

type payload struct {
MessageID string `json:"message_id"`
Expand Down
121 changes: 101 additions & 20 deletions backend/tualet/submit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,122 @@ import (
"net/url"
"strings"
"testing"

"github.com/sirupsen/logrus"
"github.com/sirupsen/logrus/hooks/test"
"gotest.tools/assert"
)

func TestSubmit(t *testing.T) {

testValues := []struct {
values submitParams
expectedMsgId string
expectedResponseCode int
expectedResponseBody string
expectedLogMessage string
}{
{
values: submitParams{
username: "froggy",
},
expectedMsgId: "xxx",
expectedResponseCode: http.StatusUnauthorized,
expectedResponseBody: `not authorized (check login and password)`,
expectedLogMessage: `not authorized (check login and password)`,
},
{
values: submitParams{
username: "froggy",
password: "snoggy",
command: "submit",
message: "testing",
dnis: "61455123456",
ani: "61455678900",
longMessageMode: "split",
},
expectedMsgId: "xxx",
expectedResponseCode: http.StatusOK,
expectedResponseBody: `{"message_id":"xxx"}`,
expectedLogMessage: `submission`,
},
{
values: submitParams{
username: "froggy",
password: "snoggy",
command: "submit",
message: "testing",
dnis: "61455121400",
ani: "61455678900",
longMessageMode: "split",
},
expectedMsgId: "xxx",
expectedResponseCode: http.StatusBadRequest,
expectedResponseBody: `NO ROUTES`,
expectedLogMessage: `NO ROUTES`,
},
}

api := NewTualetAPI(&TualetAPIOptions{TemplatePath: `templates`})
loghook := test.NewLocal(api.log.Logger)
req, err := http.NewRequest("GET", "/api", nil)
if err != nil {
t.Fatal(err)
}

q := url.Values{}
q.Add("username", "froggy")
q.Add("password", "snoggy")
q.Add("command", "submit")
q.Add("message", "testing")
q.Add("dnis", "61455123456")
for _, testValue := range testValues {
q := url.Values{}
if testValue.values.username != "" {
q.Add("username", testValue.values.username)
}
if testValue.values.password != "" {
q.Add("password", testValue.values.password)
}
if testValue.values.command != "" {
q.Add("command", testValue.values.command)
}
if testValue.values.message != "" {
q.Add("message", testValue.values.message)
}
if testValue.values.dnis != "" {
q.Add("dnis", testValue.values.dnis)
}
if testValue.values.ani != "" {
q.Add("ani", testValue.values.ani)
}
if testValue.values.longMessageMode != "" {
q.Add("longMessageMode", testValue.values.longMessageMode)
}

req.URL.RawQuery = q.Encode()
req.URL.RawQuery = q.Encode()

rr := httptest.NewRecorder()
rr := httptest.NewRecorder()

api.router.ServeHTTP(rr, req)
api.router.ServeHTTP(rr, req)

log.Printf("req: %s", req.URL.String())
log.Printf("Req URL: %s", req.URL.String())

// Check the status code is what we expect.
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
// Check the status code is what we expect.
if status := rr.Code; status != testValue.expectedResponseCode {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}

// Check the response body is what we expect.
if strings.TrimSpace(rr.Body.String()) != testValue.expectedResponseBody {
t.Errorf("handler returned unexpected body: got '%v' want '%v'",
rr.Body.String(), testValue.expectedResponseBody)
}

// Check the response body is what we expect.
expected := `{"message_id":"xxx"}`
if strings.TrimSpace(rr.Body.String()) != expected {
t.Errorf("handler returned unexpected body: got '%v' want '%v'",
rr.Body.String(), expected)
logentry := loghook.LastEntry()
assert.Equal(t, testValue.expectedLogMessage, logentry.Message)
assert.Equal(t, logrus.InfoLevel, logentry.Level)
assert.Equal(t, testValue.values.message, logentry.Data[`message`])
assert.Equal(t, testValue.values.command, logentry.Data[`command`])
assert.Equal(t, testValue.expectedMsgId, logentry.Data[`msgid`])
assert.Equal(t, testValue.values.dnis, logentry.Data[`dnis`])
assert.Equal(t, testValue.values.ani, logentry.Data[`ani`])
assert.Equal(t, testValue.values.longMessageMode, logentry.Data[`longMessageMode`])
loghook.Reset()
}
}
27 changes: 27 additions & 0 deletions backend/vendor/github.com/google/go-cmp/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 98641be

Please sign in to comment.