Example usage for org.springframework.security.authentication TestingAuthenticationToken TestingAuthenticationToken

List of usage examples for org.springframework.security.authentication TestingAuthenticationToken TestingAuthenticationToken

Introduction

In this page you can find the example usage for org.springframework.security.authentication TestingAuthenticationToken TestingAuthenticationToken.

Prototype

public TestingAuthenticationToken(Object principal, Object credentials) 

Source Link

Usage

From source file:fi.helsinki.opintoni.SpringTest.java

protected void configureStudentSecurityContext() {
    SecurityContext securityContext = SecurityContextHolder.getContext();
    securityContext.setAuthentication(new TestingAuthenticationToken(
            new AppUser.AppUserBuilder().studentNumber(TestConstants.STUDENT_NUMBER)
                    .eduPersonPrincipalName("opiskelija@helsinki.fi")
                    .eduPersonAffiliations(
                            Arrays.asList(SAMLEduPersonAffiliation.MEMBER, SAMLEduPersonAffiliation.STUDENT))
                    .eduPersonPrimaryAffiliation(SAMLEduPersonAffiliation.STUDENT).oodiPersonId("1111").build(),
            ""));
}

From source file:org.cloudfoundry.identity.uaa.oauth.TokenAdminEndpointsTests.java

@Test
public void testRevokeTokenForUserWithTokenId() throws Exception {
    DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken("FOO");
    token.setAdditionalInformation(Collections.<String, Object>singletonMap(JwtTokenEnhancer.TOKEN_ID, "BAR"));
    Mockito.when(tokenServices.findTokensByUserName("marissa"))
            .thenReturn(Collections.<OAuth2AccessToken>singleton(token));
    Mockito.when(tokenServices.revokeToken("FOO")).thenReturn(true);
    SimpleMessage result = endpoints.revokeUserToken("marissa", "BAR",
            new TestingAuthenticationToken("marissa", ""), false);
    assertEquals("ok", result.getStatus());
}

From source file:fi.helsinki.opintoni.SpringTest.java

protected void configureTeacherSecurityContext() {
    SecurityContext securityContext = SecurityContextHolder.getContext();
    securityContext.setAuthentication(new TestingAuthenticationToken(new AppUser.AppUserBuilder()
            .teacherNumber(TestConstants.TEACHER_NUMBER).eduPersonPrincipalName("opettaja@helsinki.fi")
            .eduPersonAffiliations(Arrays.asList(SAMLEduPersonAffiliation.FACULTY))
            .eduPersonPrimaryAffiliation(SAMLEduPersonAffiliation.FACULTY).teacherFacultyCode("A10000")
            .oodiPersonId("2222").build(), ""));
}

From source file:nl.ctrlaltdev.harbinger.whitelist.WhiteListParserTest.java

@Test
public void shouldParseFromFile() throws IOException {
    WhiteList wl = parser.parse(new File("./src/test/resources/whitelist/test-whitelist.txt"));

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setRemoteAddr("127.0.0.1");
    Evidence ev = new Evidence(new Evidence(request), new TestingAuthenticationToken("admin", ""));

    assertTrue(wl.isWhitelisted(ev));/*  w  w  w . j a va 2s .  c  om*/

    DetectionRule rule = new DetectionRule(new String[] { "", "HIGH", "" });
    ev = new Evidence(new Evidence(request), rule, "test", "1");

    assertTrue(wl.isWhitelisted(ev));

    request.setRemoteAddr("127.0.0.2");
    assertTrue(wl.isWhitelisted(new Evidence(request)));
    request.setRemoteAddr("127.0.0.3");
    assertTrue(wl.isWhitelisted(new Evidence(request)));

    request.setRemoteAddr("127.0.0.4");
    assertFalse(wl.isWhitelisted(new Evidence(request)));
}

From source file:org.cloudfoundry.identity.uaa.oauth.TokenAdminEndpointsTests.java

@Test(expected = NoSuchTokenException.class)
public void testRevokeInvalidTokenForUser() throws Exception {
    OAuth2AccessToken token = new DefaultOAuth2AccessToken("BAR");
    Mockito.when(tokenServices.findTokensByUserName("marissa")).thenReturn(Collections.singleton(token));
    SimpleMessage result = endpoints.revokeUserToken("marissa", "FOO",
            new TestingAuthenticationToken("marissa", ""), false);
    assertEquals("ok", result.getStatus());
}

From source file:com.jayway.restassured.module.mockmvc.SecuredControllerTest.java

@Test
public void statically_defined_auth_has_precedence_over_statically_defined_request_spec() {
    RestAssuredMockMvc.authentication = principal(
            new User("authorized_user", "password", Collections.<GrantedAuthority>emptyList()));
    RestAssuredMockMvc.requestSpecification = new MockMvcRequestSpecBuilder()
            .setAuth(authentication(new TestingAuthenticationToken("name", "pw"))).build();

    try {//from ww w . ja  va 2s  .c  om
        given().standaloneSetup(new SecuredController()).param("name", "Johan").when()
                .get("/springSecurityGreeting").then().statusCode(200)
                .body("content", equalTo("Hello, Johan!"));
    } finally {
        RestAssuredMockMvc.reset();
    }
}

From source file:io.restassured.module.mockmvc.SecuredControllerTest.java

@Test
public void statically_defined_auth_has_precedence_over_statically_defined_request_spec() {
    RestAssuredMockMvc.authentication = RestAssuredMockMvc
            .principal(new User("authorized_user", "password", Collections.<GrantedAuthority>emptyList()));
    RestAssuredMockMvc.requestSpecification = new MockMvcRequestSpecBuilder()
            .setAuth(RestAssuredMockMvc.authentication(new TestingAuthenticationToken("name", "pw"))).build();

    try {/* w  w  w .  ja  v a2s  .c  om*/
        RestAssuredMockMvc.given().standaloneSetup(new SecuredController()).param("name", "Johan").when()
                .get("/springSecurityGreeting").then().statusCode(200)
                .body("content", equalTo("Hello, Johan!"));
    } finally {
        RestAssuredMockMvc.reset();
    }
}

From source file:org.cloudfoundry.identity.uaa.oauth.TokenAdminEndpointsTests.java

@Test(expected = NoSuchTokenException.class)
public void testRevokeNullTokenForUser() throws Exception {
    SimpleMessage result = endpoints.revokeUserToken("marissa", null,
            new TestingAuthenticationToken("marissa", ""), false);
    assertEquals("ok", result.getStatus());
}

From source file:org.cloudfoundry.identity.uaa.oauth.TokenAdminEndpointsTests.java

@Test(expected = AccessDeniedException.class)
public void testListTokensForWrongUser() throws Exception {
    Collection<OAuth2AccessToken> tokens = endpoints.listTokensForUser("barry",
            new TestingAuthenticationToken("marissa", ""), false);
    assertEquals(0, tokens.size());/*  www.  j ava 2s  .co  m*/
}

From source file:org.cloudfoundry.identity.uaa.oauth.TokenAdminEndpointsTests.java

@Test(expected = AccessDeniedException.class)
public void testListTokensForWrongOAuth2User() throws Exception {
    Collection<OAuth2AccessToken> tokens = endpoints.listTokensForUser("barry",
            new OAuth2Authentication(authorizationRequest, new TestingAuthenticationToken("marissa", "")),
            false);/* w  ww.java 2s. co m*/
    assertEquals(0, tokens.size());
}