Unit Testing Controllers on the Play Platform with SecureSocial

I'm trying to invent some mocking SecureSocial action generators or SecureSocial itself to be able to test unit-test controller methods. I found several approaches, such as Unit testing methods protected with Securesocial annotation and Testing a Play2 application with SecureSocial using dependency injection , but the fact is that in these questions the authors, in fact, do not conduct unit testing, but test integration .

My unit tests look like this:

  trait MockDaoProvider extends IDaoProvider {
    def entityDao = entityDaoMock
  }

  val controller = new MyController with MockDaoProvider

  "MyController.list" should {
    "return an OK" in {
      entityDaoMock.list().returns(List())

      val result = controller.list()(FakeRequest())

      status(result) must equalTo(OK)
    }
  }

As you can see, I mocked the dependencies to isolate and test the behavior that the controller method actually performs.

, SecuredAction securesocial MyController.list. , . , , SecuredAction UserAwareAction securesocial. , (...). .

- ? , , ?

PS: Play Framework 2.2.1, securesocial - 2.1.2

+4
2

, , . jeantil :

class FakeAuthenticatorStore(app:Application) extends AuthenticatorStore(app) {
  var authenticator:Option[Authenticator] = None
  def save(authenticator: Authenticator): Either[Error, Unit] = {
    this.authenticator=Some(authenticator)
    Right()
  }
  def find(id: String): Either[Error, Option[Authenticator]] = {
    Some(authenticator.filter(_.id == id)).toRight(new Error("no such authenticator"))
  }
  def delete(id: String): Either[Error, Unit] = {
    this.authenticator=None
    Right()
  }
}

abstract class WithLoggedUser(val user:User,override val app: FakeApplication = FakeApplication()) extends WithApplication(app) with Mockito{
  lazy val mockUserService=mock[UserService]
  val identity=IdentityUser(Defaults.googleId, user)

  import helpers._
  import TestUsers._
  def cookie=Authenticator.create(identity) match {
    case Right(authenticator) => authenticator.toCookie
  }

  override def around[T: AsResult](t: =>T): execute.Result = super.around {
    mockUserService.find(Defaults.googleId) returns Some(identity)
    UserService.setService(mockUserService)
    t
  }
}

  val excludedPlugins=List(
    ,"service.login.MongoUserService"
    ,"securesocial.core.DefaultAuthenticatorStore"
  )
  val includedPlugins = List(
    "helpers.FakeAuthenticatorStore"
  )

  def minimalApp = FakeApplication(withGlobal =minimalGlobal, withoutPlugins=excludedPlugins,additionalPlugins = includedPlugins)

,

"create a new user password " in new WithLoggedUser(socialUser,minimalApp) {
  val controller = new TestController
  val req: Request[AnyContent] = FakeRequest().
    withHeaders((HeaderNames.CONTENT_TYPE, "application/x-www-form-urlencoded")).
    withCookies(cookie) // Fake cookie from the WithloggedUser trait

  val requestBody = Enumerator("password=foobarkix".getBytes) andThen Enumerator.eof
  val result = requestBody |>>> controller.create.apply(req)

  val actual: Int= status(result)
  actual must be equalTo 201
}
+3

, . " " . :

:

trait AbstractSecurity {
  def Secured(action: SecuredRequest[AnyContent] => Result): Action[AnyContent]
}

trait SecureSocialSecurity extends AbstractSecurity with securesocial.core.SecureSocial {
   def Secured(action: SecuredRequest[AnyContent] => Result): Action[AnyContent] = SecuredAction { action }
}

abstract class MyController extends Controller with AbstractSecurity {

  def entityDao: IEntityDao

  def list = Secured { request =>
    Ok(
      JsArray(entityDao.list())
    )
  }
}

object MyController extends MyController with PsqlDaoProvider with SecureSocialSecurity

:

 trait MockedSecurity extends AbstractSecurity {
    val user = Account(NotAssigned, IdentityId("test", "userpass"), "Test", "User",
      "Test user", Some("test@user.com"), AuthenticationMethod("userPassword"))

    def Secured(action: SecuredRequest[AnyContent] => play.api.mvc.Result): Action[AnyContent] = Action { request =>
      action(new SecuredRequest(user, request))
    }
  }


  val controller = new MyController with MockDaoProvider with MockedSecurity

  "IssueController.list" should {
    "return an OK" in {
      entityDaoMock.list().returns(List())

      val result = controller.list()(FakeRequest())

      status(result) must equalTo(OK)
    }
  }

- securesocial... ... ? , , .

+1

Source: https://habr.com/ru/post/1523791/


All Articles