autogits/workflow-pr/review_test.go

79 lines
1.9 KiB
Go
Raw Normal View History

2024-12-11 14:41:51 +01:00
package main
import (
"testing"
"go.uber.org/mock/gomock"
"src.opensuse.org/autogits/common"
"src.opensuse.org/autogits/common/gitea-generated/models"
mock_common "src.opensuse.org/autogits/common/mock"
mock_main "src.opensuse.org/workflow-pr/mock"
)
func TestReviewApproval(t *testing.T) {
config := common.AutogitConfig{
Branch: "bar",
Organization: "foo",
GitProjectName: common.DefaultGitPrj,
}
tests := []struct {
name string
pr *models.PullRequest
reviews []*models.PullReview
maintainerFile []byte
approved bool
}{
{
name: "Maintainer not approved",
pr: &models.PullRequest{Body: "PR: foo/foo#10", Index: 10, RequestedReviewers: []*models.User{}},
reviews: []*models.PullReview{},
maintainerFile: []byte(`{"foo": ["bingo"]}`),
approved: false,
},
{
name: "Maintainer approved",
pr: &models.PullRequest{Body: "", Index: 10, RequestedReviewers: []*models.User{}},
reviews: []*models.PullReview{
&models.PullReview{
Body: "wow!",
Stale: false,
State: common.ReviewStateApproved,
User: &models.User{
UserName: "king",
},
},
},
maintainerFile: []byte(`{"": ["king"], "foo": ["bingo"]}`),
approved: true,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ctl := gomock.NewController(t)
mi := mock_common.NewMockGiteaMaintainershipInterface(ctl)
pri := mock_main.NewMockGiteaPRInterface(ctl)
pri.EXPECT().GetPullRequestAndReviews("foo", common.DefaultGitPrj, int64(10)).
Return(test.pr, test.reviews, nil)
mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return(test.maintainerFile, nil)
approved, err := IsPrjGitPRApproved(mi, pri, config, 10)
if approved != test.approved {
t.Error("Unexpected approve state:", approved, "vs. expected", test.approved, ", or err:", err)
}
if err != nil {
t.Error("Unexpected error", err)
}
})
}
}