111 lines
2.5 KiB
Go
111 lines
2.5 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"testing"
|
|
|
|
"src.opensuse.org/autogits/common"
|
|
"src.opensuse.org/autogits/common/gitea-generated/models"
|
|
mock "src.opensuse.org/autogits/common/mock"
|
|
"src.opensuse.org/autogits/common/test_utils"
|
|
)
|
|
|
|
func TestIssueProcessor_ProcessFunc(t *testing.T) {
|
|
ctrl := test_utils.NewController(t)
|
|
defer ctrl.Finish()
|
|
|
|
mockGitea := mock.NewMockGitea(ctrl)
|
|
bot := &ReparentBot{gitea: mockGitea}
|
|
processor := &IssueProcessor{bot: bot}
|
|
|
|
tests := []struct {
|
|
name string
|
|
req *common.Request
|
|
setupMock func()
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "issue event",
|
|
req: &common.Request{
|
|
Type: common.RequestType_Issue,
|
|
Data: &common.IssueWebhookEvent{
|
|
Repository: &common.Repository{
|
|
Name: "repo",
|
|
Owner: &common.Organization{Username: "org"},
|
|
},
|
|
Issue: &common.IssueDetail{
|
|
Number: 1,
|
|
},
|
|
},
|
|
},
|
|
setupMock: func() {
|
|
mockGitea.EXPECT().GetIssue("org", "repo", int64(1)).Return(&models.Issue{
|
|
State: "closed",
|
|
Repository: &models.RepositoryMeta{Owner: "org", Name: "repo"},
|
|
}, nil)
|
|
},
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "issue comment event",
|
|
req: &common.Request{
|
|
Type: common.RequestType_IssueComment,
|
|
Data: &common.IssueCommentWebhookEvent{
|
|
Repository: &common.Repository{
|
|
Name: "repo",
|
|
Owner: &common.Organization{Username: "org"},
|
|
},
|
|
Issue: &common.IssueDetail{
|
|
Number: 2,
|
|
},
|
|
},
|
|
},
|
|
setupMock: func() {
|
|
mockGitea.EXPECT().GetIssue("org", "repo", int64(2)).Return(&models.Issue{
|
|
State: "closed",
|
|
Repository: &models.RepositoryMeta{Owner: "org", Name: "repo"},
|
|
}, nil)
|
|
},
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "unhandled type",
|
|
req: &common.Request{
|
|
Type: "unhandled",
|
|
Data: nil,
|
|
},
|
|
setupMock: func() {},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "get issue error",
|
|
req: &common.Request{
|
|
Type: common.RequestType_Issue,
|
|
Data: &common.IssueWebhookEvent{
|
|
Repository: &common.Repository{
|
|
Name: "repo",
|
|
Owner: &common.Organization{Username: "org"},
|
|
},
|
|
Issue: &common.IssueDetail{
|
|
Number: 3,
|
|
},
|
|
},
|
|
},
|
|
setupMock: func() {
|
|
mockGitea.EXPECT().GetIssue("org", "repo", int64(3)).Return(nil, errors.New("error"))
|
|
},
|
|
wantErr: true,
|
|
},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
tc.setupMock()
|
|
err := processor.ProcessFunc(tc.req)
|
|
if (err != nil) != tc.wantErr {
|
|
t.Errorf("ProcessFunc() error = %v, wantErr %v", err, tc.wantErr)
|
|
}
|
|
})
|
|
}
|
|
}
|