Flatten packages
Remove modules prefix
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
package be.simplenotes.domain.usecases.search
|
||||
|
||||
import be.simplenotes.search.SearchTerms
|
||||
import com.natpryce.hamkrest.assertion.assertThat
|
||||
import com.natpryce.hamkrest.equalTo
|
||||
import org.junit.jupiter.params.ParameterizedTest
|
||||
import org.junit.jupiter.params.provider.MethodSource
|
||||
import java.util.stream.Stream
|
||||
|
||||
internal class SearchTermsParserKtTest {
|
||||
|
||||
private fun createResult(
|
||||
input: String,
|
||||
title: String? = null,
|
||||
tag: String? = null,
|
||||
content: String? = null,
|
||||
all: String? = null,
|
||||
): Pair<String, SearchTerms> = input to SearchTerms(title, tag, content, all)
|
||||
|
||||
@Suppress("Unused")
|
||||
private fun results() = Stream.of(
|
||||
createResult("title:'example'", title = "example"),
|
||||
createResult("title:'example with words'", title = "example with words"),
|
||||
createResult("title:'example with words'", title = "example with words"),
|
||||
createResult("""title:"double quotes"""", title = "double quotes"),
|
||||
createResult("title:'example' something else", title = "example", all = "something else"),
|
||||
createResult("tag:'example'", tag = "example"),
|
||||
createResult("tag:'example' title:'other'", title = "other", tag = "example"),
|
||||
createResult("blah blah tag:'example' title:'other'", title = "other", tag = "example", all = "blah blah"),
|
||||
createResult("tag:'example' middle title:'other'", title = "other", tag = "example", all = "middle"),
|
||||
createResult("tag:'example' title:'other' end", title = "other", tag = "example", all = "end"),
|
||||
createResult(
|
||||
"tag:'example abc' title:'other with words' this is the end ",
|
||||
title = "other with words",
|
||||
tag = "example abc",
|
||||
all = "this is the end"
|
||||
),
|
||||
)
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("results")
|
||||
fun `valid search parser`(case: Pair<String, SearchTerms>) {
|
||||
assertThat(parseSearchTerms(case.first), equalTo(case.second))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package be.simplenotes.domain.usecases.users.login
|
||||
|
||||
import be.simplenotes.config.JwtConfig
|
||||
import be.simplenotes.domain.security.BcryptPasswordHash
|
||||
import be.simplenotes.domain.security.SimpleJwt
|
||||
import be.simplenotes.domain.testutils.isLeftOfType
|
||||
import be.simplenotes.domain.testutils.isRight
|
||||
import be.simplenotes.persistance.repositories.UserRepository
|
||||
import be.simplenotes.types.PersistedUser
|
||||
import com.natpryce.hamkrest.assertion.assertThat
|
||||
import io.mockk.*
|
||||
import org.junit.jupiter.api.BeforeEach
|
||||
import org.junit.jupiter.api.Test
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
internal class LoginUseCaseImplTest {
|
||||
// region setup
|
||||
private val mockUserRepository = mockk<UserRepository>()
|
||||
private val passwordHash = BcryptPasswordHash(test = true)
|
||||
private val jwtConfig = JwtConfig("a secret", 1, TimeUnit.HOURS)
|
||||
private val simpleJwt = SimpleJwt(jwtConfig)
|
||||
private val loginUseCase = LoginUseCaseImpl(mockUserRepository, passwordHash, simpleJwt)
|
||||
|
||||
@BeforeEach
|
||||
fun resetMocks() {
|
||||
clearMocks(mockUserRepository)
|
||||
}
|
||||
// endregion
|
||||
|
||||
@Test
|
||||
fun `Login should fail with invalid form`() {
|
||||
val form = LoginForm("", "a")
|
||||
assertThat(loginUseCase.login(form), isLeftOfType<InvalidLoginForm>())
|
||||
verify { mockUserRepository wasNot called }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Login should fail with non existing user`() {
|
||||
val form = LoginForm("someusername", "somepassword")
|
||||
every { mockUserRepository.find(form.username!!) } returns null
|
||||
assertThat(loginUseCase.login(form), isLeftOfType<Unregistered>())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Login should fail with wrong password`() {
|
||||
val form = LoginForm("someusername", "wrongpassword")
|
||||
|
||||
every { mockUserRepository.find(form.username!!) } returns
|
||||
PersistedUser(form.username!!, passwordHash.crypt("right password"), 1)
|
||||
|
||||
assertThat(loginUseCase.login(form), isLeftOfType<WrongPassword>())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Login should succeed with existing user and correct password`() {
|
||||
val loginForm = LoginForm("someusername", "somepassword")
|
||||
|
||||
every { mockUserRepository.find(loginForm.username!!) } returns
|
||||
PersistedUser(loginForm.username!!, passwordHash.crypt(loginForm.password!!), 1)
|
||||
|
||||
val res = loginUseCase.login(loginForm)
|
||||
assertThat(res, isRight())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package be.simplenotes.domain.usecases.users.register
|
||||
|
||||
import be.simplenotes.domain.security.BcryptPasswordHash
|
||||
import be.simplenotes.domain.testutils.isLeftOfType
|
||||
import be.simplenotes.domain.testutils.isRight
|
||||
import be.simplenotes.persistance.repositories.UserRepository
|
||||
import be.simplenotes.persistance.transactions.TransactionService
|
||||
import be.simplenotes.types.PersistedUser
|
||||
import com.natpryce.hamkrest.assertion.assertThat
|
||||
import com.natpryce.hamkrest.equalTo
|
||||
import io.mockk.*
|
||||
import org.junit.jupiter.api.BeforeEach
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
internal class RegisterUseCaseImplTest {
|
||||
|
||||
// region setup
|
||||
private val mockUserRepository = mockk<UserRepository>()
|
||||
private val passwordHash = BcryptPasswordHash(test = true)
|
||||
private val noopTransactionService = object : TransactionService {
|
||||
override fun <T> use(block: () -> T) = block()
|
||||
}
|
||||
private val registerUseCase = RegisterUseCaseImpl(mockUserRepository, passwordHash, noopTransactionService)
|
||||
|
||||
@BeforeEach
|
||||
fun resetMocks() {
|
||||
clearMocks(mockUserRepository)
|
||||
}
|
||||
// endregion
|
||||
|
||||
@Test
|
||||
fun `register should fail with invalid form`() {
|
||||
val form = RegisterForm("", "a".repeat(10))
|
||||
assertThat(registerUseCase.register(form), isLeftOfType<InvalidRegisterForm>())
|
||||
verify { mockUserRepository wasNot called }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Register should fail with existing username`() {
|
||||
val form = RegisterForm("someuser", "somepassword")
|
||||
every { mockUserRepository.exists(form.username!!) } returns true
|
||||
assertThat(registerUseCase.register(form), isLeftOfType<UserExists>())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Register should succeed with new user`() {
|
||||
val form = RegisterForm("someuser", "somepassword")
|
||||
every { mockUserRepository.exists(form.username!!) } returns false
|
||||
every { mockUserRepository.create(any()) } returns PersistedUser(form.username!!, form.password!!, 1)
|
||||
val res = registerUseCase.register(form)
|
||||
assertThat(res, isRight())
|
||||
res.map { assertThat(it.username, equalTo(form.username)) }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user