@Test
public void createUser()
throws Exception
{
TestContext testContext = TestContainer.getInstance().getTestContext();
testContext.useAdminForRequests();
// create user,
UserResource resource = new UserResource();
resource.setUserId(USER_ID);
resource.setFirstName("Marvin Velo");
resource.setEmail("velo@earth.com");
resource.setStatus("active");
resource.addRole("nx-admin");
userUtil.createUser(resource);
// get email
// two e-mails (first confirming user creating and second with users pw)
emailServerHelper.waitForMail(2);
Thread.sleep(1000); //w8 a few more
MimeMessage[] msgs = emailServerHelper.getReceivedMessages();
String password = null;
StringBuilder emailsContent = new StringBuilder();
/// make sure we have at least 1 message
Assert.assertTrue("No emails recieved.", msgs.length > 0);
for (MimeMessage mimeMessage : msgs) {
emailsContent.append(GreenMailUtil.getHeaders(mimeMessage)).append('\n');
// Sample body: Your new password is ********
String body = GreenMailUtil.getBody(mimeMessage);
emailsContent.append(body).append('\n').append('\n');
int index = body.indexOf("Your new password is ");
int passwordStartIndex = index + "Your new password is ".length();
if (index != -1) {
password = body.substring(passwordStartIndex, body.indexOf('\n', passwordStartIndex)).trim();
log.debug("New password:\n" + password);
break;
}
}
Assert.assertNotNull(password, "Didn't recieve a password. Got the following messages:\n" + emailsContent);
// login with generated password
testContext.setUsername(USER_ID);
testContext.setPassword(password);
Status status = UserCreationUtil.login();
Assert.assertTrue(status.isSuccess());
// set new password
String newPassword = "velo123";
status = ChangePasswordUtils.changePassword(USER_ID, password, newPassword);
Assert.assertTrue(status.isSuccess());
// check if the user is 'active'
testContext.useAdminForRequests();
UserResource user = userUtil.getUser(USER_ID);
Assert.assertEquals(user.getStatus(), "active");
// login with new password
testContext.setUsername(USER_ID);
testContext.setPassword(newPassword);
status = UserCreationUtil.login();
Assert.assertTrue(status.isSuccess());
}