Search in sources :

Example 1 with NacosRestTemplate

use of com.alibaba.nacos.common.http.client.NacosRestTemplate in project nacos by alibaba.

the class SecurityProxyTest method testTestLoginFailCode.

@Test
public void testTestLoginFailCode() throws Exception {
    NacosRestTemplate nacosRestTemplate = mock(NacosRestTemplate.class);
    HttpRestResult<Object> result = new HttpRestResult<>();
    result.setCode(400);
    when(nacosRestTemplate.postForm(any(), (Header) any(), any(), any(), any())).thenReturn(result);
    Properties properties = new Properties();
    properties.setProperty(PropertyKeyConst.USERNAME, "aaa");
    properties.setProperty(PropertyKeyConst.PASSWORD, "123456");
    SecurityProxy securityProxy = new SecurityProxy(properties, nacosRestTemplate);
    boolean ret = securityProxy.login("localhost");
    Assert.assertFalse(ret);
}
Also used : HttpRestResult(com.alibaba.nacos.common.http.HttpRestResult) Properties(java.util.Properties) NacosRestTemplate(com.alibaba.nacos.common.http.client.NacosRestTemplate) Test(org.junit.Test)

Example 2 with NacosRestTemplate

use of com.alibaba.nacos.common.http.client.NacosRestTemplate in project nacos by alibaba.

the class SecurityProxyTest method testLoginSuccess.

@Test
public void testLoginSuccess() throws Exception {
    // given
    NacosRestTemplate nacosRestTemplate = mock(NacosRestTemplate.class);
    HttpRestResult<Object> result = new HttpRestResult<>();
    result.setData("{\"accessToken\":\"ttttttttttttttttt\",\"tokenTtl\":1000}");
    result.setCode(200);
    when(nacosRestTemplate.postForm(any(), (Header) any(), any(), any(), any())).thenReturn(result);
    Properties properties = new Properties();
    properties.setProperty(PropertyKeyConst.USERNAME, "aaa");
    properties.setProperty(PropertyKeyConst.PASSWORD, "123456");
    SecurityProxy securityProxy = new SecurityProxy(properties, nacosRestTemplate);
    // when
    boolean ret = securityProxy.login("localhost");
    // then
    Assert.assertTrue(ret);
}
Also used : HttpRestResult(com.alibaba.nacos.common.http.HttpRestResult) Properties(java.util.Properties) NacosRestTemplate(com.alibaba.nacos.common.http.client.NacosRestTemplate) Test(org.junit.Test)

Example 3 with NacosRestTemplate

use of com.alibaba.nacos.common.http.client.NacosRestTemplate in project nacos by alibaba.

the class SecurityProxyTest method testGetAccessToken.

@Test
public void testGetAccessToken() throws Exception {
    NacosRestTemplate nacosRestTemplate = mock(NacosRestTemplate.class);
    HttpRestResult<Object> result = new HttpRestResult<>();
    result.setData("{\"accessToken\":\"abc\",\"tokenTtl\":1000}");
    result.setCode(200);
    when(nacosRestTemplate.postForm(any(), (Header) any(), any(), any(), any())).thenReturn(result);
    Properties properties = new Properties();
    properties.setProperty(PropertyKeyConst.USERNAME, "aaa");
    properties.setProperty(PropertyKeyConst.PASSWORD, "123456");
    SecurityProxy securityProxy = new SecurityProxy(properties, nacosRestTemplate);
    securityProxy.login("localhost");
    String accessToken = securityProxy.getAccessToken();
    Assert.assertEquals("abc", accessToken);
}
Also used : HttpRestResult(com.alibaba.nacos.common.http.HttpRestResult) Properties(java.util.Properties) NacosRestTemplate(com.alibaba.nacos.common.http.client.NacosRestTemplate) Test(org.junit.Test)

Example 4 with NacosRestTemplate

use of com.alibaba.nacos.common.http.client.NacosRestTemplate in project nacos by alibaba.

the class SecurityProxyTest method testIsEnabled.

@Test
public void testIsEnabled() throws Exception {
    NacosRestTemplate nacosRestTemplate = mock(NacosRestTemplate.class);
    Properties properties = new Properties();
    properties.setProperty(PropertyKeyConst.USERNAME, "aaa");
    properties.setProperty(PropertyKeyConst.PASSWORD, "123456");
    SecurityProxy securityProxy = new SecurityProxy(properties, nacosRestTemplate);
    Assert.assertTrue(securityProxy.isEnabled());
}
Also used : Properties(java.util.Properties) NacosRestTemplate(com.alibaba.nacos.common.http.client.NacosRestTemplate) Test(org.junit.Test)

Example 5 with NacosRestTemplate

use of com.alibaba.nacos.common.http.client.NacosRestTemplate in project nacos by alibaba.

the class NacosClientAuthServiceImplTest method testLoginSuccess.

@Test
public void testLoginSuccess() throws Exception {
    // given
    NacosRestTemplate nacosRestTemplate = mock(NacosRestTemplate.class);
    HttpRestResult<Object> result = new HttpRestResult<>();
    result.setData("{\"accessToken\":\"ttttttttttttttttt\",\"tokenTtl\":1000}");
    result.setCode(200);
    when(nacosRestTemplate.postForm(any(), (Header) any(), any(), any(), any())).thenReturn(result);
    Properties properties = new Properties();
    properties.setProperty(PropertyKeyConst.USERNAME, "aaa");
    properties.setProperty(PropertyKeyConst.PASSWORD, "123456");
    List<String> serverList = new ArrayList<>();
    serverList.add("localhost");
    NacosClientAuthServiceImpl nacosClientAuthService = new NacosClientAuthServiceImpl();
    nacosClientAuthService.setServerList(serverList);
    nacosClientAuthService.setNacosRestTemplate(nacosRestTemplate);
    // when
    boolean ret = nacosClientAuthService.login(properties);
    // then
    Assert.assertTrue(ret);
}
Also used : HttpRestResult(com.alibaba.nacos.common.http.HttpRestResult) ArrayList(java.util.ArrayList) Properties(java.util.Properties) NacosRestTemplate(com.alibaba.nacos.common.http.client.NacosRestTemplate) Test(org.junit.Test)

Aggregations

NacosRestTemplate (com.alibaba.nacos.common.http.client.NacosRestTemplate)37 Test (org.junit.Test)33 Properties (java.util.Properties)32 HttpRestResult (com.alibaba.nacos.common.http.HttpRestResult)29 Field (java.lang.reflect.Field)19 ServiceInfoHolder (com.alibaba.nacos.client.naming.cache.ServiceInfoHolder)18 ServerListManager (com.alibaba.nacos.client.naming.core.ServerListManager)18 SecurityProxy (com.alibaba.nacos.client.security.SecurityProxy)18 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)5 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)5 Instance (com.alibaba.nacos.api.naming.pojo.Instance)3 Service (com.alibaba.nacos.api.naming.pojo.Service)3 NoneSelector (com.alibaba.nacos.api.selector.NoneSelector)3 ServiceInfo (com.alibaba.nacos.api.naming.pojo.ServiceInfo)2 NacosException (com.alibaba.nacos.api.exception.NacosException)1 BeatInfo (com.alibaba.nacos.client.naming.beat.BeatInfo)1 DefaultHttpClientRequest (com.alibaba.nacos.common.http.client.request.DefaultHttpClientRequest)1 JdkHttpClientRequest (com.alibaba.nacos.common.http.client.request.JdkHttpClientRequest)1 SelfHostnameVerifier (com.alibaba.nacos.common.tls.SelfHostnameVerifier)1