-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1214 from WeBankPartners/query_code_permission
Query code permission
- Loading branch information
Showing
4 changed files
with
186 additions
and
12 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
109 changes: 109 additions & 0 deletions
109
cmdb-core/src/test/java/com/webank/cmdb/controller/ui/UIEnumManagementControllerTest.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,109 @@ | ||
package com.webank.cmdb.controller.ui; | ||
|
||
import com.webank.cmdb.controller.AbstractBaseControllerTest; | ||
import org.junit.Test; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.security.test.context.support.WithMockUser; | ||
|
||
import static com.webank.cmdb.domain.AdmMenu.*; | ||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.Matchers.notNullValue; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
public class UIEnumManagementControllerTest extends AbstractBaseControllerTest { | ||
private static final String QUERY_SYSTEM_CODE_URL = "/ui/v2/enum/system/codes"; | ||
private static final String QUERY_NON_SYSTEM_CODE_URL = "/ui/v2/enum/non-system/codes"; | ||
|
||
@Test | ||
@WithMockUser(value = "test") | ||
public void querySystemEnum_should_fail_for_user_without_appropriate_menu_permission() throws Exception { | ||
shouldApiCallFailForInsufficientPermission(QUERY_SYSTEM_CODE_URL); | ||
} | ||
|
||
@Test | ||
@WithMockUser(value = "test", authorities = { ROLE_PREFIX + MENU_IDC_PLANNING_DESIGN }) | ||
public void querySystemEnum_should_success_for_role_IDC_PLANNING_DESIGN() throws Exception { | ||
shouldApiCallSucceedSucceed(QUERY_SYSTEM_CODE_URL); | ||
} | ||
|
||
@Test | ||
@WithMockUser(value = "test", authorities = { ROLE_PREFIX + MENU_IDC_RESOURCE_PLANNING }) | ||
public void querySystemEnum_should_success_for_role_IDC_RESOURCE_PLANNING() throws Exception { | ||
shouldApiCallSucceedSucceed(QUERY_SYSTEM_CODE_URL); | ||
} | ||
|
||
@Test | ||
@WithMockUser(value = "test", authorities = { ROLE_PREFIX + MENU_APPLICATION_ARCHITECTURE_DESIGN }) | ||
public void querySystemEnum_should_success_for_role_APPLICATION_ARCHITECTURE_DESIGN() throws Exception { | ||
shouldApiCallSucceedSucceed(QUERY_SYSTEM_CODE_URL); | ||
} | ||
|
||
@Test | ||
@WithMockUser(value = "test", authorities = { ROLE_PREFIX + MENU_APPLICATION_ARCHITECTURE_QUERY }) | ||
public void querySystemEnum_should_success_for_role_APPLICATION_ARCHITECTURE_QUERY() throws Exception { | ||
shouldApiCallSucceedSucceed(QUERY_SYSTEM_CODE_URL); | ||
} | ||
|
||
@Test | ||
@WithMockUser(value = "test", authorities = { ROLE_PREFIX + MENU_APPLICATION_DEPLOYMENT_DESIGN }) | ||
public void querySystemEnum_should_success_for_role_APPLICATION_DEPLOYMENT_DESIGN() throws Exception { | ||
shouldApiCallSucceedSucceed(QUERY_SYSTEM_CODE_URL); | ||
} | ||
|
||
|
||
@Test | ||
@WithMockUser(value = "test") | ||
public void queryNonSystemEnum_should_fail_for_user_without_appropriate_menu_permission() throws Exception { | ||
shouldApiCallFailForInsufficientPermission(QUERY_NON_SYSTEM_CODE_URL); | ||
} | ||
|
||
@Test | ||
@WithMockUser(value = "test", authorities = { ROLE_PREFIX + MENU_IDC_PLANNING_DESIGN }) | ||
public void queryNonSystemEnum_should_success_for_role_IDC_PLANNING_DESIGN() throws Exception { | ||
shouldApiCallSucceedSucceed(QUERY_NON_SYSTEM_CODE_URL); | ||
} | ||
|
||
@Test | ||
@WithMockUser(value = "test", authorities = { ROLE_PREFIX + MENU_IDC_RESOURCE_PLANNING }) | ||
public void queryNonSystemEnum_should_success_for_role_IDC_RESOURCE_PLANNING() throws Exception { | ||
shouldApiCallSucceedSucceed(QUERY_NON_SYSTEM_CODE_URL); | ||
} | ||
|
||
@Test | ||
@WithMockUser(value = "test", authorities = { ROLE_PREFIX + MENU_APPLICATION_ARCHITECTURE_DESIGN }) | ||
public void queryNonSystemEnum_should_success_for_role_APPLICATION_ARCHITECTURE_DESIGN() throws Exception { | ||
shouldApiCallSucceedSucceed(QUERY_NON_SYSTEM_CODE_URL); | ||
} | ||
|
||
@Test | ||
@WithMockUser(value = "test", authorities = { ROLE_PREFIX + MENU_APPLICATION_ARCHITECTURE_QUERY }) | ||
public void queryNonSystemEnum_should_success_for_role_APPLICATION_ARCHITECTURE_QUERY() throws Exception { | ||
shouldApiCallSucceedSucceed(QUERY_NON_SYSTEM_CODE_URL); | ||
} | ||
|
||
@Test | ||
@WithMockUser(value = "test", authorities = { ROLE_PREFIX + MENU_APPLICATION_DEPLOYMENT_DESIGN }) | ||
public void queryNonSystemEnum_should_success_for_role_APPLICATION_DEPLOYMENT_DESIGN() throws Exception { | ||
shouldApiCallSucceedSucceed(QUERY_NON_SYSTEM_CODE_URL); | ||
} | ||
|
||
private void shouldApiCallSucceedSucceed(String url) throws Exception { | ||
mvc.perform(post(url) | ||
.contentType(MediaType.APPLICATION_JSON_UTF8).content("{}")) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.statusCode", is("OK"))) | ||
.andExpect(jsonPath("$.data", notNullValue())) | ||
; | ||
} | ||
|
||
private void shouldApiCallFailForInsufficientPermission(String url) throws Exception { | ||
mvc.perform(post(url) | ||
.contentType(MediaType.APPLICATION_JSON_UTF8).content("{}")) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.statusCode", is("ERROR"))) | ||
.andExpect(jsonPath("$.statusMessage", is("Access is denied"))) | ||
; | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
cmdb-core/src/test/java/com/webank/cmdb/controller/ui/UILogControllerTest.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,55 @@ | ||
package com.webank.cmdb.controller.ui; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.google.common.collect.ImmutableMap; | ||
import com.webank.cmdb.controller.AbstractBaseControllerTest; | ||
import org.junit.Test; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.security.test.context.support.WithMockUser; | ||
|
||
import javax.transaction.Transactional; | ||
|
||
import java.io.Serializable; | ||
import java.util.Map; | ||
|
||
import static com.webank.cmdb.domain.AdmMenu.MENU_ADMIN_QUERY_LOG; | ||
import static com.webank.cmdb.domain.AdmMenu.ROLE_PREFIX; | ||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
@WithMockUser(username = "test", authorities = { ROLE_PREFIX + MENU_ADMIN_QUERY_LOG }) | ||
public class UILogControllerTest extends AbstractBaseControllerTest { | ||
|
||
@Test | ||
public void queryLogWithEmptyQueryObject() throws Exception { | ||
mvc.perform(post("/ui/v2/log/query").contentType(MediaType.APPLICATION_JSON) | ||
.content("{}")) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.statusCode", is("OK"))); | ||
; | ||
} | ||
|
||
@Test | ||
public void queryLogWithNullSorting() throws Exception { | ||
mvc.perform(post("/ui/v2/log/query").contentType(MediaType.APPLICATION_JSON) | ||
.content("{\"sorting\": null}")) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.statusCode", is("OK"))); | ||
; | ||
} | ||
|
||
@Test | ||
public void queryLogWithSorting() throws Exception { | ||
Map<String, Object> sortingMap = ImmutableMap.of("asc", true, "field", "createdDate"); | ||
Map<String, Object> requestMap = ImmutableMap.of("sorting", sortingMap); | ||
|
||
mvc.perform(post("/ui/v2/log/query").contentType(MediaType.APPLICATION_JSON) | ||
.content(new ObjectMapper().writeValueAsString(requestMap))) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.statusCode", is("OK"))); | ||
; | ||
} | ||
|
||
} |