Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
@Slf4j
public class HookResource {

public static final String MDC_KEY_HOOK_ID = "hookId";

public static final String GITLAB_HEADER = "X-Gitlab-Event";

private final IntegrationService service;
Expand All @@ -47,9 +45,10 @@ public void hook(@Auth Principal principal,
setThreadName(principal);
metricRegistry.counter(principal.getName()).inc();

log.info("Push hook received > {}", event);
log.info("Hook received > {}", event);
switch (event.getType()) {
case PUSH:
case TAG_PUSH:
service.performPushEvent(event);
break;
}
Expand All @@ -62,7 +61,7 @@ private void setThreadName(Principal principal) {
new Date().getTime());
Thread.currentThread().setName(requestId);

log.info("initMDC(): initialized new requestId <{}>", requestId);
log.info("setThreadName(): initialized new requestId <{}>", requestId);
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package fr.mmarie.resources;

import com.codahale.metrics.MetricRegistry;
import com.fasterxml.jackson.databind.ObjectMapper;
import fr.mmarie.api.gitlab.Event;
import fr.mmarie.core.IntegrationService;
import fr.mmarie.core.auth.GitLabAuthFilter;
Expand All @@ -8,24 +10,34 @@
import io.dropwizard.auth.AuthValueFactoryProvider;
import io.dropwizard.setup.Environment;
import io.dropwizard.testing.junit.ResourceTestRule;
import org.junit.After;
import org.junit.ClassRule;
import org.junit.Test;

import javax.validation.Validation;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Response;
import java.security.Principal;
import java.util.Base64;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

public class HookResourceTestIT {

private static final IntegrationService INTEGRATION_SERVICE = mock(IntegrationService.class);

private static final String PASSWORD = "test-password";

private static final Environment ENVIRONMENT = mock(Environment.class);
private static final Environment ENVIRONMENT = new Environment("mocked-env",
new ObjectMapper(),
Validation.buildDefaultValidatorFactory().getValidator(),
new MetricRegistry(),
ClassLoader.getSystemClassLoader());

@ClassRule
public static final ResourceTestRule resources = ResourceTestRule.builder()
Expand All @@ -38,15 +50,47 @@ public class HookResourceTestIT {
.addProvider(new AuthValueFactoryProvider.Binder<>(Principal.class))
.build();

@After
public void tearDown() throws Exception {
reset(INTEGRATION_SERVICE);
}

@Test
public void callHookShouldReturnNothing() throws Exception {
public void hook_WithPushEvent_ShouldPerformIt() throws Exception {
Event event = new Event(Event.Type.PUSH);
Response response = resources.client().target("/hook")
.queryParam("token", Base64.getEncoder().encodeToString(String.format("%s:%s", "test-svc", PASSWORD).getBytes()))
.request()
.post(Entity.json(new Event(Event.Type.PUSH)));
.post(Entity.json(event));

verify(INTEGRATION_SERVICE, timeout(100)).performPushEvent(event);
assertThat(response).isNotNull();
assertThat(response.getStatus()).isEqualTo(204);
}

@Test
public void hook_WithTagPushEvent_ShouldPerformIt() throws Exception {
Event event = new Event(Event.Type.TAG_PUSH);
Response response = resources.client().target("/hook")
.queryParam("token", Base64.getEncoder().encodeToString(String.format("%s:%s", "test-svc", PASSWORD).getBytes()))
.request()
.post(Entity.json(event));

verify(INTEGRATION_SERVICE, timeout(100)).performPushEvent(event);
assertThat(response).isNotNull();
assertThat(response.getStatus()).isEqualTo(204);
}

@Test
public void hook_WithMergeEvent_ShouldDoNothing() throws Exception {
Event event = new Event(Event.Type.MERGE_REQUEST);
Response response = resources.client().target("/hook")
.queryParam("token", Base64.getEncoder().encodeToString(String.format("%s:%s", "test-svc", PASSWORD).getBytes()))
.request()
.post(Entity.json(event));

verify(INTEGRATION_SERVICE, times(0)).performPushEvent(event);
assertThat(response).isNotNull();
assertThat(response.getStatus()).isEqualTo(204);
}
}