invoking rest AsyncStack method in tests always returns 401
burovmarley opened this issue · 6 comments
I've tried to run tests using fake request and you withLoggedIn implicit but it always return 401 code.
I was debugging whole day and after few hours I've manage code which is working well in my project. Can you look on it and tell if I didn't understand your tutorial or you have bug in your code? Bellow you can see my implicit and real working test. I had to add cookie into FakeRequest after that AsyncStack function is working great in my integration tests
Implicit function and my own implementation of TokenAccessor
import info.fingo.tabularium.GlobalAuthConfig
import jp.t2v.lab.play2.auth._
import org.specs2.time.Duration
import play.api.mvc.Cookie
import play.api.test.FakeRequest
import scala.concurrent.duration._
import scala.concurrent.{duration, Await}
import scala.concurrent.ExecutionContext.Implicits._
package object it {
object config extends GlobalAuthConfig
implicit class AuthFakeRequest[A](fakeRequest: FakeRequest[A]) {
def withLoggedIn : FakeRequest[A] = {
val token = Await.result(config.idContainer.startNewSession("userId", config.sessionTimeoutInSeconds)(fakeRequest, global), 10 seconds)
TestTokenAccessor.put(token, fakeRequest)
}
}
object TestTokenAccessor extends CookieTokenAccessor {
def put[A](token: AuthenticityToken, r: FakeRequest[A]) : FakeRequest[A] = {
val c = Cookie(cookieName, sign(token), cookieMaxAge, cookiePathOption, cookieDomainOption, cookieSecureOption, cookieHttpOnlyOption)
r.withCookies(c)
}
}
}
Real test which works :)
package it
import scala.concurrent._
import duration._
import org.specs2.mutable._
import play.api.libs.json._
import play.api.test._
import play.api.test.Helpers._
import java.util.concurrent.TimeUnit
class CommitmentIT extends Specification {
val timeout: FiniteDuration = FiniteDuration(5, TimeUnit.SECONDS)
"Commitment" should {
"insert a valid json" in new WithApplication() {
implicit val request =
FakeRequest.apply(POST, "/api/commitment").withJsonBody(Json.obj(
"commitment" -> Json.obj(
"areaId" -> "559e422b8c1b931f114ea57f",
"clientId" -> "559e422b8c1b931f114ea583",
"owner" -> "Test owner",
"status" -> "development",
"subject" -> "subject1",
"teamId" -> "559e422b8c1b931f114ea590"
),
"details" -> Json.obj("clientId" -> "559e422b8c1b931f114ea583")
)).withLoggedIn
val response = route(request)
response.isDefined mustEqual true
val result = Await.result(response.get, timeout)
result.header.status must equalTo(CREATED)
}
}
}
I'm also interested in resolving this issue.
Can I see your GlobalAuthConfig
code and play2-auth version?
In your withLoggedIn
, It do startNewSession("userId", ...
,
Can GlobalAuthConfig#resolveUser
return any User
by "userId"
My global auth
trait GlobalAuthConfig extends AuthConfig {
type Id = String
type User = user.User
val idTag: ClassTag[Id] = classTag[Id]
val sessionTimeoutInSeconds = current.configuration.getInt("session.timeout.seconds").getOrElse(3600)
def resolveUser(id: Id)(implicit ctx: ExecutionContext) = Future.successful(Some(User(id)))
def authorizationFailed(request: RequestHeader)(implicit ctx: ExecutionContext) = Future.successful(Forbidden)
def authorize(user: User, authority: Authority)(implicit ctx: ExecutionContext) = Future.successful(true)
def loginSucceeded(request: RequestHeader)(implicit ctx: ExecutionContext) = Future.successful(Ok)
def logoutSucceeded(request: RequestHeader)(implicit ctx: ExecutionContext) = Future.successful(Ok)
def authenticationFailed(request: RequestHeader)(implicit ctx: ExecutionContext) = Future.successful(Unauthorized)
override def authorizationFailed(request: RequestHeader, user: User, authority: Option[Authority])(implicit context: ExecutionContext): Future[Result] = {
Future.successful(Forbidden("no permission"))
}
}
my version of the play2auth is 0.13.2
Thanks!
Ok, I see. play2-auth-test 0.13.2 is broken #136
It was fixed at 0.14.0, however it is for Play2.4
I'll publish 0.13.3 that contains this back port.
play2-auth 0.13.3 had been published, and currently latest release for Play2.3 is 0.13.5