Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add validation for openmanage host ip #785

Merged
merged 1 commit into from
May 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,11 @@ public void validateFacilityServer(FacilitySoftwareConfig config)
"Invalid user name or password.", e.getCause());
} else if (HttpStatus.FORBIDDEN.equals(e.getStatusCode())) {
throw new WormholeRequestException(HttpStatus.FORBIDDEN, "403 Forbidden", e.getCause());
} else if(HttpStatus.NOT_FOUND.equals(e.getStatusCode())) {
throw new WormholeRequestException(HttpStatus.BAD_REQUEST, "Unknown Host.Please check your server IP.", e.getCause(),
WormholeRequestException.UnknownHostCode);
}
throw new WormholeRequestException(HttpStatus.BAD_REQUEST, e.getMessage(), e.getCause());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.restdocs.JUnitRestDocumentation;
import org.springframework.restdocs.payload.FieldDescriptor;
Expand All @@ -44,6 +45,7 @@
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.context.WebApplicationContext;

import com.fasterxml.jackson.core.JsonProcessingException;
Expand Down Expand Up @@ -159,10 +161,6 @@ public void createAnFacilitySoftwareThrowException() throws JsonProcessingExcept
@Test
public void createOpenManageThrowConnectException() throws JsonProcessingException, Exception {
Mockito.doReturn(createuser()).when(tokenService).getCurrentUser(any());
ListOperations<String, String> listOperations = Mockito.mock(ListOperations.class);
Mockito.doReturn(0L).when(listOperations).leftPush(Mockito.anyString(), Mockito.anyString());
Mockito.doReturn(listOperations).when(template).opsForList();
Mockito.doNothing().when(publisher).publish(Mockito.anyString(), Mockito.anyString());
expectedEx.expect(WormholeRequestException.class);
expectedEx.expectMessage("Connect failed.");
FacilitySoftwareConfig facilitySoftware = createFacilitySoftware();
Expand All @@ -187,10 +185,6 @@ public void createOpenManageThrowAuthException() throws JsonProcessingException,
Mockito.doReturn(false).when(openmanageAuth).auth(Mockito.any(FacilitySoftwareConfig.class));
Mockito.doReturn(createuser()).when(tokenService).getCurrentUser(any());
Mockito.doReturn(openmanageAuth).when(serverValidationService).createOpenManageAuth();
ListOperations<String, String> listOperations = Mockito.mock(ListOperations.class);
Mockito.doReturn(0L).when(listOperations).leftPush(Mockito.anyString(), Mockito.anyString());
Mockito.doReturn(listOperations).when(template).opsForList();
Mockito.doNothing().when(publisher).publish(Mockito.anyString(), Mockito.anyString());
expectedEx.expect(WormholeRequestException.class);
expectedEx.expectMessage("Invalid user name or password");
FacilitySoftwareConfig facilitySoftware = createFacilitySoftware();
Expand All @@ -209,6 +203,29 @@ public void createOpenManageThrowAuthException() throws JsonProcessingException,

}

@Test
public void createOpenManageThrowNotFoundException() throws JsonProcessingException, Exception {
OpenManageAuth openmanageAuth = Mockito.mock(OpenManageAuth.class);
Mockito.doThrow(new HttpClientErrorException(HttpStatus.NOT_FOUND,"Not Found")).when(openmanageAuth).auth(Mockito.any(FacilitySoftwareConfig.class));
Mockito.doReturn(createuser()).when(tokenService).getCurrentUser(any());
Mockito.doReturn(openmanageAuth).when(serverValidationService).createOpenManageAuth();
expectedEx.expect(WormholeRequestException.class);
expectedEx.expectMessage("Unknown Host.Please check your server IP.");
FacilitySoftwareConfig facilitySoftware = createFacilitySoftware();
facilitySoftware.setType(SoftwareType.OpenManage);
facilitySoftware.setName("myOpenManage");
MvcResult result = this.mockMvc
.perform(post("/v1/facilitysoftware").contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(facilitySoftware)))
.andExpect(status().is4xxClientError())
.andReturn();
if (result.getResolvedException() != null) {
throw result.getResolvedException();
} else {
TestCase.fail();
}
}

@Test
public void syncFacilityServerDataExample() throws JsonProcessingException, Exception {
expectedEx.expect(WormholeRequestException.class);
Expand Down