Skip to content

Commit

Permalink
[ISSUE alibaba#8417] add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
onewe committed May 23, 2022
1 parent 5203788 commit 02fc0ed
Showing 1 changed file with 15 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,26 @@
import com.alibaba.nacos.plugin.auth.impl.constant.AuthSystemTypes;
import com.alibaba.nacos.plugin.auth.impl.users.NacosUser;
import com.fasterxml.jackson.databind.JsonNode;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.mock.web.MockHttpSession;
import org.springframework.web.HttpSessionRequiredException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Properties;

import static org.hamcrest.CoreMatchers.instanceOf;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
Expand Down Expand Up @@ -93,6 +99,15 @@ public void testLoginWithAuthedUser() throws AccessException {
assertTrue(actualString.contains("\"globalAdmin\":true"));
}

@Test
public void testSessionExpiredThrowHttpSessionRequiredException() throws IOException {
when(authConfigs.isAuthEnabled()).thenReturn(true);
when(request.getSession()).thenReturn(new MockHttpSession());
Object o = userController.updateUser("nacos", "qwe123", response, request);
Assert.assertNull(o);
verify(response).sendError(eq(HttpServletResponse.SC_UNAUTHORIZED), eq("session expired!"));
}

private void injectObject(String fieldName, Object value) throws NoSuchFieldException, IllegalAccessException {
Field field = UserController.class.getDeclaredField(fieldName);
field.setAccessible(true);
Expand Down

0 comments on commit 02fc0ed

Please sign in to comment.