harness-drone/plugin/webhook/webhook_test.go

86 lines
1.9 KiB
Go
Raw Normal View History

2019-02-19 23:56:41 +00:00
// Copyright 2019 Drone.IO Inc. All rights reserved.
// Use of this source code is governed by the Drone Non-Commercial License
// that can be found in the LICENSE file.
// +build !oss
2019-02-19 23:56:41 +00:00
package webhook
import (
"context"
"net/http"
"testing"
"github.com/drone/drone/core"
"github.com/99designs/httpsignatures-go"
"github.com/h2non/gock"
)
var noContext = context.Background()
func TestWebhook(t *testing.T) {
defer gock.Off()
webhook := &core.WebhookData{
Event: core.WebhookEventUser,
Action: core.WebhookActionCreated,
User: &core.User{Login: "octocat"},
}
matchSignature := func(r *http.Request, _ *gock.Request) (bool, error) {
signature, err := httpsignatures.FromRequest(r)
if err != nil {
return false, err
}
return signature.IsValid("GMEuUHQfmrMRsseWxi9YlIeBtn9lm6im", r), nil
}
gock.New("https://company.com").
Post("/hooks").
AddMatcher(matchSignature).
MatchHeader("X-Drone-Event", "user").
MatchHeader("Content-Type", "application/json").
MatchHeader("Digest", "SHA-256=Rro8edtwJUjLl6e\\/xB9m1ykaymiaC9YxFGb\\/XNuXzO4=").
JSON(webhook).
Reply(200).
Type("application/json")
sender := New([]string{"https://company.com/hooks"}, "GMEuUHQfmrMRsseWxi9YlIeBtn9lm6im")
err := sender.Send(noContext, webhook)
if err != nil {
t.Error(err)
}
if gock.IsPending() {
t.Errorf("Unfinished requests")
}
}
func TestWebhook_CustomClient(t *testing.T) {
sender := new(sender)
if sender.client() != http.DefaultClient {
t.Errorf("Expect default http client")
}
custom := &http.Client{}
sender.Client = custom
if sender.client() != custom {
t.Errorf("Expect custom http client")
}
}
func TestWebhook_NoEndpoints(t *testing.T) {
webhook := &core.WebhookData{
Event: core.WebhookEventUser,
Action: core.WebhookActionCreated,
User: &core.User{Login: "octocat"},
}
sender := New([]string{}, "correct-horse-battery-staple")
err := sender.Send(noContext, webhook)
if err != nil {
t.Error(err)
}
}