mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-08 22:45:51 +01:00
Change package name
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
package gitbucket.core.service
|
||||
|
||||
import gitbucket.core.model.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(model.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
|
||||
}}
|
||||
}
|
||||
}
|
||||
|
||||
28
src/test/scala/gitbucket/core/service/ServiceSpecBase.scala
Normal file
28
src/test/scala/gitbucket/core/service/ServiceSpecBase.scala
Normal file
@@ -0,0 +1,28 @@
|
||||
package gitbucket.core.service
|
||||
|
||||
import gitbucket.core.model.Profile
|
||||
import gitbucket.core.servlet.AutoUpdate
|
||||
import gitbucket.core.util.{ControlUtil, DatabaseConfig, FileUtil}
|
||||
import profile.simple._
|
||||
import ControlUtil._
|
||||
import java.sql.DriverManager
|
||||
import org.apache.commons.io.FileUtils
|
||||
import scala.util.Random
|
||||
import java.io.File
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
40
src/test/scala/gitbucket/core/ssh/GitCommandSpec.scala
Normal file
40
src/test/scala/gitbucket/core/ssh/GitCommandSpec.scala
Normal 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
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
56
src/test/scala/gitbucket/core/util/StringUtilSpec.scala
Normal file
56
src/test/scala/gitbucket/core/util/StringUtilSpec.scala
Normal 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 "<a href="/test">a & b</a>"
|
||||
}
|
||||
}
|
||||
|
||||
"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
|
||||
}
|
||||
}
|
||||
}
|
||||
36
src/test/scala/gitbucket/core/util/ValidationsSpec.scala
Normal file
36
src/test/scala/gitbucket/core/util/ValidationsSpec.scala
Normal 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"
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user