How to check the Zentasks sample application from Play 2.0

I play with Play 2.0, Scala. I am currently analyzing a sample Zentasks application .

One part of this application is the authentication mechanism, mainly covered by the feature Secured. I am wondering how I can check protected actions, for example. indexfrom project controller .

For an unprotected action, I will probably do something like

val result = controllers.Projects.index(FakeRequest())

to start an action and get its result.

What to do in case of a protected action?

Disclaimer: I am completely unfamiliar with both Scala and Play, so all the tips are very valuable. Thank!

+5
source share
2 answers

Playframewrk v2.1 backport 2.0.x

, ( Play 2.0.3+):

Helpers libs.

package libs

import play.api.mvc._

import play.api.libs.iteratee._
import play.api.libs.concurrent._
import play.api.test._

object Helpers {

  def routeAndCall[T](request: FakeRequest[T]): Option[Result] = {
    routeAndCall(this.getClass.getClassLoader.loadClass("Routes").asInstanceOf[Class[play.core.Router.Routes]], request)
  }
  /**
   * Use the Router to determine the Action to call for this request and executes it.
   */
  def routeAndCall[T, ROUTER <: play.core.Router.Routes](router: Class[ROUTER], request: FakeRequest[T]): Option[play.api.mvc.Result] = {
    val routes = router.getClassLoader.loadClass(router.getName + "$").getDeclaredField("MODULE$").get(null).asInstanceOf[play.core.Router.Routes]
    routes.routes.lift(request).map {
      case a: Action[_] =>
        val action = a.asInstanceOf[Action[T]]
        val parsedBody: Option[Either[play.api.mvc.Result, T]] = action.parser(request).fold(
          (a, in) => Promise.pure(Some(a)),
          k => Promise.pure(None),
          (msg, in) => Promise.pure(None)
        ).await.get

        parsedBody.map{resultOrT =>
          resultOrT.right.toOption.map{innerBody =>
            action(FakeRequest(request.method, request.uri, request.headers, innerBody))
          }.getOrElse(resultOrT.left.get)
        }.getOrElse(action(request))
    }
  }

}

, routeAndCall:

import libs.Helpers._
import play.api.test.Helpers.{routeAndCall => _,_}

Around ( application.secret, , cookie)

def appWithSecret():Map[String,String]={
    Map(("application.secret","the answer is 42 !"))
  }


  object emptyApp extends Around {
    def around[T <% Result](t: => T) = {
      running(FakeApplication(additionalConfiguration = inMemoryMongoDatabase("emptyApp")++appWithSecret())) {
        User(new ObjectId, "Jane Doe", "foobar@example.com", "id1").save()
        t // execute t inside a http session
      }
    }
  }

:

"respond to the index Action" in emptyApp {
      val request: FakeRequest[AnyContent] = FakeRequest(GET, "/expenses").withSession(("email", "foobar@example.com"))
      val Some(result) = routeAndCall(request)

      status(result) must equalTo(OK)
      contentType(result) must beSome("application/json")
      charset(result) must beSome("utf-8")
      contentAsString(result) must contain("Hello Bob")
    }

, unit test.

+2

, , .

trait InSecure trait extends Secured, . object InSecureProjects extends Projects with InSecture , - .

, Projects, InSecureProjects. .

, , ;)

+1

All Articles