Commit 21689235 authored by Greg Messner's avatar Greg Messner
Browse files

Added support and tests for User API email endpoints (#339).

parent 604f1e75
......@@ -13,6 +13,7 @@ import javax.ws.rs.core.Response;
import org.gitlab4j.api.GitLabApi.ApiVersion;
import org.gitlab4j.api.models.CustomAttribute;
import org.gitlab4j.api.models.Email;
import org.gitlab4j.api.models.ImpersonationToken;
import org.gitlab4j.api.models.ImpersonationToken.Scope;
import org.gitlab4j.api.models.SshKey;
......@@ -1012,4 +1013,105 @@ public class UserApi extends AbstractApi {
Response response = putUpload(Response.Status.OK, "avatar", avatarFile, "users", getUserIdOrUsername(userIdOrUsername));
return (response.readEntity(User.class));
}
/**
* Get a list of emails for the current user.
*
* <pre><code>GitLab Endpoint: GET /users/emails</code></pre>
*
* @return a List of Email instances for the current user
* @throws GitLabApiException if any exception occurs
*/
public List<Email> getEmails() throws GitLabApiException {
Response response = get(Response.Status.OK, null, "user", "emails");
return (response.readEntity(new GenericType<List<Email>>() {}));
}
/**
* Get a list of a specified user’s emails. Available only for admin users.
*
* <pre><code>GitLab Endpoint: GET /user/:id/emails</code></pre>
*
* @param userIdOrUsername the user in the form of an Integer(ID), String(username), or User instance
* @return a List of Email instances for the specified user
* @throws GitLabApiException if any exception occurs
*/
public List<Email> getEmails(final Object userIdOrUsername) throws GitLabApiException {
Response response = get(Response.Status.OK, null, "users", getUserIdOrUsername(userIdOrUsername), "emails");
return (response.readEntity(new GenericType<List<Email>>() {}));
}
/**
* Add an email to the current user's emails.
*
* <pre><code>GitLab Endpoint: POST /user/:id/emails</code></pre>
*
* @param email the email address to add
* @return the Email instance for the added email
* @throws GitLabApiException if any exception occurs
*/
public Email addEmail(String email) throws GitLabApiException {
GitLabApiForm formData = new GitLabApiForm().withParam("email", email, true);
Response response = post(Response.Status.CREATED, formData, "user", "emails");
return (response.readEntity(Email.class));
}
/**
* Get a single Email instance specified by he email ID
*
* <pre><code>GitLab Endpoint: GET /user/emails/:emailId</code></pre>
*
* @param emailId the email ID to get
* @return the Email instance for the provided email ID
* @throws GitLabApiException if any exception occurs
*/
public Email getEmail(final Long emailId) throws GitLabApiException {
Response response = get(Response.Status.CREATED, null, "user", "emails", emailId);
return (response.readEntity(Email.class));
}
/**
* Add an email to the user's emails.
*
* <pre><code>GitLab Endpoint: POST /user/:id/emails</code></pre>
*
* @param userIdOrUsername the user in the form of an Integer(ID), String(username), or User instance
* @param email the email address to add
* @param skipConfirmation skip confirmation and assume e-mail is verified - true or false (default)
* @return the Email instance for the added email
* @throws GitLabApiException if any exception occurs
*/
public Email addEmail(final Object userIdOrUsername, String email, Boolean skipConfirmation) throws GitLabApiException {
GitLabApiForm formData = new GitLabApiForm()
.withParam("email", email, true)
.withParam("skip_confirmation ", skipConfirmation);
Response response = post(Response.Status.CREATED, formData, "users", getUserIdOrUsername(userIdOrUsername), "emails");
return (response.readEntity(Email.class));
}
/**
* Deletes an email belonging to the current user.
*
* <pre><code>GitLab Endpoint: DELETE /user/emails/:emailId</code></pre>
*
* @param emailId the email ID to delete
* @throws GitLabApiException if any exception occurs
*/
public void deleteEmail(final Long emailId) throws GitLabApiException {
delete(Response.Status.NO_CONTENT, null, "user", "emails", emailId);
}
/**
* Deletes a user's email
*
* <pre><code>GitLab Endpoint: DELETE /user/:id/emails/:emailId</code></pre>
*
* @param userIdOrUsername the user in the form of an Integer(ID), String(username), or User instance
* @param emailId the email ID to delete
* @throws GitLabApiException if any exception occurs
*/
public void deleteEmail(final Object userIdOrUsername, final Long emailId) throws GitLabApiException {
delete(Response.Status.NO_CONTENT, null, "users", getUserIdOrUsername(userIdOrUsername), "emails", emailId);
}
}
......@@ -16,6 +16,7 @@ import java.util.Optional;
import javax.ws.rs.core.Response;
import org.gitlab4j.api.models.Email;
import org.gitlab4j.api.models.ImpersonationToken;
import org.gitlab4j.api.models.ImpersonationToken.Scope;
import org.gitlab4j.api.models.SshKey;
......@@ -60,6 +61,7 @@ public class TestUserApi extends AbstractIntegrationTest {
"vNWfEmp2N1mpBTwi2mIYKurCKv6UpIpGK9D+ezNk5H0waVTK8EvZ/ey69Nu7C7RsbTYeyi5WY/jaUG5JbsEeKY" +
"IW/2DIlUts7gcB2hzXtt7r7+6DLx82Vb+S2jPZu2JQaB4zfgS7LQgzHUy1aAAgUUpuAbvWzuGHKO0p551Ru4qi" +
"tyXN2+OUVXcYAsuIIdGGB0wLvTDgiOOSZWnSE+sg6XX user@example.com";
private static final String TEST_USER_EMAIL = "test-user-email123@gitlab4j.org";
private static GitLabApi gitLabApi;
......@@ -73,10 +75,6 @@ public class TestUserApi extends AbstractIntegrationTest {
public static void setup() {
String problems = "";
if (TEST_PRIVATE_TOKEN == null || TEST_PRIVATE_TOKEN.trim().isEmpty()) {
problems += "TEST_PRIVATE_TOKEN cannot be empty\n";
}
if (TEST_USERNAME == null || TEST_USERNAME.trim().isEmpty()) {
problems += "TEST_USER_NAME cannot be empty\n";
}
......@@ -109,6 +107,15 @@ public class TestUserApi extends AbstractIntegrationTest {
}
} catch (Exception ignore) {}
}
try {
List<Email> emails = gitLabApi.getUserApi().getEmails();
for (Email email : emails) {
if (TEST_USER_EMAIL.equals(email.getEmail())) {
gitLabApi.getUserApi().deleteEmail(email.getId());
}
}
} catch (Exception ignore) {}
}
} else {
......@@ -312,4 +319,50 @@ public class TestUserApi extends AbstractIntegrationTest {
assertFalse(optional.isPresent());
assertEquals(Response.Status.NOT_FOUND.getStatusCode(), GitLabApi.getOptionalException(optional).getHttpStatus());
}
@Test
public void testCurrentUserEmails() throws GitLabApiException {
List<Email> currentUserEmails = gitLabApi.getUserApi().getEmails();
assertNotNull(currentUserEmails);
int currentSize = currentUserEmails.size();
Email email = gitLabApi.getUserApi().addEmail(TEST_USER_EMAIL);
currentUserEmails = gitLabApi.getUserApi().getEmails();
assertTrue(currentUserEmails.size() == currentSize + 1);
Email found = currentUserEmails.stream().filter(e -> e.getEmail().equals(TEST_USER_EMAIL)).findAny().orElse(null);
assertNotNull(found);
Email email1 = gitLabApi.getUserApi().getEmail(email.getId());
assertEquals(email.getEmail(), email1.getEmail());
gitLabApi.getUserApi().deleteEmail(email.getId());
currentUserEmails = gitLabApi.getUserApi().getEmails();
assertEquals(currentSize, currentUserEmails.size());
found = currentUserEmails.stream().filter(e -> e.getEmail().equals(TEST_USER_EMAIL)).findAny().orElse(null);
assertNull(found);
}
@Test
public void testEmails() throws GitLabApiException {
User currentUser = gitLabApi.getUserApi().getCurrentUser();
assertNotNull(currentUser);
List<Email> emails = gitLabApi.getUserApi().getEmails(currentUser);
assertNotNull(emails);
int currentSize = emails.size();
Email email = gitLabApi.getUserApi().addEmail(currentUser, TEST_USER_EMAIL, true);
emails = gitLabApi.getUserApi().getEmails(currentUser);
assertTrue(emails.size() == currentSize + 1);
Email found = emails.stream().filter(e -> e.getEmail().equals(TEST_USER_EMAIL)).findAny().orElse(null);
assertNotNull(found);
gitLabApi.getUserApi().deleteEmail(currentUser, email.getId());
emails = gitLabApi.getUserApi().getEmails(currentUser);
assertEquals(currentSize, emails.size());
found = emails.stream().filter(e -> e.getEmail().equals(TEST_USER_EMAIL)).findAny().orElse(null);
assertNull(found);
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment