|
| 1 | +package hook |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/lucasgomide/snitch/types" |
| 7 | + "gopkg.in/jarcoal/httpmock.v1" |
| 8 | +) |
| 9 | + |
| 10 | +var hangoutWebhookUrl = "https://hangouts.chat/123" |
| 11 | + |
| 12 | +func TestHangoutWhenNotificatedSuccessful(t *testing.T) { |
| 13 | + httpmock.Activate() |
| 14 | + defer httpmock.DeactivateAndReset() |
| 15 | + |
| 16 | + httpmock.RegisterResponder("POST", hangoutWebhookUrl, |
| 17 | + httpmock.NewStringResponder(200, `ok`)) |
| 18 | + |
| 19 | + hangout := &HangoutsChat{WebhookUrl: hangoutWebhookUrl} |
| 20 | + var deploy []types.Deploy |
| 21 | + deploy = append(deploy, types.Deploy{App: "app-sample"}) |
| 22 | + |
| 23 | + err := hangout.CallHook(deploy) |
| 24 | + if err != nil { |
| 25 | + t.Error(err) |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +func TestHangoutWhenResponseStatusCodeIsnt200(t *testing.T) { |
| 30 | + httpmock.Activate() |
| 31 | + defer httpmock.DeactivateAndReset() |
| 32 | + |
| 33 | + httpmock.RegisterResponder("POST", hangoutWebhookUrl, |
| 34 | + httpmock.NewStringResponder(400, `error`)) |
| 35 | + |
| 36 | + hangout := &HangoutsChat{WebhookUrl: hangoutWebhookUrl} |
| 37 | + var deploy []types.Deploy |
| 38 | + deploy = append(deploy, types.Deploy{App: "app-sample"}) |
| 39 | + |
| 40 | + err := hangout.CallHook(deploy) |
| 41 | + expected := "HangoutsChat - response status code is 400" |
| 42 | + if err == nil || err.Error() != expected { |
| 43 | + t.Error("Expected: "+expected+", but got", err.Error()) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func TestHangoutReturnsErrorWhenRequestFail(t *testing.T) { |
| 48 | + httpmock.Activate() |
| 49 | + defer httpmock.DeactivateAndReset() |
| 50 | + |
| 51 | + httpmock.RegisterNoResponder(nil) |
| 52 | + |
| 53 | + hangout := &HangoutsChat{WebhookUrl: hangoutWebhookUrl} |
| 54 | + var deploy []types.Deploy |
| 55 | + deploy = append(deploy, types.Deploy{App: "app-sample"}) |
| 56 | + |
| 57 | + err := hangout.CallHook(deploy) |
| 58 | + if err == nil { |
| 59 | + t.Error("The request has been failed but no error was raised") |
| 60 | + } |
| 61 | +} |
0 commit comments