diff --git a/springboot/api/src/main/kotlin/webapp/users/payment/payment.kt b/springboot/api/src/main/kotlin/webapp/users/payment/payment.kt new file mode 100644 index 0000000..d5c4437 --- /dev/null +++ b/springboot/api/src/main/kotlin/webapp/users/payment/payment.kt @@ -0,0 +1,2 @@ +package webapp.users.payment + diff --git a/springboot/api/src/test/kotlin/core/UserTests.kt b/springboot/api/src/test/kotlin/core/UserTests.kt new file mode 100755 index 0000000..8617227 --- /dev/null +++ b/springboot/api/src/test/kotlin/core/UserTests.kt @@ -0,0 +1,114 @@ +@file:Suppress("JUnitMalformedDeclaration") + +package core + +import com.fasterxml.jackson.databind.ObjectMapper +import jakarta.validation.Validator +import kotlinx.coroutines.runBlocking +import org.junit.jupiter.api.assertDoesNotThrow +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.beans.factory.getBean +import org.springframework.boot.test.context.SpringBootTest +import org.springframework.context.ApplicationContext +import org.springframework.context.annotation.ComponentScan +import org.springframework.dao.EmptyResultDataAccessException +import org.springframework.test.context.ActiveProfiles +import webapp.Application +import webapp.TestUtils.Data.user +import webapp.TestUtils.countRoles +import webapp.TestUtils.countUsers +import webapp.TestUtils.defaultRoles +import webapp.TestUtils.deleteAllUsersOnly +import webapp.core.model.EntityModel.Members.withId +import webapp.core.utils.AppUtils.cleanField +import webapp.core.utils.AppUtils.toJson +import webapp.core.utils.i +import webapp.users.User +import webapp.users.UserDao.Dao.findOneByEmail +import webapp.users.UserDao.Dao.save +import kotlin.test.* + + +@SpringBootTest( + properties = ["spring.main.web-application-type=reactive"], + classes = [Application::class] +) +@ActiveProfiles("test") +class UserTests { + + @Autowired + lateinit var context: ApplicationContext + val mapper: ObjectMapper by lazy { context.getBean() } + val validator: Validator by lazy { context.getBean() } + + @AfterTest + fun cleanUp() = runBlocking { context.deleteAllUsersOnly() } + + @Test + fun `save default user should work in this context `() = runBlocking { + val count = context.countUsers() + (user to context).save() + assertEquals(expected = count + 1, context.countUsers()) + } + + @Test + fun `count users, expected 0`() = + runBlocking { + assertEquals( + 0, + context.countUsers(), + "because init sql script does not inserts default users." + ) + } + + @Test + fun `count roles, expected 3`() = runBlocking { + context.run { + assertEquals( + defaultRoles.size, + countRoles(), + "Because init sql script does insert default roles." + ) + } + } + + @Test + fun `display user formatted in JSON`() = assertDoesNotThrow { + (user to context).toJson.let(::i) + } + + @Test + fun `check toJson build a valid json format`(): Unit = assertDoesNotThrow { + (user to context).toJson.let(mapper::readTree) + } + + + @Test + fun `test cleanField extension function`() = assertEquals( + "login", + "`login`".cleanField(), + "Backtick should be removed" + ) + + + @Test + fun `check findOneByEmail with non-existent email`(): Unit = runBlocking { + assertEquals(0, context.countUsers(), "context should not have a user recorded in database") + context.findOneByEmail("user@dummy.com").apply { + assertFalse(isRight()) + assertTrue(isLeft()) + }.mapLeft { assertTrue(it is EmptyResultDataAccessException) } + } + + + @Test + fun `check findOneByEmail with existant email`(): Unit = runBlocking { + assertEquals(0, context.countUsers(), "context should not have a user recorded in database") + (user to context).save() + assertEquals(1, context.countUsers(), "context should have only one user recorded in database") + context.findOneByEmail(user.email).apply { + assertTrue(isRight()) + assertFalse(isLeft()) + }.map { assertEquals(it, user.withId(it.id!!)) } + } +} \ No newline at end of file diff --git a/springboot/api/src/test/kotlin/webapp/users/UserDaoTests.kt b/springboot/api/src/test/kotlin/webapp/users/UserDaoTests.kt new file mode 100755 index 0000000..3bdd9ca --- /dev/null +++ b/springboot/api/src/test/kotlin/webapp/users/UserDaoTests.kt @@ -0,0 +1,108 @@ +@file:Suppress("JUnitMalformedDeclaration") + +package webapp.users + +import com.fasterxml.jackson.databind.ObjectMapper +import jakarta.validation.Validator +import kotlinx.coroutines.runBlocking +import org.junit.jupiter.api.assertDoesNotThrow +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.beans.factory.getBean +import org.springframework.boot.test.context.SpringBootTest +import org.springframework.context.ApplicationContext +import org.springframework.dao.EmptyResultDataAccessException +import org.springframework.test.context.ActiveProfiles +import webapp.TestUtils.Data.user +import webapp.TestUtils.countRoles +import webapp.TestUtils.countUsers +import webapp.TestUtils.defaultRoles +import webapp.TestUtils.deleteAllUsersOnly +import webapp.core.model.EntityModel.Members.withId +import webapp.core.utils.AppUtils.cleanField +import webapp.core.utils.AppUtils.toJson +import webapp.core.utils.i +import webapp.users.UserDao.Dao.findOneByEmail +import webapp.users.UserDao.Dao.save +import kotlin.test.* + + +@SpringBootTest(properties = ["spring.main.web-application-type=reactive"]) +@ActiveProfiles("test") +class UserDaoTests { + + @Autowired + lateinit var context: ApplicationContext + val mapper: ObjectMapper by lazy { context.getBean() } + val validator: Validator by lazy { context.getBean() } + + @AfterTest + fun cleanUp() = runBlocking { context.deleteAllUsersOnly() } + + @Test + fun `save default user should work in this context `() = runBlocking { + val count = context.countUsers() + (user to context).save() + assertEquals(expected = count + 1, context.countUsers()) + } + + @Test + fun `count users, expected 0`() = + runBlocking { + assertEquals( + 0, + context.countUsers(), + "because init sql script does not inserts default users." + ) + } + + @Test + fun `count roles, expected 3`() = runBlocking { + context.run { + assertEquals( + defaultRoles.size, + countRoles(), + "Because init sql script does insert default roles." + ) + } + } + + @Test + fun `display user formatted in JSON`() = assertDoesNotThrow { + (user to context).toJson.let(::i) + } + + @Test + fun `check toJson build a valid json format`(): Unit = assertDoesNotThrow { + (user to context).toJson.let(mapper::readTree) + } + + + @Test + fun `test cleanField extension function`() = assertEquals( + "login", + "`login`".cleanField(), + "Backtick should be removed" + ) + + + @Test + fun `check findOneByEmail with non-existent email`(): Unit = runBlocking { + assertEquals(0, context.countUsers(), "context should not have a user recorded in database") + context.findOneByEmail("user@dummy.com").apply { + assertFalse(isRight()) + assertTrue(isLeft()) + }.mapLeft { assertTrue(it is EmptyResultDataAccessException) } + } + + + @Test + fun `check findOneByEmail with existant email`(): Unit = runBlocking { + assertEquals(0, context.countUsers(), "context should not have a user recorded in database") + (user to context).save() + assertEquals(1, context.countUsers(), "context should have only one user recorded in database") + context.findOneByEmail(user.email).apply { + assertTrue(isRight()) + assertFalse(isLeft()) + }.map { assertEquals(it, user.withId(it.id!!)) } + } +} \ No newline at end of file