CommonTestUtils.java 1.17 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
package pwc.taxtech.atms;

import java.util.Arrays;

import org.springframework.security.authentication.ProviderManager;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.test.util.ReflectionTestUtils;

import pwc.taxtech.atms.security.JwtAuthenticationProvider;
import pwc.taxtech.atms.security.JwtAuthenticationToken;
import pwc.taxtech.atms.security.JwtUtil;

public abstract class CommonTestUtils {
    public static void initTestAuth() {
        Authentication request = new JwtAuthenticationToken("xxxx");
        JwtAuthenticationProvider jwtAuthenticationProvider = new JwtAuthenticationProvider();
        JwtUtil jwtutil = new JwtUtil();
        jwtutil.setJwtBase64Secret("testkey");
        jwtutil.setJwtPowerToken("xxxx");
        ReflectionTestUtils.setField(jwtAuthenticationProvider, "jwtUtil", jwtutil);
        ProviderManager providerManager = new ProviderManager(Arrays.asList(jwtAuthenticationProvider));
        Authentication authenticaiton = providerManager.authenticate(request);
        SecurityContextHolder.getContext().setAuthentication(authenticaiton);
    }
}