Add unit test

This commit is contained in:
shimamoto
2019-01-07 20:16:29 +09:00
parent 9e8015f475
commit aef8e32da3
5 changed files with 274 additions and 106 deletions

View File

@@ -53,9 +53,6 @@ object ApiRepository {
def apply(repositoryInfo: RepositoryInfo, owner: Account): ApiRepository = def apply(repositoryInfo: RepositoryInfo, owner: Account): ApiRepository =
this(repositoryInfo, ApiUser(owner)) this(repositoryInfo, ApiUser(owner))
def forWebhookPayload(repositoryInfo: RepositoryInfo, owner: ApiUser): ApiRepository =
ApiRepository(repositoryInfo.repository, owner, forkedCount = repositoryInfo.forkedCount, urlIsHtmlUrl = true)
def forDummyPayload(owner: ApiUser): ApiRepository = def forDummyPayload(owner: ApiUser): ApiRepository =
ApiRepository( ApiRepository(
name = "dummy", name = "dummy",

View File

@@ -540,11 +540,8 @@ object WebHookService {
object WebHookCreatePayload { object WebHookCreatePayload {
def apply( def apply(
git: Git,
sender: Account, sender: Account,
refName: String,
repositoryInfo: RepositoryInfo, repositoryInfo: RepositoryInfo,
commits: List[CommitInfo],
repositoryOwner: Account, repositoryOwner: Account,
ref: String, ref: String,
refType: String refType: String
@@ -555,7 +552,7 @@ object WebHookService {
ref_type = refType, ref_type = refType,
description = repositoryInfo.repository.description.getOrElse(""), description = repositoryInfo.repository.description.getOrElse(""),
master_branch = repositoryInfo.repository.defaultBranch, master_branch = repositoryInfo.repository.defaultBranch,
repository = ApiRepository.forWebhookPayload(repositoryInfo, owner = ApiUser(repositoryOwner)) repository = ApiRepository(repositoryInfo, repositoryOwner)
) )
} }
@@ -599,7 +596,7 @@ object WebHookService {
commits = commits.map { commit => commits = commits.map { commit =>
ApiCommit.forWebhookPayload(git, RepositoryName(repositoryInfo), commit) ApiCommit.forWebhookPayload(git, RepositoryName(repositoryInfo), commit)
}, },
repository = ApiRepository.forWebhookPayload(repositoryInfo, owner = ApiUser(repositoryOwner)) repository = ApiRepository(repositoryInfo, repositoryOwner)
) )
def createDummyPayload(sender: Account): WebHookPushPayload = def createDummyPayload(sender: Account): WebHookPushPayload =

View File

@@ -380,11 +380,8 @@ class CommitLogHook(owner: String, repository: String, pusher: String, baseUrl:
} yield { } yield {
val refType = if (refName(1) == "tags") "tag" else "branch" val refType = if (refName(1) == "tags") "tag" else "branch"
WebHookCreatePayload( WebHookCreatePayload(
git,
pusherAccount, pusherAccount,
command.getRefName,
repositoryInfo, repositoryInfo,
newCommits,
ownerAccount, ownerAccount,
ref = branchName, ref = branchName,
refType = refType refType = refType

View File

@@ -2,7 +2,7 @@ package gitbucket.core.api
import java.util.{Base64, Calendar, Date, TimeZone} 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.service.RepositoryService.RepositoryInfo
import gitbucket.core.util.JGitUtil.{CommitInfo, DiffInfo, TagInfo} import gitbucket.core.util.JGitUtil.{CommitInfo, DiffInfo, TagInfo}
import gitbucket.core.util.RepositoryName import gitbucket.core.util.RepositoryName
@@ -82,6 +82,79 @@ object ApiSpecModels {
managers = Seq("myboss") 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 apiUser = ApiUser(account)
val apiRepository = ApiRepository( val apiRepository = ApiRepository(
@@ -92,6 +165,60 @@ object ApiSpecModels {
urlIsHtmlUrl = false 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 ------------ // TODO ------------
val apiCommitStatus = ApiCommitStatus( val apiCommitStatus = ApiCommitStatus(
@@ -116,22 +243,6 @@ object ApiSpecModels {
committer = ApiPersonIdent("baxterthehacker", "baxterthehacker@users.noreply.github.com", date1) committer = ApiPersonIdent("baxterthehacker", "baxterthehacker@users.noreply.github.com", date1)
)(RepositoryName("baxterthehacker", "public-repo"), true) )(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 apiPersonIdent = ApiPersonIdent("Monalisa Octocat", "support@example.com", date1)
val apiCommitListItem = ApiCommitListItem( val apiCommitListItem = ApiCommitListItem(
@@ -154,66 +265,6 @@ object ApiSpecModels {
repository = apiRepository 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( val apiBranchProtection = ApiBranchProtection(
true, true,
Some(ApiBranchProtection.Status(ApiBranchProtection.Everyone, Seq("continuous-integration/travis-ci"))) Some(ApiBranchProtection.Status(ApiBranchProtection.Everyone, Seq("continuous-integration/travis-ci")))

View File

@@ -40,32 +40,7 @@ class WebHookJsonFormatSpec extends FunSuite {
|"html_url":"http://gitbucket.exmple.com/octocat/Hello-World" |"html_url":"http://gitbucket.exmple.com/octocat/Hello-World"
|}""".stripMargin |}""".stripMargin
private def assert(payload: WebHookPayload, expected: String): Assertion = { val jsonIssue = s"""{
val json = JsonFormat(payload)
assert(json == expected.replaceAll("\n", ""))
}
test("WebHookCreatePayload") {
fail("TODO")
}
test("WebHookPushPayload") {
fail("TODO")
}
test("WebHookIssuesPayload") {
val payload = WebHookIssuesPayload(
action = "edited",
number = 1,
repository = apiRepository,
issue = apiIssue,
sender = apiUser
)
val expected = s"""{
|"action":"edited",
|"number":1,
|"repository":$jsonRepository,
|"issue":{
|"number":1347, |"number":1347,
|"title":"Found a bug", |"title":"Found a bug",
|"user":$jsonUser, |"user":$jsonUser,
@@ -76,22 +51,173 @@ class WebHookJsonFormatSpec extends FunSuite {
|"body":"I'm having a problem with this.", |"body":"I'm having a problem with this.",
|"id":0, |"id":0,
|"comments_url":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/issues/1347/comments", |"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"}, |"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") {
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") {
fail("TODO")
}
test("WebHookIssuesPayload") {
val payload = WebHookIssuesPayload(
action = "edited",
number = 1347,
repository = apiRepository,
issue = apiIssue,
sender = apiUser
)
val expected = s"""{
|"action":"edited",
|"number":1347,
|"repository":$jsonRepository,
|"issue":$jsonIssue,
|"sender":$jsonUser |"sender":$jsonUser
|}""".stripMargin |}""".stripMargin
assert(payload, expected) assert(payload, expected)
} }
test("WebHookPullRequestPayload") { 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") { 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") { 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") { test("WebHookGollumPayload") {