Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: Extend events_controller setup method of Azure DevOps #2852

Merged
merged 2 commits into from
Dec 22, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
158 changes: 53 additions & 105 deletions server/controllers/events/events_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const githubHeader = "X-Github-Event"
const gitlabHeader = "X-Gitlab-Event"
const azuredevopsHeader = "Request-Id"

var user = []byte("user")
var secret = []byte("secret")

func AnyRepo() models.Repo {
Expand All @@ -53,7 +54,7 @@ func AnyRepo() models.Repo {

func TestPost_NotGithubOrGitlab(t *testing.T) {
t.Log("when the request is not for gitlab or github a 400 is returned")
e, _, _, _, _, _, _, _ := setup(t)
e, _, _, _, _, _, _, _, _ := setup(t)
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "", bytes.NewBuffer(nil))
e.Post(w, req)
Expand All @@ -62,7 +63,7 @@ func TestPost_NotGithubOrGitlab(t *testing.T) {

func TestPost_UnsupportedVCSGithub(t *testing.T) {
t.Log("when the request is for an unsupported vcs a 400 is returned")
e, _, _, _, _, _, _, _ := setup(t)
e, _, _, _, _, _, _, _, _ := setup(t)
e.SupportedVCSHosts = nil
req, _ := http.NewRequest("GET", "", bytes.NewBuffer(nil))
req.Header.Set(githubHeader, "value")
Expand All @@ -73,7 +74,7 @@ func TestPost_UnsupportedVCSGithub(t *testing.T) {

func TestPost_UnsupportedVCSGitlab(t *testing.T) {
t.Log("when the request is for an unsupported vcs a 400 is returned")
e, _, _, _, _, _, _, _ := setup(t)
e, _, _, _, _, _, _, _, _ := setup(t)
e.SupportedVCSHosts = nil
req, _ := http.NewRequest("GET", "", bytes.NewBuffer(nil))
req.Header.Set(gitlabHeader, "value")
Expand All @@ -84,7 +85,7 @@ func TestPost_UnsupportedVCSGitlab(t *testing.T) {

func TestPost_InvalidGithubSecret(t *testing.T) {
t.Log("when the github payload can't be validated a 400 is returned")
e, v, _, _, _, _, _, _ := setup(t)
e, v, _, _, _, _, _, _, _ := setup(t)
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "", bytes.NewBuffer(nil))
req.Header.Set(githubHeader, "value")
Expand All @@ -95,7 +96,7 @@ func TestPost_InvalidGithubSecret(t *testing.T) {

func TestPost_InvalidGitlabSecret(t *testing.T) {
t.Log("when the gitlab payload can't be validated a 400 is returned")
e, _, gl, _, _, _, _, _ := setup(t)
e, _, gl, _, _, _, _, _, _ := setup(t)
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "", bytes.NewBuffer(nil))
req.Header.Set(gitlabHeader, "value")
Expand All @@ -106,7 +107,7 @@ func TestPost_InvalidGitlabSecret(t *testing.T) {

func TestPost_UnsupportedGithubEvent(t *testing.T) {
t.Log("when the event type is an unsupported github event we ignore it")
e, v, _, _, _, _, _, _ := setup(t)
e, v, _, _, _, _, _, _, _ := setup(t)
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "", bytes.NewBuffer(nil))
req.Header.Set(githubHeader, "value")
Expand All @@ -117,7 +118,7 @@ func TestPost_UnsupportedGithubEvent(t *testing.T) {

func TestPost_UnsupportedGitlabEvent(t *testing.T) {
t.Log("when the event type is an unsupported gitlab event we ignore it")
e, _, gl, _, _, _, _, _ := setup(t)
e, _, gl, _, _, _, _, _, _ := setup(t)
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "", bytes.NewBuffer(nil))
req.Header.Set(gitlabHeader, "value")
Expand All @@ -129,7 +130,7 @@ func TestPost_UnsupportedGitlabEvent(t *testing.T) {
// Test that if the comment comes from a commit rather than a merge request,
// we give an error and ignore it.
func TestPost_GitlabCommentOnCommit(t *testing.T) {
e, _, gl, _, _, _, _, _ := setup(t)
e, _, gl, _, _, _, _, _, _ := setup(t)
req, _ := http.NewRequest("GET", "", bytes.NewBuffer(nil))
w := httptest.NewRecorder()
req.Header.Set(gitlabHeader, "value")
Expand All @@ -140,7 +141,7 @@ func TestPost_GitlabCommentOnCommit(t *testing.T) {

func TestPost_GithubCommentNotCreated(t *testing.T) {
t.Log("when the event is a github comment but it's not a created event we ignore it")
e, v, _, _, _, _, _, _ := setup(t)
e, v, _, _, _, _, _, _, _ := setup(t)
req, _ := http.NewRequest("GET", "", bytes.NewBuffer(nil))
req.Header.Set(githubHeader, "issue_comment")
// comment action is deleted, not created
Expand All @@ -153,7 +154,7 @@ func TestPost_GithubCommentNotCreated(t *testing.T) {

func TestPost_GithubInvalidComment(t *testing.T) {
t.Log("when the event is a github comment without all expected data we return a 400")
e, v, _, p, _, _, _, _ := setup(t)
e, v, _, _, p, _, _, _, _ := setup(t)
req, _ := http.NewRequest("GET", "", bytes.NewBuffer(nil))
req.Header.Set(githubHeader, "issue_comment")
event := `{"action": "created"}`
Expand All @@ -166,7 +167,7 @@ func TestPost_GithubInvalidComment(t *testing.T) {

func TestPost_GitlabCommentInvalidCommand(t *testing.T) {
t.Log("when the event is a gitlab comment with an invalid command we ignore it")
e, _, gl, _, _, _, _, cp := setup(t)
e, _, gl, _, _, _, _, _, cp := setup(t)
req, _ := http.NewRequest("GET", "", bytes.NewBuffer(nil))
req.Header.Set(gitlabHeader, "value")
When(gl.ParseAndValidate(req, secret)).ThenReturn(gitlab.MergeCommentEvent{}, nil)
Expand All @@ -178,7 +179,7 @@ func TestPost_GitlabCommentInvalidCommand(t *testing.T) {

func TestPost_GithubCommentInvalidCommand(t *testing.T) {
t.Log("when the event is a github comment with an invalid command we ignore it")
e, v, _, p, _, _, _, cp := setup(t)
e, v, _, _, p, _, _, _, cp := setup(t)
req, _ := http.NewRequest("GET", "", bytes.NewBuffer(nil))
req.Header.Set(githubHeader, "issue_comment")
event := `{"action": "created"}`
Expand Down Expand Up @@ -319,7 +320,7 @@ func TestPost_GithubCommentNotAllowlistedWithSilenceErrors(t *testing.T) {

func TestPost_GitlabCommentResponse(t *testing.T) {
// When the event is a gitlab comment that warrants a comment response we comment back.
e, _, gl, _, _, _, vcsClient, cp := setup(t)
e, _, gl, _, _, _, _, vcsClient, cp := setup(t)
req, _ := http.NewRequest("GET", "", bytes.NewBuffer(nil))
req.Header.Set(gitlabHeader, "value")
When(gl.ParseAndValidate(req, secret)).ThenReturn(gitlab.MergeCommentEvent{}, nil)
Expand All @@ -332,7 +333,7 @@ func TestPost_GitlabCommentResponse(t *testing.T) {

func TestPost_GithubCommentResponse(t *testing.T) {
t.Log("when the event is a github comment that warrants a comment response we comment back")
e, v, _, p, _, _, vcsClient, cp := setup(t)
e, v, _, _, p, _, _, vcsClient, cp := setup(t)
req, _ := http.NewRequest("GET", "", bytes.NewBuffer(nil))
req.Header.Set(githubHeader, "issue_comment")
event := `{"action": "created"}`
Expand All @@ -350,7 +351,7 @@ func TestPost_GithubCommentResponse(t *testing.T) {

func TestPost_GitlabCommentSuccess(t *testing.T) {
t.Log("when the event is a gitlab comment with a valid command we call the command handler")
e, _, gl, _, cr, _, _, _ := setup(t)
e, _, gl, _, _, cr, _, _, _ := setup(t)
req, _ := http.NewRequest("GET", "", bytes.NewBuffer(nil))
req.Header.Set(gitlabHeader, "value")
When(gl.ParseAndValidate(req, secret)).ThenReturn(gitlab.MergeCommentEvent{}, nil)
Expand All @@ -363,7 +364,7 @@ func TestPost_GitlabCommentSuccess(t *testing.T) {

func TestPost_GithubCommentSuccess(t *testing.T) {
t.Log("when the event is a github comment with a valid command we call the command handler")
e, v, _, p, cr, _, _, cp := setup(t)
e, v, _, _, p, cr, _, _, cp := setup(t)
req, _ := http.NewRequest("GET", "", bytes.NewBuffer(nil))
req.Header.Set(githubHeader, "issue_comment")
event := `{"action": "created"}`
Expand All @@ -382,7 +383,7 @@ func TestPost_GithubCommentSuccess(t *testing.T) {

func TestPost_GithubPullRequestInvalid(t *testing.T) {
t.Log("when the event is a github pull request with invalid data we return a 400")
e, v, _, p, _, _, _, _ := setup(t)
e, v, _, _, p, _, _, _, _ := setup(t)
req, _ := http.NewRequest("GET", "", bytes.NewBuffer(nil))
req.Header.Set(githubHeader, "pull_request")

Expand All @@ -396,7 +397,7 @@ func TestPost_GithubPullRequestInvalid(t *testing.T) {

func TestPost_GitlabMergeRequestInvalid(t *testing.T) {
t.Log("when the event is a gitlab merge request with invalid data we return a 400")
e, _, gl, p, _, _, _, _ := setup(t)
e, _, gl, _, p, _, _, _, _ := setup(t)
req, _ := http.NewRequest("GET", "", bytes.NewBuffer(nil))
req.Header.Set(gitlabHeader, "value")
When(gl.ParseAndValidate(req, secret)).ThenReturn(gitlab.MergeEvent{}, nil)
Expand All @@ -410,7 +411,7 @@ func TestPost_GitlabMergeRequestInvalid(t *testing.T) {

func TestPost_GithubPullRequestNotAllowlisted(t *testing.T) {
t.Log("when the event is a github pull request to a non-allowlisted repo we return a 400")
e, v, _, _, _, _, _, _ := setup(t)
e, v, _, _, _, _, _, _, _ := setup(t)
var err error
e.RepoAllowlistChecker, err = events.NewRepoAllowlistChecker("github.com/nevermatch")
Ok(t, err)
Expand All @@ -426,7 +427,7 @@ func TestPost_GithubPullRequestNotAllowlisted(t *testing.T) {

func TestPost_GitlabMergeRequestNotAllowlisted(t *testing.T) {
t.Log("when the event is a gitlab merge request to a non-allowlisted repo we return a 400")
e, _, gl, p, _, _, _, _ := setup(t)
e, _, gl, _, p, _, _, _, _ := setup(t)
req, _ := http.NewRequest("GET", "", bytes.NewBuffer(nil))
req.Header.Set(gitlabHeader, "value")

Expand All @@ -445,7 +446,7 @@ func TestPost_GitlabMergeRequestNotAllowlisted(t *testing.T) {

func TestPost_GithubPullRequestUnsupportedAction(t *testing.T) {
t.Skip("relies too much on mocks, should use real event parser")
e, v, _, _, _, _, _, _ := setup(t)
e, v, _, _, _, _, _, _, _ := setup(t)
req, _ := http.NewRequest("GET", "", bytes.NewBuffer(nil))
req.Header.Set(githubHeader, "pull_request")

Expand All @@ -460,7 +461,7 @@ func TestPost_GithubPullRequestUnsupportedAction(t *testing.T) {
func TestPost_GitlabMergeRequestUnsupportedAction(t *testing.T) {
t.Skip("relies too much on mocks, should use real event parser")
t.Log("when the event is a gitlab merge request to a non-allowlisted repo we return a 400")
e, _, gl, p, _, _, _, _ := setup(t)
e, _, gl, _, p, _, _, _, _ := setup(t)
req, _ := http.NewRequest("GET", "", bytes.NewBuffer(nil))
req.Header.Set(gitlabHeader, "value")
var event gitlab.MergeEvent
Expand All @@ -476,37 +477,8 @@ func TestPost_GitlabMergeRequestUnsupportedAction(t *testing.T) {
}

func TestPost_AzureDevopsPullRequestIgnoreEvent(t *testing.T) {
u := "user"
user := []byte(u)

t.Log("when the event is an azure devops pull request update that should not trigger workflow we ignore it")
RegisterMockTestingT(t)
v := mocks.NewMockAzureDevopsRequestValidator()
p := emocks.NewMockEventParsing()
cp := emocks.NewMockCommentParsing()
cr := emocks.NewMockCommandRunner()
c := emocks.NewMockPullCleaner()
vcsmock := vcsmocks.NewMockClient()
repoAllowlistChecker, err := events.NewRepoAllowlistChecker("*")
Ok(t, err)
logger := logging.NewNoopLogger(t)
scope, _, _ := metrics.NewLoggingScope(logger, "null")
e := events_controllers.VCSEventsController{
TestingMode: true,
Logger: logger,
Scope: scope,
ApplyDisabled: false,
AzureDevopsWebhookBasicUser: user,
AzureDevopsWebhookBasicPassword: secret,
AzureDevopsRequestValidator: v,
Parser: p,
CommentParser: cp,
CommandRunner: cr,
PullCleaner: c,
SupportedVCSHosts: []models.VCSHostType{models.AzureDevops},
RepoAllowlistChecker: repoAllowlistChecker,
VCSClient: vcsmock,
}
e, _, _, ado, _, _, _, _, _ := setup(t)

event := `{
"subscriptionId": "11111111-1111-1111-1111-111111111111",
Expand Down Expand Up @@ -547,7 +519,7 @@ func TestPost_AzureDevopsPullRequestIgnoreEvent(t *testing.T) {
payload := fmt.Sprintf(event, c.message)
req, _ := http.NewRequest("GET", "", strings.NewReader(payload))
req.Header.Set(azuredevopsHeader, "reqID")
When(v.Validate(req, user, secret)).ThenReturn([]byte(payload), nil)
When(ado.Validate(req, user, secret)).ThenReturn([]byte(payload), nil)
w := httptest.NewRecorder()
e.Parser = &events.EventParser{}
e.Post(w, req)
Expand All @@ -557,37 +529,8 @@ func TestPost_AzureDevopsPullRequestIgnoreEvent(t *testing.T) {
}

func TestPost_AzureDevopsPullRequestDeletedCommentIgnoreEvent(t *testing.T) {
u := "user"
user := []byte(u)

t.Log("when the event is an azure devops pull request deleted comment event we ignore it")
RegisterMockTestingT(t)
v := mocks.NewMockAzureDevopsRequestValidator()
p := emocks.NewMockEventParsing()
cp := emocks.NewMockCommentParsing()
cr := emocks.NewMockCommandRunner()
c := emocks.NewMockPullCleaner()
vcsmock := vcsmocks.NewMockClient()
repoAllowlistChecker, err := events.NewRepoAllowlistChecker("*")
Ok(t, err)
logger := logging.NewNoopLogger(t)
scope, _, _ := metrics.NewLoggingScope(logger, "null")
e := events_controllers.VCSEventsController{
TestingMode: true,
Logger: logger,
Scope: scope,
ApplyDisabled: false,
AzureDevopsWebhookBasicUser: user,
AzureDevopsWebhookBasicPassword: secret,
AzureDevopsRequestValidator: v,
Parser: p,
CommentParser: cp,
CommandRunner: cr,
PullCleaner: c,
SupportedVCSHosts: []models.VCSHostType{models.AzureDevops},
RepoAllowlistChecker: repoAllowlistChecker,
VCSClient: vcsmock,
}
e, _, _, ado, _, _, _, _, _ := setup(t)

payload := `{
"subscriptionId": "11111111-1111-1111-1111-111111111111",
Expand All @@ -610,7 +553,7 @@ func TestPost_AzureDevopsPullRequestDeletedCommentIgnoreEvent(t *testing.T) {
t.Run("Dev has deleted a pull request comment", func(t *testing.T) {
req, _ := http.NewRequest("GET", "", strings.NewReader(payload))
req.Header.Set(azuredevopsHeader, "reqID")
When(v.Validate(req, user, secret)).ThenReturn([]byte(payload), nil)
When(ado.Validate(req, user, secret)).ThenReturn([]byte(payload), nil)
w := httptest.NewRecorder()
e.Parser = &events.EventParser{}
e.Post(w, req)
Expand All @@ -622,7 +565,7 @@ func TestPost_GithubPullRequestClosedErrCleaningPull(t *testing.T) {
t.Skip("relies too much on mocks, should use real event parser")
t.Log("when the event is a closed pull request and we have an error calling CleanUpPull we return a 503")
RegisterMockTestingT(t)
e, v, _, p, _, c, _, _ := setup(t)
e, v, _, _, p, _, c, _, _ := setup(t)
req, _ := http.NewRequest("GET", "", bytes.NewBuffer(nil))
req.Header.Set(githubHeader, "pull_request")

Expand All @@ -640,7 +583,7 @@ func TestPost_GithubPullRequestClosedErrCleaningPull(t *testing.T) {
func TestPost_GitlabMergeRequestClosedErrCleaningPull(t *testing.T) {
t.Skip("relies too much on mocks, should use real event parser")
t.Log("when the event is a closed gitlab merge request and an error occurs calling CleanUpPull we return a 500")
e, _, gl, p, _, c, _, _ := setup(t)
e, _, gl, _, p, _, c, _, _ := setup(t)
req, _ := http.NewRequest("GET", "", bytes.NewBuffer(nil))
req.Header.Set(gitlabHeader, "value")
var event gitlab.MergeEvent
Expand All @@ -658,7 +601,7 @@ func TestPost_GitlabMergeRequestClosedErrCleaningPull(t *testing.T) {
func TestPost_GithubClosedPullRequestSuccess(t *testing.T) {
t.Skip("relies too much on mocks, should use real event parser")
t.Log("when the event is a pull request and everything works we return a 200")
e, v, _, p, _, c, _, _ := setup(t)
e, v, _, _, p, _, c, _, _ := setup(t)
req, _ := http.NewRequest("GET", "", bytes.NewBuffer(nil))
req.Header.Set(githubHeader, "pull_request")

Expand All @@ -676,7 +619,7 @@ func TestPost_GithubClosedPullRequestSuccess(t *testing.T) {
func TestPost_GitlabMergeRequestSuccess(t *testing.T) {
t.Skip("relies too much on mocks, should use real event parser")
t.Log("when the event is a gitlab merge request and the cleanup works we return a 200")
e, _, gl, p, _, _, _, _ := setup(t)
e, _, gl, _, p, _, _, _, _ := setup(t)
req, _ := http.NewRequest("GET", "", bytes.NewBuffer(nil))
req.Header.Set(gitlabHeader, "value")
When(gl.ParseAndValidate(req, secret)).ThenReturn(gitlab.MergeEvent{}, nil)
Expand Down Expand Up @@ -799,7 +742,7 @@ func TestPost_PullOpenedOrUpdated(t *testing.T) {

for _, c := range cases {
t.Run(c.Description, func(t *testing.T) {
e, v, gl, p, cr, _, _, _ := setup(t)
e, v, gl, _, p, cr, _, _, _ := setup(t)
req, _ := http.NewRequest("GET", "", bytes.NewBuffer(nil))
var pullRequest models.PullRequest
var repo models.Repo
Expand Down Expand Up @@ -830,10 +773,11 @@ func TestPost_PullOpenedOrUpdated(t *testing.T) {
}
}

func setup(t *testing.T) (events_controllers.VCSEventsController, *mocks.MockGithubRequestValidator, *mocks.MockGitlabRequestParserValidator, *emocks.MockEventParsing, *emocks.MockCommandRunner, *emocks.MockPullCleaner, *vcsmocks.MockClient, *emocks.MockCommentParsing) {
func setup(t *testing.T) (events_controllers.VCSEventsController, *mocks.MockGithubRequestValidator, *mocks.MockGitlabRequestParserValidator, *mocks.MockAzureDevopsRequestValidator, *emocks.MockEventParsing, *emocks.MockCommandRunner, *emocks.MockPullCleaner, *vcsmocks.MockClient, *emocks.MockCommentParsing) {
RegisterMockTestingT(t)
v := mocks.NewMockGithubRequestValidator()
gl := mocks.NewMockGitlabRequestParserValidator()
ado := mocks.NewMockAzureDevopsRequestValidator()
p := emocks.NewMockEventParsing()
cp := emocks.NewMockCommentParsing()
cr := emocks.NewMockCommandRunner()
Expand All @@ -844,20 +788,24 @@ func setup(t *testing.T) (events_controllers.VCSEventsController, *mocks.MockGit
logger := logging.NewNoopLogger(t)
scope, _, _ := metrics.NewLoggingScope(logger, "null")
e := events_controllers.VCSEventsController{
TestingMode: true,
Logger: logger,
Scope: scope,
GithubRequestValidator: v,
Parser: p,
CommentParser: cp,
CommandRunner: cr,
PullCleaner: c,
GithubWebhookSecret: secret,
SupportedVCSHosts: []models.VCSHostType{models.Github, models.Gitlab},
GitlabWebhookSecret: secret,
GitlabRequestParserValidator: gl,
RepoAllowlistChecker: repoAllowlistChecker,
VCSClient: vcsmock,
TestingMode: true,
Logger: logger,
Scope: scope,
ApplyDisabled: false,
AzureDevopsWebhookBasicUser: user,
AzureDevopsWebhookBasicPassword: secret,
AzureDevopsRequestValidator: ado,
GithubRequestValidator: v,
Parser: p,
CommentParser: cp,
CommandRunner: cr,
PullCleaner: c,
GithubWebhookSecret: secret,
SupportedVCSHosts: []models.VCSHostType{models.Github, models.Gitlab, models.AzureDevops},
GitlabWebhookSecret: secret,
GitlabRequestParserValidator: gl,
RepoAllowlistChecker: repoAllowlistChecker,
VCSClient: vcsmock,
}
return e, v, gl, p, cr, c, vcsmock, cp
return e, v, gl, ado, p, cr, c, vcsmock, cp
}