~jan0sch/smederee
Showing details for patch 470d883bf1b1c11c76ff03b0bf1ad6135cc958a1.
diff -rN -u old-smederee/modules/hub/src/test/scala/de/smederee/hub/ResetPasswordRoutesTest.scala new-smederee/modules/hub/src/test/scala/de/smederee/hub/ResetPasswordRoutesTest.scala --- old-smederee/modules/hub/src/test/scala/de/smederee/hub/ResetPasswordRoutesTest.scala 2024-09-08 02:21:47.060701422 +0000 +++ new-smederee/modules/hub/src/test/scala/de/smederee/hub/ResetPasswordRoutesTest.scala 2024-09-08 02:21:47.060701422 +0000 @@ -9,7 +9,7 @@ import cats.effect.* import cats.syntax.all.* import com.typesafe.config.ConfigFactory -import de.smederee.hub.Generators.* +import de.smederee.hub.Generators.given import de.smederee.hub.config.* import org.http4s.* import org.http4s.implicits.* @@ -18,7 +18,9 @@ import munit.* -final class ResetPasswordRoutesTest extends CatsEffectSuite { +import org.scalacheck.effect.PropF + +final class ResetPasswordRoutesTest extends CatsEffectSuite with ScalaCheckEffectSuite { private val resetChangePasswordPath = uri"/forgot-password/change-password" protected final val configuration: SmedereeHubConfig = @@ -28,83 +30,79 @@ .loadOrThrow[SmedereeHubConfig] test("GET forgot-password/change-password/TOKEN must return 200 and the change password form") { - genValidAccount.sample match { - case Some(account) => - val token = ResetToken.generate - val changePasswordUri = resetChangePasswordPath.addSegment(token.toString) - val expectedHtml = views.html - .changePassword()(changePasswordUri, None, title = "Smederee - Change your password.".some, token)() - - val authenticationConfig = configuration.service.authentication - val authenticationRepo = new TestAuthenticationRepository[IO](List.empty, List.empty) - val emailMiddleware = new TestEmailMiddleware[IO] - val externalConfig = configuration.service.external - val resetPasswordRepo = new TestResetPasswordRepository[IO](account, token.some) - - def service: HttpRoutes[IO] = - Router( - "/" -> new ResetPasswordRoutes[IO]( - authenticationConfig, - authenticationRepo, - emailMiddleware, - externalConfig, - resetPasswordRepo - ).routes - ) - - def request = Request[IO](method = Method.GET, uri = changePasswordUri) - def response: IO[Response[IO]] = service.orNotFound.run(request) - - val test = for { - result <- response - body <- result.as[String] - } yield (result, body) - - test.map { output => - val (result, body) = output - assertEquals(result.status, Status.Ok) - assertEquals(body, expectedHtml.toString) - } - case _ => fail("Could not generate data samples!") + PropF.forAllF { (account: Account) => + val token = ResetToken.generate + val changePasswordUri = resetChangePasswordPath.addSegment(token.toString) + val expectedHtml = views.html + .changePassword()(changePasswordUri, None, title = "Smederee - Change your password.".some, token)() + + val authenticationConfig = configuration.service.authentication + val authenticationRepo = new TestAuthenticationRepository[IO](List.empty, List.empty) + val emailMiddleware = new TestEmailMiddleware[IO] + val externalConfig = configuration.service.external + val resetPasswordRepo = new TestResetPasswordRepository[IO](account, token.some) + + def service: HttpRoutes[IO] = + Router( + "/" -> new ResetPasswordRoutes[IO]( + authenticationConfig, + authenticationRepo, + emailMiddleware, + externalConfig, + resetPasswordRepo + ).routes + ) + + def request = Request[IO](method = Method.GET, uri = changePasswordUri) + def response: IO[Response[IO]] = service.orNotFound.run(request) + + val test = for { + result <- response + body <- result.as[String] + } yield (result, body) + + test.start.flatMap(_.joinWithNever).map { output => + val (result, body) = output + assertEquals(result.status, Status.Ok) + assertEquals(body, expectedHtml.toString) + } } } test("GET forgot-password/change-password/TOKEN must return 404 for non existing token") { - genValidAccount.sample match { - case Some(account) => - val token = ResetToken.generate - val changePasswordUri = resetChangePasswordPath.addSegment(token.toString) - - val authenticationConfig = configuration.service.authentication - val authenticationRepo = new TestAuthenticationRepository[IO](List.empty, List.empty) - val emailMiddleware = new TestEmailMiddleware[IO] - val externalConfig = configuration.service.external - val resetPasswordRepo = new TestResetPasswordRepository[IO](account, None) - - def service: HttpRoutes[IO] = - Router( - "/" -> new ResetPasswordRoutes[IO]( - authenticationConfig, - authenticationRepo, - emailMiddleware, - externalConfig, - resetPasswordRepo - ).routes - ) - - def request = Request[IO](method = Method.GET, uri = changePasswordUri) - def response: IO[Response[IO]] = service.orNotFound.run(request) - - val test = for { - result <- response - body <- result.as[String] - } yield (result, body) - - test.map { output => - val (result, _) = output - assertEquals(result.status, Status.NotFound) - } - case _ => fail("Could not generate data samples!") + PropF.forAllF { (account: Account) => + val token = ResetToken.generate + val changePasswordUri = resetChangePasswordPath.addSegment(token.toString) + + val authenticationConfig = configuration.service.authentication + val authenticationRepo = new TestAuthenticationRepository[IO](List.empty, List.empty) + val emailMiddleware = new TestEmailMiddleware[IO] + val externalConfig = configuration.service.external + val resetPasswordRepo = new TestResetPasswordRepository[IO](account, None) + + def service: HttpRoutes[IO] = + Router( + "/" -> new ResetPasswordRoutes[IO]( + authenticationConfig, + authenticationRepo, + emailMiddleware, + externalConfig, + resetPasswordRepo + ).routes + ) + + def request = Request[IO](method = Method.GET, uri = changePasswordUri) + def response: IO[Response[IO]] = service.orNotFound.run(request) + + val test = for { + result <- response + body <- result.as[String] + } yield (result, body) + + test.start.flatMap(_.joinWithNever).map { output => + val (result, _) = output + assertEquals(result.status, Status.NotFound) + } } } }