2023-03-08 16:57:41 +00:00
|
|
|
package gitea_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
2023-03-09 13:28:33 +00:00
|
|
|
giteasdk "code.gitea.io/sdk/gitea"
|
2023-03-08 16:57:41 +00:00
|
|
|
"github.com/golangci/golangci-lint/pkg/report"
|
2023-03-10 09:19:41 +00:00
|
|
|
"github.com/golangci/golangci-lint/pkg/result"
|
2023-03-08 16:57:41 +00:00
|
|
|
"github.com/stretchr/testify/mock"
|
|
|
|
|
2023-03-10 09:19:41 +00:00
|
|
|
"code.icb4dc0.de/prskr/nitter/internal/logging"
|
2023-03-08 16:57:41 +00:00
|
|
|
"code.icb4dc0.de/prskr/nitter/nitters/gitea"
|
|
|
|
"code.icb4dc0.de/prskr/nitter/nitters/gitea/mocks"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Test_PRNitter_Report_MockCreatorError_Error(t *testing.T) {
|
2023-03-09 13:28:33 +00:00
|
|
|
t.Parallel()
|
2023-03-08 16:57:41 +00:00
|
|
|
expectedError := errors.New("error")
|
2023-03-09 13:28:33 +00:00
|
|
|
client := mocks.NewClient(t)
|
|
|
|
client.
|
2023-03-08 16:57:41 +00:00
|
|
|
EXPECT().
|
|
|
|
CreatePullReview(mock.Anything, mock.Anything, mock.Anything, mock.Anything).
|
|
|
|
Return(nil, nil, expectedError).
|
|
|
|
Times(1)
|
|
|
|
|
2023-03-09 13:28:33 +00:00
|
|
|
client.
|
|
|
|
EXPECT().
|
|
|
|
GetMyUserInfo().
|
|
|
|
Return(&giteasdk.User{ID: 11}, nil, nil).
|
|
|
|
Times(1)
|
|
|
|
|
2023-03-09 14:46:32 +00:00
|
|
|
client.
|
|
|
|
EXPECT().
|
|
|
|
GetPullRequest(mock.Anything, mock.Anything, mock.Anything).
|
|
|
|
Return(&giteasdk.PullRequest{Poster: &giteasdk.User{}}, nil, nil)
|
|
|
|
|
2023-03-09 13:28:33 +00:00
|
|
|
client.
|
|
|
|
EXPECT().
|
|
|
|
ListPullReviews(mock.Anything, mock.Anything, mock.Anything, mock.Anything).
|
|
|
|
Return(nil, nil, nil).
|
|
|
|
Times(1)
|
|
|
|
|
2023-03-10 09:19:41 +00:00
|
|
|
p := gitea.NewPRNitter(logging.NewLogger(t), client, new(gitea.GiteaPRConfig))
|
2023-03-08 16:57:41 +00:00
|
|
|
|
|
|
|
if err := p.Report(new(report.Data), nil); err == nil {
|
|
|
|
t.Error("expected error bot got none")
|
|
|
|
} else if !errors.Is(err, expectedError) {
|
|
|
|
t.Errorf("Error %v was not expected", err)
|
|
|
|
}
|
|
|
|
}
|
2023-03-10 09:19:41 +00:00
|
|
|
|
|
|
|
func Test_PRNitter_RequestChangesIfIssues_ReviewStateRequestChanges(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
client := mocks.NewClient(t)
|
|
|
|
client.
|
|
|
|
EXPECT().
|
|
|
|
CreatePullReview(mock.Anything, mock.Anything, mock.Anything, expectReviewState(giteasdk.ReviewStateRequestChanges)).
|
|
|
|
Return(nil, nil, nil).
|
|
|
|
Times(1)
|
|
|
|
|
|
|
|
client.
|
|
|
|
EXPECT().
|
|
|
|
GetMyUserInfo().
|
|
|
|
Return(&giteasdk.User{ID: 11}, nil, nil).
|
|
|
|
Times(1)
|
|
|
|
|
|
|
|
client.
|
|
|
|
EXPECT().
|
|
|
|
GetPullRequest(mock.Anything, mock.Anything, mock.Anything).
|
|
|
|
Return(&giteasdk.PullRequest{Poster: &giteasdk.User{ID: 42}}, nil, nil)
|
|
|
|
|
|
|
|
client.
|
|
|
|
EXPECT().
|
|
|
|
ListPullReviews(mock.Anything, mock.Anything, mock.Anything, mock.Anything).
|
|
|
|
Return(nil, nil, nil).
|
|
|
|
Times(1)
|
|
|
|
|
|
|
|
p := gitea.NewPRNitter(logging.NewLogger(t), client, &gitea.GiteaPRConfig{ReviewState: giteasdk.ReviewStateRequestChanges})
|
|
|
|
|
|
|
|
if err := p.Report(new(report.Data), make([]result.Issue, 1)); err != nil {
|
|
|
|
t.Errorf("Did not expect error but got %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_PRNitter_ApproveOthersPR_ReviewStateApproved(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
client := mocks.NewClient(t)
|
|
|
|
client.
|
|
|
|
EXPECT().
|
|
|
|
CreatePullReview(mock.Anything, mock.Anything, mock.Anything, expectReviewState(giteasdk.ReviewStateApproved)).
|
|
|
|
Return(nil, nil, nil).
|
|
|
|
Times(1)
|
|
|
|
|
|
|
|
client.
|
|
|
|
EXPECT().
|
|
|
|
GetMyUserInfo().
|
|
|
|
Return(&giteasdk.User{ID: 11}, nil, nil).
|
|
|
|
Times(1)
|
|
|
|
|
|
|
|
client.
|
|
|
|
EXPECT().
|
|
|
|
GetPullRequest(mock.Anything, mock.Anything, mock.Anything).
|
|
|
|
Return(&giteasdk.PullRequest{Poster: &giteasdk.User{ID: 42}}, nil, nil)
|
|
|
|
|
|
|
|
client.
|
|
|
|
EXPECT().
|
|
|
|
ListPullReviews(mock.Anything, mock.Anything, mock.Anything, mock.Anything).
|
|
|
|
Return(nil, nil, nil).
|
|
|
|
Times(1)
|
|
|
|
|
|
|
|
p := gitea.NewPRNitter(logging.NewLogger(t), client, new(gitea.GiteaPRConfig))
|
|
|
|
|
|
|
|
if err := p.Report(new(report.Data), nil); err != nil {
|
|
|
|
t.Errorf("Did not expect error but got %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_PRNitter_DontApproveMyOwnPR_ReviewStateComment(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
client := mocks.NewClient(t)
|
|
|
|
client.
|
|
|
|
EXPECT().
|
|
|
|
CreatePullReview(mock.Anything, mock.Anything, mock.Anything, expectReviewState(giteasdk.ReviewStateComment)).
|
|
|
|
Return(nil, nil, nil).
|
|
|
|
Times(1)
|
|
|
|
|
|
|
|
client.
|
|
|
|
EXPECT().
|
|
|
|
GetMyUserInfo().
|
|
|
|
Return(&giteasdk.User{ID: 11}, nil, nil).
|
|
|
|
Times(1)
|
|
|
|
|
|
|
|
client.
|
|
|
|
EXPECT().
|
|
|
|
GetPullRequest(mock.Anything, mock.Anything, mock.Anything).
|
|
|
|
Return(&giteasdk.PullRequest{Poster: &giteasdk.User{ID: 11}}, nil, nil)
|
|
|
|
|
|
|
|
client.
|
|
|
|
EXPECT().
|
|
|
|
ListPullReviews(mock.Anything, mock.Anything, mock.Anything, mock.Anything).
|
|
|
|
Return(nil, nil, nil).
|
|
|
|
Times(1)
|
|
|
|
|
|
|
|
p := gitea.NewPRNitter(logging.NewLogger(t), client, new(gitea.GiteaPRConfig))
|
|
|
|
|
|
|
|
if err := p.Report(new(report.Data), nil); err != nil {
|
|
|
|
t.Errorf("Did not expect error but got %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func match[T any](matcher func(arg T) bool) any {
|
|
|
|
return mock.MatchedBy(matcher)
|
|
|
|
}
|
|
|
|
|
|
|
|
func expectReviewState(expected giteasdk.ReviewStateType) any {
|
|
|
|
return match[giteasdk.CreatePullReviewOptions](func(arg giteasdk.CreatePullReviewOptions) bool {
|
|
|
|
return arg.State == expected
|
|
|
|
})
|
|
|
|
}
|