org.apigw.authserver.x509.CertifiedClientAuthenticationUserDetailsServiceTest.java Source code

Java tutorial

Introduction

Here is the source code for org.apigw.authserver.x509.CertifiedClientAuthenticationUserDetailsServiceTest.java

Source

/**
 *   Copyright 2013 Stockholm County Council
 *
 *   This file is part of APIGW
 *
 *   APIGW is free software; you can redistribute it and/or modify
 *   it under the terms of version 2.1 of the GNU Lesser General Public
 *   License as published by the Free Software Foundation.
 *
 *   APIGW is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public
 *   License along with APIGW; if not, write to the
 *   Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 *   Boston, MA 02111-1307  USA
 *
 */
package org.apigw.authserver.x509;

import org.apigw.authserver.svc.CertifiedClientDetailsService;
import org.apigw.authserver.types.domain.CertifiedClient;
import org.joda.time.DateTime;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken;

import java.math.BigInteger;
import java.util.Collections;
import java.util.Date;

import static org.junit.Assert.*;
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.*;

public class CertifiedClientAuthenticationUserDetailsServiceTest {

    @Mock
    private CertifiedClientDetailsService service;
    @Mock
    private PreAuthenticatedAuthenticationToken token;
    private CertifiedClientAuthenticationUserDetailsService uut;

    @Before
    public void initMocks() {
        MockitoAnnotations.initMocks(this);
        uut = new CertifiedClientAuthenticationUserDetailsService();
        uut.setClientDetailsService(service);
    }

    @SuppressWarnings("unchecked")
    @Test
    public void loadUserDetailsByIssuerDnAndSubjectDn() {

        String subjectDN = "CN=dianne,OU=Spring Security,O=Spring Framework,C=AU";
        String issuerDN = "CN=Spring Security Test CA,OU=Spring Security,O=Spring Framework,L=Glasgow,ST=Scotland,C=GB";
        CertifiedClient client = new CertifiedClient();
        client.setClientId("clientA");
        client.setCertifiedClientRoles(Collections.EMPTY_LIST);

        X509ClientPrincipal principal = new X509ClientPrincipal(subjectDN, issuerDN);
        when(token.getPrincipal()).thenReturn(principal);
        when(token.getName()).thenReturn("clientA");
        when(service.loadClientByX509Cert(issuerDN, subjectDN)).thenReturn(client);
        UserDetails user = uut.loadUserDetails(token);
        assertEquals(user.getUsername(), client.getClientId());
        verify(service).loadClientByX509Cert(issuerDN, subjectDN);
    }

    @SuppressWarnings("unchecked")
    @Test(expected = UsernameNotFoundException.class)
    public void loadUserDetailsThrowsNotFoundException() {
        String subjectDN = "CN=dianne,OU=Spring Security,O=Spring Framework,C=AU";
        String issuerDN = "CN=Spring Security Test CA,OU=Spring Security,O=Spring Framework,L=Glasgow,ST=Scotland,C=GB";
        CertifiedClient client = new CertifiedClient();
        client.setClientId("clientA");
        client.setCertifiedClientRoles(Collections.EMPTY_LIST);

        X509ClientPrincipal principal = new X509ClientPrincipal(subjectDN, issuerDN);
        when(token.getPrincipal()).thenReturn(principal);
        when(token.getName()).thenReturn("clientA");
        when(service.loadClientByX509Cert(issuerDN, subjectDN)).thenThrow(UsernameNotFoundException.class);
        UserDetails user = uut.loadUserDetails(token);
    }

    @Test
    public void testHasExpired() {
        uut = spy(new CertifiedClientAuthenticationUserDetailsService());
        CertifiedClient client = new CertifiedClient();
        client.setExpireDate(DateTime.parse("2014-06-19T14:00:00.000Z").toDate());
        Date current = DateTime.parse("2014-06-20T14:00:00.000Z").toDate();
        doReturn(current).when(uut).getCurrentDate();
        assertTrue("Has expired", uut.hasExpired(client));
        client.setExpireDate(DateTime.parse("2014-06-20T14:00:00.000Z").toDate());
        assertFalse("Same day", uut.hasExpired(client));
        client.setExpireDate(DateTime.parse("2014-06-20T13:00:00.000Z").toDate());
        assertFalse("Same day, ignore time", uut.hasExpired(client));
        client.setExpireDate(DateTime.parse("2014-06-21T14:00:00.000Z").toDate());
        assertFalse("One day left", uut.hasExpired(client));
    }

}