org.apigw.authserver.web.controller.TokensControllerTest.java Source code

Java tutorial

Introduction

Here is the source code for org.apigw.authserver.web.controller.TokensControllerTest.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.web.controller;

import org.apigw.authserver.svc.repository.AuthorizationGrantRepository;
import org.apigw.authserver.types.domain.AuthorizationGrant;
import org.apigw.authserver.types.domain.CertifiedClientPermission;
import org.apigw.authserver.types.domain.Permission;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.test.context.transaction.TransactionConfiguration;

import java.util.*;

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

/**
 * Created by martin on 27/02/15.
 */
@RunWith(MockitoJUnitRunner.class)
@TransactionConfiguration(defaultRollback = true)
public class TokensControllerTest {

    private static final String CLIENT = "client";
    private static final String READ_SCOPE = "SCOPE_READ";
    private static final String WRITE_SCOPE = "SCOPE_WRITE";
    public static final String TOKEN_VALUE = "ABC";

    private Date checkDate;

    @Mock
    private Authentication authentication;

    @Mock
    private SecurityContext securityContext;

    @Mock
    private AuthorizationGrantRepository authorizationGrantRepository;

    @InjectMocks
    private TokensController tokensController;

    @Before
    public void setup() {
        checkDate = new Date(1000);
        mockSecurityContext();
        when(authorizationGrantRepository.findByAccessTokenAndClientIdJoinRoles(anyString(), anyString()))
                .thenReturn(buildAuthorizationGrant());
    }

    @After
    public void clean() {
        authorizationGrantRepository.deleteAll();

    }

    @Test
    public void testGetTokenStatus() throws Exception {
        Map<String, Object> tokenMap = tokensController.getTokenStatus(null);
        String issueDateString = (String) tokenMap.get("issueDate");
        Date issueDate = tokensController.sdf.parse(issueDateString);
        assertEquals(0, issueDate.compareTo(checkDate));
        String accessTokenExpiresString = (String) tokenMap.get("accessTokenExpires");
        Date accessTokenExpires = tokensController.sdf.parse(accessTokenExpiresString);
        assertEquals(0, accessTokenExpires.compareTo(checkDate));
        List<String> scopes = (List<String>) tokenMap.get("scope");
        assertEquals(2, scopes.size());
        assertEquals(TOKEN_VALUE, tokenMap.get("accessToken"));
    }

    private void mockSecurityContext() {
        Mockito.when(securityContext.getAuthentication()).thenReturn(authentication);
        Mockito.when(authentication.getPrincipal()).thenReturn(new UserDetails() {
            @Override
            public boolean isEnabled() {
                return true;
            }

            @Override
            public boolean isCredentialsNonExpired() {
                return false;
            }

            @Override
            public boolean isAccountNonLocked() {
                return false;
            }

            @Override
            public boolean isAccountNonExpired() {
                return false;
            }

            @Override
            public String getUsername() {
                return CLIENT;
            }

            @Override
            public String getPassword() {
                return null;
            }

            @Override
            public Collection<? extends GrantedAuthority> getAuthorities() {
                Collection<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
                authorities.add(new GrantedAuthorityImpl("A_ROLE"));
                return authorities;
            }
        });
        SecurityContextHolder.setContext(securityContext);
    }

    private AuthorizationGrant buildAuthorizationGrant() {
        System.out.println(checkDate);
        Permission scopeReadSchedule = new Permission();
        scopeReadSchedule.setName(READ_SCOPE);
        Permission scopeWriteSchedule = new Permission();
        scopeWriteSchedule.setName(WRITE_SCOPE);
        CertifiedClientPermission ccPermissionRead = new CertifiedClientPermission();
        ccPermissionRead.setPermission(scopeReadSchedule);
        CertifiedClientPermission ccPermissionWrite = new CertifiedClientPermission();
        ccPermissionWrite.setPermission(scopeWriteSchedule);
        AuthorizationGrant authorizationGrant = new AuthorizationGrant();
        authorizationGrant.setGrantedPermissions(Arrays.asList(ccPermissionRead, ccPermissionWrite));
        authorizationGrant.setAccessToken(TOKEN_VALUE);
        authorizationGrant.setAccessTokenExpires(checkDate);
        authorizationGrant.setClientId(CLIENT);
        authorizationGrant.setIssueDate(checkDate);
        authorizationGrant.setResidentIdentificationNumber(null);
        authorizationGrant.setVersion(0);
        return authorizationGrant;
    }
}