package com.fanruan.api.decision.login; import com.fanruan.api.security.JwtKit; import com.fr.config.dao.DaoContext; import com.fr.config.dao.impl.LocalClassHelperDao; import com.fr.config.dao.impl.LocalEntityDao; import com.fr.config.dao.impl.LocalXmlEntityDao; import com.fr.decision.config.AppearanceConfig; import com.fr.decision.webservice.bean.authentication.LoginClientBean; import com.fr.decision.webservice.exception.login.LoginInfoNotAvailableException; import com.fr.decision.webservice.utils.DecisionStatusService; import com.fr.runtime.FineRuntime; import com.fr.store.StateHubManager; import com.fr.store.impl.MemoryLock; import com.fr.store.impl.MemoryStore; import com.fr.transaction.Configurations; import com.fr.transaction.LocalConfigurationHelper; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.lang.reflect.Method; /** * @author lidongy * @version 10.0 * Created by lidongy on 2019/10/25 */ public class LoginKitTest { @Before public void start() { FineRuntime.start(); DaoContext.setEntityDao(new LocalEntityDao()); DaoContext.setClassHelperDao(new LocalClassHelperDao()); DaoContext.setXmlEntityDao(new LocalXmlEntityDao()); Configurations.setHelper(new LocalConfigurationHelper()); StateHubManager.setStorage(new MemoryStore()); StateHubManager.setLock(new MemoryLock()); } @Test public void testRefreshToken() throws Exception { String jwtString = JwtKit.createDefaultJWT("123"); try { LoginKit.refreshToken(jwtString, 1000 * 60, null); } catch (Exception e) { Assert.assertTrue(e instanceof LoginInfoNotAvailableException); } LoginClientBean loginClientBean = new LoginClientBean(); DecisionStatusService.loginStatusService().put(jwtString, loginClientBean); try { LoginKit.refreshToken(jwtString, 1000 * 60, null); } catch (Exception e) { Assert.assertFalse(e instanceof LoginInfoNotAvailableException); } } @Test public void TestGetLoginPageId() { AppearanceConfig.getInstance().setLoginPageId("111"); Assert.assertEquals(LoginKit.getLoginPageId(), "111"); AppearanceConfig.getInstance().setLoginPageId("aaa"); Assert.assertEquals(LoginKit.getLoginPageId(), "aaa"); } @Test public void getUserNameFromRequest() throws Exception { Class classBook = Class.forName("com.fanruan.api.decision.login.LoginKit"); Assert.assertNotNull(classBook); Method method1 = classBook.getDeclaredMethod("getUserNameFromRequest", HttpServletRequest.class); Assert.assertNotNull(method1); Method method2 = classBook.getDeclaredMethod("getUserNameFromRequestCookie", HttpServletRequest.class); Assert.assertNotNull(method2); Method method3 = classBook.getDeclaredMethod("getDisplayNameFromRequest", HttpServletRequest.class); Assert.assertNotNull(method3); Method method4 = classBook.getDeclaredMethod("getDisplayNameFromRequestCookie", HttpServletRequest.class); Assert.assertNotNull(method4); Method method5 = classBook.getDeclaredMethod("getCurrentUserNameFromRequest", HttpServletRequest.class); Assert.assertNotNull(method5); Method method6 = classBook.getDeclaredMethod("getCurrentUserNameFromRequestCookie", HttpServletRequest.class); Assert.assertNotNull(method6); Method method7 = classBook.getDeclaredMethod("login", HttpServletRequest.class, HttpServletResponse.class, String.class); Assert.assertNotNull(method7); Method method8 = classBook.getDeclaredMethod("getTokenByHeader", HttpServletRequest.class); Assert.assertNotNull(method8); Method method9 = classBook.getDeclaredMethod("getTokenByCookie", HttpServletRequest.class); Assert.assertNotNull(method9); Method method10 = classBook.getDeclaredMethod("checkTokenValid", HttpServletRequest.class, String.class, String.class); Assert.assertNotNull(method10); } }