Search in sources :

Example 16 with WormholeUser

use of com.vmware.flowgate.common.model.WormholeUser in project flowgate by vmware.

the class AuthControllerTest method readOneUserByIdExample2.

@Test
public void readOneUserByIdExample2() throws Exception {
    expectedEx.expect(WormholeRequestException.class);
    expectedEx.expectMessage("Forbidden");
    String userId = UUID.randomUUID().toString();
    WormholeUserDetails userdetail = new WormholeUserDetails();
    userdetail.setUserId(userId);
    List<String> rolenames = new ArrayList<String>();
    rolenames.add("sysuser");
    WormholeUser sysuser = new WormholeUser();
    sysuser.setRoleNames(rolenames);
    sysuser.setId(userId);
    userRepository.save(sysuser);
    Mockito.doReturn(userdetail).when(tokenService).getCurrentUser(any());
    MvcResult result = this.mockMvc.perform(get("/v1/auth/user/123")).andExpect(status().isForbidden()).andReturn();
    if (result.getResolvedException() != null) {
        userRepository.deleteById(userId);
        throw result.getResolvedException();
    }
}
Also used : WormholeUserDetails(com.vmware.flowgate.util.WormholeUserDetails) ArrayList(java.util.ArrayList) MvcResult(org.springframework.test.web.servlet.MvcResult) WormholeUser(com.vmware.flowgate.common.model.WormholeUser) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Example 17 with WormholeUser

use of com.vmware.flowgate.common.model.WormholeUser in project flowgate by vmware.

the class AuthControllerTest method testRefreshToken.

@Test
public void testRefreshToken() throws Exception {
    ValueOperations<String, String> valueOperations = Mockito.mock(ValueOperations.class);
    Mockito.doReturn(valueOperations).when(template).opsForValue();
    WormholeUser wormholeuser = createUser();
    String userId = UUID.randomUUID().toString();
    wormholeuser.setId(userId);
    wormholeuser.setUserName("tomRefresh");
    wormholeuser.setPassword("$2a$10$Vm8MLIkGwinuICfcqW5RDOoE.aJqnvsaPhnxl7.N4H7oLKVIu3o0.");
    wormholeuser.setRoleNames(Arrays.asList("admin"));
    userRepository.save(wormholeuser);
    MvcResult result = this.mockMvc.perform(post("/v1/auth/token").contentType(MediaType.APPLICATION_JSON).content("{\"userName\":\"tomRefresh\",\"password\":\"123456\"}")).andExpect(status().isOk()).andReturn();
    String access_token = "";
    Cookie[] cookies = result.getResponse().getCookies();
    if (cookies != null && cookies.length != 0) {
        for (Cookie currentcookie : cookies) {
            if (JwtTokenUtil.Token_Name.equals(currentcookie.getName())) {
                access_token = currentcookie.getValue();
                break;
            }
        }
    }
    DecodedJWT jwtcre = jwtUtil.getDecodedJwt(access_token);
    ObjectMapper mapper = new ObjectMapper();
    WormholeUserDetails userdetail = new WormholeUserDetails();
    userdetail.setUserId(userId);
    userdetail.setUsername("tomRefresh");
    userdetail.setPassword("$2a$10$Vm8MLIkGwinuICfcqW5RDOoE.aJqnvsaPhnxl7.N4H7oLKVIu3o0.");
    Mockito.doReturn(userdetail).when(tokenService).getCurrentUser(any());
    Mockito.doReturn(mapper.writeValueAsString(userdetail)).when(tokenService).getUserJsonString(any());
    MvcResult result1 = this.mockMvc.perform(get("/v1/auth/token/refresh").header("Authorization", "Bearer " + access_token)).andDo(document("AuthController-RefreshAccessToken-example")).andReturn();
    // JwtTokenUtil jwtUtil = new JwtTokenUtil();
    DecodedJWT jwt = jwtUtil.getDecodedJwt(result1.getResponse().getHeader("Authorization"));
    TestCase.assertEquals("tomRefresh", jwt.getSubject());
    userRepository.deleteById(wormholeuser.getId());
}
Also used : Cookie(javax.servlet.http.Cookie) WormholeUserDetails(com.vmware.flowgate.util.WormholeUserDetails) MvcResult(org.springframework.test.web.servlet.MvcResult) DecodedJWT(com.auth0.jwt.interfaces.DecodedJWT) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) WormholeUser(com.vmware.flowgate.common.model.WormholeUser) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Example 18 with WormholeUser

use of com.vmware.flowgate.common.model.WormholeUser in project flowgate by vmware.

the class AuthControllerTest method readOneUserByIdExample.

@Test
public void readOneUserByIdExample() throws Exception {
    WormholeUserDetails userdetail = new WormholeUserDetails();
    String userId = UUID.randomUUID().toString();
    userdetail.setUserId(userId);
    List<String> rolenames = new ArrayList<String>();
    rolenames.add("admin");
    WormholeUser adminUser = new WormholeUser();
    adminUser.setRoleNames(rolenames);
    adminUser.setId(userId);
    userRepository.save(adminUser);
    Mockito.doReturn(userdetail).when(tokenService).getCurrentUser(any());
    WormholeUser user = createUser();
    userRepository.save(user);
    this.mockMvc.perform(get("/v1/auth/user/{userId}", user.getId())).andExpect(status().isOk()).andDo(document("AuthController-readOneUserById-example", pathParameters(parameterWithName("userId").description("The id of user, generated by flowgate.")), responseFields(fieldWithPath("id").description("ID of User, created by flowgate"), fieldWithPath("userName").description("userName.").type(JsonFieldType.STRING), fieldWithPath("gender").description("gender").type(JsonFieldType.NUMBER), fieldWithPath("password").description("password"), fieldWithPath("mobile").description("mobile").type(JsonFieldType.STRING), fieldWithPath("status").description("status").type(JsonFieldType.NUMBER), fieldWithPath("createTime").description("createTime").type(Date.class), fieldWithPath("emailAddress").description("emailAddress").type(JsonFieldType.STRING), fieldWithPath("roleNames").description("roleNames").type(List.class), fieldWithPath("userGroupIDs").description("userGroupIDs").type(List.class), fieldWithPath("lastPasswordResetDate").description("lastPasswordResetDate").type(JsonFieldType.NUMBER)))).andReturn();
    userRepository.deleteById(user.getId());
    userRepository.deleteById(adminUser.getId());
}
Also used : WormholeUserDetails(com.vmware.flowgate.util.WormholeUserDetails) ArrayList(java.util.ArrayList) WormholeUser(com.vmware.flowgate.common.model.WormholeUser) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Example 19 with WormholeUser

use of com.vmware.flowgate.common.model.WormholeUser in project flowgate by vmware.

the class AuthControllerTest method readOneUserByIdExample1.

@Test
public void readOneUserByIdExample1() throws Exception {
    WormholeUserDetails userdetail = new WormholeUserDetails();
    String userId = UUID.randomUUID().toString();
    userdetail.setUserId(userId);
    List<String> rolenames = new ArrayList<String>();
    rolenames.add("sysuser");
    WormholeUser sysuser = new WormholeUser();
    sysuser.setRoleNames(rolenames);
    sysuser.setId(userId);
    userRepository.save(sysuser);
    Mockito.doReturn(userdetail).when(tokenService).getCurrentUser(any());
    this.mockMvc.perform(get("/v1/auth/user/" + sysuser.getId() + "")).andExpect(status().isOk()).andReturn();
    userRepository.deleteById(sysuser.getId());
}
Also used : WormholeUserDetails(com.vmware.flowgate.util.WormholeUserDetails) ArrayList(java.util.ArrayList) WormholeUser(com.vmware.flowgate.common.model.WormholeUser) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Example 20 with WormholeUser

use of com.vmware.flowgate.common.model.WormholeUser in project flowgate by vmware.

the class AuthControllerTest method createUser.

WormholeUser createUser() {
    List<String> rolenames = new ArrayList<String>();
    rolenames.add("role1");
    rolenames.add("role2");
    List<String> userGroupIDs = new ArrayList<String>();
    userGroupIDs.add("userGroupIDs1");
    userGroupIDs.add("userGroupIDs2");
    WormholeUser user = new WormholeUser();
    long time = System.currentTimeMillis();
    user.setId(UUID.randomUUID().toString());
    user.setCreateTime(new Date(time));
    user.setEmailAddress("emailAddress");
    user.setGender(1);
    user.setMobile("mobile");
    user.setPassword("password");
    user.setRoleNames(rolenames);
    user.setStatus(0);
    user.setUserGroupIDs(userGroupIDs);
    user.setUserName("userName");
    return user;
}
Also used : ArrayList(java.util.ArrayList) Date(java.util.Date) WormholeUser(com.vmware.flowgate.common.model.WormholeUser)

Aggregations

WormholeUser (com.vmware.flowgate.common.model.WormholeUser)27 WormholeUserDetails (com.vmware.flowgate.util.WormholeUserDetails)18 Test (org.junit.Test)18 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)18 ArrayList (java.util.ArrayList)14 MvcResult (org.springframework.test.web.servlet.MvcResult)5 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)5 FacilitySoftwareConfig (com.vmware.flowgate.common.model.FacilitySoftwareConfig)4 WormholeRequestException (com.vmware.flowgate.exception.WormholeRequestException)3 DecodedJWT (com.auth0.jwt.interfaces.DecodedJWT)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 Date (java.util.Date)2 Optional (java.util.Optional)2 PageRequest (org.springframework.data.domain.PageRequest)2 AuthToken (com.vmware.flowgate.common.model.AuthToken)1 SoftwareType (com.vmware.flowgate.common.model.FacilitySoftwareConfig.SoftwareType)1 AuthorityUtil (com.vmware.flowgate.util.AuthorityUtil)1 IOException (java.io.IOException)1 Cookie (javax.servlet.http.Cookie)1 GrantedAuthority (org.springframework.security.core.GrantedAuthority)1