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?

Community
  • 1
  • 1
Ricardo Gladwell
  • 3,716
  • 4
  • 32
  • 59

1 Answers1

1

Try changing this:

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

into this:

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

And then it should work.

Krzysztof Kur
  • 81
  • 1
  • 2