Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nicpottier committed May 11, 2017
1 parent 1813270 commit 3830856
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 29 deletions.
12 changes: 6 additions & 6 deletions contact.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,20 @@ VALUES(:org_id, TRUE, FALSE, FALSE, FALSE, :uuid, :created_on, :modified_on, :cr
RETURNING id
`

// InsertContact inserts the passed in contact, the id field will be populated with the result on success
func InsertContact(db *sqlx.DB, contact *Contact) error {
// insertContact inserts the passed in contact, the id field will be populated with the result on success
func insertContact(db *sqlx.DB, contact *Contact) error {
rows, err := db.NamedQuery(insertContactSQL, contact)
if err != nil {
return err
}
if rows.Next() {
rows.Scan(&contact.ID)
err = rows.Scan(&contact.ID)
}
return err
}

// ContactForURN first tries to look up a contact for the passed in URN, if not finding one then creating one
func ContactForURN(db *sqlx.DB, org OrgID, channel ChannelID, urn URN, name string) (*Contact, error) {
// contactForURN first tries to look up a contact for the passed in URN, if not finding one then creating one
func contactForURN(db *sqlx.DB, org OrgID, channel ChannelID, urn URN, name string) (*Contact, error) {
// try to look up our contact by URN
var contact Contact
err := db.Get(&contact, lookupContactFromURNSQL, urn, org)
Expand All @@ -81,7 +81,7 @@ func ContactForURN(db *sqlx.DB, org OrgID, channel ChannelID, urn URN, name stri
contact.ModifiedBy = 1

// Insert it
err = InsertContact(db, &contact)
err = insertContact(db, &contact)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion handlers/twilio/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func (h *twHandler) validateSignature(channel *courier.Channel, r *http.Request)
return err
}

url := fmt.Sprintf("%s%s", h.Server().GetConfig().Base_URL, r.URL.RequestURI())
url := fmt.Sprintf("%s%s", h.Server().GetConfig().BaseURL, r.URL.RequestURI())
authToken := channel.GetConfig(courier.ConfigAuthToken)

expected, err := twCalculateSignature(url, r.PostForm, authToken)
Expand Down
53 changes: 33 additions & 20 deletions msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ func NewMsg(channel *Channel, urn URN, text string) *Msg {
m.clear()

m.UUID = NewMsgUUID()
m.OrgID = channel.OrgID
m.ChannelID = channel.ID
m.ChannelUUID = channel.UUID
m.ContactURN = urn
m.Text = text
Expand Down Expand Up @@ -135,18 +137,20 @@ func queueMsg(s *server, m *Msg) error {
}
}

// marshal this msg to JSON
msgJSON, err := json.Marshal(m)
// grab the contact for this msg
_, err := contactForURN(s.db, m.OrgID, m.ChannelID, m.ContactURN, m.ContactName)

// our db is down, write to the spool, we will write/queue this later
if err != nil {
return err
return writeToSpool(s, "msgs", m)
}

// try to write this to redis
err = writeMsgToRedis(s, m, msgJSON)
err = writeMsgToRedis(s, m)

// we failed our write to redis, write to disk instead
if err != nil {
err = writeToSpool(s, "msgs", msgJSON)
err = writeToSpool(s, "msgs", m)
}

return err
Expand All @@ -160,17 +164,11 @@ func queueMsgStatus(s *server, status *MsgStatusUpdate) error {
return err
}

// other errors are DB errors, courier keeps running in this case and queues up the status anyways
statusJSON, err := json.Marshal(status)
if err != nil {
return err
}

err = writeMsgStatusToRedis(s, status, statusJSON)
err = writeMsgStatusToRedis(s, status)

// failed writing, write to our spool instead
if err != nil {
err = writeToSpool(s, "statuses", statusJSON)
err = writeToSpool(s, "statuses", status)
}

return err
Expand Down Expand Up @@ -206,7 +204,12 @@ func startMsgSpoolFlusher(s *server) {

}

func writeMsgToRedis(s *server, m *Msg, msgJSON []byte) error {
func writeMsgToRedis(s *server, m *Msg) error {
msgJSON, err := json.Marshal(m)
if err != nil {
return err
}

// write it to redis
r := s.redisPool.Get()
defer r.Close()
Expand All @@ -215,7 +218,7 @@ func writeMsgToRedis(s *server, m *Msg, msgJSON []byte) error {
r.Send("MULTI")
r.Send("RPUSH", fmt.Sprintf("c:u:%s", m.ContactURN), msgJSON)
r.Send("RPUSH", "c:msgs", m.ContactURN)
_, err := r.Do("EXEC")
_, err = r.Do("EXEC")
if err != nil {
return err
}
Expand Down Expand Up @@ -246,7 +249,12 @@ func checkMsgExists(s *server, status *MsgStatusUpdate) (err error) {
return err
}

func writeMsgStatusToRedis(s *server, status *MsgStatusUpdate, statusJSON []byte) (err error) {
func writeMsgStatusToRedis(s *server, status *MsgStatusUpdate) (err error) {
statusJSON, err := json.Marshal(status)
if err != nil {
return err
}

// write it to redis
r := s.redisPool.Get()
defer r.Close()
Expand Down Expand Up @@ -324,9 +332,14 @@ func testSpoolDirs(s *server) (err error) {
return err
}

func writeToSpool(s *server, subdir string, contents []byte) error {
func writeToSpool(s *server, subdir string, contents interface{}) error {
contentBytes, err := json.Marshal(contents)
if err != nil {
return err
}

filename := path.Join(s.config.SpoolDir, subdir, fmt.Sprintf("%d.json", time.Now().UnixNano()))
return ioutil.WriteFile(filename, contents, 0640)
return ioutil.WriteFile(filename, contentBytes, 0640)
}

type fileFlusher func(filename string, contents []byte) error
Expand Down Expand Up @@ -385,7 +398,7 @@ func (s *server) msgSpoolWalker(dir string) filepath.WalkFunc {
}

// try to flush to redis
return writeMsgToRedis(s, msg, contents)
return writeMsgToRedis(s, msg)
})
}

Expand All @@ -399,7 +412,7 @@ func (s *server) statusSpoolWalker(dir string) filepath.WalkFunc {
}

// try to flush to redis
return writeMsgStatusToRedis(s, status, contents)
return writeMsgStatusToRedis(s, status)
})
}

Expand Down
3 changes: 1 addition & 2 deletions urn.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,10 @@ func ContactURNForURN(db *sqlx.DB, org OrgID, channel ChannelID, contact Contact
if contactURN.Channel != channel || contactURN.Contact != contact {
contactURN.Channel = channel
contactURN.Contact = contact

err = UpdateContactURN(db, contactURN)
}

return contactURN, nil
return contactURN, err
}

// InsertContactURN inserts the passed in urn, the id field will be populated with the result on success
Expand Down

0 comments on commit 3830856

Please sign in to comment.