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());
}
}
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);
}
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());
}
}
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());
}
}
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();
}
Aggregations