Search in sources :

Example 16 with UserModel

use of com.serotonin.m2m2.web.mvc.rest.v1.model.user.UserModel in project ma-modules-public by infiniteautomation.

the class LogoutFunctionalTests method testLogout.

public void testLogout() {
    User standardUser = UserTestData.standardUser();
    // Mock the Dao Get User Call
    when(userDao.getUser(standardUser.getUsername())).thenReturn(standardUser);
    // Mock the return to normal for the Logged In User that DNE anyway
    Mockito.doNothing().when(Common.eventManager).returnToNormal(new SystemEventType(SystemEventType.TYPE_USER_LOGIN, standardUser.getId()), System.currentTimeMillis());
    try {
        MvcResult result = this.mockMvc.perform(post("/v1/logout/{username}", standardUser.getUsername()).sessionAttr("sessionUser", standardUser).accept(MediaType.APPLICATION_JSON)).andDo(print()).andExpect(status().isOk()).andReturn();
        UserModel loggedOutUserModel = this.objectMapper.readValue(result.getResponse().getContentAsString(), UserModel.class);
        User loggedOutUser = loggedOutUserModel.getData();
        // Check to see that the User is correct
        assertEquals(standardUser.getUsername(), loggedOutUser.getUsername());
        // Ensure the User is no longer in the Session
        // Because Common.SESSION_USER is not public
        User sessionUser = (User) result.getRequest().getSession().getAttribute("sessionUser");
        assertEquals(null, sessionUser);
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : UserModel(com.serotonin.m2m2.web.mvc.rest.v1.model.user.UserModel) SystemEventType(com.serotonin.m2m2.rt.event.type.SystemEventType) User(com.serotonin.m2m2.vo.User) MvcResult(org.springframework.test.web.servlet.MvcResult)

Example 17 with UserModel

use of com.serotonin.m2m2.web.mvc.rest.v1.model.user.UserModel in project ma-modules-public by infiniteautomation.

the class UserFullStackTests method getAll.

public void getAll() {
    // Setup For Rest Call
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
    RestTemplate template = new RestTemplate();
    List<UserModel> data = template.getForObject("http://localhost:8080/v1/users/", List.class);
}
Also used : UserModel(com.serotonin.m2m2.web.mvc.rest.v1.model.user.UserModel) HttpHeaders(org.springframework.http.HttpHeaders) RestTemplate(org.springframework.web.client.RestTemplate)

Example 18 with UserModel

use of com.serotonin.m2m2.web.mvc.rest.v1.model.user.UserModel in project ma-modules-public by infiniteautomation.

the class UserFunctionalTests method testNonAdminCreateUserError.

/**
 * Test a non-admin user creating a User
 *
 * Should show 401 - Unauthorized
 *
 * @throws Exception
 */
public void testNonAdminCreateUserError() throws Exception {
    User standardUser = UserTestData.standardUser();
    List<User> users = new ArrayList<>();
    users.add(standardUser);
    // This will ensure that the getUsers() method returns
    // the mock list of users
    when(userDao.getUser(standardUser.getUsername())).thenReturn(standardUser);
    standardUser.setEmail(null);
    String userJson = this.objectMapper.writeValueAsString(new UserModel(standardUser));
    try {
        this.mockMvc.perform(post("/v1/users/").content(userJson).contentType(MediaType.APPLICATION_JSON).sessionAttr("sessionUser", standardUser).accept(MediaType.APPLICATION_JSON)).andDo(print()).andExpect(status().isUnauthorized());
    } catch (Exception e) {
        fail(e.getMessage());
    }
}
Also used : UserModel(com.serotonin.m2m2.web.mvc.rest.v1.model.user.UserModel) User(com.serotonin.m2m2.vo.User) ArrayList(java.util.ArrayList)

Example 19 with UserModel

use of com.serotonin.m2m2.web.mvc.rest.v1.model.user.UserModel in project ma-modules-public by infiniteautomation.

the class UserFunctionalTests method testValidationFailure.

/**
 * Test Posting an empty user
 *
 * @throws Exception
 */
public void testValidationFailure() {
    User standardUser = UserTestData.standardUser();
    User adminUser = UserTestData.adminUser();
    List<User> users = new ArrayList<>();
    users.add(standardUser);
    // This will ensure that the getUsers() method returns
    // the mock list of users
    when(userDao.getUser(standardUser.getUsername())).thenReturn(null);
    standardUser.setEmail("");
    standardUser.setPassword("testing-password");
    ObjectWriter writer = this.objectMapper.writerWithView(JsonViews.Test.class);
    try {
        String userJson = writer.writeValueAsString(new UserModel(standardUser));
        this.mockMvc.perform(post("/v1/users/").content(userJson).contentType(MediaType.APPLICATION_JSON).sessionAttr("sessionUser", adminUser).accept(MediaType.APPLICATION_JSON)).andDo(print()).andExpect(status().isBadRequest());
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : UserModel(com.serotonin.m2m2.web.mvc.rest.v1.model.user.UserModel) User(com.serotonin.m2m2.vo.User) JsonViews(com.serotonin.m2m2.web.mvc.rest.v1.mapping.JsonViews) ArrayList(java.util.ArrayList) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter)

Example 20 with UserModel

use of com.serotonin.m2m2.web.mvc.rest.v1.model.user.UserModel in project ma-modules-public by infiniteautomation.

the class UserRestController method getAll.

@ApiOperation(value = "Get all users", notes = "Returns a list of all users")
@RequestMapping(method = RequestMethod.GET, produces = { "application/json", "text/csv" }, value = "list")
public ResponseEntity<List<UserModel>> getAll(HttpServletRequest request) {
    RestProcessResult<List<UserModel>> result = new RestProcessResult<List<UserModel>>(HttpStatus.OK);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        if (Permissions.hasAdmin(user)) {
            List<UserModel> userModelList = new ArrayList<UserModel>();
            List<User> users = UserDao.instance.getUsers();
            for (User u : users) {
                userModelList.add(new UserModel(u));
            }
            return result.createResponseEntity(userModelList);
        } else {
            LOG.warn("Non admin user: " + user.getUsername() + " attempted to access all users");
            result.addRestMessage(this.getUnauthorizedMessage());
            return result.createResponseEntity();
        }
    }
    return result.createResponseEntity();
}
Also used : UserModel(com.serotonin.m2m2.web.mvc.rest.v1.model.user.UserModel) RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) User(com.serotonin.m2m2.vo.User) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

UserModel (com.serotonin.m2m2.web.mvc.rest.v1.model.user.UserModel)21 User (com.serotonin.m2m2.vo.User)17 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)14 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)14 RestProcessResult (com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult)11 ArrayList (java.util.ArrayList)7 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)5 AccessDeniedException (com.infiniteautomation.mango.rest.v2.exception.AccessDeniedException)4 List (java.util.List)4 ProcessMessage (com.serotonin.m2m2.i18n.ProcessMessage)3 ResponseEntity (org.springframework.http.ResponseEntity)3 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)3 AuthenticationException (org.springframework.security.core.AuthenticationException)3 MvcResult (org.springframework.test.web.servlet.MvcResult)3 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)2 JsonViews (com.serotonin.m2m2.web.mvc.rest.v1.mapping.JsonViews)2 UserAccessModel (com.serotonin.m2m2.web.mvc.rest.v1.model.user.UserAccessModel)2 URI (java.net.URI)2 InvalidRQLRestException (com.infiniteautomation.mango.rest.v2.exception.InvalidRQLRestException)1 NotFoundRestException (com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException)1