0

I have a controller that is protected by an Authentication trait. The trait looks like this:

def isAuthenticated(f: => AccountDTO => Request[AnyContent] => Result) =
    Security.Authenticated(username, onUnauthorized) { user =>
      Action.async {
        request => {
          Future.successful(f(user)(request))
        }
      }
    }

Everything works well when doing a normal request, but when doing unit tests I encounter problems. I create the following fake request:

val goodRequest = FakeRequest("POST", "/platform/api/v1/files")
.withBody(Json.toJson(ScalaMockingUtil.fileValidMetadataJson))
      .withHeaders((HeaderNames.AUTHORIZATION, "4322tertf2643t34t34"))

Next, I get my controller object and call the method by applying that FakeRequest:

val result: Iteratee[Array[Byte], Result] = filesController.createFileMetadata()(goodRequest)

The problem that I am facing is that somewhere along the line the FakeReuqest gets down-casted to RequestHeader. The problem seems to be the one described here: Unable to test controller using Action.async where the Action has two apply methods instead of one. however, I can't seem to be able to force the one that I need.

Any help is appreciated.

Community
  • 1
  • 1
Petre Popescu
  • 1,950
  • 3
  • 24
  • 38

1 Answers1

0

The solution (or a workaround) was to use call() instead of apply():

val result = call(filesController.createFileMetadata(), goodRequest)

Now everything works as intended.

Petre Popescu
  • 1,950
  • 3
  • 24
  • 38