Skip to content

Instantly share code, notes, and snippets.

@jeroenbourgois
Created June 1, 2015 16:52
Show Gist options
  • Save jeroenbourgois/3324db33622256de2109 to your computer and use it in GitHub Desktop.
Save jeroenbourgois/3324db33622256de2109 to your computer and use it in GitHub Desktop.
package api
import (
"bitbucket.org/jackjoe/timr/cmd/timr"
"net/http"
"net/http/httptest"
"strings"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestSlackPostHandler(t *testing.T) {
Convey("Subject: test the Slack POST handler", t, func() {
Convey("Given the API receives a Slack POST", func() {
timr.OpenDb()
// TODO: should we check the token @pierot?
Convey("When the body contains invalid POST data", func() {
req, w := newTestRequestRecorder("POST", "%@^#$^@!*&(#P{")
slackPostHandler(w, req)
Convey("The server returns HTTP 500 - Internal Server Error", func() {
So(w.Code, ShouldEqual, http.StatusInternalServerError)
})
Convey("The response body should contain the message 'Cannot parseForm'", func() {
So(w.Body.String(), ShouldEqual, "Cannot ParseForm")
})
})
Convey("When the command is NOT time", func() {
req, w := newTestRequestRecorder("POST", "command=%2Ffoo&text=proj+10")
slackPostHandler(w, req)
Convey("The server returns HTTP 403 - Forbidden", func() {
So(w.Code, ShouldEqual, http.StatusForbidden)
})
Convey("The response body should contain the message 'Wrong command, accepting `Time` only'", func() {
So(w.Body.String(), ShouldEqual, "Wrong command, accepting `time` only")
})
})
Convey("When a command is passed", func() {
type Command struct {
Command string
HttpStatus int
HttpResponse string
}
commands := []Command{
Command{
Command: "command=%2Ftime&text=foo+proj+10",
HttpStatus: 400,
HttpResponse: "Wrong subcommand, options: add|show",
},
Command{
Command: "command=%2Ftime&text=bar+proj+10",
HttpStatus: 400,
HttpResponse: "Wrong subcommand, options: add|show",
},
Command{
Command: "command=%2Ftime&text=add",
HttpStatus: 400,
HttpResponse: "Invalid parameters (add CLIENT PROJECT DURATION message)",
},
Command{
Command: "command=%2Ftime&text=add+client+proj+10",
HttpStatus: 200,
HttpResponse: "10 minutes added to proj",
},
Command{
Command: "command=%2Ftime&text=",
HttpStatus: 400,
HttpResponse: "No subcommand found (options: add|show)",
},
}
for _, command := range commands {
Convey("It should be handled accordingly: "+command.Command+" | "+command.HttpResponse,
func() {
req, w := newTestRequestRecorder("POST", command.Command)
slackPostHandler(w, req)
// So(w.Code, ShouldEqual, command.HttpStatus)
So(w.Body.String(), ShouldContainSubstring, command.HttpResponse)
})
}
})
})
})
}
// helpers
func newTestRequestRecorder(method string, postData string) (*http.Request, *httptest.ResponseRecorder) {
req, _ := http.NewRequest("POST", "/", strings.NewReader(postData))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; param=value")
w := httptest.NewRecorder()
return req, w
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment