Merge remote-tracking branch 'origin/master' into api-support

Conflicts:
	src/main/scala/ScalatraBootstrap.scala
	src/main/scala/gitbucket/core/controller/AccountController.scala
	src/main/scala/gitbucket/core/controller/ControllerBase.scala
	src/main/scala/gitbucket/core/controller/IssuesController.scala
	src/main/scala/gitbucket/core/controller/PullRequestsController.scala
	src/main/scala/gitbucket/core/controller/RepositorySettingsController.scala
	src/main/scala/gitbucket/core/controller/RepositoryViewerController.scala
	src/main/scala/gitbucket/core/model/Profile.scala
	src/main/scala/gitbucket/core/service/PullRequestService.scala
	src/main/scala/gitbucket/core/service/WebHookService.scala
	src/main/scala/gitbucket/core/servlet/InitializeListener.scala
	src/main/scala/gitbucket/core/view/helpers.scala
	src/main/twirl/gitbucket/core/pulls/conversation.scala.html
	src/main/twirl/gitbucket/core/pulls/mergeguide.scala.html
	src/main/twirl/issues/listparts.scala.html
This commit is contained in:
Tomofumi Tanaka
2015-03-16 22:49:47 +09:00
215 changed files with 1127 additions and 916 deletions

View File

@@ -0,0 +1,79 @@
package gitbucket.core.service
import gitbucket.core.model.{Account, GroupMember}
import org.specs2.mutable.Specification
import java.util.Date
class AccountServiceSpec extends Specification with ServiceSpecBase {
"AccountService" should {
val RootMailAddress = "root@localhost"
"getAllUsers" in { withTestDB { implicit session =>
AccountService.getAllUsers() must be like{
case List(Account("root", "root", RootMailAddress, _, true, _, _, _, None, None, false, false)) => ok
}
}}
"getAccountByUserName" in { withTestDB { implicit session =>
AccountService.getAccountByUserName("root") must beSome.like {
case user => user.userName must_== "root"
}
AccountService.getAccountByUserName("invalid user name") must beNone
}}
"getAccountByMailAddress" in { withTestDB { implicit session =>
AccountService.getAccountByMailAddress(RootMailAddress) must beSome
}}
"updateLastLoginDate" in { withTestDB { implicit session =>
val root = "root"
def user() =
AccountService.getAccountByUserName(root).getOrElse(sys.error(s"user $root does not exists"))
user().lastLoginDate must beNone
val date1 = new Date
AccountService.updateLastLoginDate(root)
user().lastLoginDate must beSome.like{ case date =>
date must be_>(date1)
}
val date2 = new Date
Thread.sleep(1000)
AccountService.updateLastLoginDate(root)
user().lastLoginDate must beSome.like{ case date =>
date must be_>(date2)
}
}}
"updateAccount" in { withTestDB { implicit session =>
val root = "root"
def user() =
AccountService.getAccountByUserName(root).getOrElse(sys.error(s"user $root does not exists"))
val newAddress = "new mail address"
AccountService.updateAccount(user().copy(mailAddress = newAddress))
user().mailAddress must_== newAddress
}}
"group" in { withTestDB { implicit session =>
val group1 = "group1"
val user1 = "root"
AccountService.createGroup(group1, None)
AccountService.getGroupMembers(group1) must_== Nil
AccountService.getGroupsByUserName(user1) must_== Nil
AccountService.updateGroupMembers(group1, List((user1, true)))
AccountService.getGroupMembers(group1) must_== List(GroupMember(group1, user1, true))
AccountService.getGroupsByUserName(user1) must_== List(group1)
AccountService.updateGroupMembers(group1, Nil)
AccountService.getGroupMembers(group1) must_== Nil
AccountService.getGroupsByUserName(user1) must_== Nil
}}
}
}

View File

@@ -0,0 +1,71 @@
package gitbucket.core.service
import gitbucket.core.servlet.AutoUpdate
import gitbucket.core.util.{ControlUtil, DatabaseConfig, FileUtil}
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.io.File
import model._
trait ServiceSpecBase {
def withTestDB[A](action: (Session) => A): A = {
FileUtil.withTmpDir(new File(FileUtils.getTempDirectory(), Random.alphanumeric.take(10).mkString)){ dir =>
val (url, user, pass) = (DatabaseConfig.url(Some(dir.toString)), DatabaseConfig.user, DatabaseConfig.password)
org.h2.Driver.load()
using(DriverManager.getConnection(url, user, pass)){ conn =>
AutoUpdate.versions.reverse.foreach(_.update(conn, Thread.currentThread.getContextClassLoader))
}
Database.forURL(url, user, pass).withSession { session =>
action(session)
}
}
}
def generateNewAccount(name:String)(implicit s:Session):Account = {
AccountService.createAccount(name, name, name, s"${name}@example.com", false, None)
AccountService.getAccountByUserName(name).get
}
lazy val dummyService = new RepositoryService with AccountService with IssuesService with PullRequestService
with CommitStatusService (){}
def generateNewUserWithDBRepository(userName:String, repositoryName:String)(implicit s:Session):Account = {
val ac = generateNewAccount(userName)
dummyService.createRepository(repositoryName, userName, None, false)
ac
}
def generateNewIssue(userName:String, repositoryName:String, requestUserName:String="root")(implicit s:Session): Int = {
dummyService.createIssue(
owner = userName,
repository = repositoryName,
loginUser = requestUserName,
title = "issue title",
content = None,
assignedUserName = None,
milestoneId = None,
isPullRequest = true)
}
def generateNewPullRequest(base:String, request:String)(implicit s:Session):(Issue, PullRequest) = {
val Array(baseUserName, baseRepositoryName, baesBranch)=base.split("/")
val Array(requestUserName, requestRepositoryName, requestBranch)=request.split("/")
val issueId = generateNewIssue(baseUserName, baseRepositoryName, requestUserName)
dummyService.createPullRequest(
originUserName = baseUserName,
originRepositoryName = baseRepositoryName,
issueId = issueId,
originBranch = baesBranch,
requestUserName = requestUserName,
requestRepositoryName = requestRepositoryName,
requestBranch = requestBranch,
commitIdFrom = baesBranch,
commitIdTo = requestBranch)
dummyService.getPullRequest(baseUserName, baseRepositoryName, issueId).get
}
}

View File

@@ -0,0 +1,40 @@
package gitbucket.core.ssh
import org.specs2.mutable._
import org.specs2.mock.Mockito
import org.apache.sshd.server.command.UnknownCommand
import javax.servlet.ServletContext
class GitCommandFactorySpec extends Specification with Mockito {
val factory = new GitCommandFactory("http://localhost:8080")
"createCommand" should {
"returns GitReceivePack when command is git-receive-pack" in {
factory.createCommand("git-receive-pack '/owner/repo.git'").isInstanceOf[GitReceivePack] must beTrue
factory.createCommand("git-receive-pack '/owner/repo.wiki.git'").isInstanceOf[GitReceivePack] must beTrue
}
"returns GitUploadPack when command is git-upload-pack" in {
factory.createCommand("git-upload-pack '/owner/repo.git'").isInstanceOf[GitUploadPack] must beTrue
factory.createCommand("git-upload-pack '/owner/repo.wiki.git'").isInstanceOf[GitUploadPack] must beTrue
}
"returns UnknownCommand when command is not git-(upload|receive)-pack" in {
factory.createCommand("git- '/owner/repo.git'").isInstanceOf[UnknownCommand] must beTrue
factory.createCommand("git-pack '/owner/repo.git'").isInstanceOf[UnknownCommand] must beTrue
factory.createCommand("git-a-pack '/owner/repo.git'").isInstanceOf[UnknownCommand] must beTrue
factory.createCommand("git-up-pack '/owner/repo.git'").isInstanceOf[UnknownCommand] must beTrue
factory.createCommand("\ngit-upload-pack '/owner/repo.git'").isInstanceOf[UnknownCommand] must beTrue
}
"returns UnknownCommand when git command has no valid arguments" in {
// must be: git-upload-pack '/owner/repository_name.git'
factory.createCommand("git-upload-pack").isInstanceOf[UnknownCommand] must beTrue
factory.createCommand("git-upload-pack /owner/repo.git").isInstanceOf[UnknownCommand] must beTrue
factory.createCommand("git-upload-pack 'owner/repo.git'").isInstanceOf[UnknownCommand] must beTrue
factory.createCommand("git-upload-pack '/ownerrepo.git'").isInstanceOf[UnknownCommand] must beTrue
factory.createCommand("git-upload-pack '/owner/repo.wiki'").isInstanceOf[UnknownCommand] must beTrue
}
}
}

View File

@@ -0,0 +1,56 @@
package gitbucket.core.util
import org.specs2.mutable._
class StringUtilSpec extends Specification {
"urlDecode" should {
"decode encoded string to original string" in {
val encoded = StringUtil.urlEncode("あいうえお")
StringUtil.urlDecode(encoded) mustEqual "あいうえお"
}
}
"splitWords" should {
"split string by whitespaces" in {
val split = StringUtil.splitWords("aa bb\tcc dd \t ee")
split mustEqual Array("aa", "bb", "cc", "dd", "ee")
}
}
"escapeHtml" should {
"escape &, <, > and \"" in {
StringUtil.escapeHtml("<a href=\"/test\">a & b</a>") mustEqual "&lt;a href=&quot;/test&quot;&gt;a &amp; b&lt;/a&gt;"
}
}
"md5" should {
"generate MD5 hash" in {
StringUtil.md5("abc") mustEqual "900150983cd24fb0d6963f7d28e17f72"
}
}
"sha1" should {
"generate SHA1 hash" in {
StringUtil.sha1("abc") mustEqual "a9993e364706816aba3e25717850c26c9cd0d89d"
}
}
"extractIssueId" should {
"extract '#xxx' and return extracted id" in {
StringUtil.extractIssueId("(refs #123)").toSeq mustEqual Seq("123")
}
"returns Nil from message which does not contain #xxx" in {
StringUtil.extractIssueId("this is test!").toSeq mustEqual Nil
}
}
"extractCloseId" should {
"extract 'close #xxx' and return extracted id" in {
StringUtil.extractCloseId("(close #123)").toSeq mustEqual Seq("123")
}
"returns Nil from message which does not contain close command" in {
StringUtil.extractCloseId("(refs #123)").toSeq mustEqual Nil
}
}
}

View File

@@ -0,0 +1,36 @@
package gitbucket.core.util
import org.specs2.mutable._
import org.scalatra.i18n.Messages
class ValidationsSpec extends Specification with Validations {
"identifier" should {
"validate id string " in {
identifier.validate("id", "aa_ZZ-00.01", null) mustEqual None
identifier.validate("id", "_aaaa", null) mustEqual Some("id starts with invalid character.")
identifier.validate("id", "-aaaa", null) mustEqual Some("id starts with invalid character.")
identifier.validate("id", "aa_ZZ#01", null) mustEqual Some("id contains invalid character.")
}
}
"color" should {
"validate color string " in {
val messages = Messages()
color.validate("color", "#88aaff", messages) mustEqual None
color.validate("color", "#gghhii", messages) mustEqual Some("color must be '#[0-9a-fA-F]{6}'.")
}
}
"date" should {
// "validate date string " in {
// date().validate("date", "2013-10-05", Map[String, String]()) mustEqual Nil
// date().validate("date", "2013-10-5" , Map[String, String]()) mustEqual List(("date", "date must be '\\d{4}-\\d{2}-\\d{2}'."))
// }
"convert date string " in {
val result = date().convert("2013-10-05", null)
new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(result) mustEqual "2013-10-05 00:00:00"
}
}
}

View File

@@ -0,0 +1,121 @@
package gitbucket.core.view
import java.util.Date
import gitbucket.core.model.Account
import gitbucket.core.service.{SystemSettingsService, RequestCache}
import gitbucket.core.controller.Context
import org.specs2.mutable._
import org.specs2.mock.Mockito
import SystemSettingsService.SystemSettings
import javax.servlet.http.HttpServletRequest
import play.twirl.api.Html
class AvatarImageProviderSpec extends Specification with Mockito {
val request = mock[HttpServletRequest]
request.getRequestURL returns new StringBuffer("http://localhost:8080/path.html")
request.getRequestURI returns "/path.html"
request.getContextPath returns ""
"getAvatarImageHtml" should {
"show Gravatar image for no image account if gravatar integration is enabled" in {
implicit val context = Context(createSystemSettings(true), None, request)
val provider = new AvatarImageProviderImpl(Some(createAccount(None)))
provider.toHtml("user", 32).toString mustEqual
"<img src=\"https://www.gravatar.com/avatar/d41d8cd98f00b204e9800998ecf8427e?s=32&d=retro&r=g\" class=\"avatar\" style=\"width: 32px; height: 32px;\" />"
}
"show uploaded image even if gravatar integration is enabled" in {
implicit val context = Context(createSystemSettings(true), None, request)
val provider = new AvatarImageProviderImpl(Some(createAccount(Some("icon.png"))))
provider.toHtml("user", 32).toString mustEqual
"<img src=\"/user/_avatar\" class=\"avatar\" style=\"width: 32px; height: 32px;\" />"
}
"show local image for no image account if gravatar integration is disabled" in {
implicit val context = Context(createSystemSettings(false), None, request)
val provider = new AvatarImageProviderImpl(Some(createAccount(None)))
provider.toHtml("user", 32).toString mustEqual
"<img src=\"/user/_avatar\" class=\"avatar\" style=\"width: 32px; height: 32px;\" />"
}
"show Gravatar image for specified mail address if gravatar integration is enabled" in {
implicit val context = Context(createSystemSettings(true), None, request)
val provider = new AvatarImageProviderImpl(None)
provider.toHtml("user", 20, "hoge@hoge.com").toString mustEqual
"<img src=\"https://www.gravatar.com/avatar/4712f9b0e63f56ad952ad387eaa23b9c?s=20&d=retro&r=g\" class=\"avatar-mini\" style=\"width: 20px; height: 20px;\" />"
}
"show unknown image for unknown user if gravatar integration is enabled" in {
implicit val context = Context(createSystemSettings(true), None, request)
val provider = new AvatarImageProviderImpl(None)
provider.toHtml("user", 20).toString mustEqual
"<img src=\"/_unknown/_avatar\" class=\"avatar-mini\" style=\"width: 20px; height: 20px;\" />"
}
"show unknown image for specified mail address if gravatar integration is disabled" in {
implicit val context = Context(createSystemSettings(false), None, request)
val provider = new AvatarImageProviderImpl(None)
provider.toHtml("user", 20, "hoge@hoge.com").toString mustEqual
"<img src=\"/_unknown/_avatar\" class=\"avatar-mini\" style=\"width: 20px; height: 20px;\" />"
}
"add tooltip if it's enabled" in {
implicit val context = Context(createSystemSettings(false), None, request)
val provider = new AvatarImageProviderImpl(None)
provider.toHtml("user", 20, "hoge@hoge.com", true).toString mustEqual
"<img src=\"/_unknown/_avatar\" class=\"avatar-mini\" style=\"width: 20px; height: 20px;\" data-toggle=\"tooltip\" title=\"user\"/>"
}
}
private def createAccount(image: Option[String]) =
Account(
userName = "user",
fullName = "user@localhost",
mailAddress = "",
password = "",
isAdmin = false,
url = None,
registeredDate = new Date(),
updatedDate = new Date(),
lastLoginDate = None,
image = image,
isGroupAccount = false,
isRemoved = false)
private def createSystemSettings(useGravatar: Boolean) =
SystemSettings(
baseUrl = None,
information = None,
allowAccountRegistration = false,
allowAnonymousAccess = true,
isCreateRepoOptionPublic = true,
gravatar = useGravatar,
notification = false,
ssh = false,
sshPort = None,
smtp = None,
ldapAuthentication = false,
ldap = None)
/**
* Adapter to test AvatarImageProviderImpl.
*/
class AvatarImageProviderImpl(account: Option[Account]) extends AvatarImageProvider with RequestCache {
def toHtml(userName: String, size: Int, mailAddress: String = "", tooltip: Boolean = false)
(implicit context: Context): Html = getAvatarImageHtml(userName, size, mailAddress, tooltip)
override def getAccountByMailAddress(mailAddress: String)(implicit context: Context): Option[Account] = account
override def getAccountByUserName(userName: String)(implicit context: Context): Option[Account] = account
}
}

View File

@@ -0,0 +1,93 @@
package gitbucket.core.view
import org.specs2.mutable._
class GitBucketHtmlSerializerSpec extends Specification {
import GitBucketHtmlSerializer._
"generateAnchorName" should {
"convert whitespace characters to hyphens" in {
val before = "foo bar baz"
val after = generateAnchorName(before)
after mustEqual "foo-bar-baz"
}
"normalize characters with diacritics" in {
val before = "Dónde estará mi vida"
val after = generateAnchorName(before)
after mustEqual "do%cc%81nde-estara%cc%81-mi-vida"
}
"omit special characters" in {
val before = "foo!bar@baz>9000"
val after = generateAnchorName(before)
after mustEqual "foo%21bar%40baz%3e9000"
}
}
"escapeTaskList" should {
"convert '- [ ] ' to '* task: :'" in {
val before = "- [ ] aaaa"
val after = escapeTaskList(before)
after mustEqual "* task: : aaaa"
}
"convert ' - [ ] ' to ' * task: :'" in {
val before = " - [ ] aaaa"
val after = escapeTaskList(before)
after mustEqual " * task: : aaaa"
}
"convert only first '- [ ] '" in {
val before = " - [ ] aaaa - [ ] bbb"
val after = escapeTaskList(before)
after mustEqual " * task: : aaaa - [ ] bbb"
}
"convert '- [x] ' to '* task:x:'" in {
val before = " - [x] aaaa"
val after = escapeTaskList(before)
after mustEqual " * task:x: aaaa"
}
"convert multi lines" in {
val before = """
tasks
- [x] aaaa
- [ ] bbb
"""
val after = escapeTaskList(before)
after mustEqual """
tasks
* task:x: aaaa
* task: : bbb
"""
}
"no convert if inserted before '- [ ] '" in {
val before = " a - [ ] aaaa"
val after = escapeTaskList(before)
after mustEqual " a - [ ] aaaa"
}
"no convert '- [] '" in {
val before = " - [] aaaa"
val after = escapeTaskList(before)
after mustEqual " - [] aaaa"
}
"no convert '- [ ]a'" in {
val before = " - [ ]a aaaa"
val after = escapeTaskList(before)
after mustEqual " - [ ]a aaaa"
}
"no convert '-[ ] '" in {
val before = " -[ ] aaaa"
val after = escapeTaskList(before)
after mustEqual " -[ ] aaaa"
}
}
}

View File

@@ -0,0 +1,70 @@
package gitbucket.core.view
import gitbucket.core.util.ControlUtil
import org.specs2.mutable._
import ControlUtil._
class PaginationSpec extends Specification {
"max" should {
"return max page number" in {
val pagination = Pagination(1, 100, 10, 6)
pagination.max mustEqual 10
}
}
"omitLeft and omitRight" should {
"return true if pagination links at their side will be omitted" in {
defining(Pagination(1, 100, 10, 6)){ pagination =>
pagination.omitLeft mustEqual false
pagination.omitRight mustEqual true
}
defining(Pagination(9, 100, 10, 6)){ pagination =>
pagination.omitLeft mustEqual true
pagination.omitRight mustEqual false
}
}
}
"visibleFor" should {
"return true for visible pagination links" in {
defining(Pagination(1, 100, 10, 6)){ pagination =>
pagination.visibleFor(1) mustEqual true
pagination.visibleFor(2) mustEqual true
pagination.visibleFor(3) mustEqual true
pagination.visibleFor(4) mustEqual true
pagination.visibleFor(5) mustEqual true
pagination.visibleFor(6) mustEqual false
pagination.visibleFor(7) mustEqual false
pagination.visibleFor(8) mustEqual false
pagination.visibleFor(9) mustEqual false
pagination.visibleFor(10) mustEqual true
}
defining(Pagination(5, 100, 10, 6)){ pagination =>
pagination.visibleFor(1) mustEqual true
pagination.visibleFor(2) mustEqual false
pagination.visibleFor(3) mustEqual false
pagination.visibleFor(4) mustEqual true
pagination.visibleFor(5) mustEqual true
pagination.visibleFor(6) mustEqual true
pagination.visibleFor(7) mustEqual false
pagination.visibleFor(8) mustEqual false
pagination.visibleFor(9) mustEqual false
pagination.visibleFor(10) mustEqual true
}
defining(Pagination(8, 100, 10, 6)){ pagination =>
pagination.visibleFor(1) mustEqual true
pagination.visibleFor(2) mustEqual false
pagination.visibleFor(3) mustEqual false
pagination.visibleFor(4) mustEqual false
pagination.visibleFor(5) mustEqual false
pagination.visibleFor(6) mustEqual true
pagination.visibleFor(7) mustEqual true
pagination.visibleFor(8) mustEqual true
pagination.visibleFor(9) mustEqual true
pagination.visibleFor(10) mustEqual true
}
}
}
}