diff --git a/src/main/scala/gitbucket/core/servlet/InitializeListener.scala b/src/main/scala/gitbucket/core/servlet/InitializeListener.scala index a20bcfd..96654f4 100644 --- a/src/main/scala/gitbucket/core/servlet/InitializeListener.scala +++ b/src/main/scala/gitbucket/core/servlet/InitializeListener.scala @@ -21,7 +21,7 @@ * The history of versions. A head of this sequence is the current BitBucket version. */ val versions = Seq( - // TODO(tanacasino): Add 3.1 migration + new Version(3, 1), new Version(3, 0), new Version(2, 8), new Version(2, 7) { diff --git a/src/test/scala/api/JsonFormatSpec.scala b/src/test/scala/api/JsonFormatSpec.scala deleted file mode 100644 index 0f145a6..0000000 --- a/src/test/scala/api/JsonFormatSpec.scala +++ /dev/null @@ -1,275 +0,0 @@ -package api -import org.specs2.mutable.Specification -import java.util.{Date, Calendar, TimeZone} -import util.RepositoryName -import org.json4s.jackson.JsonMethods.{pretty, parse} -import org.json4s._ -import org.specs2.matcher._ -class JsonFormatSpec extends Specification { - val date1 = { - val d = Calendar.getInstance(TimeZone.getTimeZone("UTC")) - d.set(2011,3,14,16,0,49) - d.getTime - } - val sha1 = "6dcb09b5b57875f334f61aebed695e2e4193db5e" - val repo1Name = RepositoryName("octocat/Hello-World") - implicit val context = JsonFormat.Context("http://gitbucket.exmple.com") - - val apiUser = ApiUser( - login= "octocat", - email= "octocat@example.com", - `type`= "User", - site_admin= false, - created_at= date1) - val apiUserJson = """{ - "login":"octocat", - "email":"octocat@example.com", - "type":"User", - "site_admin":false, - "created_at":"2011-04-14T16:00:49Z", - "url":"http://gitbucket.exmple.com/api/v3/users/octocat", - "html_url":"http://gitbucket.exmple.com/octocat" - }""" - - val repository = ApiRepository( - name = repo1Name.name, - full_name = repo1Name.fullName, - description = "This your first repo!", - watchers = 0, - forks = 0, - `private` = false, - default_branch = "master", - owner = apiUser) - val repositoryJson = s"""{ - "name" : "Hello-World", - "full_name" : "octocat/Hello-World", - "description" : "This your first repo!", - "watchers" : 0, - "forks" : 0, - "private" : false, - "default_branch" : "master", - "owner" : $apiUserJson, - "forks_count" : 0, - "watchers_coun" : 0, - "url" : "${context.baseUrl}/api/v3/repos/octocat/Hello-World", - "http_url" : "${context.baseUrl}/git/octocat/Hello-World.git", - "clone_url" : "${context.baseUrl}/git/octocat/Hello-World.git", - "html_url" : "${context.baseUrl}/octocat/Hello-World" - }""" - - val apiCommitStatus = ApiCommitStatus( - created_at = date1, - updated_at = date1, - state = "success", - target_url = Some("https://ci.example.com/1000/output"), - description = Some("Build has completed successfully"), - id = 1, - context = "Default", - creator = apiUser - )(sha1, repo1Name) - val apiCommitStatusJson = s"""{ - "created_at":"2011-04-14T16:00:49Z", - "updated_at":"2011-04-14T16:00:49Z", - "state":"success", - "target_url":"https://ci.example.com/1000/output", - "description":"Build has completed successfully", - "id":1, - "context":"Default", - "creator":$apiUserJson, - "url": "http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e/statuses" - }""" - - val apiComment = ApiComment( - id =1, - user = apiUser, - body= "Me too", - created_at= date1, - updated_at= date1) - val apiCommentJson = s"""{ - "id": 1, - "body": "Me too", - "user": $apiUserJson, - "created_at": "2011-04-14T16:00:49Z", - "updated_at": "2011-04-14T16:00:49Z" - }""" - - val apiPersonIdent = ApiPersonIdent("Monalisa Octocat","support@example.com",date1) - val apiPersonIdentJson = """ { - "name": "Monalisa Octocat", - "email": "support@example.com", - "date": "2011-04-14T16:00:49Z" - }""" - - val apiCommitListItem = ApiCommitListItem( - sha = sha1, - commit = ApiCommitListItem.Commit( - message = "Fix all the bugs", - author = apiPersonIdent, - committer = apiPersonIdent - )(sha1, repo1Name), - author = Some(apiUser), - committer= Some(apiUser), - parents= Seq(ApiCommitListItem.Parent("6dcb09b5b57875f334f61aebed695e2e4193db5e")(repo1Name)))(repo1Name) - val apiCommitListItemJson = s"""{ - "url": "${context.baseUrl}/api/v3/repos/octocat/Hello-World/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e", - "sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e", - "commit": { - "url": "${context.baseUrl}/api/v3/repos/octocat/Hello-World/git/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e", - "author": $apiPersonIdentJson, - "committer": $apiPersonIdentJson, - "message": "Fix all the bugs" - }, - "author": $apiUserJson, - "committer": $apiUserJson, - "parents": [ - { - "url": "${context.baseUrl}/api/v3/repos/octocat/Hello-World/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e", - "sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e" - } - ] - }""" - - val apiCombinedCommitStatus = ApiCombinedCommitStatus( - state = "success", - sha = sha1, - total_count = 2, - statuses = List(apiCommitStatus), - repository = repository) - val apiCombinedCommitStatusJson = s"""{ - "state": "success", - "sha": "$sha1", - "total_count": 2, - "statuses": [ $apiCommitStatusJson ], - "repository": $repositoryJson, - "url": "${context.baseUrl}/api/v3/repos/octocat/Hello-World/commits/$sha1/status" - }""" - - val apiIssue = ApiIssue( - number = 1347, - title = "Found a bug", - user = apiUser, - state = "open", - body = "I'm having a problem with this.", - created_at = date1, - updated_at = date1) - val apiIssueJson = s"""{ - "number": 1347, - "state": "open", - "title": "Found a bug", - "body": "I'm having a problem with this.", - "user": $apiUserJson, - "created_at": "2011-04-14T16:00:49Z", - "updated_at": "2011-04-14T16:00:49Z" - }""" - - val apiPullRequest = ApiPullRequest( - number = 1347, - updated_at = date1, - created_at = date1, - head = ApiPullRequest.Commit( - sha = sha1, - ref = "new-topic", - repo = repository)("octocat"), - base = ApiPullRequest.Commit( - sha = sha1, - ref = "master", - repo = repository)("octocat"), - mergeable = None, - title = "new-feature", - body = "Please pull these awesome changes", - user = apiUser - ) - val apiPullRequestJson = s"""{ - "url": "${context.baseUrl}/api/v3/repos/octocat/Hello-World/pulls/1347", - "html_url": "${context.baseUrl}/octocat/Hello-World/pull/1347", - // "diff_url": "${context.baseUrl}/octocat/Hello-World/pull/1347.diff", - // "patch_url": "${context.baseUrl}/octocat/Hello-World/pull/1347.patch", - // "issue_url": "${context.baseUrl}/api/v3/repos/octocat/Hello-World/issues/1347", - "commits_url": "${context.baseUrl}/api/v3/repos/octocat/Hello-World/pulls/1347/commits", - "review_comments_url": "${context.baseUrl}/api/v3/repos/octocat/Hello-World/pulls/1347/comments", - "review_comment_url": "${context.baseUrl}/api/v3/repos/octocat/Hello-World/pulls/comments/{number}", - "comments_url": "${context.baseUrl}/api/v3/repos/octocat/Hello-World/issues/1347/comments", - "statuses_url": "${context.baseUrl}/api/v3/repos/octocat/Hello-World/statuses/6dcb09b5b57875f334f61aebed695e2e4193db5e", - "number": 1347, - // "state": "open", - "title": "new-feature", - "body": "Please pull these awesome changes", - "created_at": "2011-04-14T16:00:49Z", - "updated_at": "2011-04-14T16:00:49Z", - // "closed_at": "2011-04-14T16:00:49Z", - // "merged_at": "2011-04-14T16:00:49Z", - "head": { - "label": "new-topic", - "ref": "new-topic", - "sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e", - "user": $apiUserJson, - "repo": $repositoryJson - }, - "base": { - "label": "master", - "ref": "master", - "sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e", - "user": $apiUserJson, - "repo": $repositoryJson - }, - "user": $apiUserJson - // "merge_commit_sha": "e5bd3914e2e596debea16f433f57875b5b90bcd6", - // "merged": false, - // "mergeable": true, - // "merged_by": $$apiUserJson, - // "comments": 10, - // "commits": 3, - // "additions": 100, - // "deletions": 3, - // "changed_files": 5 - }""" - def beFormatted(json2Arg:String) = new Matcher[String] { - def apply[S <: String](e: Expectable[S]) = { - import java.util.regex.Pattern - val json2 = Pattern.compile("""^\s*//.*$""", Pattern.MULTILINE).matcher(json2Arg).replaceAll("") - val js2 = try{ - parse(json2) - }catch{ - case e:com.fasterxml.jackson.core.JsonParseException => { - val p = java.lang.Math.max(e.getLocation.getCharOffset()-10,0).toInt - val message = json2.substring(p,java.lang.Math.min(p+100,json2.length)) - throw new com.fasterxml.jackson.core.JsonParseException(message + e.getMessage , e.getLocation) - } - } - val js1 = parse(e.value) - result(js1 == js2, - "expected", - { - val diff = js2 diff js1 - s"${pretty(js1)} is not ${pretty(js2)} \n\n ${pretty(Extraction.decompose(diff)(org.json4s.DefaultFormats))}" - }, - e) - } - } - "JsonFormat" should { - "apiUser" in { - JsonFormat(apiUser) must beFormatted(apiUserJson) - } - "repository" in { - JsonFormat(repository) must beFormatted(repositoryJson) - } - "apiComment" in { - JsonFormat(apiComment) must beFormatted(apiCommentJson) - } - "apiCommitListItem" in { - JsonFormat(apiCommitListItem) must beFormatted(apiCommitListItemJson) - } - "apiCommitStatus" in { - JsonFormat(apiCommitStatus) must beFormatted(apiCommitStatusJson) - } - "apiCombinedCommitStatus" in { - JsonFormat(apiCombinedCommitStatus) must beFormatted(apiCombinedCommitStatusJson) - } - "apiIssue" in { - JsonFormat(apiIssue) must beFormatted(apiIssueJson) - } - "apiPullRequest" in { - JsonFormat(apiPullRequest) must beFormatted(apiPullRequestJson) - } - } -} diff --git a/src/test/scala/gitbucket/core/api/JsonFormatSpec.scala b/src/test/scala/gitbucket/core/api/JsonFormatSpec.scala new file mode 100644 index 0000000..73d4ef7 --- /dev/null +++ b/src/test/scala/gitbucket/core/api/JsonFormatSpec.scala @@ -0,0 +1,281 @@ +package gitbucket.core.api + +import gitbucket.core.util.RepositoryName + +import org.specs2.mutable.Specification +import org.json4s.jackson.JsonMethods.{pretty, parse} +import org.json4s._ +import org.specs2.matcher._ + +import java.util.{Calendar, TimeZone} + + + +class JsonFormatSpec extends Specification { + val date1 = { + val d = Calendar.getInstance(TimeZone.getTimeZone("UTC")) + d.set(2011,3,14,16,0,49) + d.getTime + } + val sha1 = "6dcb09b5b57875f334f61aebed695e2e4193db5e" + val repo1Name = RepositoryName("octocat/Hello-World") + implicit val context = JsonFormat.Context("http://gitbucket.exmple.com") + + val apiUser = ApiUser( + login= "octocat", + email= "octocat@example.com", + `type`= "User", + site_admin= false, + created_at= date1) + val apiUserJson = """{ + "login":"octocat", + "email":"octocat@example.com", + "type":"User", + "site_admin":false, + "created_at":"2011-04-14T16:00:49Z", + "url":"http://gitbucket.exmple.com/api/v3/users/octocat", + "html_url":"http://gitbucket.exmple.com/octocat" + }""" + + val repository = ApiRepository( + name = repo1Name.name, + full_name = repo1Name.fullName, + description = "This your first repo!", + watchers = 0, + forks = 0, + `private` = false, + default_branch = "master", + owner = apiUser) + val repositoryJson = s"""{ + "name" : "Hello-World", + "full_name" : "octocat/Hello-World", + "description" : "This your first repo!", + "watchers" : 0, + "forks" : 0, + "private" : false, + "default_branch" : "master", + "owner" : $apiUserJson, + "forks_count" : 0, + "watchers_coun" : 0, + "url" : "${context.baseUrl}/api/v3/repos/octocat/Hello-World", + "http_url" : "${context.baseUrl}/git/octocat/Hello-World.git", + "clone_url" : "${context.baseUrl}/git/octocat/Hello-World.git", + "html_url" : "${context.baseUrl}/octocat/Hello-World" + }""" + + val apiCommitStatus = ApiCommitStatus( + created_at = date1, + updated_at = date1, + state = "success", + target_url = Some("https://ci.example.com/1000/output"), + description = Some("Build has completed successfully"), + id = 1, + context = "Default", + creator = apiUser + )(sha1, repo1Name) + val apiCommitStatusJson = s"""{ + "created_at":"2011-04-14T16:00:49Z", + "updated_at":"2011-04-14T16:00:49Z", + "state":"success", + "target_url":"https://ci.example.com/1000/output", + "description":"Build has completed successfully", + "id":1, + "context":"Default", + "creator":$apiUserJson, + "url": "http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e/statuses" + }""" + + val apiComment = ApiComment( + id =1, + user = apiUser, + body= "Me too", + created_at= date1, + updated_at= date1) + val apiCommentJson = s"""{ + "id": 1, + "body": "Me too", + "user": $apiUserJson, + "created_at": "2011-04-14T16:00:49Z", + "updated_at": "2011-04-14T16:00:49Z" + }""" + + val apiPersonIdent = ApiPersonIdent("Monalisa Octocat","support@example.com",date1) + val apiPersonIdentJson = """ { + "name": "Monalisa Octocat", + "email": "support@example.com", + "date": "2011-04-14T16:00:49Z" + }""" + + val apiCommitListItem = ApiCommitListItem( + sha = sha1, + commit = ApiCommitListItem.Commit( + message = "Fix all the bugs", + author = apiPersonIdent, + committer = apiPersonIdent + )(sha1, repo1Name), + author = Some(apiUser), + committer= Some(apiUser), + parents= Seq(ApiCommitListItem.Parent("6dcb09b5b57875f334f61aebed695e2e4193db5e")(repo1Name)))(repo1Name) + val apiCommitListItemJson = s"""{ + "url": "${context.baseUrl}/api/v3/repos/octocat/Hello-World/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e", + "sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e", + "commit": { + "url": "${context.baseUrl}/api/v3/repos/octocat/Hello-World/git/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e", + "author": $apiPersonIdentJson, + "committer": $apiPersonIdentJson, + "message": "Fix all the bugs" + }, + "author": $apiUserJson, + "committer": $apiUserJson, + "parents": [ + { + "url": "${context.baseUrl}/api/v3/repos/octocat/Hello-World/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e", + "sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e" + } + ] + }""" + + val apiCombinedCommitStatus = ApiCombinedCommitStatus( + state = "success", + sha = sha1, + total_count = 2, + statuses = List(apiCommitStatus), + repository = repository) + val apiCombinedCommitStatusJson = s"""{ + "state": "success", + "sha": "$sha1", + "total_count": 2, + "statuses": [ $apiCommitStatusJson ], + "repository": $repositoryJson, + "url": "${context.baseUrl}/api/v3/repos/octocat/Hello-World/commits/$sha1/status" + }""" + + val apiIssue = ApiIssue( + number = 1347, + title = "Found a bug", + user = apiUser, + state = "open", + body = "I'm having a problem with this.", + created_at = date1, + updated_at = date1) + val apiIssueJson = s"""{ + "number": 1347, + "state": "open", + "title": "Found a bug", + "body": "I'm having a problem with this.", + "user": $apiUserJson, + "created_at": "2011-04-14T16:00:49Z", + "updated_at": "2011-04-14T16:00:49Z" + }""" + + val apiPullRequest = ApiPullRequest( + number = 1347, + updated_at = date1, + created_at = date1, + head = ApiPullRequest.Commit( + sha = sha1, + ref = "new-topic", + repo = repository)("octocat"), + base = ApiPullRequest.Commit( + sha = sha1, + ref = "master", + repo = repository)("octocat"), + mergeable = None, + title = "new-feature", + body = "Please pull these awesome changes", + user = apiUser + ) + val apiPullRequestJson = s"""{ + "url": "${context.baseUrl}/api/v3/repos/octocat/Hello-World/pulls/1347", + "html_url": "${context.baseUrl}/octocat/Hello-World/pull/1347", + // "diff_url": "${context.baseUrl}/octocat/Hello-World/pull/1347.diff", + // "patch_url": "${context.baseUrl}/octocat/Hello-World/pull/1347.patch", + // "issue_url": "${context.baseUrl}/api/v3/repos/octocat/Hello-World/issues/1347", + "commits_url": "${context.baseUrl}/api/v3/repos/octocat/Hello-World/pulls/1347/commits", + "review_comments_url": "${context.baseUrl}/api/v3/repos/octocat/Hello-World/pulls/1347/comments", + "review_comment_url": "${context.baseUrl}/api/v3/repos/octocat/Hello-World/pulls/comments/{number}", + "comments_url": "${context.baseUrl}/api/v3/repos/octocat/Hello-World/issues/1347/comments", + "statuses_url": "${context.baseUrl}/api/v3/repos/octocat/Hello-World/statuses/6dcb09b5b57875f334f61aebed695e2e4193db5e", + "number": 1347, + // "state": "open", + "title": "new-feature", + "body": "Please pull these awesome changes", + "created_at": "2011-04-14T16:00:49Z", + "updated_at": "2011-04-14T16:00:49Z", + // "closed_at": "2011-04-14T16:00:49Z", + // "merged_at": "2011-04-14T16:00:49Z", + "head": { + "label": "new-topic", + "ref": "new-topic", + "sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e", + "user": $apiUserJson, + "repo": $repositoryJson + }, + "base": { + "label": "master", + "ref": "master", + "sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e", + "user": $apiUserJson, + "repo": $repositoryJson + }, + "user": $apiUserJson + // "merge_commit_sha": "e5bd3914e2e596debea16f433f57875b5b90bcd6", + // "merged": false, + // "mergeable": true, + // "merged_by": $$apiUserJson, + // "comments": 10, + // "commits": 3, + // "additions": 100, + // "deletions": 3, + // "changed_files": 5 + }""" + def beFormatted(json2Arg:String) = new Matcher[String] { + def apply[S <: String](e: Expectable[S]) = { + import java.util.regex.Pattern + val json2 = Pattern.compile("""^\s*//.*$""", Pattern.MULTILINE).matcher(json2Arg).replaceAll("") + val js2 = try{ + parse(json2) + }catch{ + case e:com.fasterxml.jackson.core.JsonParseException => { + val p = java.lang.Math.max(e.getLocation.getCharOffset()-10,0).toInt + val message = json2.substring(p,java.lang.Math.min(p+100,json2.length)) + throw new com.fasterxml.jackson.core.JsonParseException(message + e.getMessage , e.getLocation) + } + } + val js1 = parse(e.value) + result(js1 == js2, + "expected", + { + val diff = js2 diff js1 + s"${pretty(js1)} is not ${pretty(js2)} \n\n ${pretty(Extraction.decompose(diff)(org.json4s.DefaultFormats))}" + }, + e) + } + } + "JsonFormat" should { + "apiUser" in { + JsonFormat(apiUser) must beFormatted(apiUserJson) + } + "repository" in { + JsonFormat(repository) must beFormatted(repositoryJson) + } + "apiComment" in { + JsonFormat(apiComment) must beFormatted(apiCommentJson) + } + "apiCommitListItem" in { + JsonFormat(apiCommitListItem) must beFormatted(apiCommitListItemJson) + } + "apiCommitStatus" in { + JsonFormat(apiCommitStatus) must beFormatted(apiCommitStatusJson) + } + "apiCombinedCommitStatus" in { + JsonFormat(apiCombinedCommitStatus) must beFormatted(apiCombinedCommitStatusJson) + } + "apiIssue" in { + JsonFormat(apiIssue) must beFormatted(apiIssueJson) + } + "apiPullRequest" in { + JsonFormat(apiPullRequest) must beFormatted(apiPullRequestJson) + } + } +} diff --git a/src/test/scala/gitbucket/core/model/CommitStateSpec.scala b/src/test/scala/gitbucket/core/model/CommitStateSpec.scala new file mode 100644 index 0000000..a161908 --- /dev/null +++ b/src/test/scala/gitbucket/core/model/CommitStateSpec.scala @@ -0,0 +1,26 @@ +package gitbucket.core.model + +import gitbucket.core.model.CommitState._ + +import org.specs2.mutable.Specification + + +class CommitStateSpec extends Specification { + "CommitState" should { + "combine empty must eq PENDING" in { + combine(Set()) must_== PENDING + } + "combine includes ERROR must eq FAILURE" in { + combine(Set(ERROR, SUCCESS, PENDING)) must_== FAILURE + } + "combine includes FAILURE must eq peinding" in { + combine(Set(FAILURE, SUCCESS, PENDING)) must_== FAILURE + } + "combine includes PENDING must eq peinding" in { + combine(Set(PENDING, SUCCESS)) must_== PENDING + } + "combine only SUCCESS must eq SUCCESS" in { + combine(Set(SUCCESS)) must_== SUCCESS + } + } +} diff --git a/src/test/scala/gitbucket/core/service/AccessTokenServiceSpec.scala b/src/test/scala/gitbucket/core/service/AccessTokenServiceSpec.scala new file mode 100644 index 0000000..460e020 --- /dev/null +++ b/src/test/scala/gitbucket/core/service/AccessTokenServiceSpec.scala @@ -0,0 +1,91 @@ +package gitbucket.core.service + +import gitbucket.core.model._ + +import org.specs2.mutable.Specification + +import java.util.Date + + +class AccessTokenServiceSpec extends Specification with ServiceSpecBase { + + "AccessTokenService" should { + "generateAccessToken" in { withTestDB { implicit session => + AccessTokenService.generateAccessToken("root", "note") must be like{ + case (id, token) if id != 0 => ok + } + }} + + "getAccessTokens" in { withTestDB { implicit session => + val (id, token) = AccessTokenService.generateAccessToken("root", "note") + val tokenHash = AccessTokenService.tokenToHash(token) + + AccessTokenService.getAccessTokens("root") must be like{ + case List(AccessToken(`id`, "root", `tokenHash`, "note")) => ok + } + }} + + "getAccessTokens(root) get root's tokens" in { withTestDB { implicit session => + val (id, token) = AccessTokenService.generateAccessToken("root", "note") + val tokenHash = AccessTokenService.tokenToHash(token) + val user2 = generateNewAccount("user2") + AccessTokenService.generateAccessToken("user2", "note2") + + AccessTokenService.getAccessTokens("root") must be like{ + case List(AccessToken(`id`, "root", `tokenHash`, "note")) => ok + } + }} + + "deleteAccessToken" in { withTestDB { implicit session => + val (id, token) = AccessTokenService.generateAccessToken("root", "note") + val user2 = generateNewAccount("user2") + AccessTokenService.generateAccessToken("user2", "note2") + + AccessTokenService.deleteAccessToken("root", id) + + AccessTokenService.getAccessTokens("root") must beEmpty + }} + + "getAccountByAccessToken" in { withTestDB { implicit session => + val (id, token) = AccessTokenService.generateAccessToken("root", "note") + AccessTokenService.getAccountByAccessToken(token) must beSome.like { + case user => user.userName must_== "root" + } + }} + + "getAccountByAccessToken don't get removed account" in { withTestDB { implicit session => + val user2 = generateNewAccount("user2") + val (id, token) = AccessTokenService.generateAccessToken("user2", "note") + AccountService.updateAccount(user2.copy(isRemoved=true)) + + AccessTokenService.getAccountByAccessToken(token) must beEmpty + }} + + "generateAccessToken create uniq token" in { withTestDB { implicit session => + val tokenIt = List("token1","token1","token1","token2").iterator + val service = new AccessTokenService{ + override def makeAccessTokenString:String = tokenIt.next + } + + service.generateAccessToken("root", "note1") must like{ + case (_, "token1") => ok + } + service.generateAccessToken("root", "note2") must like{ + case (_, "token2") => ok + } + }} + + "when update Account.userName then AccessToken.userName changed" in { withTestDB { implicit session => + val user2 = generateNewAccount("user2") + val (id, token) = AccessTokenService.generateAccessToken("user2", "note") + import gitbucket.core.model.Profile._ + import profile.simple._ + Accounts.filter(_.userName === "user2".bind).map(_.userName).update("user3") + + AccessTokenService.getAccountByAccessToken(token) must beSome.like { + case user => user.userName must_== "user3" + } + }} + } +} + diff --git a/src/test/scala/gitbucket/core/service/CommitStateServiceSpec.scala b/src/test/scala/gitbucket/core/service/CommitStateServiceSpec.scala new file mode 100644 index 0000000..5bc109f --- /dev/null +++ b/src/test/scala/gitbucket/core/service/CommitStateServiceSpec.scala @@ -0,0 +1,78 @@ +package gitbucket.core.service + +import gitbucket.core.model._ +import gitbucket.core.model.Profile._ +import profile.simple._ + +import org.specs2.mutable.Specification + +import java.util.Date + + +class CommitStatusServiceSpec extends Specification with ServiceSpecBase with CommitStatusService + with RepositoryService with AccountService{ + val now = new java.util.Date() + val fixture1 = CommitStatus( + userName = "root", + repositoryName = "repo", + commitId = "0e97b8f59f7cdd709418bb59de53f741fd1c1bd7", + context = "jenkins/test", + creator = "tester", + state = CommitState.PENDING, + targetUrl = Some("http://example.com/target"), + description = Some("description"), + updatedDate = now, + registeredDate = now) + def findById(id: Int)(implicit s:Session) = CommitStatuses.filter(_.byPrimaryKey(id)).firstOption + def generateFixture1(tester:Account)(implicit s:Session) = createCommitStatus( + userName = fixture1.userName, + repositoryName = fixture1.repositoryName, + sha = fixture1.commitId, + context = fixture1.context, + state = fixture1.state, + targetUrl = fixture1.targetUrl, + description = fixture1.description, + creator = tester, + now = fixture1.registeredDate) + "CommitStatusService" should { + "createCommitState can insert and update" in { withTestDB { implicit session => + val tester = generateNewAccount(fixture1.creator) + createRepository(fixture1.repositoryName,fixture1.userName,None,false) + val id = generateFixture1(tester:Account) + getCommitStatus(fixture1.userName, fixture1.repositoryName, id) must_== + Some(fixture1.copy(commitStatusId=id)) + // other one can update + val tester2 = generateNewAccount("tester2") + val time2 = new java.util.Date(); + val id2 = createCommitStatus( + userName = fixture1.userName, + repositoryName = fixture1.repositoryName, + sha = fixture1.commitId, + context = fixture1.context, + state = CommitState.SUCCESS, + targetUrl = Some("http://example.com/target2"), + description = Some("description2"), + creator = tester2, + now = time2) + getCommitStatus(fixture1.userName, fixture1.repositoryName, id2) must_== Some(fixture1.copy( + commitStatusId = id, + creator = "tester2", + state = CommitState.SUCCESS, + targetUrl = Some("http://example.com/target2"), + description = Some("description2"), + updatedDate = time2)) + }} + "getCommitStatus can find by commitId and context" in { withTestDB { implicit session => + val tester = generateNewAccount(fixture1.creator) + createRepository(fixture1.repositoryName,fixture1.userName,None,false) + val id = generateFixture1(tester:Account) + getCommitStatus(fixture1.userName, fixture1.repositoryName, fixture1.commitId, fixture1.context) must_== Some(fixture1.copy(commitStatusId=id)) + }} + "getCommitStatus can find by commitStatusId" in { withTestDB { implicit session => + val tester = generateNewAccount(fixture1.creator) + createRepository(fixture1.repositoryName,fixture1.userName,None,false) + val id = generateFixture1(tester:Account) + getCommitStatus(fixture1.userName, fixture1.repositoryName, id) must_== Some(fixture1.copy(commitStatusId=id)) + }} + } +} \ No newline at end of file diff --git a/src/test/scala/gitbucket/core/service/IssuesServiceSpec.scala b/src/test/scala/gitbucket/core/service/IssuesServiceSpec.scala new file mode 100644 index 0000000..f4d75bb --- /dev/null +++ b/src/test/scala/gitbucket/core/service/IssuesServiceSpec.scala @@ -0,0 +1,50 @@ +package gitbucket.core.service + +import gitbucket.core.model._ +import gitbucket.core.service.IssuesService._ + +import org.specs2.mutable.Specification + +import java.util.Date + + +class IssuesServiceSpec extends Specification with ServiceSpecBase { + "IssuesService" should { + "getCommitStatues" in { withTestDB { implicit session => + val user1 = generateNewUserWithDBRepository("user1","repo1") + + def getCommitStatues = dummyService.getCommitStatues(List(("user1","repo1",1),("user1","repo1",2))) + + getCommitStatues must_== Map.empty + + val now = new java.util.Date() + val issueId = generateNewIssue("user1","repo1") + issueId must_== 1 + + getCommitStatues must_== Map.empty + + val cs = dummyService.createCommitStatus("user1","repo1","shasha", "default", CommitState.SUCCESS, Some("http://exmple.com/ci"), Some("exampleService"), now, user1) + + getCommitStatues must_== Map.empty + + val (is2, pr2) = generateNewPullRequest("user1/repo1/master","user1/repo1/feature1") + pr2.issueId must_== 2 + + // if there are no statuses, state is none + getCommitStatues must_== Map.empty + + // if there is a status, state is that + val cs2 = dummyService.createCommitStatus("user1","repo1","feature1", "default", CommitState.SUCCESS, Some("http://exmple.com/ci"), Some("exampleService"), now, user1) + getCommitStatues must_== Map(("user1","repo1",2) -> CommitStatusInfo(1,1,Some("default"),Some(CommitState.SUCCESS),Some("http://exmple.com/ci"),Some("exampleService"))) + + // if there are two statuses, state is none + val cs3 = dummyService.createCommitStatus("user1","repo1","feature1", "pend", CommitState.PENDING, Some("http://exmple.com/ci"), Some("exampleService"), now, user1) + getCommitStatues must_== Map(("user1","repo1",2) -> CommitStatusInfo(2,1,None,None,None,None)) + + // get only statuses in query issues + val (is3, pr3) = generateNewPullRequest("user1/repo1/master","user1/repo1/feature3") + val cs4 = dummyService.createCommitStatus("user1","repo1","feature3", "none", CommitState.PENDING, None, None, now, user1) + getCommitStatues must_== Map(("user1","repo1",2) -> CommitStatusInfo(2,1,None,None,None,None)) + } } + } +} \ No newline at end of file diff --git a/src/test/scala/gitbucket/core/service/MergeServiceSpec.scala b/src/test/scala/gitbucket/core/service/MergeServiceSpec.scala new file mode 100644 index 0000000..ce4142a --- /dev/null +++ b/src/test/scala/gitbucket/core/service/MergeServiceSpec.scala @@ -0,0 +1,159 @@ +package gitbucket.core.service + +import gitbucket.core.model._ +import gitbucket.core.util.JGitUtil +import gitbucket.core.util.Directory._ +import gitbucket.core.util.Implicits._ +import gitbucket.core.util.ControlUtil._ + +import org.apache.commons.io.FileUtils +import org.eclipse.jgit.api.Git +import org.eclipse.jgit.dircache.DirCache +import org.eclipse.jgit.lib._ +import org.eclipse.jgit.revwalk._ +import org.eclipse.jgit.treewalk._ +import org.specs2.mutable.Specification + +import java.nio.file._ +import java.util.Date + + +class MergeServiceSpec extends Specification { + sequential + val service = new MergeService{} + val branch = "master" + val issueId = 10 + def initRepository(owner:String, name:String) = { + val repo1Dir = getRepositoryDir(owner, name) + RepositoryCache.clear() + FileUtils.deleteQuietly(repo1Dir) + Files.createDirectories(repo1Dir.toPath()) + JGitUtil.initRepository(repo1Dir) + using(Git.open(repo1Dir)){ git => + createFile(git, s"refs/heads/master", "test.txt", "hoge" ) + git.branchCreate().setStartPoint(s"refs/heads/master").setName(s"refs/pull/${issueId}/head").call() + } + repo1Dir + } + def createFile(git:Git, branch:String, name:String, content:String){ + val builder = DirCache.newInCore.builder() + val inserter = git.getRepository.newObjectInserter() + val headId = git.getRepository.resolve(branch + "^{commit}") + builder.add(JGitUtil.createDirCacheEntry(name, FileMode.REGULAR_FILE, + inserter.insert(Constants.OBJ_BLOB, content.getBytes("UTF-8")))) + builder.finish() + JGitUtil.createNewCommit(git, inserter, headId, builder.getDirCache.writeTree(inserter), + branch, "dummy", "dummy@example.com", "Initial commit") + } + def getFile(git:Git, branch:String, path:String) = { + val revCommit = JGitUtil.getRevCommitFromId(git, git.getRepository.resolve(branch)) + val objectId = using(new TreeWalk(git.getRepository)){ walk => + walk.addTree(revCommit.getTree) + walk.setRecursive(true) + @scala.annotation.tailrec + def _getPathObjectId: ObjectId = walk.next match { + case true if(walk.getPathString == path) => walk.getObjectId(0) + case true => _getPathObjectId + case false => throw new Exception(s"not found ${branch} / ${path}") + } + _getPathObjectId + } + JGitUtil.getContentInfo(git, path, objectId) + } + def createConfrict(git:Git) = { + createFile(git, s"refs/heads/${branch}", "test.txt", "hoge2" ) + createFile(git, s"refs/pull/${issueId}/head", "test.txt", "hoge4" ) + } + "checkConflict, checkConflictCache" should { + "checkConflict false if not conflicted, and create cache" in { + val repo1Dir = initRepository("user1","repo1") + service.checkConflictCache("user1", "repo1", branch, issueId) mustEqual None + val conflicted = service.checkConflict("user1", "repo1", branch, issueId) + service.checkConflictCache("user1", "repo1", branch, issueId) mustEqual Some(false) + conflicted mustEqual false + } + "checkConflict true if not conflicted, and create cache" in { + val repo1Dir = initRepository("user1","repo1") + using(Git.open(repo1Dir)){ git => + createConfrict(git) + } + service.checkConflictCache("user1", "repo1", branch, issueId) mustEqual None + val conflicted = service.checkConflict("user1", "repo1", branch, issueId) + conflicted mustEqual true + service.checkConflictCache("user1", "repo1", branch, issueId) mustEqual Some(true) + } + } + "checkConflictCache" should { + "merged cache invalid if origin branch moved" in { + val repo1Dir = initRepository("user1","repo1") + service.checkConflict("user1", "repo1", branch, issueId) mustEqual false + service.checkConflictCache("user1", "repo1", branch, issueId) mustEqual Some(false) + using(Git.open(repo1Dir)){ git => + createFile(git, s"refs/heads/${branch}", "test.txt", "hoge2" ) + } + service.checkConflictCache("user1", "repo1", branch, issueId) mustEqual None + } + "merged cache invalid if request branch moved" in { + val repo1Dir = initRepository("user1","repo1") + service.checkConflict("user1", "repo1", branch, issueId) mustEqual false + service.checkConflictCache("user1", "repo1", branch, issueId) mustEqual Some(false) + using(Git.open(repo1Dir)){ git => + createFile(git, s"refs/pull/${issueId}/head", "test.txt", "hoge4" ) + } + service.checkConflictCache("user1", "repo1", branch, issueId) mustEqual None + } + "merged cache invalid if origin branch moved" in { + val repo1Dir = initRepository("user1","repo1") + service.checkConflict("user1", "repo1", branch, issueId) mustEqual false + service.checkConflictCache("user1", "repo1", branch, issueId) mustEqual Some(false) + using(Git.open(repo1Dir)){ git => + createFile(git, s"refs/heads/${branch}", "test.txt", "hoge2" ) + } + service.checkConflictCache("user1", "repo1", branch, issueId) mustEqual None + } + "conflicted cache invalid if request branch moved" in { + val repo1Dir = initRepository("user1","repo1") + using(Git.open(repo1Dir)){ git => + createConfrict(git) + } + service.checkConflict("user1", "repo1", branch, issueId) mustEqual true + service.checkConflictCache("user1", "repo1", branch, issueId) mustEqual Some(true) + using(Git.open(repo1Dir)){ git => + createFile(git, s"refs/pull/${issueId}/head", "test.txt", "hoge4" ) + } + service.checkConflictCache("user1", "repo1", branch, issueId) mustEqual None + } + "conflicted cache invalid if origin branch moved" in { + val repo1Dir = initRepository("user1","repo1") + using(Git.open(repo1Dir)){ git => + createConfrict(git) + } + service.checkConflict("user1", "repo1", branch, issueId) mustEqual true + service.checkConflictCache("user1", "repo1", branch, issueId) mustEqual Some(true) + using(Git.open(repo1Dir)){ git => + createFile(git, s"refs/heads/${branch}", "test.txt", "hoge4" ) + } + service.checkConflictCache("user1", "repo1", branch, issueId) mustEqual None + } + } + "mergePullRequest" should { + "can merge" in { + val repo1Dir = initRepository("user1","repo1") + using(Git.open(repo1Dir)){ git => + createFile(git, s"refs/pull/${issueId}/head", "test.txt", "hoge2" ) + val committer = new PersonIdent("dummy2", "dummy2@example.com") + getFile(git, branch, "test.txt").content.get mustEqual "hoge" + val requestBranchId = git.getRepository.resolve(s"refs/pull/${issueId}/head") + val masterId = git.getRepository.resolve(branch) + service.mergePullRequest(git, branch, issueId, "merged", committer) + val lastCommitId = git.getRepository.resolve(branch); + val commit = using(new RevWalk(git.getRepository))(_.parseCommit(lastCommitId)) + commit.getCommitterIdent() mustEqual committer + commit.getAuthorIdent() mustEqual committer + commit.getFullMessage() mustEqual "merged" + commit.getParents.toSet mustEqual Set( requestBranchId, masterId ) + getFile(git, branch, "test.txt").content.get mustEqual "hoge2" + } + } + } +} \ No newline at end of file diff --git a/src/test/scala/gitbucket/core/service/RepositoryServiceSpec.scala b/src/test/scala/gitbucket/core/service/RepositoryServiceSpec.scala new file mode 100644 index 0000000..b8f7bd4 --- /dev/null +++ b/src/test/scala/gitbucket/core/service/RepositoryServiceSpec.scala @@ -0,0 +1,35 @@ +package gitbucket.core.service + +import gitbucket.core.model._ +import gitbucket.core.model.Profile._ + +import org.specs2.mutable.Specification + + +class RepositoryServiceSpec extends Specification with ServiceSpecBase with RepositoryService with AccountService{ + "RepositoryService" should { + "renameRepository can rename CommitState" in { withTestDB { implicit session => + val tester = generateNewAccount("tester") + createRepository("repo","root",None,false) + val commitStatusService = new CommitStatusService{} + val id = commitStatusService.createCommitStatus( + userName = "root", + repositoryName = "repo", + sha = "0e97b8f59f7cdd709418bb59de53f741fd1c1bd7", + context = "jenkins/test", + state = CommitState.PENDING, + targetUrl = Some("http://example.com/target"), + description = Some("description"), + creator = tester, + now = new java.util.Date) + val org = commitStatusService.getCommitStatus("root","repo", id).get + renameRepository("root","repo","tester","repo2") + val neo = commitStatusService.getCommitStatus("tester","repo2", org.commitId, org.context).get + neo must_== + org.copy( + commitStatusId=neo.commitStatusId, + repositoryName="repo2", + userName="tester") + }} + } +} diff --git a/src/test/scala/gitbucket/core/service/ServiceSpecBase.scala b/src/test/scala/gitbucket/core/service/ServiceSpecBase.scala index d2fb7c4..103372b 100644 --- a/src/test/scala/gitbucket/core/service/ServiceSpecBase.scala +++ b/src/test/scala/gitbucket/core/service/ServiceSpecBase.scala @@ -2,14 +2,18 @@ import gitbucket.core.servlet.AutoUpdate import gitbucket.core.util.{ControlUtil, DatabaseConfig, FileUtil} +import gitbucket.core.util.ControlUtil._ +import gitbucket.core.model._ import gitbucket.core.model.Profile._ import profile.simple._ -import ControlUtil._ -import java.sql.DriverManager + import org.apache.commons.io.FileUtils -import scala.util.Random + +import java.sql.DriverManager import java.io.File -import model._ + +import scala.util.Random + trait ServiceSpecBase { diff --git a/src/test/scala/gitbucket/core/service/WebHookServiceSpec.scala b/src/test/scala/gitbucket/core/service/WebHookServiceSpec.scala new file mode 100644 index 0000000..a3d3d34 --- /dev/null +++ b/src/test/scala/gitbucket/core/service/WebHookServiceSpec.scala @@ -0,0 +1,42 @@ +package gitbucket.core.service + +import org.specs2.mutable.Specification + + +class WebHookServiceSpec extends Specification with ServiceSpecBase { + lazy val service = new WebHookPullRequestService with AccountService with RepositoryService with PullRequestService with IssuesService + + "WebHookPullRequestService.getPullRequestsByRequestForWebhook" should { + "find from request branch" in { withTestDB { implicit session => + val user1 = generateNewUserWithDBRepository("user1","repo1") + val user2 = generateNewUserWithDBRepository("user2","repo2") + val user3 = generateNewUserWithDBRepository("user3","repo3") + val (issue1, pullreq1) = generateNewPullRequest("user1/repo1/master1", "user2/repo2/master2") + val (issue3, pullreq3) = generateNewPullRequest("user3/repo3/master3", "user2/repo2/master2") + val (issue32, pullreq32) = generateNewPullRequest("user3/repo3/master32", "user2/repo2/master2") + generateNewPullRequest("user2/repo2/master2", "user1/repo1/master2") + service.addWebHookURL("user1", "repo1", "webhook1-1") + service.addWebHookURL("user1", "repo1", "webhook1-2") + service.addWebHookURL("user2", "repo2", "webhook2-1") + service.addWebHookURL("user2", "repo2", "webhook2-2") + service.addWebHookURL("user3", "repo3", "webhook3-1") + service.addWebHookURL("user3", "repo3", "webhook3-2") + + service.getPullRequestsByRequestForWebhook("user1","repo1","master1") must_== Map.empty + + var r = service.getPullRequestsByRequestForWebhook("user2","repo2","master2").mapValues(_.map(_.url).toSet) + r.size must_== 3 + r((issue1, pullreq1, user1, user2)) must_== Set("webhook1-1","webhook1-2") + r((issue3, pullreq3, user3, user2)) must_== Set("webhook3-1","webhook3-2") + r((issue32, pullreq32, user3, user2)) must_== Set("webhook3-1","webhook3-2") + + // when closed, it not founds. + service.updateClosed("user1","repo1",issue1.issueId, true) + + var r2 = service.getPullRequestsByRequestForWebhook("user2","repo2","master2").mapValues(_.map(_.url).toSet) + r2.size must_== 2 + r2((issue3, pullreq3, user3, user2)) must_== Set("webhook3-1","webhook3-2") + r2((issue32, pullreq32, user3, user2)) must_== Set("webhook3-1","webhook3-2") + } } + } +} \ No newline at end of file diff --git a/src/test/scala/gitbucket/core/util/DirectorySpec.scala b/src/test/scala/gitbucket/core/util/DirectorySpec.scala new file mode 100644 index 0000000..a824fe8 --- /dev/null +++ b/src/test/scala/gitbucket/core/util/DirectorySpec.scala @@ -0,0 +1,15 @@ +package gitbucket.core.util + +import org.specs2.mutable._ + + +class DirectorySpec extends Specification { + "GitBucketHome" should { + "set under target in test scope" in { + Directory.GitBucketHome mustEqual new java.io.File("target/gitbucket_home_for_test").getAbsolutePath + } + "exists" in { + new java.io.File(Directory.GitBucketHome).exists + } + } +} diff --git a/src/test/scala/model/CommitStateSpec.scala b/src/test/scala/model/CommitStateSpec.scala deleted file mode 100644 index 231322f..0000000 --- a/src/test/scala/model/CommitStateSpec.scala +++ /dev/null @@ -1,25 +0,0 @@ -package model - -import org.specs2.mutable.Specification - -import CommitState._ - -class CommitStateSpec extends Specification { - "CommitState" should { - "combine empty must eq PENDING" in { - combine(Set()) must_== PENDING - } - "combine includes ERROR must eq FAILURE" in { - combine(Set(ERROR, SUCCESS, PENDING)) must_== FAILURE - } - "combine includes FAILURE must eq peinding" in { - combine(Set(FAILURE, SUCCESS, PENDING)) must_== FAILURE - } - "combine includes PENDING must eq peinding" in { - combine(Set(PENDING, SUCCESS)) must_== PENDING - } - "combine only SUCCESS must eq SUCCESS" in { - combine(Set(SUCCESS)) must_== SUCCESS - } - } -} diff --git a/src/test/scala/service/AccessTokenServiceSpec.scala b/src/test/scala/service/AccessTokenServiceSpec.scala deleted file mode 100644 index 7729158..0000000 --- a/src/test/scala/service/AccessTokenServiceSpec.scala +++ /dev/null @@ -1,88 +0,0 @@ -package service - -import org.specs2.mutable.Specification -import java.util.Date -import model._ - -class AccessTokenServiceSpec extends Specification with ServiceSpecBase { - - "AccessTokenService" should { - "generateAccessToken" in { withTestDB { implicit session => - AccessTokenService.generateAccessToken("root", "note") must be like{ - case (id, token) if id != 0 => ok - } - }} - - "getAccessTokens" in { withTestDB { implicit session => - val (id, token) = AccessTokenService.generateAccessToken("root", "note") - val tokenHash = AccessTokenService.tokenToHash(token) - - AccessTokenService.getAccessTokens("root") must be like{ - case List(model.AccessToken(`id`, "root", `tokenHash`, "note")) => ok - } - }} - - "getAccessTokens(root) get root's tokens" in { withTestDB { implicit session => - val (id, token) = AccessTokenService.generateAccessToken("root", "note") - val tokenHash = AccessTokenService.tokenToHash(token) - val user2 = generateNewAccount("user2") - AccessTokenService.generateAccessToken("user2", "note2") - - AccessTokenService.getAccessTokens("root") must be like{ - case List(model.AccessToken(`id`, "root", `tokenHash`, "note")) => ok - } - }} - - "deleteAccessToken" in { withTestDB { implicit session => - val (id, token) = AccessTokenService.generateAccessToken("root", "note") - val user2 = generateNewAccount("user2") - AccessTokenService.generateAccessToken("user2", "note2") - - AccessTokenService.deleteAccessToken("root", id) - - AccessTokenService.getAccessTokens("root") must beEmpty - }} - - "getAccountByAccessToken" in { withTestDB { implicit session => - val (id, token) = AccessTokenService.generateAccessToken("root", "note") - AccessTokenService.getAccountByAccessToken(token) must beSome.like { - case user => user.userName must_== "root" - } - }} - - "getAccountByAccessToken don't get removed account" in { withTestDB { implicit session => - val user2 = generateNewAccount("user2") - val (id, token) = AccessTokenService.generateAccessToken("user2", "note") - AccountService.updateAccount(user2.copy(isRemoved=true)) - - AccessTokenService.getAccountByAccessToken(token) must beEmpty - }} - - "generateAccessToken create uniq token" in { withTestDB { implicit session => - val tokenIt = List("token1","token1","token1","token2").iterator - val service = new AccessTokenService{ - override def makeAccessTokenString:String = tokenIt.next - } - - service.generateAccessToken("root", "note1") must like{ - case (_, "token1") => ok - } - service.generateAccessToken("root", "note2") must like{ - case (_, "token2") => ok - } - }} - - "when update Account.userName then AccessToken.userName changed" in { withTestDB { implicit session => - val user2 = generateNewAccount("user2") - val (id, token) = AccessTokenService.generateAccessToken("user2", "note") - import model.Profile._ - import profile.simple._ - Accounts.filter(_.userName === "user2".bind).map(_.userName).update("user3") - - AccessTokenService.getAccountByAccessToken(token) must beSome.like { - case user => user.userName must_== "user3" - } - }} - } -} - diff --git a/src/test/scala/service/CommitStateServiceSpec.scala b/src/test/scala/service/CommitStateServiceSpec.scala deleted file mode 100644 index 4983fc5..0000000 --- a/src/test/scala/service/CommitStateServiceSpec.scala +++ /dev/null @@ -1,73 +0,0 @@ -package service -import org.specs2.mutable.Specification -import java.util.Date -import model._ -import model.Profile._ -import profile.simple._ -class CommitStatusServiceSpec extends Specification with ServiceSpecBase with CommitStatusService - with RepositoryService with AccountService{ - val now = new java.util.Date() - val fixture1 = CommitStatus( - userName = "root", - repositoryName = "repo", - commitId = "0e97b8f59f7cdd709418bb59de53f741fd1c1bd7", - context = "jenkins/test", - creator = "tester", - state = CommitState.PENDING, - targetUrl = Some("http://example.com/target"), - description = Some("description"), - updatedDate = now, - registeredDate = now) - def findById(id: Int)(implicit s:Session) = CommitStatuses.filter(_.byPrimaryKey(id)).firstOption - def generateFixture1(tester:Account)(implicit s:Session) = createCommitStatus( - userName = fixture1.userName, - repositoryName = fixture1.repositoryName, - sha = fixture1.commitId, - context = fixture1.context, - state = fixture1.state, - targetUrl = fixture1.targetUrl, - description = fixture1.description, - creator = tester, - now = fixture1.registeredDate) - "CommitStatusService" should { - "createCommitState can insert and update" in { withTestDB { implicit session => - val tester = generateNewAccount(fixture1.creator) - createRepository(fixture1.repositoryName,fixture1.userName,None,false) - val id = generateFixture1(tester:Account) - getCommitStatus(fixture1.userName, fixture1.repositoryName, id) must_== - Some(fixture1.copy(commitStatusId=id)) - // other one can update - val tester2 = generateNewAccount("tester2") - val time2 = new java.util.Date(); - val id2 = createCommitStatus( - userName = fixture1.userName, - repositoryName = fixture1.repositoryName, - sha = fixture1.commitId, - context = fixture1.context, - state = CommitState.SUCCESS, - targetUrl = Some("http://example.com/target2"), - description = Some("description2"), - creator = tester2, - now = time2) - getCommitStatus(fixture1.userName, fixture1.repositoryName, id2) must_== Some(fixture1.copy( - commitStatusId = id, - creator = "tester2", - state = CommitState.SUCCESS, - targetUrl = Some("http://example.com/target2"), - description = Some("description2"), - updatedDate = time2)) - }} - "getCommitStatus can find by commitId and context" in { withTestDB { implicit session => - val tester = generateNewAccount(fixture1.creator) - createRepository(fixture1.repositoryName,fixture1.userName,None,false) - val id = generateFixture1(tester:Account) - getCommitStatus(fixture1.userName, fixture1.repositoryName, fixture1.commitId, fixture1.context) must_== Some(fixture1.copy(commitStatusId=id)) - }} - "getCommitStatus can find by commitStatusId" in { withTestDB { implicit session => - val tester = generateNewAccount(fixture1.creator) - createRepository(fixture1.repositoryName,fixture1.userName,None,false) - val id = generateFixture1(tester:Account) - getCommitStatus(fixture1.userName, fixture1.repositoryName, id) must_== Some(fixture1.copy(commitStatusId=id)) - }} - } -} \ No newline at end of file diff --git a/src/test/scala/service/IssuesServiceSpec.scala b/src/test/scala/service/IssuesServiceSpec.scala deleted file mode 100644 index 8105f06..0000000 --- a/src/test/scala/service/IssuesServiceSpec.scala +++ /dev/null @@ -1,47 +0,0 @@ -package service - -import org.specs2.mutable.Specification -import java.util.Date -import model._ -import service.IssuesService._ - -class IssuesServiceSpec extends Specification with ServiceSpecBase { - "IssuesService" should { - "getCommitStatues" in { withTestDB { implicit session => - val user1 = generateNewUserWithDBRepository("user1","repo1") - - def getCommitStatues = dummyService.getCommitStatues(List(("user1","repo1",1),("user1","repo1",2))) - - getCommitStatues must_== Map.empty - - val now = new java.util.Date() - val issueId = generateNewIssue("user1","repo1") - issueId must_== 1 - - getCommitStatues must_== Map.empty - - val cs = dummyService.createCommitStatus("user1","repo1","shasha", "default", CommitState.SUCCESS, Some("http://exmple.com/ci"), Some("exampleService"), now, user1) - - getCommitStatues must_== Map.empty - - val (is2, pr2) = generateNewPullRequest("user1/repo1/master","user1/repo1/feature1") - pr2.issueId must_== 2 - - // if there are no statuses, state is none - getCommitStatues must_== Map.empty - - // if there is a status, state is that - val cs2 = dummyService.createCommitStatus("user1","repo1","feature1", "default", CommitState.SUCCESS, Some("http://exmple.com/ci"), Some("exampleService"), now, user1) - getCommitStatues must_== Map(("user1","repo1",2) -> CommitStatusInfo(1,1,Some("default"),Some(CommitState.SUCCESS),Some("http://exmple.com/ci"),Some("exampleService"))) - - // if there are two statuses, state is none - val cs3 = dummyService.createCommitStatus("user1","repo1","feature1", "pend", CommitState.PENDING, Some("http://exmple.com/ci"), Some("exampleService"), now, user1) - getCommitStatues must_== Map(("user1","repo1",2) -> CommitStatusInfo(2,1,None,None,None,None)) - - // get only statuses in query issues - val (is3, pr3) = generateNewPullRequest("user1/repo1/master","user1/repo1/feature3") - val cs4 = dummyService.createCommitStatus("user1","repo1","feature3", "none", CommitState.PENDING, None, None, now, user1) - getCommitStatues must_== Map(("user1","repo1",2) -> CommitStatusInfo(2,1,None,None,None,None)) - } } - } -} \ No newline at end of file diff --git a/src/test/scala/service/MergeServiceSpec.scala b/src/test/scala/service/MergeServiceSpec.scala deleted file mode 100644 index 9f09304..0000000 --- a/src/test/scala/service/MergeServiceSpec.scala +++ /dev/null @@ -1,155 +0,0 @@ -package service -import org.specs2.mutable.Specification -import java.util.Date -import model._ -import util.JGitUtil -import util.Directory._ -import java.nio.file._ -import util.Implicits._ -import util.ControlUtil._ -import org.eclipse.jgit.api.Git -import org.eclipse.jgit.dircache.DirCache -import org.eclipse.jgit.lib._ -import org.eclipse.jgit.treewalk._ -import org.eclipse.jgit.revwalk._ -import org.apache.commons.io.FileUtils - -class MergeServiceSpec extends Specification { - sequential - val service = new MergeService{} - val branch = "master" - val issueId = 10 - def initRepository(owner:String, name:String) = { - val repo1Dir = getRepositoryDir(owner, name) - RepositoryCache.clear() - FileUtils.deleteQuietly(repo1Dir) - Files.createDirectories(repo1Dir.toPath()) - JGitUtil.initRepository(repo1Dir) - using(Git.open(repo1Dir)){ git => - createFile(git, s"refs/heads/master", "test.txt", "hoge" ) - git.branchCreate().setStartPoint(s"refs/heads/master").setName(s"refs/pull/${issueId}/head").call() - } - repo1Dir - } - def createFile(git:Git, branch:String, name:String, content:String){ - val builder = DirCache.newInCore.builder() - val inserter = git.getRepository.newObjectInserter() - val headId = git.getRepository.resolve(branch + "^{commit}") - builder.add(JGitUtil.createDirCacheEntry(name, FileMode.REGULAR_FILE, - inserter.insert(Constants.OBJ_BLOB, content.getBytes("UTF-8")))) - builder.finish() - JGitUtil.createNewCommit(git, inserter, headId, builder.getDirCache.writeTree(inserter), - branch, "dummy", "dummy@example.com", "Initial commit") - } - def getFile(git:Git, branch:String, path:String) = { - val revCommit = JGitUtil.getRevCommitFromId(git, git.getRepository.resolve(branch)) - val objectId = using(new TreeWalk(git.getRepository)){ walk => - walk.addTree(revCommit.getTree) - walk.setRecursive(true) - @scala.annotation.tailrec - def _getPathObjectId: ObjectId = walk.next match { - case true if(walk.getPathString == path) => walk.getObjectId(0) - case true => _getPathObjectId - case false => throw new Exception(s"not found ${branch} / ${path}") - } - _getPathObjectId - } - JGitUtil.getContentInfo(git, path, objectId) - } - def createConfrict(git:Git) = { - createFile(git, s"refs/heads/${branch}", "test.txt", "hoge2" ) - createFile(git, s"refs/pull/${issueId}/head", "test.txt", "hoge4" ) - } - "checkConflict, checkConflictCache" should { - "checkConflict false if not conflicted, and create cache" in { - val repo1Dir = initRepository("user1","repo1") - service.checkConflictCache("user1", "repo1", branch, issueId) mustEqual None - val conflicted = service.checkConflict("user1", "repo1", branch, issueId) - service.checkConflictCache("user1", "repo1", branch, issueId) mustEqual Some(false) - conflicted mustEqual false - } - "checkConflict true if not conflicted, and create cache" in { - val repo1Dir = initRepository("user1","repo1") - using(Git.open(repo1Dir)){ git => - createConfrict(git) - } - service.checkConflictCache("user1", "repo1", branch, issueId) mustEqual None - val conflicted = service.checkConflict("user1", "repo1", branch, issueId) - conflicted mustEqual true - service.checkConflictCache("user1", "repo1", branch, issueId) mustEqual Some(true) - } - } - "checkConflictCache" should { - "merged cache invalid if origin branch moved" in { - val repo1Dir = initRepository("user1","repo1") - service.checkConflict("user1", "repo1", branch, issueId) mustEqual false - service.checkConflictCache("user1", "repo1", branch, issueId) mustEqual Some(false) - using(Git.open(repo1Dir)){ git => - createFile(git, s"refs/heads/${branch}", "test.txt", "hoge2" ) - } - service.checkConflictCache("user1", "repo1", branch, issueId) mustEqual None - } - "merged cache invalid if request branch moved" in { - val repo1Dir = initRepository("user1","repo1") - service.checkConflict("user1", "repo1", branch, issueId) mustEqual false - service.checkConflictCache("user1", "repo1", branch, issueId) mustEqual Some(false) - using(Git.open(repo1Dir)){ git => - createFile(git, s"refs/pull/${issueId}/head", "test.txt", "hoge4" ) - } - service.checkConflictCache("user1", "repo1", branch, issueId) mustEqual None - } - "merged cache invalid if origin branch moved" in { - val repo1Dir = initRepository("user1","repo1") - service.checkConflict("user1", "repo1", branch, issueId) mustEqual false - service.checkConflictCache("user1", "repo1", branch, issueId) mustEqual Some(false) - using(Git.open(repo1Dir)){ git => - createFile(git, s"refs/heads/${branch}", "test.txt", "hoge2" ) - } - service.checkConflictCache("user1", "repo1", branch, issueId) mustEqual None - } - "conflicted cache invalid if request branch moved" in { - val repo1Dir = initRepository("user1","repo1") - using(Git.open(repo1Dir)){ git => - createConfrict(git) - } - service.checkConflict("user1", "repo1", branch, issueId) mustEqual true - service.checkConflictCache("user1", "repo1", branch, issueId) mustEqual Some(true) - using(Git.open(repo1Dir)){ git => - createFile(git, s"refs/pull/${issueId}/head", "test.txt", "hoge4" ) - } - service.checkConflictCache("user1", "repo1", branch, issueId) mustEqual None - } - "conflicted cache invalid if origin branch moved" in { - val repo1Dir = initRepository("user1","repo1") - using(Git.open(repo1Dir)){ git => - createConfrict(git) - } - service.checkConflict("user1", "repo1", branch, issueId) mustEqual true - service.checkConflictCache("user1", "repo1", branch, issueId) mustEqual Some(true) - using(Git.open(repo1Dir)){ git => - createFile(git, s"refs/heads/${branch}", "test.txt", "hoge4" ) - } - service.checkConflictCache("user1", "repo1", branch, issueId) mustEqual None - } - } - "mergePullRequest" should { - "can merge" in { - val repo1Dir = initRepository("user1","repo1") - using(Git.open(repo1Dir)){ git => - createFile(git, s"refs/pull/${issueId}/head", "test.txt", "hoge2" ) - val committer = new PersonIdent("dummy2", "dummy2@example.com") - getFile(git, branch, "test.txt").content.get mustEqual "hoge" - val requestBranchId = git.getRepository.resolve(s"refs/pull/${issueId}/head") - val masterId = git.getRepository.resolve(branch) - service.mergePullRequest(git, branch, issueId, "merged", committer) - val lastCommitId = git.getRepository.resolve(branch); - val commit = using(new RevWalk(git.getRepository))(_.parseCommit(lastCommitId)) - commit.getCommitterIdent() mustEqual committer - commit.getAuthorIdent() mustEqual committer - commit.getFullMessage() mustEqual "merged" - commit.getParents.toSet mustEqual Set( requestBranchId, masterId ) - getFile(git, branch, "test.txt").content.get mustEqual "hoge2" - } - } - } -} \ No newline at end of file diff --git a/src/test/scala/service/RepositoryServiceSpec.scala b/src/test/scala/service/RepositoryServiceSpec.scala deleted file mode 100644 index 23a2215..0000000 --- a/src/test/scala/service/RepositoryServiceSpec.scala +++ /dev/null @@ -1,33 +0,0 @@ -package service -import org.specs2.mutable.Specification -import java.util.Date -import model._ -import model.Profile._ -import profile.simple._ -class RepositoryServiceSpec extends Specification with ServiceSpecBase with RepositoryService with AccountService{ - "RepositoryService" should { - "renameRepository can rename CommitState" in { withTestDB { implicit session => - val tester = generateNewAccount("tester") - createRepository("repo","root",None,false) - val commitStatusService = new CommitStatusService{} - val id = commitStatusService.createCommitStatus( - userName = "root", - repositoryName = "repo", - sha = "0e97b8f59f7cdd709418bb59de53f741fd1c1bd7", - context = "jenkins/test", - state = CommitState.PENDING, - targetUrl = Some("http://example.com/target"), - description = Some("description"), - creator = tester, - now = new java.util.Date) - val org = commitStatusService.getCommitStatus("root","repo", id).get - renameRepository("root","repo","tester","repo2") - val neo = commitStatusService.getCommitStatus("tester","repo2", org.commitId, org.context).get - neo must_== - org.copy( - commitStatusId=neo.commitStatusId, - repositoryName="repo2", - userName="tester") - }} - } -} diff --git a/src/test/scala/service/WebHookServiceSpec.scala b/src/test/scala/service/WebHookServiceSpec.scala deleted file mode 100644 index 13c587c..0000000 --- a/src/test/scala/service/WebHookServiceSpec.scala +++ /dev/null @@ -1,43 +0,0 @@ -package service - -import org.specs2.mutable.Specification -import java.util.Date -import model._ - -class WebHookServiceSpec extends Specification with ServiceSpecBase { - lazy val service = new WebHookPullRequestService with AccountService with RepositoryService with PullRequestService with IssuesService - - "WebHookPullRequestService.getPullRequestsByRequestForWebhook" should { - "find from request branch" in { withTestDB { implicit session => - val user1 = generateNewUserWithDBRepository("user1","repo1") - val user2 = generateNewUserWithDBRepository("user2","repo2") - val user3 = generateNewUserWithDBRepository("user3","repo3") - val (issue1, pullreq1) = generateNewPullRequest("user1/repo1/master1", "user2/repo2/master2") - val (issue3, pullreq3) = generateNewPullRequest("user3/repo3/master3", "user2/repo2/master2") - val (issue32, pullreq32) = generateNewPullRequest("user3/repo3/master32", "user2/repo2/master2") - generateNewPullRequest("user2/repo2/master2", "user1/repo1/master2") - service.addWebHookURL("user1", "repo1", "webhook1-1") - service.addWebHookURL("user1", "repo1", "webhook1-2") - service.addWebHookURL("user2", "repo2", "webhook2-1") - service.addWebHookURL("user2", "repo2", "webhook2-2") - service.addWebHookURL("user3", "repo3", "webhook3-1") - service.addWebHookURL("user3", "repo3", "webhook3-2") - - service.getPullRequestsByRequestForWebhook("user1","repo1","master1") must_== Map.empty - - var r = service.getPullRequestsByRequestForWebhook("user2","repo2","master2").mapValues(_.map(_.url).toSet) - r.size must_== 3 - r((issue1, pullreq1, user1, user2)) must_== Set("webhook1-1","webhook1-2") - r((issue3, pullreq3, user3, user2)) must_== Set("webhook3-1","webhook3-2") - r((issue32, pullreq32, user3, user2)) must_== Set("webhook3-1","webhook3-2") - - // when closed, it not founds. - service.updateClosed("user1","repo1",issue1.issueId, true) - - var r2 = service.getPullRequestsByRequestForWebhook("user2","repo2","master2").mapValues(_.map(_.url).toSet) - r2.size must_== 2 - r2((issue3, pullreq3, user3, user2)) must_== Set("webhook3-1","webhook3-2") - r2((issue32, pullreq32, user3, user2)) must_== Set("webhook3-1","webhook3-2") - } } - } -} \ No newline at end of file diff --git a/src/test/scala/util/DirectorySpec.scala b/src/test/scala/util/DirectorySpec.scala deleted file mode 100644 index c40aafe..0000000 --- a/src/test/scala/util/DirectorySpec.scala +++ /dev/null @@ -1,14 +0,0 @@ -package util - -import org.specs2.mutable._ - -class DirectorySpec extends Specification { - "GitBucketHome" should { - "set under target in test scope" in { - Directory.GitBucketHome mustEqual new java.io.File("target/gitbucket_home_for_test").getAbsolutePath - } - "exists" in { - new java.io.File(Directory.GitBucketHome).exists - } - } -}