mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-07 05:55:51 +01:00
Add unit test
This commit is contained in:
@@ -2,7 +2,7 @@ package gitbucket.core.api
|
||||
|
||||
import java.util.{Base64, Calendar, Date, TimeZone}
|
||||
|
||||
import gitbucket.core.model.{Account, Repository, RepositoryOptions}
|
||||
import gitbucket.core.model._
|
||||
import gitbucket.core.service.RepositoryService.RepositoryInfo
|
||||
import gitbucket.core.util.JGitUtil.{CommitInfo, DiffInfo, TagInfo}
|
||||
import gitbucket.core.util.RepositoryName
|
||||
@@ -82,6 +82,79 @@ object ApiSpecModels {
|
||||
managers = Seq("myboss")
|
||||
)
|
||||
|
||||
val label = Label(
|
||||
userName = repo1Name.owner,
|
||||
repositoryName = repo1Name.name,
|
||||
labelId = 10,
|
||||
labelName = "bug",
|
||||
color = "f29513"
|
||||
)
|
||||
|
||||
val issue = Issue(
|
||||
userName = repo1Name.owner,
|
||||
repositoryName = repo1Name.name,
|
||||
issueId = 1347,
|
||||
openedUserName = "bear",
|
||||
milestoneId = None,
|
||||
priorityId = None,
|
||||
assignedUserName = None,
|
||||
title = "Found a bug",
|
||||
content = Some("I'm having a problem with this."),
|
||||
closed = false,
|
||||
registeredDate = date1,
|
||||
updatedDate = date1,
|
||||
isPullRequest = false
|
||||
)
|
||||
|
||||
val issuePR = issue.copy(
|
||||
title = "new-feature",
|
||||
content = Some("Please pull these awesome changes"),
|
||||
closed = true,
|
||||
isPullRequest = true
|
||||
)
|
||||
|
||||
val issueComment = IssueComment(
|
||||
userName = repo1Name.owner,
|
||||
repositoryName = repo1Name.name,
|
||||
issueId = issue.issueId,
|
||||
commentId = 1,
|
||||
action = "comment",
|
||||
commentedUserName = "bear",
|
||||
content = "Me too",
|
||||
registeredDate = date1,
|
||||
updatedDate = date1
|
||||
)
|
||||
|
||||
val pullRequest = PullRequest(
|
||||
userName = repo1Name.owner,
|
||||
repositoryName = repo1Name.name,
|
||||
issueId = issuePR.issueId,
|
||||
branch = "master",
|
||||
requestUserName = "bear",
|
||||
requestRepositoryName = repo1Name.name,
|
||||
requestBranch = "new-topic",
|
||||
commitIdFrom = sha1,
|
||||
commitIdTo = sha1
|
||||
)
|
||||
|
||||
val commitComment = CommitComment(
|
||||
userName = repo1Name.owner,
|
||||
repositoryName = repo1Name.name,
|
||||
commitId = "0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c",
|
||||
commentId = 29724692,
|
||||
commentedUserName = "bear",
|
||||
content = "Maybe you should use more emoji on this line.",
|
||||
fileName = Some("README.md"),
|
||||
oldLine = Some(1),
|
||||
newLine = Some(1),
|
||||
registeredDate = date("2015-05-05T23:40:27Z"),
|
||||
updatedDate = date("2015-05-05T23:40:27Z"),
|
||||
issueId = Some(issuePR.issueId),
|
||||
originalCommitId = "0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c",
|
||||
originalOldLine = None,
|
||||
originalNewLine = None
|
||||
)
|
||||
|
||||
val apiUser = ApiUser(account)
|
||||
|
||||
val apiRepository = ApiRepository(
|
||||
@@ -92,6 +165,60 @@ object ApiSpecModels {
|
||||
urlIsHtmlUrl = false
|
||||
)
|
||||
|
||||
val apiLabel = ApiLabel(
|
||||
label = label,
|
||||
repositoryName = repo1Name
|
||||
)
|
||||
|
||||
val apiIssue = ApiIssue(
|
||||
issue = issue,
|
||||
repositoryName = repo1Name,
|
||||
user = apiUser,
|
||||
labels = List(apiLabel)
|
||||
)
|
||||
|
||||
val apiIssuePR = ApiIssue(
|
||||
issue = issuePR,
|
||||
repositoryName = repo1Name,
|
||||
user = apiUser,
|
||||
labels = List(apiLabel)
|
||||
)
|
||||
|
||||
val apiComment = ApiComment(
|
||||
comment = issueComment,
|
||||
repositoryName = repo1Name,
|
||||
issueId = issueComment.issueId,
|
||||
user = apiUser,
|
||||
isPullRequest = false
|
||||
)
|
||||
|
||||
val apiCommentPR = ApiComment(
|
||||
comment = issueComment,
|
||||
repositoryName = repo1Name,
|
||||
issueId = issueComment.issueId,
|
||||
user = apiUser,
|
||||
isPullRequest = true
|
||||
)
|
||||
|
||||
val apiPullRequest = ApiPullRequest(
|
||||
issue = issuePR,
|
||||
pullRequest = pullRequest,
|
||||
headRepo = apiRepository,
|
||||
baseRepo = apiRepository,
|
||||
user = apiUser,
|
||||
labels = List(apiLabel),
|
||||
assignee = Some(apiUser),
|
||||
mergedComment = Some((issueComment, account))
|
||||
)
|
||||
|
||||
// https://developer.github.com/v3/activity/events/types/#pullrequestreviewcommentevent
|
||||
val apiPullRequestReviewComment = ApiPullRequestReviewComment(
|
||||
comment = commitComment,
|
||||
commentedUser = apiUser,
|
||||
repositoryName = repo1Name,
|
||||
issueId = commitComment.issueId.get
|
||||
)
|
||||
|
||||
// TODO ------------
|
||||
|
||||
val apiCommitStatus = ApiCommitStatus(
|
||||
@@ -116,22 +243,6 @@ object ApiSpecModels {
|
||||
committer = ApiPersonIdent("baxterthehacker", "baxterthehacker@users.noreply.github.com", date1)
|
||||
)(RepositoryName("baxterthehacker", "public-repo"), true)
|
||||
|
||||
val apiComment = ApiComment(
|
||||
id = 1,
|
||||
user = apiUser,
|
||||
body = "Me too",
|
||||
created_at = date1,
|
||||
updated_at = date1
|
||||
)(RepositoryName("octocat", "Hello-World"), 100, false)
|
||||
|
||||
val apiCommentPR = ApiComment(
|
||||
id = 1,
|
||||
user = apiUser,
|
||||
body = "Me too",
|
||||
created_at = date1,
|
||||
updated_at = date1
|
||||
)(RepositoryName("octocat", "Hello-World"), 100, true)
|
||||
|
||||
val apiPersonIdent = ApiPersonIdent("Monalisa Octocat", "support@example.com", date1)
|
||||
|
||||
val apiCommitListItem = ApiCommitListItem(
|
||||
@@ -154,66 +265,6 @@ object ApiSpecModels {
|
||||
repository = apiRepository
|
||||
)
|
||||
|
||||
val apiLabel = ApiLabel(
|
||||
name = "bug",
|
||||
color = "f29513"
|
||||
)(RepositoryName("octocat", "Hello-World"))
|
||||
|
||||
val apiIssue = ApiIssue(
|
||||
number = 1347,
|
||||
title = "Found a bug",
|
||||
user = apiUser,
|
||||
labels = List(apiLabel),
|
||||
state = "open",
|
||||
body = "I'm having a problem with this.",
|
||||
created_at = date1,
|
||||
updated_at = date1
|
||||
)(RepositoryName("octocat", "Hello-World"), false)
|
||||
|
||||
val apiIssuePR = ApiIssue(
|
||||
number = 1347,
|
||||
title = "Found a bug",
|
||||
user = apiUser,
|
||||
labels = List(apiLabel),
|
||||
state = "open",
|
||||
body = "I'm having a problem with this.",
|
||||
created_at = date1,
|
||||
updated_at = date1
|
||||
)(RepositoryName("octocat", "Hello-World"), true)
|
||||
|
||||
val apiPullRequest = ApiPullRequest(
|
||||
number = 1347,
|
||||
state = "open",
|
||||
updated_at = date1,
|
||||
created_at = date1,
|
||||
head = ApiPullRequest.Commit(sha = sha1, ref = "new-topic", repo = apiRepository)("octocat"),
|
||||
base = ApiPullRequest.Commit(sha = sha1, ref = "master", repo = apiRepository)("octocat"),
|
||||
mergeable = None,
|
||||
merged = false,
|
||||
merged_at = Some(date1),
|
||||
merged_by = Some(apiUser),
|
||||
title = "new-feature",
|
||||
body = "Please pull these awesome changes",
|
||||
user = apiUser,
|
||||
labels = List(apiLabel),
|
||||
assignee = Some(apiUser)
|
||||
)
|
||||
|
||||
// https://developer.github.com/v3/activity/events/types/#pullrequestreviewcommentevent
|
||||
val apiPullRequestReviewComment = ApiPullRequestReviewComment(
|
||||
id = 29724692,
|
||||
// "diff_hunk": "@@ -1 +1 @@\n-# public-repo",
|
||||
path = "README.md",
|
||||
// "position": 1,
|
||||
// "original_position": 1,
|
||||
commit_id = "0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c",
|
||||
// "original_commit_id": "0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c",
|
||||
user = apiUser,
|
||||
body = "Maybe you should use more emoji on this line.",
|
||||
created_at = date("2015-05-05T23:40:27Z"),
|
||||
updated_at = date("2015-05-05T23:40:27Z")
|
||||
)(RepositoryName("baxterthehacker/public-repo"), 1)
|
||||
|
||||
val apiBranchProtection = ApiBranchProtection(
|
||||
true,
|
||||
Some(ApiBranchProtection.Status(ApiBranchProtection.Everyone, Seq("continuous-integration/travis-ci")))
|
||||
|
||||
@@ -40,13 +40,68 @@ class WebHookJsonFormatSpec extends FunSuite {
|
||||
|"html_url":"http://gitbucket.exmple.com/octocat/Hello-World"
|
||||
|}""".stripMargin
|
||||
|
||||
val jsonIssue = s"""{
|
||||
|"number":1347,
|
||||
|"title":"Found a bug",
|
||||
|"user":$jsonUser,
|
||||
|"labels":[{"name":"bug","color":"f29513","url":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/labels/bug"}],
|
||||
|"state":"open",
|
||||
|"created_at":"2011-04-14T16:00:49Z",
|
||||
|"updated_at":"2011-04-14T16:00:49Z",
|
||||
|"body":"I'm having a problem with this.",
|
||||
|"id":0,
|
||||
|"comments_url":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/issues/1347/comments",
|
||||
|"html_url":"http://gitbucket.exmple.com/octocat/Hello-World/issues/1347"
|
||||
|}""".stripMargin
|
||||
|
||||
val jsonPullRequest = s"""{
|
||||
|"number":1347,
|
||||
|"state":"closed",
|
||||
|"updated_at":"2011-04-14T16:00:49Z",
|
||||
|"created_at":"2011-04-14T16:00:49Z",
|
||||
|"head":{"sha":"6dcb09b5b57875f334f61aebed695e2e4193db5e","ref":"new-topic","repo":$jsonRepository,"label":"new-topic","user":$jsonUser},
|
||||
|"base":{"sha":"6dcb09b5b57875f334f61aebed695e2e4193db5e","ref":"master","repo":$jsonRepository,"label":"master","user":$jsonUser},
|
||||
|"merged":true,
|
||||
|"merged_at":"2011-04-14T16:00:49Z",
|
||||
|"merged_by":$jsonUser,
|
||||
|"title":"new-feature",
|
||||
|"body":"Please pull these awesome changes",
|
||||
|"user":$jsonUser,
|
||||
|"labels":[{"name":"bug","color":"f29513","url":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/labels/bug"}],
|
||||
|"assignee":$jsonUser,
|
||||
|"id":0,
|
||||
|"html_url":"http://gitbucket.exmple.com/octocat/Hello-World/pull/1347",
|
||||
|"url":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/pulls/1347",
|
||||
|"commits_url":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/pulls/1347/commits",
|
||||
|"review_comments_url":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/pulls/1347/comments",
|
||||
|"review_comment_url":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/pulls/comments/{number}",
|
||||
|"comments_url":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/issues/1347/comments",
|
||||
|"statuses_url":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/statuses/6dcb09b5b57875f334f61aebed695e2e4193db5e"
|
||||
|}""".stripMargin
|
||||
|
||||
private def assert(payload: WebHookPayload, expected: String): Assertion = {
|
||||
val json = JsonFormat(payload)
|
||||
assert(json == expected.replaceAll("\n", ""))
|
||||
}
|
||||
|
||||
test("WebHookCreatePayload") {
|
||||
fail("TODO")
|
||||
val payload = WebHookCreatePayload(
|
||||
sender = account,
|
||||
repositoryInfo = repositoryInfo,
|
||||
repositoryOwner = account,
|
||||
ref = "v1.0",
|
||||
refType = "tag"
|
||||
)
|
||||
val expected = s"""{
|
||||
|"sender":$jsonUser,
|
||||
|"description":"This your first repo!",
|
||||
|"ref":"v1.0",
|
||||
|"ref_type":"tag",
|
||||
|"master_branch":"master",
|
||||
|"repository":$jsonRepository,
|
||||
|"pusher_type":"user"
|
||||
|}""".stripMargin
|
||||
assert(payload, expected)
|
||||
}
|
||||
|
||||
test("WebHookPushPayload") {
|
||||
@@ -56,42 +111,113 @@ class WebHookJsonFormatSpec extends FunSuite {
|
||||
test("WebHookIssuesPayload") {
|
||||
val payload = WebHookIssuesPayload(
|
||||
action = "edited",
|
||||
number = 1,
|
||||
number = 1347,
|
||||
repository = apiRepository,
|
||||
issue = apiIssue,
|
||||
sender = apiUser
|
||||
)
|
||||
val expected = s"""{
|
||||
|"action":"edited",
|
||||
|"number":1,
|
||||
|"number":1347,
|
||||
|"repository":$jsonRepository,
|
||||
|"issue":{
|
||||
|"number":1347,
|
||||
|"title":"Found a bug",
|
||||
|"user":$jsonUser,
|
||||
|"labels":[{"name":"bug","color":"f29513","url":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/labels/bug"}],
|
||||
|"state":"open",
|
||||
|"created_at":"2011-04-14T16:00:49Z",
|
||||
|"updated_at":"2011-04-14T16:00:49Z",
|
||||
|"body":"I'm having a problem with this.",
|
||||
|"id":0,
|
||||
|"comments_url":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/issues/1347/comments",
|
||||
|"html_url":"http://gitbucket.exmple.com/octocat/Hello-World/issues/1347"},
|
||||
|"issue":$jsonIssue,
|
||||
|"sender":$jsonUser
|
||||
|}""".stripMargin
|
||||
assert(payload, expected)
|
||||
}
|
||||
|
||||
test("WebHookPullRequestPayload") {
|
||||
fail("TODO")
|
||||
val payload = WebHookPullRequestPayload(
|
||||
action = "closed",
|
||||
issue = issuePR,
|
||||
issueUser = account,
|
||||
assignee = Some(account),
|
||||
pullRequest = pullRequest,
|
||||
headRepository = repositoryInfo,
|
||||
headOwner = account,
|
||||
baseRepository = repositoryInfo,
|
||||
baseOwner = account,
|
||||
labels = List(apiLabel),
|
||||
sender = account,
|
||||
mergedComment = Some((issueComment, account))
|
||||
)
|
||||
val expected = s"""{
|
||||
|"action":"closed",
|
||||
|"number":1347,
|
||||
|"repository":$jsonRepository,
|
||||
|"pull_request":$jsonPullRequest,
|
||||
|"sender":$jsonUser
|
||||
|}""".stripMargin
|
||||
assert(payload, expected)
|
||||
}
|
||||
|
||||
test("WebHookIssueCommentPayload") {
|
||||
fail("TODO")
|
||||
val payload = WebHookIssueCommentPayload(
|
||||
issue = issue,
|
||||
issueUser = account,
|
||||
comment = issueComment,
|
||||
commentUser = account,
|
||||
repository = repositoryInfo,
|
||||
repositoryUser = account,
|
||||
sender = account,
|
||||
labels = List(label)
|
||||
)
|
||||
val expected = s"""{
|
||||
|"action":"created",
|
||||
|"repository":$jsonRepository,
|
||||
|"issue":$jsonIssue,
|
||||
|"comment":{
|
||||
|"id":1,
|
||||
|"user":$jsonUser,
|
||||
|"body":"Me too",
|
||||
|"created_at":"2011-04-14T16:00:49Z",
|
||||
|"updated_at":"2011-04-14T16:00:49Z",
|
||||
|"html_url":"http://gitbucket.exmple.com/octocat/Hello-World/issues/1347#comment-1"},
|
||||
|"sender":$jsonUser
|
||||
|}""".stripMargin
|
||||
assert(payload, expected)
|
||||
}
|
||||
|
||||
test("WebHookPullRequestReviewCommentPayload") {
|
||||
fail("TODO")
|
||||
val payload = WebHookPullRequestReviewCommentPayload(
|
||||
action = "create",
|
||||
comment = commitComment,
|
||||
issue = issuePR,
|
||||
issueUser = account,
|
||||
assignee = Some(account),
|
||||
pullRequest = pullRequest,
|
||||
headRepository = repositoryInfo,
|
||||
headOwner = account,
|
||||
baseRepository = repositoryInfo,
|
||||
baseOwner = account,
|
||||
labels = List(apiLabel),
|
||||
sender = account,
|
||||
mergedComment = Some((issueComment, account))
|
||||
)
|
||||
val expected = s"""{
|
||||
|"action":"create",
|
||||
|"comment":{
|
||||
|"id":29724692,
|
||||
|"path":"README.md",
|
||||
|"commit_id":"0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c",
|
||||
|"user":$jsonUser,
|
||||
|"body":"Maybe you should use more emoji on this line.",
|
||||
|"created_at":"2015-05-05T23:40:27Z",
|
||||
|"updated_at":"2015-05-05T23:40:27Z",
|
||||
|"url":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/pulls/comments/29724692",
|
||||
|"html_url":"http://gitbucket.exmple.com/octocat/Hello-World/pull/1347#discussion_r29724692",
|
||||
|"pull_request_url":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/pulls/1347",
|
||||
|"_links":{
|
||||
|"self":{"href":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/pulls/comments/29724692"},
|
||||
|"html":{"href":"http://gitbucket.exmple.com/octocat/Hello-World/pull/1347#discussion_r29724692"},
|
||||
|"pull_request":{"href":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/pulls/1347"}
|
||||
|}
|
||||
|},
|
||||
|"pull_request":$jsonPullRequest,
|
||||
|"repository":$jsonRepository,
|
||||
|"sender":$jsonUser
|
||||
|}""".stripMargin
|
||||
assert(payload, expected)
|
||||
}
|
||||
|
||||
test("WebHookGollumPayload") {
|
||||
|
||||
Reference in New Issue
Block a user