com.hp.autonomy.frontend.configuration.authentication.IdolPreAuthenticatedAuthenticationProviderTest.java Source code

Java tutorial

Introduction

Here is the source code for com.hp.autonomy.frontend.configuration.authentication.IdolPreAuthenticatedAuthenticationProviderTest.java

Source

/*
 * Copyright 2014-2017 Hewlett-Packard Development Company, L.P.
 * Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
 */

package com.hp.autonomy.frontend.configuration.authentication;

import com.autonomy.aci.client.services.AciErrorException;
import com.hp.autonomy.user.UserRoles;
import com.hp.autonomy.user.UserService;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.mapping.GrantedAuthoritiesMapper;
import org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken;

import java.security.Principal;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.stream.Collectors;

import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class IdolPreAuthenticatedAuthenticationProviderTest {
    private static final String SAMPLE_USER = "some_user";

    private AuthenticationProvider authenticationProvider;

    @Mock
    private UserService userService;

    @Mock
    private GrantedAuthoritiesMapper authoritiesMapper;

    @Mock
    private Authentication authentication;

    @Mock
    private Principal principal;

    @SuppressWarnings("unchecked")
    @Before
    public void setUp() {
        when(authentication.getPrincipal()).thenReturn(principal);
        when(principal.toString()).thenReturn(SAMPLE_USER);

        when(authoritiesMapper.mapAuthorities(any()))
                .thenAnswer(invocation -> ((Collection<? extends GrantedAuthority>) invocation.getArgumentAt(0,
                        Collection.class)).stream().map(x -> mock(GrantedAuthority.class))
                                .collect(Collectors.toList()));

        authenticationProvider = new IdolPreAuthenticatedAuthenticationProvider(userService, authoritiesMapper,
                new HashSet<>(Arrays.asList("SomeRole", "SomeOtherRole")));
    }

    @Test
    public void authenticateWithExistingUser() {
        when(userService.getUser(SAMPLE_USER, true)).thenReturn(new UserRoles(SAMPLE_USER));
        final Authentication communityAuthentication = authenticationProvider.authenticate(authentication);
        assertTrue(communityAuthentication.isAuthenticated());
        assertThat(communityAuthentication.getAuthorities(), hasSize(2));
    }

    @Test(expected = BadCredentialsException.class)
    public void authenticateWithNoPrincipal() {
        when(authentication.getPrincipal()).thenReturn(null);
        authenticationProvider.authenticate(authentication);
    }

    @Test(expected = AciErrorException.class)
    public void communityError() {
        when(userService.getUser(SAMPLE_USER, true)).thenThrow(new AciErrorException());
        authenticationProvider.authenticate(authentication);
    }

    @Test
    public void supports() {
        assertTrue(authenticationProvider.supports(PreAuthenticatedAuthenticationToken.class));
        assertFalse(authenticationProvider.supports(UsernamePasswordAuthenticationToken.class));
    }
}