Commit 2eb52040 authored by Martin Lowe's avatar Martin Lowe 🇨🇦 Committed by Martin Lowe
Browse files

Fix tests associated with changes to bot detection logic


Signed-off-by: Martin Lowe's avatarMartin Lowe <martin.lowe@eclipse-foundation.org>
parent 070fad17
......@@ -291,6 +291,7 @@ public class ValidationResource {
List<JsonNode> botObjs = getBots();
// for each of the matched projects, check the bot for the matching project ID
for (Project p : filteredProjects) {
LOGGER.debug("Checking project {} for matching bots", p.getProjectId());
for (JsonNode bot : botObjs) {
// if the project ID match, and one of the email fields matches, then user is bot
if (p.getProjectId().equalsIgnoreCase(bot.get("projectId").asText())
......
......@@ -16,8 +16,11 @@ import javax.annotation.PostConstruct;
import javax.enterprise.context.ApplicationScoped;
import org.eclipse.microprofile.rest.client.inject.RestClient;
import org.eclipsefoundation.git.eca.model.BotUser;
import org.eclipsefoundation.git.eca.model.BotUser.SiteSpecificBot;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.TextNode;
import io.quarkus.test.Mock;
......@@ -26,43 +29,43 @@ import io.quarkus.test.Mock;
@ApplicationScoped
public class MockBotsAPI implements BotsAPI {
private List<BotUser> src;
private List<JsonNode> src;
@PostConstruct
public void build() {
this.src = new ArrayList<>();
BotUser b1 = new BotUser();
b1.setId("1");
b1.setEmail("1.bot@eclipse.org");
b1.setProjectId("sample.proj");
b1.setUsername("projbot");
ObjectNode b1 = new ObjectNode(JsonNodeFactory.instance);
b1.set("id", new TextNode("1"));
b1.set("email", new TextNode("1.bot@eclipse.org"));
b1.set("projectId", new TextNode("sample.proj"));
b1.set("username", new TextNode("projbot"));
src.add(b1);
BotUser b2 = new BotUser();
b2.setId("10");
b2.setEmail("2.bot@eclipse.org");
b2.setProjectId("sample.proto");
b2.setUsername("protobot");
SiteSpecificBot ssbGH = new SiteSpecificBot();
ssbGH.setEmail("2.bot-github@eclipse.org");
ssbGH.setUsername("protobot-gh");
b2.setGithubBot(ssbGH);
ObjectNode b2 = new ObjectNode(JsonNodeFactory.instance);
b2.set("id", new TextNode("10"));
b2.set("email", new TextNode("2.bot@eclipse.org"));
b2.set("projectId", new TextNode("sample.proto"));
b2.set("username", new TextNode("protobot"));
ObjectNode ssbGH = new ObjectNode(JsonNodeFactory.instance);
ssbGH.set("email", new TextNode("2.bot-github@eclipse.org"));
ssbGH.set("username", new TextNode("protobot-gh"));
b2.set("github.com", ssbGH);
src.add(b2);
BotUser b3 = new BotUser();
b3.setId("11");
b3.setEmail("3.bot@eclipse.org");
b3.setProjectId("spec.proj");
b3.setUsername("specbot");
SiteSpecificBot ssbGL = new SiteSpecificBot();
ssbGL.setEmail("3.bot-gitlab@eclipse.org");
ssbGL.setUsername("protobot-gl");
b3.setGitlabBot(ssbGL);
ObjectNode b3 = new ObjectNode(JsonNodeFactory.instance);
b3.set("id", new TextNode("11"));
b3.set("email", new TextNode("3.bot@eclipse.org"));
b3.set("projectId", new TextNode("spec.proj"));
b3.set("username", new TextNode("specbot"));
ObjectNode ssbGL = new ObjectNode(JsonNodeFactory.instance);
ssbGL.set("email", new TextNode("3.bot-gitlab@eclipse.org"));
ssbGL.set("username", new TextNode("protobot-gl"));
b3.set("gitlab.eclipse.org",ssbGL);
src.add(b3);
}
@Override
public List<BotUser> getBots() {
public List<JsonNode> getBots() {
return new ArrayList<>(src);
}
}
......@@ -77,8 +77,8 @@ public class MockProjectsAPI implements ProjectsAPI {
p2.setProjectId("sample.proto");
p2.setSpecWorkingGroup(null);
p2.setGithubRepos(Arrays.asList(r3));
p1.setGerritRepos(Arrays.asList(r6));
p1.setGitlabRepos(Arrays.asList(r8));
p2.setGerritRepos(Arrays.asList(r6));
p2.setGitlabRepos(Arrays.asList(r8));
p2.setCommitters(Arrays.asList(u2));
src.add(p2);
......@@ -87,7 +87,7 @@ public class MockProjectsAPI implements ProjectsAPI {
p3.setProjectId("spec.proj");
p3.setSpecWorkingGroup("proj1");
p3.setGithubRepos(Arrays.asList(r4));
p1.setGitlabRepos(Arrays.asList(r7));
p3.setGitlabRepos(Arrays.asList(r7));
p3.setCommitters(Arrays.asList(u1, u2));
src.add(p3);
}
......
......@@ -669,6 +669,32 @@ class ValidationResourceTest {
@Test
void validateBotCommiterAccessGithub() throws URISyntaxException {
// set up test users
GitUser g1 = new GitUser();
g1.setName("grunter");
g1.setMail("grunt@important.co");
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.GITHUB);
vr.setRepoUrl(new URI("http://www.github.com/eclipsefdn/sample"));
vr.setCommits(commits);
// test output w/ assertions
// Should be valid as user is a committer
given().body(vr).contentType(ContentType.JSON).when().post("/eca").then().statusCode(200);
}
@Test
void validateBotCommiterAccessGithub_invalidBot() throws URISyntaxException {
// set up test users
GitUser g1 = new GitUser();
g1.setName("protobot-gh");
......@@ -689,8 +715,8 @@ class ValidationResourceTest {
vr.setRepoUrl(new URI("http://www.github.com/eclipsefdn/sample"));
vr.setCommits(commits);
// test output w/ assertions
// Should be valid as bots don't need sign off and should be valid on any proj
given().body(vr).contentType(ContentType.JSON).when().post("/eca").then().statusCode(200);
// Should be invalid as bots should only commit on their own projects (including aliases)
given().body(vr).contentType(ContentType.JSON).when().post("/eca").then().statusCode(403);
}
@Test
......@@ -723,7 +749,7 @@ class ValidationResourceTest {
void validateBotCommiterAccessGitlab() throws URISyntaxException {
// set up test users
GitUser g1 = new GitUser();
g1.setName("specbot-gh");
g1.setName("specbot-gl");
g1.setMail("3.bot-gitlab@eclipse.org");
List<Commit> commits = new ArrayList<>();
......@@ -741,13 +767,13 @@ class ValidationResourceTest {
vr.setRepoUrl(new URI("https://gitlab.eclipse.org/eclipse/dash/dash.handbook.test"));
vr.setCommits(commits);
// test output w/ assertions
// Should be valid as bots don't need sign off and should be valid on any proj
given().body(vr).contentType(ContentType.JSON).when().post("/eca").then().statusCode(200);
// Should be invalid as bots should only commit on their own projects
given().body(vr).contentType(ContentType.JSON).when().post("/eca").then().statusCode(403);
}
@Test
void validateBotCommiterAccessGitlab_wrongEmail() throws URISyntaxException {
// set up test users - uses Gerrit/LDAP email (wrong for case)
// set up test users - uses Gerrit/LDAP email (expects Gitlab email)
GitUser g1 = new GitUser();
g1.setName("specbot");
g1.setMail("3.bot@eclipse.org");
......@@ -768,7 +794,7 @@ class ValidationResourceTest {
vr.setCommits(commits);
// test output w/ assertions
// 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);
given().body(vr).contentType(ContentType.JSON).when().post("/eca").then().statusCode(200);
}
@Test
......@@ -794,13 +820,13 @@ class ValidationResourceTest {
vr.setCommits(commits);
vr.setStrictMode(true);
// test output w/ assertions
// Should be valid as bots don't need sign off and should be valid on any proj
// Should be valid as bots should only commit on their own projects (including aliases)
given().body(vr).contentType(ContentType.JSON).when().post("/eca").then().statusCode(200);
}
@Test
void validateBotCommiterAccessGerrit_wrongEmail() throws URISyntaxException {
// set up test users - uses Gerrit/LDAP email (wrong for case)
void validateBotCommiterAccessGerrit_aliasEmail() throws URISyntaxException {
// set up test users - uses GH (instead of expected Gerrit/LDAP email)
GitUser g1 = new GitUser();
g1.setName("protobot-gh");
g1.setMail("2.bot-github@eclipse.org");
......@@ -821,7 +847,7 @@ class ValidationResourceTest {
vr.setCommits(commits);
vr.setStrictMode(true);
// test output w/ assertions
// 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);
// Should be valid as bots should only commit on their own projects (including aliases)
given().body(vr).contentType(ContentType.JSON).when().post("/eca").then().statusCode(200);
}
}
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