Fix test code package name

This commit is contained in:
Tomofumi Tanaka
2015-03-16 23:38:00 +09:00
parent 825f2518e9
commit 7e92f1abd5
11 changed files with 71 additions and 43 deletions

View File

@@ -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"
}
}}
}
}

View File

@@ -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))
}}
}
}

View File

@@ -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))
} }
}
}

View File

@@ -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"
}
}
}
}

View File

@@ -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")
}}
}
}

View File

@@ -2,14 +2,18 @@ package gitbucket.core.service
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 {

View File

@@ -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")
} }
}
}