test cases for basic authentication actor added

This commit is contained in:
Eckart Hertzler 2009-10-24 22:34:29 +02:00
parent d7a1944cc5
commit 1db43e7f03
3 changed files with 111 additions and 0 deletions

View file

@ -60,6 +60,28 @@
<artifactId>lift-util</artifactId> <artifactId>lift-util</artifactId>
<version>1.1-SNAPSHOT</version> <version>1.1-SNAPSHOT</version>
</dependency> </dependency>
<!-- For Testing -->
<dependency>
<groupId>org.scalatest</groupId>
<artifactId>scalatest</artifactId>
<version>1.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.8.0</version>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
</project> </project>

View file

@ -0,0 +1,15 @@
package se.scalablesolutions.akka.security
import junit.framework.Test
import junit.framework.TestCase
import junit.framework.TestSuite
object AllTest extends TestCase {
def suite(): Test = {
val suite = new TestSuite("All Scala tests")
suite.addTestSuite(classOf[BasicAuthenticatorSpec])
suite
}
def main(args: Array[String]) = junit.textui.TestRunner.run(suite)
}

View file

@ -0,0 +1,74 @@
/**
* Copyright (C) 2009 Scalable Solutions.
*/
package se.scalablesolutions.akka.security
import config.ScalaConfig._
import org.scalatest.Suite
import org.scalatest.junit.JUnitSuite
import org.scalatest.matchers.MustMatchers
import org.scalatest.mock.MockitoSugar
import org.mockito.Mockito._
import org.mockito.Matchers._
import org.junit.{Before, After, Test}
import _root_.javax.ws.rs.core.{SecurityContext,Context,Response}
import _root_.com.sun.jersey.spi.container.{ResourceFilterFactory,ContainerRequest,ContainerRequestFilter,ContainerResponse,ContainerResponseFilter,ResourceFilter}
import _root_.com.sun.jersey.core.util.Base64
class BasicAuthenticatorSpec extends junit.framework.TestCase with Suite with MockitoSugar with MustMatchers {
val authenticator = new BasicAuthenticator
authenticator.start
@Test def testChallenge = {
val req = mock[ContainerRequest]
val result: Response = (authenticator !? Authenticate(req, List("foo")))
// the actor replies with a challenge for the browser
result.getStatus must equal (Response.Status.UNAUTHORIZED.getStatusCode)
result.getMetadata.get("WWW-Authenticate").get(0).toString must startWith ("Basic")
}
@Test def testAuthenticationSuccess = {
val req = mock[ContainerRequest]
// fake a basic auth header -> this will authenticate the user
when(req.getHeaderValue("Authorization")).thenReturn("Basic " + new String(Base64.encode("foo:bar")))
// fake a request authorization -> this will authorize the user
when(req.isUserInRole("chef")).thenReturn(true)
val result: AnyRef = (authenticator !? Authenticate(req, List("chef")))
result must be (OK)
// the authenticator must have set a security context
verify(req).setSecurityContext(any[SecurityContext])
}
@Test def testUnauthorized = {
val req = mock[ContainerRequest]
// fake a basic auth header -> this will authenticate the user
when(req.getHeaderValue("Authorization")).thenReturn("Basic " + new String(Base64.encode("foo:bar")))
when(req.isUserInRole("chef")).thenReturn(false) // this will deny access
val result: Response = (authenticator !? Authenticate(req, List("chef")))
result.getStatus must equal (Response.Status.FORBIDDEN.getStatusCode)
// the authenticator must have set a security context
verify(req).setSecurityContext(any[SecurityContext])
}
class BasicAuthenticator extends BasicAuthenticationActor {
def verify(odc : Option[BasicCredentials]) : Option[UserInfo] = odc match {
case Some(dc) => Some(UserInfo("foo","bar","ninja" :: "chef" :: Nil))
case _ => None
}
override def realm = "test"
}
}