Secured trait causing spec2 unit test compilation errors
Asked Answered
F

1

2

I've implemented authorization for my Play Framework (version 2.3.5) application as per the official security documentation:

trait Secured {

  def username(request: RequestHeader) = request.session.get(Security.username)

  def onUnauthorized(request: RequestHeader) = Results.Redirect(routes.Login.index)

  def withAuth(f: => String => Request[AnyContent] => Result) = {
    Security.Authenticated(username, onUnauthorized) { user =>
      Action(request => f(user)(request))
    }
  }

}

However, when I decorate my controller actions with the withAuth function like so:

def export = withAuth { username => implicit request =>
  val csv = csv()
  Ok(csv)
    .as("text/csv")
    .withHeaders(
      CONTENT_DISPOSITION -> ("attachment; filename=export.csv"),
      CONTENT_LENGTH -> csv.length.toString
    )
}

I get compilation errors in my controller specs2 unit tests:

"export data as CSV" in {
  val controller = new controllers.DataController
  val result = controller.export().apply(FakeRequest())
  headers(result) must havePair("Content-Disposition" -> ("attachment; filename=export.csv"))
}

The call to the headers test helper function fails with the following compiler error message:

type mismatch; found : play.api.libs.iteratee.Iteratee[Array[Byte],play.api.mvc.Result] required: scala.concurrent.Future[play.api.mvc.Result]

Am I doing something wrong? I've tried some of the remedies suggested by other Stackoverflow users but they all seem to rely on setting the body type, but my actions do not have a body type.

It looks as though the call to withAuth obscures some of the types of the Action being wrapped so maybe there is a more type-safe way of expressing the Secured trait?

Fingerstall answered 10/10, 2014 at 16:12 Comment(0)
D
1

Try changing this:

val result = controller.export().apply(FakeRequest())

into this:

val result = controller.export().apply(FakeRequest()).run

And then it should work.

Drugstore answered 30/12, 2015 at 17:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.