Commit 329621fe authored by Martin Lowe's avatar Martin Lowe 🇨🇦 Committed by Martin Lowe
Browse files

Add tests, tidy up logic

parent f02f2295
......@@ -432,6 +432,11 @@ public class ValidationResource {
* @return the user account if found by mail, or null if none found.
*/
private EclipseUser retrieveUser(GitUser user) {
// check for noreply (no reply will never have user account, and fails fast)
EclipseUser eclipseUser = checkForNoReplyUser(user);
if (eclipseUser != null) {
return eclipseUser;
}
// standard user check (returns best match)
LOGGER.debug("Checking user with mail {}", user.getMail());
try {
......@@ -442,8 +447,7 @@ public class ValidationResource {
} catch(WebApplicationException e) {
LOGGER.warn("Could not find user account with mail '{}'", user.getMail());
}
// return results for no-reply
return checkForNoReplyUser(user);
return null;
}
/**
......@@ -464,8 +468,9 @@ public class ValidationResource {
String uname = nameParts[0].trim();
LOGGER.debug("User with mail {} detected as noreply account, checking services for username match on '{}'",
user.getMail(), uname);
// check github for no-reply (only allowed noreply currently)
if (user.getMail().contains("noreply.github.com")) {
if (user.getMail().endsWith("noreply.github.com")) {
try {
// check for Github no reply + return as its the last shot
return accounts.getUserByGithubUname("Bearer " + oauth.getToken(), uname);
......
......@@ -11,6 +11,7 @@ package org.eclipsefoundation.git.eca.api;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import javax.annotation.PostConstruct;
......@@ -100,4 +101,10 @@ public class MockAccountsAPI implements AccountsAPI {
}).collect(Collectors.toList());
}
@Override
public EclipseUser getUserByGithubUname(String authBearer, String uname) {
// assume GH username == Eclipse uname for simplicity of test
return src.stream().filter(user -> uname.equalsIgnoreCase(user.getName())).findFirst().orElse(null);
}
}
......@@ -826,4 +826,54 @@ class ValidationResourceTest {
// Should be invalid as wrong email was used for bot (uses Gerrit bot email)
given().body(vr).contentType(ContentType.JSON).when().post("/eca").then().statusCode(403);
}
void validateGithubNoReply_success() throws URISyntaxException {
GitUser g1 = new GitUser();
g1.setName("grunter");
g1.setMail("grunter+123456789@users.noreply.github.com");
List<Commit> commits = new ArrayList<>();
// create sample commits
Commit c1 = new Commit();
c1.setAuthor(g1);
c1.setCommitter(g1);
c1.setHash("123456789abcdefghijklmnop");
c1.setSubject("All of the things");
c1.setParents(Arrays.asList("46bb69bf6aa4ed26b2bf8c322ae05bef0bcc5c10"));
commits.add(c1);
ValidationRequest vr = new ValidationRequest();
vr.setProvider(ProviderType.GERRIT);
vr.setRepoUrl(new URI("/gitroot/sample/gerrit.other-project"));
vr.setCommits(commits);
vr.setStrictMode(true);
// test output w/ assertions
// Should be valid as grunter used a no-reply Github account and has a matching GH handle
given().body(vr).contentType(ContentType.JSON).when().post("/eca").then().statusCode(200);
}
void validateGithubNoReply_nomatch() throws URISyntaxException {
GitUser g1 = new GitUser();
g1.setName("some_guy");
g1.setMail("some_guy+123456789@users.noreply.github.com");
List<Commit> commits = new ArrayList<>();
// create sample commits
Commit c1 = new Commit();
c1.setAuthor(g1);
c1.setCommitter(g1);
c1.setHash("123456789abcdefghijklmnop");
c1.setSubject("All of the things");
c1.setParents(Arrays.asList("46bb69bf6aa4ed26b2bf8c322ae05bef0bcc5c10"));
commits.add(c1);
ValidationRequest vr = new ValidationRequest();
vr.setProvider(ProviderType.GERRIT);
vr.setRepoUrl(new URI("/gitroot/sample/gerrit.other-project"));
vr.setCommits(commits);
vr.setStrictMode(true);
// test output w/ assertions
// Should be invalid as no user exists with "Github" handle that matches some_guy
given().body(vr).contentType(ContentType.JSON).when().post("/eca").then().statusCode(403);
}
}
......@@ -2,6 +2,8 @@ org.eclipsefoundation.git.eca.api.AccountsAPI/mp-rest/url=https://api.eclipse.or
org.eclipsefoundation.git.eca.api.ProjectsAPI/mp-rest/url=https://projects.eclipse.org
org.eclipsefoundation.git.eca.api.BotsAPI/mp-rest/url=https://api.eclipse.org
eclipse.noreply.email-patterns=@users.noreply.github.com\$
## Expect to be mounted to '/git' to match current URL spec
quarkus.http.root-path=/git
......@@ -13,6 +15,4 @@ quarkus.http.port=8080
oauth2.client-id=placeholder
oauth2.client-secret=placeholder
quarkus.log.level=DEBUG
eclipse.mail.allowlist=noreply@github.com
\ No newline at end of file
Markdown is supported
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