com.jevontech.wabl.TokenUtilsTest.java Source code

Java tutorial

Introduction

Here is the source code for com.jevontech.wabl.TokenUtilsTest.java

Source

package com.jevontech.wabl;

import com.jevontech.wabl.entities.SecurityUser;
import com.jevontech.wabl.security.TokenUtils;
import com.jevontech.wabl.services.WablConfigurationService;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.mockito.BDDMockito.given;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.mobile.device.Device;
import org.springframework.security.core.userdetails.UserDetails;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author jeroen
 */
//@RunWith(SpringRunner.class)
//@SpringBootTest
//@ComponentScan(basePackages = {"com.jevontech.wabl.security" })
@RunWith(MockitoJUnitRunner.class)
public class TokenUtilsTest {

    private static final String AUDIENCE_UNKNOWN = "unknown";
    private static final String AUDIENCE_WEB = "web";
    private static final String AUDIENCE_MOBILE = "mobile";
    private static final String AUDIENCE_TABLET = "tablet";

    private final String SUB = "sub";
    private final String CREATED = "created";
    private final String AUDIENCE = "audience";

    @Mock
    Device device;

    @Mock
    UserDetails userDetails;

    @Mock
    SecurityUser securityUser;

    @Mock
    WablConfigurationService configurationService;

    @InjectMocks
    TokenUtils tokenUtils = new TokenUtils();

    @Test
    public void generateToken_normal() {

        given(configurationService.getSecret()).willReturn("A");
        given(configurationService.getExpirationTime()).willReturn(new Long(1));
        given(device.isNormal()).willReturn(true);
        given(userDetails.getUsername()).willReturn("test");
        //given(userDetails.getPassword()).willReturn("password");
        //UserDetails userDetails = (UserDetails) new TestUserDetails();
        String S = tokenUtils.generateToken(userDetails, device);

    }

    @Test
    public void getUserNameFromToken() {

        given(configurationService.getSecret()).willReturn("A");
        given(configurationService.getExpirationTime()).willReturn(new Long(1));
        given(device.isNormal()).willReturn(true);
        given(userDetails.getUsername()).willReturn("test");
        //given(userDetails.getPassword()).willReturn("password");
        //UserDetails userDetails = (UserDetails) new TestUserDetails();
        String token = tokenUtils.generateToken(userDetails, device);
        String username = tokenUtils.getUsernameFromToken(token);
        assertEquals("username should be test", username, "test");

    }

    @Test
    public void generateToken_tablet() {

        given(configurationService.getSecret()).willReturn("A");
        given(configurationService.getExpirationTime()).willReturn(new Long(1));
        given(device.isNormal()).willReturn(false);
        given(userDetails.getUsername()).willReturn("test");
        //given(userDetails.getPassword()).willReturn("password");
        String S = tokenUtils.generateToken(userDetails, device);

    }

    @Test
    public void validateToken_valid() {

        given(configurationService.getSecret()).willReturn("A");
        given(configurationService.getExpirationTime()).willReturn(new Long(1));
        given(device.isNormal()).willReturn(true);
        given(userDetails.getUsername()).willReturn("test");
        //given(userDetails.getPassword()).willReturn("password");
        given(securityUser.getUsername()).willReturn("test");
        //given(securityUser.getPassword()).willReturn("password");

        String token = tokenUtils.generateToken(userDetails, device);
        Boolean valid = tokenUtils.validateToken(token, securityUser);
        assertTrue("token should be valid", valid);

    }

    @Test
    public void validateToken_invalid() {

        given(configurationService.getSecret()).willReturn("A");
        given(configurationService.getExpirationTime()).willReturn(new Long(1));
        given(device.isNormal()).willReturn(true);
        given(userDetails.getUsername()).willReturn("test");

        String token = tokenUtils.generateToken(userDetails, device);
        token = token + "a";
        Boolean valid = tokenUtils.validateToken(token, securityUser);
        assertFalse("token should be invalid", valid);

    }

}