Skip to content

Instantly share code, notes, and snippets.

@asm0dey
Created December 6, 2019 05:18
Show Gist options
  • Save asm0dey/f6c4597ea6ea21a00a5d08bd8e4f8cc0 to your computer and use it in GitHub Desktop.
Save asm0dey/f6c4597ea6ea21a00a5d08bd8e4f8cc0 to your computer and use it in GitHub Desktop.
Tests for Kotlintest demo
package org.ort.school.app.service
import com.winterbe.expekt.should
import io.kotlintest.shouldThrow
import io.kotlintest.specs.ShouldSpec
import io.mockk.every
import io.mockk.mockk
import org.ort.school.app.repo.UserRepo
import org.pac4j.core.credentials.UsernamePasswordCredentials
import org.pac4j.core.exception.CredentialsException
class DBAuthKTest : ShouldSpec({
val userRepo = mockk<UserRepo>()
val passwordService = mockk<PasswordService>()
val auth = DBAuth(userRepo, passwordService)
"auth username"{
should("not be null") {
shouldThrow<CredentialsException> { auth.validate(UsernamePasswordCredentials(null, null, null), mockk()) }
.message.should.equal("Username and password cannot be blank")
}
should("not be empty") {
shouldThrow<CredentialsException> { auth.validate(UsernamePasswordCredentials("", null, null), mockk()) }
.message.should.equal("Username and password cannot be blank")
}
}
"auth password"{
should("not be null") {
shouldThrow<CredentialsException> { auth.validate(UsernamePasswordCredentials(" ", null, null), mockk()) }
.message.should.equal("Username and password cannot be blank")
}
should("not be empty") {
shouldThrow<CredentialsException> { auth.validate(UsernamePasswordCredentials(" ", "", null), mockk()) }
.message.should.equal("Username and password cannot be blank")
}
}
"when user not found"{
should("throw exception") {
every { userRepo.userRecordBy("test") } returns mockk {
every { password } returns "123"
}
shouldThrow<CredentialsException> { auth.validate(UsernamePasswordCredentials("test", "test", null), mockk()) }
.message.should.equal("Username or password invalid")
}
}
})
package org.ort.school.app
import com.github.kittinunf.fuel.httpGet
import com.winterbe.expekt.should
import io.kotlintest.Spec
import io.kotlintest.shouldBe
import io.kotlintest.specs.BehaviorSpec
import org.testcontainers.containers.PostgreSQLContainer
import java.net.ServerSocket
class KPostgreSQLContainer : PostgreSQLContainer<KPostgreSQLContainer>()
abstract class JoobyIntegrationSpec(body: JoobyIntegrationSpec.(port: Int) -> Unit = {}) : BehaviorSpec() {
private val postgres = KPostgreSQLContainer().apply { start() }
val httpPort = ServerSocket(0).use { it.localPort }
private val httpsPort = ServerSocket(0).use { it.localPort }
private val app = SchoolCRM()
override fun beforeSpec(spec: Spec) {
super.beforeSpec(spec)
app.start(
"server.join=false",
"application.port=$httpPort",
"application.securePort=$httpsPort",
"db.url=${postgres.jdbcUrl.replace("\\?.*".toRegex(), "")}",
"db.user=${postgres.username}",
"db.password=${postgres.password}"
)
}
override fun afterSpec(spec: Spec) {
super.afterSpec(spec)
app.stop()
}
init {
body(httpPort)
}
}
class JoobyFirstIT : JoobyIntegrationSpec({ port ->
given("We launch our application"){
`when`("we enter main page"){
val response = "httpL//localhost:$port".httpGet()
.request
.response()
then("We get 200 status code"){
response.second.statusCode shouldBe 200
}
}
}
})
package org.ort.school.app.service
import com.sangupta.murmur.Murmur2
import io.kotlintest.data.forall
import io.kotlintest.properties.Gen
import io.kotlintest.properties.assertAll
import io.kotlintest.properties.shrinking.Shrinker
import io.kotlintest.properties.shrinking.StringShrinker
import io.kotlintest.shouldBe
import io.kotlintest.specs.ShouldSpec
import io.kotlintest.tables.row
import kotlin.random.Random
class MurMurTest : ShouldSpec({
"our murmur"{
should("match reference murmur") {
forall(
row("a"),
row("test"),
row("longstring")) {
MurMur2Hash.evaluate(it) shouldBe Murmur2.hash(it.toByteArray(), it.length, 0)
}
assertAll(Gen.wideString()) { input: String ->
MurMur2Hash.evaluate(input) shouldBe Murmur2.hash(input.toByteArray(), input.length, 0)
}
}
}
})
fun Gen.Companion.wideString(maxSize: Int = 100): Gen<String> = object : Gen<String> {
override fun constants(): Iterable<String> = listOf()
override fun random(): Sequence<String> = generateSequence { nextWideString(Random.nextInt(maxSize)) }
override fun shrinker(): Shrinker<String>? = StringShrinker
}
fun Gen.Companion.nextWideString(length: Int): String {
return (0 until Random.nextInt(0, if (length < 1) 1 else length))
.map { Random.nextInt(0, Short.MAX_VALUE.toInt()).toChar() }
.joinToString("")
}
package org.ort.school.app.service
import io.kotlintest.data.forall
import io.kotlintest.matchers.string.shouldNotBeEqualIgnoringCase
import io.kotlintest.matchers.string.shouldStartWith
import io.kotlintest.properties.Gen
import io.kotlintest.properties.assertAll
import io.kotlintest.shouldBe
import io.kotlintest.shouldThrow
import io.kotlintest.specs.ShouldSpec
import io.kotlintest.tables.row
import org.junit.jupiter.api.assertThrows
import java.lang.IllegalArgumentException
class PasswordServiceKTest : ShouldSpec({
val passwordService = PasswordService()
"Password"{
should("start with \$2a\$10 when calling with default number of rounds") {
passwordService.encryptPassword("test") shouldStartWith ("\$2a\$10")
}
should("start with \$2a\$12 when calling with 12 rounds") {
passwordService.encryptPassword("test", 12).shouldStartWith("\$2a\$12")
}
should("not be equal to unencrypted") {
val encrypted = passwordService.encryptPassword("test", 12)
encrypted shouldNotBeEqualIgnoringCase "test"
}
should("not be shorter than 4 symbols") {
shouldThrow<IllegalArgumentException> { passwordService.encryptPassword("123") }
}
}
"checkPw"{
should("should return true for pass encrypted by password service") {
forall(
row("test1", passwordService.encryptPassword("test1")),
row("test2", passwordService.encryptPassword("test2")),
row("test3", passwordService.encryptPassword("test3"))) { plain, encrypted ->
passwordService.checkPw(plain, encrypted) shouldBe true
}
}
should("return true for pass encrypted by password service propertu-based") {
assertAll(50, Gen.wideString().filter { it.length >= 4 }) { plain: String ->
val encrypted = passwordService.encryptPassword(plain/* + (if (Random(100).nextInt(100) % 70 == 23) "1" else "")*/)
passwordService.checkPw(plain, encrypted) shouldBe true
}
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment