-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
821150d
commit 98a6e22
Showing
4 changed files
with
156 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
client/src/test/java/com/alibaba/nacos/client/auth/impl/process/HttpLoginProcessorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* Copyright 1999-2023 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.alibaba.nacos.client.auth.impl.process; | ||
|
||
import com.alibaba.nacos.api.common.Constants; | ||
import com.alibaba.nacos.client.auth.impl.NacosAuthLoginConstant; | ||
import com.alibaba.nacos.common.http.HttpRestResult; | ||
import com.alibaba.nacos.common.http.client.NacosRestTemplate; | ||
import com.alibaba.nacos.common.http.param.Header; | ||
import com.alibaba.nacos.common.http.param.Query; | ||
import com.alibaba.nacos.common.utils.JacksonUtils; | ||
import com.alibaba.nacos.plugin.auth.api.LoginIdentityContext; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Properties; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.anyMap; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.Mockito.when; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class HttpLoginProcessorTest { | ||
|
||
@Mock | ||
NacosRestTemplate restTemplate; | ||
|
||
@Mock | ||
HttpRestResult result; | ||
|
||
Properties properties; | ||
|
||
HttpLoginProcessor loginProcessor; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
loginProcessor = new HttpLoginProcessor(restTemplate); | ||
properties = new Properties(); | ||
} | ||
|
||
@Test | ||
void testGetResponseSuccess() throws Exception { | ||
properties.setProperty(NacosAuthLoginConstant.SERVER, "http://localhost:8848"); | ||
when(restTemplate.postForm(eq("http://localhost:8848/nacos/v1/auth/users/login"), eq(Header.EMPTY), | ||
any(Query.class), anyMap(), eq(String.class))).thenReturn(result); | ||
when(result.ok()).thenReturn(true); | ||
Map<String, String> mockMap = new HashMap<>(); | ||
mockMap.put(Constants.ACCESS_TOKEN, "mock_access_token"); | ||
mockMap.put(Constants.TOKEN_TTL, "100L"); | ||
when(result.getData()).thenReturn(JacksonUtils.toJson(mockMap)); | ||
LoginIdentityContext actual = loginProcessor.getResponse(properties); | ||
assertEquals("mock_access_token", actual.getParameter(NacosAuthLoginConstant.ACCESSTOKEN)); | ||
assertEquals("100L", actual.getParameter(NacosAuthLoginConstant.TOKENTTL)); | ||
} | ||
|
||
@Test | ||
void testGetResponseFailed() throws Exception { | ||
properties.setProperty(NacosAuthLoginConstant.SERVER, "localhost"); | ||
when(restTemplate.postForm(eq("http://localhost:8848/nacos/v1/auth/users/login"), eq(Header.EMPTY), | ||
any(Query.class), anyMap(), eq(String.class))).thenReturn(result); | ||
assertNull(loginProcessor.getResponse(properties)); | ||
} | ||
|
||
@Test | ||
void testGetResponseException() throws Exception { | ||
properties.setProperty(NacosAuthLoginConstant.SERVER, "localhost"); | ||
when(restTemplate.postForm(eq("http://localhost:8848/nacos/v1/auth/users/login"), eq(Header.EMPTY), | ||
any(Query.class), anyMap(), eq(String.class))).thenThrow(new RuntimeException("test")); | ||
assertNull(loginProcessor.getResponse(properties)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters