From 7f8c36ba89534bdcf517076a4da0e9afd0c131b3 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Wed, 16 Nov 2016 13:09:17 +0000 Subject: [PATCH] Add a few more tests --- .../go-neb/services/travisci/travisci.go | 2 +- .../go-neb/services/travisci/travisci_test.go | 42 +++++++++++++++---- 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/src/github.com/matrix-org/go-neb/services/travisci/travisci.go b/src/github.com/matrix-org/go-neb/services/travisci/travisci.go index 7391268..9fc4fed 100644 --- a/src/github.com/matrix-org/go-neb/services/travisci/travisci.go +++ b/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 { 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 { // duration: total duration of all builds in the matrix -- TODO diff --git a/src/github.com/matrix-org/go-neb/services/travisci/travisci_test.go b/src/github.com/matrix-org/go-neb/services/travisci/travisci_test.go index 0affa2f..81d17b7 100644 --- a/src/github.com/matrix-org/go-neb/services/travisci/travisci_test.go +++ b/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}", "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 { @@ -151,19 +162,32 @@ func TestTravisCI(t *testing.T) { req.Header.Set("Content-Type", "application/x-www-form-urlencoded") 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)) - continue - } - if msgs[0].Body != test.ExpectedOutput { - t.Errorf("TestTravisCI want matrix body '%s', got '%s'", test.ExpectedOutput, msgs[0].Body) + if test.ValidSignature { + if !assertResponse(t, mockWriter, msgs, 200, 1) { + continue + } + + if msgs[0].Body != test.ExpectedOutput { + 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 { srv, err := types.CreateService("id", ServiceType, "@travisci:hyrule", []byte( `{