Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

invoking rest AsyncStack method in tests always returns 401 #144

Closed
burovmarley opened this issue Jul 14, 2015 · 6 comments
Closed

invoking rest AsyncStack method in tests always returns 401 #144

burovmarley opened this issue Jul 14, 2015 · 6 comments
Labels

Comments

@burovmarley
Copy link

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)

    }

  }

}
@swiatek25
Copy link

I'm also interested in resolving this issue.

@gakuzzzz
Copy link
Member

Can I see your GlobalAuthConfig code and play2-auth version?

@gakuzzzz
Copy link
Member

In your withLoggedIn, It do startNewSession("userId", ...,
Can GlobalAuthConfig#resolveUser return any User by "userId"

@burovmarley
Copy link
Author

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

@gakuzzzz
Copy link
Member

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.

@gakuzzzz
Copy link
Member

play2-auth 0.13.3 had been published, and currently latest release for Play2.3 is 0.13.5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants