You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
77 lines
2.6 KiB
77 lines
2.6 KiB
package org.tbasket.test.pages
|
|
|
|
import io.getquill.jdbczio.Quill
|
|
import io.getquill.{SnakeCase, SqliteZioJdbcContext}
|
|
import org.tbasket.auth.Authentificator
|
|
import org.tbasket.data.{Database, DatabaseContext}
|
|
import org.tbasket.error.RegularException.InvalidRequest
|
|
import org.tbasket.handler.HandlerUtils.parseAttribute
|
|
import org.tbasket.handler.LoginPageHandler
|
|
import org.tbasket.handler.LoginPageHandler.post
|
|
import org.tbasket.test.TestLayers
|
|
import zio.*
|
|
import zio.http.netty.client.ConnectionPool
|
|
import zio.http.*
|
|
import zio.http.model.{HeaderNames, Headers}
|
|
import zio.http.model.Headers.Header
|
|
import zio.json.*
|
|
import zio.json.ast.{Json, JsonCursor}
|
|
import zio.test.{TestAspect, *}
|
|
import zio.test.Assertion.*
|
|
|
|
object LoginPageHandlerTests extends ZIOSpecDefault {
|
|
import LoginPageHandler.post
|
|
import TestLayers.*
|
|
|
|
private def getJsonBody(r: Response): Task[Json] = {
|
|
for
|
|
body <- r.body.asString
|
|
json <- ZIO.fromEither(body.fromJson[Json]).mapError(new Exception(_))
|
|
yield json
|
|
}
|
|
|
|
private def requestsSpec = suite("erroned request body tests")(
|
|
ZIO.attempt(Map(
|
|
"empty packet" -> Body.empty,
|
|
"with no mail attribute" -> Body.fromString("""{"password":"1234"}"""),
|
|
"with no password attribute" -> Body.fromString("""{"email":"valid.mail@x.y"}"""),
|
|
"with invalid json" -> Body.fromString("""this is a corrupted json""")
|
|
)).map(_.map((name, body) =>
|
|
test(name) {
|
|
for
|
|
response <- post(Request.post(body, URL.empty))
|
|
json <- getJsonBody(response)
|
|
errorType <- parseAttribute(json, "error", JsonCursor.field("error").isString)
|
|
yield assertTrue(errorType == "invalid request")
|
|
}
|
|
))
|
|
)
|
|
|
|
private def loginSpec = {
|
|
suite("login situation tests") (
|
|
test("login with unknown account") {
|
|
for
|
|
response <- post(Request.post(Body.fromString("""{"password":"123456","email":"maximebatista18@gmail.com"}"""), URL.empty))
|
|
json <- getJsonBody(response)
|
|
errorType <- parseAttribute(json, "error", JsonCursor.field("error").isString)
|
|
yield
|
|
//assert that the response error is of type unauthorized and headers are Location: /register
|
|
assert(errorType)(equalTo("unauthorized"))
|
|
&& assert(response)(hasField("headers", _.headers, hasSameElements(Headers.location("/register"))))
|
|
}
|
|
)
|
|
}
|
|
|
|
override def spec = suite("/login page handler") (
|
|
requestsSpec,
|
|
loginSpec
|
|
).provide(
|
|
db.datasourceLayer,
|
|
db.contextLayer,
|
|
auth,
|
|
ConnectionPool.fixed(1),
|
|
Scope.default,
|
|
ClientConfig.default,
|
|
Client.live)
|
|
}
|