changed trapExit from Boolean to "trapExit = List(classOf[..], classOf[..])" + cleaned up security code

This commit is contained in:
jboner 2009-11-17 22:26:25 +01:00
parent 495adb7898
commit d7ac449d26
7 changed files with 465 additions and 371 deletions

View file

@ -13,12 +13,13 @@ 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_.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 {
class BasicAuthenticatorSpec extends junit.framework.TestCase
with Suite with MockitoSugar with MustMatchers {
val authenticator = new BasicAuthenticator
authenticator.start
@ -28,47 +29,46 @@ class BasicAuthenticatorSpec extends junit.framework.TestCase with Suite with Mo
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")
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)
result must be(OK)
// the authenticator must have set a security context
verify(req).setSecurityContext(any[SecurityContext])
}
@Test def testUnauthorized = {
val req = mock[ContainerRequest]
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])
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
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"
}
}
}