Skip to content

Commit 2168923

Browse files
committed
Added support and tests for User API email endpoints (#339).
1 parent 604f1e7 commit 2168923

File tree

2 files changed

+159
-4
lines changed

2 files changed

+159
-4
lines changed

src/main/java/org/gitlab4j/api/UserApi.java

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import org.gitlab4j.api.GitLabApi.ApiVersion;
1515
import org.gitlab4j.api.models.CustomAttribute;
16+
import org.gitlab4j.api.models.Email;
1617
import org.gitlab4j.api.models.ImpersonationToken;
1718
import org.gitlab4j.api.models.ImpersonationToken.Scope;
1819
import org.gitlab4j.api.models.SshKey;
@@ -1012,4 +1013,105 @@ public User setUserAvatar(final Object userIdOrUsername, File avatarFile) throws
10121013
Response response = putUpload(Response.Status.OK, "avatar", avatarFile, "users", getUserIdOrUsername(userIdOrUsername));
10131014
return (response.readEntity(User.class));
10141015
}
1016+
1017+
/**
1018+
* Get a list of emails for the current user.
1019+
*
1020+
* <pre><code>GitLab Endpoint: GET /users/emails</code></pre>
1021+
*
1022+
* @return a List of Email instances for the current user
1023+
* @throws GitLabApiException if any exception occurs
1024+
*/
1025+
public List<Email> getEmails() throws GitLabApiException {
1026+
Response response = get(Response.Status.OK, null, "user", "emails");
1027+
return (response.readEntity(new GenericType<List<Email>>() {}));
1028+
}
1029+
1030+
/**
1031+
* Get a list of a specified user’s emails. Available only for admin users.
1032+
*
1033+
* <pre><code>GitLab Endpoint: GET /user/:id/emails</code></pre>
1034+
*
1035+
* @param userIdOrUsername the user in the form of an Integer(ID), String(username), or User instance
1036+
* @return a List of Email instances for the specified user
1037+
* @throws GitLabApiException if any exception occurs
1038+
*/
1039+
public List<Email> getEmails(final Object userIdOrUsername) throws GitLabApiException {
1040+
Response response = get(Response.Status.OK, null, "users", getUserIdOrUsername(userIdOrUsername), "emails");
1041+
return (response.readEntity(new GenericType<List<Email>>() {}));
1042+
}
1043+
1044+
/**
1045+
* Add an email to the current user's emails.
1046+
*
1047+
* <pre><code>GitLab Endpoint: POST /user/:id/emails</code></pre>
1048+
*
1049+
* @param email the email address to add
1050+
* @return the Email instance for the added email
1051+
* @throws GitLabApiException if any exception occurs
1052+
*/
1053+
public Email addEmail(String email) throws GitLabApiException {
1054+
GitLabApiForm formData = new GitLabApiForm().withParam("email", email, true);
1055+
Response response = post(Response.Status.CREATED, formData, "user", "emails");
1056+
return (response.readEntity(Email.class));
1057+
}
1058+
1059+
/**
1060+
* Get a single Email instance specified by he email ID
1061+
*
1062+
* <pre><code>GitLab Endpoint: GET /user/emails/:emailId</code></pre>
1063+
*
1064+
* @param emailId the email ID to get
1065+
* @return the Email instance for the provided email ID
1066+
* @throws GitLabApiException if any exception occurs
1067+
*/
1068+
public Email getEmail(final Long emailId) throws GitLabApiException {
1069+
Response response = get(Response.Status.CREATED, null, "user", "emails", emailId);
1070+
return (response.readEntity(Email.class));
1071+
}
1072+
1073+
/**
1074+
* Add an email to the user's emails.
1075+
*
1076+
* <pre><code>GitLab Endpoint: POST /user/:id/emails</code></pre>
1077+
*
1078+
* @param userIdOrUsername the user in the form of an Integer(ID), String(username), or User instance
1079+
* @param email the email address to add
1080+
* @param skipConfirmation skip confirmation and assume e-mail is verified - true or false (default)
1081+
* @return the Email instance for the added email
1082+
* @throws GitLabApiException if any exception occurs
1083+
*/
1084+
public Email addEmail(final Object userIdOrUsername, String email, Boolean skipConfirmation) throws GitLabApiException {
1085+
1086+
GitLabApiForm formData = new GitLabApiForm()
1087+
.withParam("email", email, true)
1088+
.withParam("skip_confirmation ", skipConfirmation);
1089+
Response response = post(Response.Status.CREATED, formData, "users", getUserIdOrUsername(userIdOrUsername), "emails");
1090+
return (response.readEntity(Email.class));
1091+
}
1092+
1093+
/**
1094+
* Deletes an email belonging to the current user.
1095+
*
1096+
* <pre><code>GitLab Endpoint: DELETE /user/emails/:emailId</code></pre>
1097+
*
1098+
* @param emailId the email ID to delete
1099+
* @throws GitLabApiException if any exception occurs
1100+
*/
1101+
public void deleteEmail(final Long emailId) throws GitLabApiException {
1102+
delete(Response.Status.NO_CONTENT, null, "user", "emails", emailId);
1103+
}
1104+
1105+
/**
1106+
* Deletes a user's email
1107+
*
1108+
* <pre><code>GitLab Endpoint: DELETE /user/:id/emails/:emailId</code></pre>
1109+
*
1110+
* @param userIdOrUsername the user in the form of an Integer(ID), String(username), or User instance
1111+
* @param emailId the email ID to delete
1112+
* @throws GitLabApiException if any exception occurs
1113+
*/
1114+
public void deleteEmail(final Object userIdOrUsername, final Long emailId) throws GitLabApiException {
1115+
delete(Response.Status.NO_CONTENT, null, "users", getUserIdOrUsername(userIdOrUsername), "emails", emailId);
1116+
}
10151117
}

src/test/java/org/gitlab4j/api/TestUserApi.java

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import javax.ws.rs.core.Response;
1818

19+
import org.gitlab4j.api.models.Email;
1920
import org.gitlab4j.api.models.ImpersonationToken;
2021
import org.gitlab4j.api.models.ImpersonationToken.Scope;
2122
import org.gitlab4j.api.models.SshKey;
@@ -60,6 +61,7 @@ public class TestUserApi extends AbstractIntegrationTest {
6061
"vNWfEmp2N1mpBTwi2mIYKurCKv6UpIpGK9D+ezNk5H0waVTK8EvZ/ey69Nu7C7RsbTYeyi5WY/jaUG5JbsEeKY" +
6162
"IW/2DIlUts7gcB2hzXtt7r7+6DLx82Vb+S2jPZu2JQaB4zfgS7LQgzHUy1aAAgUUpuAbvWzuGHKO0p551Ru4qi" +
6263
"tyXN2+OUVXcYAsuIIdGGB0wLvTDgiOOSZWnSE+sg6XX user@example.com";
64+
private static final String TEST_USER_EMAIL = "test-user-email123@gitlab4j.org";
6365

6466

6567
private static GitLabApi gitLabApi;
@@ -73,10 +75,6 @@ public TestUserApi() {
7375
public static void setup() {
7476

7577
String problems = "";
76-
if (TEST_PRIVATE_TOKEN == null || TEST_PRIVATE_TOKEN.trim().isEmpty()) {
77-
problems += "TEST_PRIVATE_TOKEN cannot be empty\n";
78-
}
79-
8078
if (TEST_USERNAME == null || TEST_USERNAME.trim().isEmpty()) {
8179
problems += "TEST_USER_NAME cannot be empty\n";
8280
}
@@ -109,6 +107,15 @@ public static void setup() {
109107
}
110108
} catch (Exception ignore) {}
111109
}
110+
111+
try {
112+
List<Email> emails = gitLabApi.getUserApi().getEmails();
113+
for (Email email : emails) {
114+
if (TEST_USER_EMAIL.equals(email.getEmail())) {
115+
gitLabApi.getUserApi().deleteEmail(email.getId());
116+
}
117+
}
118+
} catch (Exception ignore) {}
112119
}
113120

114121
} else {
@@ -312,4 +319,50 @@ public void testGetOptionalSshKey() throws GitLabApiException {
312319
assertFalse(optional.isPresent());
313320
assertEquals(Response.Status.NOT_FOUND.getStatusCode(), GitLabApi.getOptionalException(optional).getHttpStatus());
314321
}
322+
323+
@Test
324+
public void testCurrentUserEmails() throws GitLabApiException {
325+
326+
List<Email> currentUserEmails = gitLabApi.getUserApi().getEmails();
327+
assertNotNull(currentUserEmails);
328+
int currentSize = currentUserEmails.size();
329+
330+
Email email = gitLabApi.getUserApi().addEmail(TEST_USER_EMAIL);
331+
currentUserEmails = gitLabApi.getUserApi().getEmails();
332+
assertTrue(currentUserEmails.size() == currentSize + 1);
333+
334+
Email found = currentUserEmails.stream().filter(e -> e.getEmail().equals(TEST_USER_EMAIL)).findAny().orElse(null);
335+
assertNotNull(found);
336+
337+
Email email1 = gitLabApi.getUserApi().getEmail(email.getId());
338+
assertEquals(email.getEmail(), email1.getEmail());
339+
340+
gitLabApi.getUserApi().deleteEmail(email.getId());
341+
currentUserEmails = gitLabApi.getUserApi().getEmails();
342+
assertEquals(currentSize, currentUserEmails.size());
343+
found = currentUserEmails.stream().filter(e -> e.getEmail().equals(TEST_USER_EMAIL)).findAny().orElse(null);
344+
assertNull(found);
345+
}
346+
347+
@Test
348+
public void testEmails() throws GitLabApiException {
349+
350+
User currentUser = gitLabApi.getUserApi().getCurrentUser();
351+
assertNotNull(currentUser);
352+
List<Email> emails = gitLabApi.getUserApi().getEmails(currentUser);
353+
assertNotNull(emails);
354+
int currentSize = emails.size();
355+
356+
Email email = gitLabApi.getUserApi().addEmail(currentUser, TEST_USER_EMAIL, true);
357+
emails = gitLabApi.getUserApi().getEmails(currentUser);
358+
assertTrue(emails.size() == currentSize + 1);
359+
Email found = emails.stream().filter(e -> e.getEmail().equals(TEST_USER_EMAIL)).findAny().orElse(null);
360+
assertNotNull(found);
361+
362+
gitLabApi.getUserApi().deleteEmail(currentUser, email.getId());
363+
emails = gitLabApi.getUserApi().getEmails(currentUser);
364+
assertEquals(currentSize, emails.size());
365+
found = emails.stream().filter(e -> e.getEmail().equals(TEST_USER_EMAIL)).findAny().orElse(null);
366+
assertNull(found);
367+
}
315368
}

0 commit comments

Comments
 (0)