use of org.olat.user.restapi.UserVO in project OpenOLAT by OpenOLAT.
the class UserMgmtTest method testPortrait.
@Test
public void testPortrait() throws IOException, URISyntaxException {
URL portraitUrl = UserMgmtTest.class.getResource("portrait.jpg");
assertNotNull(portraitUrl);
File portrait = new File(portraitUrl.toURI());
RestConnection conn = new RestConnection();
assertTrue(conn.login(id1.getName(), "A6B7C8"));
// upload portrait
URI request = UriBuilder.fromUri(getContextURI()).path("/users/" + id1.getKey() + "/portrait").build();
HttpPost method = conn.createPost(request, MediaType.APPLICATION_JSON);
conn.addMultipart(method, "portrait.jpg", portrait);
HttpResponse response = conn.execute(method);
assertEquals(200, response.getStatusLine().getStatusCode());
EntityUtils.consume(response.getEntity());
// check if big and small portraits exist
DisplayPortraitManager dps = DisplayPortraitManager.getInstance();
File bigPortrait = dps.getBigPortrait(id1.getName());
assertNotNull(bigPortrait);
assertTrue(bigPortrait.exists());
assertTrue(bigPortrait.exists());
// check get portrait
URI getRequest = UriBuilder.fromUri(getContextURI()).path("/users/" + id1.getKey() + "/portrait").build();
HttpGet getMethod = conn.createGet(getRequest, MediaType.APPLICATION_OCTET_STREAM, true);
HttpResponse getResponse = conn.execute(getMethod);
assertEquals(200, getResponse.getStatusLine().getStatusCode());
InputStream in = getResponse.getEntity().getContent();
int b = 0;
int count = 0;
while ((b = in.read()) > -1) {
count++;
}
// up to end of file
assertEquals(-1, b);
// enough bytes
assertTrue(count > 1000);
bigPortrait = dps.getBigPortrait(id1.getName());
assertNotNull(bigPortrait);
assertEquals(count, bigPortrait.length());
// check get portrait as Base64
UriBuilder getRequest2 = UriBuilder.fromUri(getContextURI()).path("users").path(id1.getKey().toString()).queryParam("withPortrait", "true");
HttpGet getMethod2 = conn.createGet(getRequest2.build(), MediaType.APPLICATION_JSON, true);
HttpResponse getCode2 = conn.execute(getMethod2);
assertEquals(200, getCode2.getStatusLine().getStatusCode());
UserVO userVo = conn.parse(getCode2, UserVO.class);
assertNotNull(userVo);
assertNotNull(userVo.getPortrait());
byte[] datas = Base64.decodeBase64(userVo.getPortrait().getBytes());
assertNotNull(datas);
assertTrue(datas.length > 0);
File smallPortrait = dps.getSmallPortrait(id1.getName());
assertNotNull(smallPortrait);
assertEquals(datas.length, smallPortrait.length());
try {
ImageIO.read(new ByteArrayInputStream(datas));
} catch (Exception e) {
assertFalse("Cannot read the portrait after Base64 encoding/decoding", false);
}
conn.shutdown();
}
use of org.olat.user.restapi.UserVO in project OpenOLAT by OpenOLAT.
the class UserMgmtTest method testFindUsersByLogin.
@Test
public void testFindUsersByLogin() throws IOException, URISyntaxException {
RestConnection conn = new RestConnection();
assertTrue(conn.login("administrator", "openolat"));
URI request = UriBuilder.fromUri(getContextURI()).path("users").queryParam("login", "administrator").queryParam("authProvider", "OLAT").build();
HttpGet method = conn.createGet(request, MediaType.APPLICATION_JSON, true);
HttpResponse response = conn.execute(method);
assertEquals(200, response.getStatusLine().getStatusCode());
InputStream body = response.getEntity().getContent();
List<UserVO> vos = parseUserArray(body);
String[] authProviders = new String[] { "OLAT" };
List<Identity> identities = BaseSecurityManager.getInstance().getIdentitiesByPowerSearch("administrator", null, true, null, null, authProviders, null, null, null, null, Identity.STATUS_VISIBLE_LIMIT);
assertNotNull(vos);
assertFalse(vos.isEmpty());
assertEquals(vos.size(), identities.size());
boolean onlyLikeAdmin = true;
for (UserVO vo : vos) {
if (!vo.getLogin().startsWith("administrator")) {
onlyLikeAdmin = false;
}
}
assertTrue(onlyLikeAdmin);
conn.shutdown();
}
use of org.olat.user.restapi.UserVO in project OpenOLAT by OpenOLAT.
the class UserMgmtTest method testCreateAndUpdateUser_same_email.
/**
* Test if we can create two users with the same email addresses.
*/
@Test
public void testCreateAndUpdateUser_same_email() throws IOException, URISyntaxException {
RestConnection conn = new RestConnection();
assertTrue(conn.login("administrator", "openolat"));
String email = UUID.randomUUID() + "@frentix.com";
UserVO vo = new UserVO();
String username = UUID.randomUUID().toString();
vo.setLogin(username);
vo.setFirstName("John");
vo.setLastName("Smith");
vo.setEmail(email);
// male or female
vo.putProperty("gender", "male");
URI request = UriBuilder.fromUri(getContextURI()).path("users").build();
HttpPut method = conn.createPut(request, MediaType.APPLICATION_JSON, true);
conn.addJsonEntity(method, vo);
method.addHeader("Accept-Language", "en");
HttpResponse response = conn.execute(method);
assertTrue(response.getStatusLine().getStatusCode() == 200 || response.getStatusLine().getStatusCode() == 201);
UserVO savedVo = conn.parse(response, UserVO.class);
Identity savedIdent = securityManager.findIdentityByName(username);
Assert.assertNotNull(savedVo);
Assert.assertNotNull(savedIdent);
Assert.assertEquals(savedVo.getKey(), savedIdent.getKey());
// second user
String secondEmail = UUID.randomUUID() + "@frentix.com";
String secondUsername = UUID.randomUUID().toString();
UserVO secondVo = new UserVO();
secondVo.setLogin(secondUsername);
secondVo.setFirstName("Eva");
secondVo.setLastName("Smith");
secondVo.setEmail(secondEmail);
secondVo.putProperty("gender", "female");
URI secondRequest = UriBuilder.fromUri(getContextURI()).path("users").build();
HttpPut secondMethod = conn.createPut(secondRequest, MediaType.APPLICATION_JSON, true);
conn.addJsonEntity(secondMethod, secondVo);
secondMethod.addHeader("Accept-Language", "en");
HttpResponse secondResponse = conn.execute(secondMethod);
int secondStatusCode = secondResponse.getStatusLine().getStatusCode();
Assert.assertEquals(200, secondStatusCode);
UserVO secondSavedVo = conn.parse(secondResponse, UserVO.class);
Assert.assertNotNull(secondSavedVo);
dbInstance.commitAndCloseSession();
Identity secondSavedIdent = securityManager.findIdentityByName(secondUsername);
Assert.assertNotNull(secondSavedIdent);
Assert.assertEquals(secondSavedVo.getKey(), secondSavedIdent.getKey());
Assert.assertEquals(secondEmail, secondSavedIdent.getUser().getEmail());
// update second with new first name and the mail of the first user
UserVO updateVo = new UserVO();
updateVo.setKey(secondSavedVo.getKey());
updateVo.setLogin(secondUsername);
updateVo.setFirstName("Caprice");
updateVo.setLastName("Smith");
updateVo.setEmail(email);
updateVo.putProperty("gender", "female");
URI updateRequest = UriBuilder.fromUri(getContextURI()).path("users").path(secondSavedVo.getKey().toString()).build();
HttpPost updateMethod = conn.createPost(updateRequest, MediaType.APPLICATION_JSON);
conn.addJsonEntity(updateMethod, updateVo);
updateMethod.addHeader("Accept-Language", "en");
HttpResponse updateResponse = conn.execute(updateMethod);
int statusCode = updateResponse.getStatusLine().getStatusCode();
Assert.assertEquals(406, statusCode);
String errorMessage = EntityUtils.toString(updateResponse.getEntity());
Assert.assertNotNull(errorMessage);
// check that nothing has changed for the second user
dbInstance.commitAndCloseSession();
Identity notUpdatedIdent = securityManager.findIdentityByName(secondUsername);
Assert.assertNotNull(notUpdatedIdent);
Assert.assertEquals(secondSavedVo.getKey(), notUpdatedIdent.getKey());
Assert.assertEquals("Eva", notUpdatedIdent.getUser().getFirstName());
Assert.assertEquals("Smith", notUpdatedIdent.getUser().getLastName());
Assert.assertEquals(secondEmail, notUpdatedIdent.getUser().getEmail());
conn.shutdown();
}
use of org.olat.user.restapi.UserVO in project OpenOLAT by OpenOLAT.
the class UserMgmtTest method testCreateUser_same_email.
/**
* Test if we can create two users with the same email addresses.
*/
@Test
public void testCreateUser_same_email() throws IOException, URISyntaxException {
RestConnection conn = new RestConnection();
assertTrue(conn.login("administrator", "openolat"));
String email = UUID.randomUUID() + "@frentix.com";
UserVO vo = new UserVO();
String username = UUID.randomUUID().toString();
vo.setLogin(username);
vo.setFirstName("John");
vo.setLastName("Smith");
vo.setEmail(email);
// male or female
vo.putProperty("gender", "male");
URI request = UriBuilder.fromUri(getContextURI()).path("users").build();
HttpPut method = conn.createPut(request, MediaType.APPLICATION_JSON, true);
conn.addJsonEntity(method, vo);
method.addHeader("Accept-Language", "en");
HttpResponse response = conn.execute(method);
assertTrue(response.getStatusLine().getStatusCode() == 200 || response.getStatusLine().getStatusCode() == 201);
UserVO savedVo = conn.parse(response, UserVO.class);
Identity savedIdent = BaseSecurityManager.getInstance().findIdentityByName(username);
assertNotNull(savedVo);
assertNotNull(savedIdent);
assertEquals(savedVo.getKey(), savedIdent.getKey());
// second
UserVO vo2 = new UserVO();
vo2.setLogin(UUID.randomUUID().toString());
vo2.setFirstName("Eva");
vo2.setLastName("Smith");
vo2.setEmail(email);
vo2.putProperty("gender", "female");
URI request2 = UriBuilder.fromUri(getContextURI()).path("users").build();
HttpPut method2 = conn.createPut(request2, MediaType.APPLICATION_JSON, true);
conn.addJsonEntity(method2, vo2);
method2.addHeader("Accept-Language", "en");
HttpResponse response2 = conn.execute(method2);
int statusCode2 = response2.getStatusLine().getStatusCode();
Assert.assertEquals(406, statusCode2);
String errorMessage = EntityUtils.toString(response2.getEntity());
Assert.assertNotNull(errorMessage);
conn.shutdown();
}
use of org.olat.user.restapi.UserVO in project OpenOLAT by OpenOLAT.
the class UserMgmtTest method testFindUsersByProperty.
@Test
public void testFindUsersByProperty() throws IOException, URISyntaxException {
RestConnection conn = new RestConnection();
assertTrue(conn.login("administrator", "openolat"));
URI request = UriBuilder.fromUri(getContextURI()).path("users").queryParam("telMobile", "39847592").queryParam("gender", "Female").queryParam("birthDay", "12/12/2009").build();
HttpGet method = conn.createGet(request, MediaType.APPLICATION_JSON, true);
method.addHeader("Accept-Language", "en");
HttpResponse response = conn.execute(method);
assertEquals(200, response.getStatusLine().getStatusCode());
InputStream body = response.getEntity().getContent();
List<UserVO> vos = parseUserArray(body);
assertNotNull(vos);
assertFalse(vos.isEmpty());
conn.shutdown();
}
Aggregations