2019-11-02 01:51:22 +03:00
// Copyright 2019 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package webhook
import (
"testing"
2021-12-10 04:27:50 +03:00
repo_model "code.gitea.io/gitea/models/repo"
2021-11-12 17:36:47 +03:00
"code.gitea.io/gitea/models/unittest"
2021-11-10 08:13:16 +03:00
webhook_model "code.gitea.io/gitea/models/webhook"
2019-11-02 01:51:22 +03:00
api "code.gitea.io/gitea/modules/structs"
2021-11-17 15:34:35 +03:00
2019-11-02 01:51:22 +03:00
"github.com/stretchr/testify/assert"
)
2019-11-04 01:13:25 +03:00
func TestWebhook_GetSlackHook ( t * testing . T ) {
2021-11-10 08:13:16 +03:00
w := & webhook_model . Webhook {
2019-11-04 01:13:25 +03:00
Meta : ` { "channel": "foo", "username": "username", "color": "blue"} ` ,
}
slackHook := GetSlackHook ( w )
assert . Equal ( t , * slackHook , SlackMeta {
Channel : "foo" ,
Username : "username" ,
Color : "blue" ,
} )
}
2019-11-02 01:51:22 +03:00
func TestPrepareWebhooks ( t * testing . T ) {
2021-11-12 17:36:47 +03:00
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
2019-11-02 01:51:22 +03:00
2021-12-10 04:27:50 +03:00
repo := unittest . AssertExistsAndLoadBean ( t , & repo_model . Repository { ID : 1 } ) . ( * repo_model . Repository )
2021-11-10 08:13:16 +03:00
hookTasks := [ ] * webhook_model . HookTask {
{ RepoID : repo . ID , HookID : 1 , EventType : webhook_model . HookEventPush } ,
2019-11-02 01:51:22 +03:00
}
for _ , hookTask := range hookTasks {
2021-11-16 11:53:21 +03:00
unittest . AssertNotExistsBean ( t , hookTask )
2019-11-02 01:51:22 +03:00
}
2021-11-10 08:13:16 +03:00
assert . NoError ( t , PrepareWebhooks ( repo , webhook_model . HookEventPush , & api . PushPayload { Commits : [ ] * api . PayloadCommit { { } } } ) )
2019-11-02 01:51:22 +03:00
for _ , hookTask := range hookTasks {
2021-11-16 11:53:21 +03:00
unittest . AssertExistsAndLoadBean ( t , hookTask )
2019-11-02 01:51:22 +03:00
}
}
func TestPrepareWebhooksBranchFilterMatch ( t * testing . T ) {
2021-11-12 17:36:47 +03:00
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
2019-11-02 01:51:22 +03:00
2021-12-10 04:27:50 +03:00
repo := unittest . AssertExistsAndLoadBean ( t , & repo_model . Repository { ID : 2 } ) . ( * repo_model . Repository )
2021-11-10 08:13:16 +03:00
hookTasks := [ ] * webhook_model . HookTask {
{ RepoID : repo . ID , HookID : 4 , EventType : webhook_model . HookEventPush } ,
2019-11-02 01:51:22 +03:00
}
for _ , hookTask := range hookTasks {
2021-11-16 11:53:21 +03:00
unittest . AssertNotExistsBean ( t , hookTask )
2019-11-02 01:51:22 +03:00
}
// this test also ensures that * doesn't handle / in any special way (like shell would)
2021-11-10 08:13:16 +03:00
assert . NoError ( t , PrepareWebhooks ( repo , webhook_model . HookEventPush , & api . PushPayload { Ref : "refs/heads/feature/7791" , Commits : [ ] * api . PayloadCommit { { } } } ) )
2019-11-02 01:51:22 +03:00
for _ , hookTask := range hookTasks {
2021-11-16 11:53:21 +03:00
unittest . AssertExistsAndLoadBean ( t , hookTask )
2019-11-02 01:51:22 +03:00
}
}
func TestPrepareWebhooksBranchFilterNoMatch ( t * testing . T ) {
2021-11-12 17:36:47 +03:00
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
2019-11-02 01:51:22 +03:00
2021-12-10 04:27:50 +03:00
repo := unittest . AssertExistsAndLoadBean ( t , & repo_model . Repository { ID : 2 } ) . ( * repo_model . Repository )
2021-11-10 08:13:16 +03:00
hookTasks := [ ] * webhook_model . HookTask {
{ RepoID : repo . ID , HookID : 4 , EventType : webhook_model . HookEventPush } ,
2019-11-02 01:51:22 +03:00
}
for _ , hookTask := range hookTasks {
2021-11-16 11:53:21 +03:00
unittest . AssertNotExistsBean ( t , hookTask )
2019-11-02 01:51:22 +03:00
}
2021-11-10 08:13:16 +03:00
assert . NoError ( t , PrepareWebhooks ( repo , webhook_model . HookEventPush , & api . PushPayload { Ref : "refs/heads/fix_weird_bug" } ) )
2019-11-02 01:51:22 +03:00
for _ , hookTask := range hookTasks {
2021-11-16 11:53:21 +03:00
unittest . AssertNotExistsBean ( t , hookTask )
2019-11-02 01:51:22 +03:00
}
}
// TODO TestHookTask_deliver
// TODO TestDeliverHooks