|
|
@ -7,9 +7,8 @@ import org.tbasket.data.{Database, DatabaseContext}
|
|
|
|
import org.tbasket.endpoint.Endpoint
|
|
|
|
import org.tbasket.endpoint.Endpoint
|
|
|
|
import org.tbasket.endpoint.Endpoint.handle
|
|
|
|
import org.tbasket.endpoint.Endpoint.handle
|
|
|
|
import org.tbasket.error.*
|
|
|
|
import org.tbasket.error.*
|
|
|
|
import org.tbasket.handler.HandlerUtils.parseAttribute
|
|
|
|
|
|
|
|
import org.tbasket.handler.LoginPageHandler
|
|
|
|
import org.tbasket.handler.LoginPageHandler
|
|
|
|
import org.tbasket.test.TestUtils.{getJsonBody, makeFormBody}
|
|
|
|
import org.tbasket.test.TestUtils.*
|
|
|
|
import org.tbasket.test.{TestLayers, TestUtils}
|
|
|
|
import org.tbasket.test.{TestLayers, TestUtils}
|
|
|
|
import zio.*
|
|
|
|
import zio.*
|
|
|
|
import zio.http.*
|
|
|
|
import zio.http.*
|
|
|
@ -23,65 +22,65 @@ import zio.test.Assertion.*
|
|
|
|
|
|
|
|
|
|
|
|
object LoginPageHandlerTests extends TBasketPageSpec("/login") {
|
|
|
|
object LoginPageHandlerTests extends TBasketPageSpec("/login") {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private def requestsSpec = suite("bad request tests")(
|
|
|
|
|
|
|
|
ZIO.attempt(Map(
|
|
|
|
|
|
|
|
"empty packet" -> Body.empty,
|
|
|
|
|
|
|
|
"with no mail attribute" -> makeFormBody("password" -> "bouhours"),
|
|
|
|
|
|
|
|
"with no password attribute" -> makeFormBody("email" -> "valid.email@not.very"),
|
|
|
|
|
|
|
|
"with invalid form data" -> Body.fromString("""this is a corrupted form data""")
|
|
|
|
|
|
|
|
)).map(_.map { case (name, body) =>
|
|
|
|
|
|
|
|
test(name) {
|
|
|
|
|
|
|
|
for
|
|
|
|
|
|
|
|
response <- handle(Request.post(body, url))
|
|
|
|
|
|
|
|
json <- getJsonBody(response)
|
|
|
|
|
|
|
|
errorType <- parseAttribute(json, "error", JsonCursor.field("error").isString)
|
|
|
|
|
|
|
|
yield
|
|
|
|
|
|
|
|
assert(response)(hasField("status", _.status, equalTo(Status.BadRequest)))
|
|
|
|
|
|
|
|
&& assertTrue(errorType == "invalid request")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private def loginSpec = {
|
|
|
|
private def requestsSpec = suite("bad request tests")(
|
|
|
|
suite("login situation tests")(
|
|
|
|
ZIO.attempt(Map(
|
|
|
|
test("login with unknown account") {
|
|
|
|
"empty packet" -> Body.empty,
|
|
|
|
for
|
|
|
|
"with no mail attribute" -> makeFormBody("password" -> "bouhours"),
|
|
|
|
response <- handle(Request.post(makeFormBody("password" -> "bouhours", "email" -> "unknownaccount@gmail.com"), url))
|
|
|
|
"with no password attribute" -> makeFormBody("email" -> "valid.email@not.very"),
|
|
|
|
json <- getJsonBody(response)
|
|
|
|
"with invalid form data" -> Body.fromString("""this is a corrupted form data""")
|
|
|
|
errorType <- parseAttribute(json, "error", JsonCursor.field("error").isString)
|
|
|
|
)).map(_.map { case (name, body) =>
|
|
|
|
yield
|
|
|
|
test(name) {
|
|
|
|
//assert that the response error is of type unauthorized and headers are Location: /register
|
|
|
|
for
|
|
|
|
assert(response)(hasField("status", _.status, equalTo(Status.Found)))
|
|
|
|
response <- handle(Request.post(body, url))
|
|
|
|
&& assert(errorType)(equalTo("unauthorized"))
|
|
|
|
json <- getJsonBody(response)
|
|
|
|
&& assert(response)(hasField("headers", _.headers, contains(Headers.location("/register"))))
|
|
|
|
errorType <- parseAttribute(json, JsonCursor.field("error").isString)
|
|
|
|
},
|
|
|
|
yield
|
|
|
|
|
|
|
|
assert(response)(hasField("status", _.status, equalTo(Status.BadRequest)))
|
|
|
|
|
|
|
|
&& assertTrue(errorType == "invalid request")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private def loginSpec = {
|
|
|
|
|
|
|
|
suite("login situation tests")(
|
|
|
|
|
|
|
|
test("login with unknown account") {
|
|
|
|
|
|
|
|
for
|
|
|
|
|
|
|
|
response <- handle(Request.post(makeFormBody("password" -> "bouhours", "email" -> "unknownaccount@gmail.com"), url))
|
|
|
|
|
|
|
|
json <- getJsonBody(response)
|
|
|
|
|
|
|
|
errorType <- parseAttribute(json, JsonCursor.field("error").isString)
|
|
|
|
|
|
|
|
yield
|
|
|
|
|
|
|
|
//assert that the response error is of type unauthorized and headers are Location: /register
|
|
|
|
|
|
|
|
assert(response)(hasField("status", _.status, equalTo(Status.Found)))
|
|
|
|
|
|
|
|
&& assert(errorType)(equalTo("unauthorized"))
|
|
|
|
|
|
|
|
&& assert(response)(hasField("headers", _.headers, contains(Headers.location("/register"))))
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
test("login with known account") {
|
|
|
|
test("login with known account") {
|
|
|
|
for
|
|
|
|
for
|
|
|
|
response <- handle(Request.post(makeFormBody("password" -> "123456", "email" -> "maximebatista18@gmail.com"), url))
|
|
|
|
response <- handle(Request.post(makeFormBody("password" -> "123456", "email" -> "maximebatista18@gmail.com"), url))
|
|
|
|
yield
|
|
|
|
yield
|
|
|
|
assert(response)(hasField("status", _.status, equalTo(Status.Found)))
|
|
|
|
assert(response)(hasField("status", _.status, equalTo(Status.Found)))
|
|
|
|
&& assert(response)(hasField("body", _.body, equalTo(Body.empty))) //TODO assert that the cookie name is JWT
|
|
|
|
&& assert(response)(hasField("body", _.body, equalTo(Body.empty))) //TODO assert that the cookie name is JWT
|
|
|
|
&& assert(response)(hasField("headers", _.headers, exists(hasField("key", _.key, equalTo(HeaderNames.setCookie)))))
|
|
|
|
&& assert(response)(hasField("headers", _.headers, exists(hasField("key", _.key, equalTo(HeaderNames.setCookie)))))
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
test("login with known account wrong password") {
|
|
|
|
test("login with known account wrong password") {
|
|
|
|
for
|
|
|
|
for
|
|
|
|
fiber <- handle(Request.post(makeFormBody("password" -> "wrong", "email" -> "maximebatista18@gmail.com"), url)).fork
|
|
|
|
fiber <- handle(Request.post(makeFormBody("password" -> "wrong", "email" -> "maximebatista18@gmail.com"), url)).fork
|
|
|
|
_ <- TestClock.adjust(1.seconds)
|
|
|
|
_ <- TestClock.adjust(1.seconds)
|
|
|
|
response <- ZIO.fromFiber(fiber)
|
|
|
|
response <- ZIO.fromFiber(fiber)
|
|
|
|
json <- getJsonBody(response)
|
|
|
|
json <- getJsonBody(response)
|
|
|
|
errorType <- parseAttribute(json, "error", JsonCursor.field("error").isString)
|
|
|
|
errorType <- parseAttribute(json, JsonCursor.field("error").isString)
|
|
|
|
yield
|
|
|
|
yield
|
|
|
|
assert(response)(hasField("status", _.status, equalTo(Status.Unauthorized)))
|
|
|
|
assert(response)(hasField("status", _.status, equalTo(Status.Unauthorized)))
|
|
|
|
&& assert(errorType)(equalTo("unauthorized"))
|
|
|
|
&& assert(errorType)(equalTo("unauthorized"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
override def tspec = suite("/login page handler")(
|
|
|
|
|
|
|
|
requestsSpec,
|
|
|
|
|
|
|
|
loginSpec
|
|
|
|
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
override def tspec = suite("/login page handler")(
|
|
|
|
|
|
|
|
requestsSpec,
|
|
|
|
|
|
|
|
loginSpec
|
|
|
|
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|