Skip to content
Next Next commit
Added tests for MS Teams.
  • Loading branch information
KN4CK3R committed Jun 18, 2021
commit 4834df21a57c63cabc0ca8b987632c3500db1761
143 changes: 126 additions & 17 deletions services/webhook/general_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,93 @@ import (
api "code.gitea.io/gitea/modules/structs"
)

func createTestPayload() *api.CreatePayload {
return &api.CreatePayload{
Sha: "2020558fe2e34debb818a514715839cabd25e777",
Ref: "refs/heads/test",
RefType: "branch",
Repo: &api.Repository{
HTMLURL: "http://localhost:3000/test/repo",
Name: "repo",
FullName: "test/repo",
},
Sender: &api.User{
UserName: "user1",
},
}
}

func deleteTestPayload() *api.DeletePayload {
return &api.DeletePayload{
Ref: "refs/heads/test",
RefType: "branch",
Repo: &api.Repository{
HTMLURL: "http://localhost:3000/test/repo",
Name: "repo",
FullName: "test/repo",
},
Sender: &api.User{
UserName: "user1",
},
}
}

func forkTestPayload() *api.ForkPayload {
return &api.ForkPayload{
Forkee: &api.Repository{
HTMLURL: "http://localhost:3000/test/repo2",
Name: "repo2",
FullName: "test/repo2",
},
Repo: &api.Repository{
HTMLURL: "http://localhost:3000/test/repo",
Name: "repo",
FullName: "test/repo",
},
Sender: &api.User{
UserName: "user1",
},
}
}

func pushTestPayload() *api.PushPayload {
commit := &api.PayloadCommit{
ID: "2020558fe2e34debb818a514715839cabd25e778",
Message: "commit message",
URL: "http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778",
Author: &api.PayloadUser{
Name: "user1",
Email: "user1@localhost",
UserName: "user1",
},
Committer: &api.PayloadUser{
Name: "user1",
Email: "user1@localhost",
UserName: "user1",
},
}

return &api.PushPayload{
Ref: "refs/heads/test",
Before: "2020558fe2e34debb818a514715839cabd25e777",
After: "2020558fe2e34debb818a514715839cabd25e778",
CompareURL: "",
HeadCommit: commit,
Commits: []*api.PayloadCommit{commit, commit},
Repo: &api.Repository{
HTMLURL: "http://localhost:3000/test/repo",
Name: "repo",
FullName: "test/repo",
},
Pusher: &api.User{
UserName: "user1",
},
Sender: &api.User{
UserName: "user1",
},
}
}

func issueTestPayload() *api.IssuePayload {
return &api.IssuePayload{
Index: 2,
Expand All @@ -20,10 +107,12 @@ func issueTestPayload() *api.IssuePayload {
FullName: "test/repo",
},
Issue: &api.Issue{
ID: 2,
Index: 2,
URL: "http://localhost:3000/api/v1/repos/test/repo/issues/2",
Title: "crash",
ID: 2,
Index: 2,
URL: "http://localhost:3000/api/v1/repos/test/repo/issues/2",
HTMLURL: "http://localhost:3000/test/repo/issues/2",
Title: "crash",
Body: "issue body",
},
}
}
Expand All @@ -45,11 +134,12 @@ func issueCommentTestPayload() *api.IssueCommentPayload {
Body: "more info needed",
},
Issue: &api.Issue{
ID: 2,
Index: 2,
URL: "http://localhost:3000/api/v1/repos/test/repo/issues/2",
Title: "crash",
Body: "this happened",
ID: 2,
Index: 2,
URL: "http://localhost:3000/api/v1/repos/test/repo/issues/2",
HTMLURL: "http://localhost:3000/test/repo/issues/2",
Title: "crash",
Body: "this happened",
},
}
}
Expand All @@ -71,11 +161,12 @@ func pullRequestCommentTestPayload() *api.IssueCommentPayload {
Body: "changes requested",
},
Issue: &api.Issue{
ID: 2,
Index: 2,
URL: "http://localhost:3000/api/v1/repos/test/repo/issues/2",
Title: "Fix bug",
Body: "fixes bug #2",
ID: 2,
Index: 2,
URL: "http://localhost:3000/api/v1/repos/test/repo/issues/2",
HTMLURL: "http://localhost:3000/test/repo/issues/2",
Title: "Fix bug",
Body: "fixes bug #2",
},
IsPull: true,
}
Expand Down Expand Up @@ -104,7 +195,7 @@ func pullReleaseTestPayload() *api.ReleasePayload {
func pullRequestTestPayload() *api.PullRequestPayload {
return &api.PullRequestPayload{
Action: api.HookIssueOpened,
Index: 2,
Index: 12,
Sender: &api.User{
UserName: "user1",
},
Expand All @@ -114,12 +205,30 @@ func pullRequestTestPayload() *api.PullRequestPayload {
FullName: "test/repo",
},
PullRequest: &api.PullRequest{
ID: 2,
Index: 2,
ID: 12,
Index: 12,
URL: "http://localhost:3000/test/repo/pulls/12",
HTMLURL: "http://localhost:3000/test/repo/pulls/12",
Title: "Fix bug",
Body: "fixes bug #2",
Mergeable: true,
},
Review: &api.ReviewPayload{
Content: "good job",
},
}
}

func repositoryTestPayload() *api.RepositoryPayload {
return &api.RepositoryPayload{
Action: api.HookRepoCreated,
Sender: &api.User{
UserName: "user1",
},
Repository: &api.Repository{
HTMLURL: "http://localhost:3000/test/repo",
Name: "repo",
FullName: "test/repo",
},
}
}
4 changes: 2 additions & 2 deletions services/webhook/matrix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ func TestMatrixPullRequestPayload(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, pl)

assert.Equal(t, "[[test/repo](http://localhost:3000/test/repo)] Pull request opened: [#2 Fix bug](http://localhost:3000/test/repo/pulls/12) by [user1](https://try.gitea.io/user1)", pl.(*MatrixPayloadUnsafe).Body)
assert.Equal(t, "[<a href=\"http://localhost:3000/test/repo\">test/repo</a>] Pull request opened: <a href=\"http://localhost:3000/test/repo/pulls/12\">#2 Fix bug</a> by <a href=\"https://try.gitea.io/user1\">user1</a>", pl.(*MatrixPayloadUnsafe).FormattedBody)
assert.Equal(t, "[[test/repo](http://localhost:3000/test/repo)] Pull request opened: [#12 Fix bug](http://localhost:3000/test/repo/pulls/12) by [user1](https://try.gitea.io/user1)", pl.(*MatrixPayloadUnsafe).Body)
assert.Equal(t, "[<a href=\"http://localhost:3000/test/repo\">test/repo</a>] Pull request opened: <a href=\"http://localhost:3000/test/repo/pulls/12\">#12 Fix bug</a> by <a href=\"https://try.gitea.io/user1\">user1</a>", pl.(*MatrixPayloadUnsafe).FormattedBody)
}

func TestMatrixHookRequest(t *testing.T) {
Expand Down
Loading