Browse Source

Add a few more tests

kegan/travis
Kegan Dougal 8 years ago
parent
commit
7f8c36ba89
  1. 2
      src/github.com/matrix-org/go-neb/services/travisci/travisci.go
  2. 34
      src/github.com/matrix-org/go-neb/services/travisci/travisci_test.go

2
src/github.com/matrix-org/go-neb/services/travisci/travisci.go

@ -125,7 +125,7 @@ func notifToTemplate(n webhookNotification) map[string]string {
if n.Status != nil { if n.Status != nil {
t["result"] = strconv.Itoa(*n.Status) t["result"] = strconv.Itoa(*n.Status)
} }
t["message"] = n.StatusMessage
t["message"] = n.StatusMessage // message: Travis CI message to the build
if n.StartedAt != nil && n.FinishedAt != nil { if n.StartedAt != nil && n.FinishedAt != nil {
// duration: total duration of all builds in the matrix -- TODO // duration: total duration of all builds in the matrix -- TODO

34
src/github.com/matrix-org/go-neb/services/travisci/travisci_test.go

@ -73,6 +73,17 @@ var travisTests = []struct {
"%{repository}#%{build_number} (%{branch} - %{commit} : %{author}): %{message}", "%{repository}#%{build_number} (%{branch} - %{commit} : %{author}): %{message}",
"Kegsay/flow-jsdoc#18 (master - 3a092c3a6032ebb50384c99b445f947e9ce86e2a : Kegan Dougal): Passed", "Kegsay/flow-jsdoc#18 (master - 3a092c3a6032ebb50384c99b445f947e9ce86e2a : Kegan Dougal): Passed",
}, },
{
"obviously_invalid_signature", false, exampleBody,
"%{repository}#%{build_number} (%{branch} - %{commit} : %{author}): %{message}",
"Kegsay/flow-jsdoc#18 (master - 3a092c3a6032ebb50384c99b445f947e9ce86e2a : Kegan Dougal): Passed",
},
{
// Payload is valid but doesn't match signature now
exampleSignature, false, strings.TrimSuffix(exampleBody, "%7D") + "%2C%22EXTRA_KEY%22%3Anull%7D",
"%{repository}#%{build_number} (%{branch} - %{commit} : %{author}): %{message}",
"Kegsay/flow-jsdoc#18 (master - 3a092c3a6032ebb50384c99b445f947e9ce86e2a : Kegan Dougal): Passed",
},
} }
type MockTransport struct { type MockTransport struct {
@ -151,17 +162,30 @@ func TestTravisCI(t *testing.T) {
req.Header.Set("Content-Type", "application/x-www-form-urlencoded") req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
travis.OnReceiveWebhook(mockWriter, req, matrixCli) travis.OnReceiveWebhook(mockWriter, req, matrixCli)
if mockWriter.Code != 200 {
t.Errorf("TestTravisCI OnReceiveWebhook want code %d, got %d", 200, mockWriter.Code)
}
if len(msgs) != 1 {
t.Errorf("TestTravisCI want sent messages %d, got %d ", 1, len(msgs))
if test.ValidSignature {
if !assertResponse(t, mockWriter, msgs, 200, 1) {
continue continue
} }
if msgs[0].Body != test.ExpectedOutput { if msgs[0].Body != test.ExpectedOutput {
t.Errorf("TestTravisCI want matrix body '%s', got '%s'", test.ExpectedOutput, msgs[0].Body) t.Errorf("TestTravisCI want matrix body '%s', got '%s'", test.ExpectedOutput, msgs[0].Body)
} }
} else {
assertResponse(t, mockWriter, msgs, 403, 0)
}
}
}
func assertResponse(t *testing.T, w *httptest.ResponseRecorder, msgs []matrix.TextMessage, expectCode int, expectMsgLength int) bool {
if w.Code != expectCode {
t.Errorf("TestTravisCI OnReceiveWebhook want HTTP code %d, got %d", expectCode, w.Code)
return false
}
if len(msgs) != expectMsgLength {
t.Errorf("TestTravisCI want %d sent messages, got %d ", expectMsgLength, len(msgs))
return false
} }
return true
} }
func makeService(t *testing.T, template string) *Service { func makeService(t *testing.T, template string) *Service {

Loading…
Cancel
Save