Search in sources :

Example 6 with SubjectAware

use of com.github.sdorra.shiro.SubjectAware in project scm-review-plugin by scm-manager.

the class CommentRootResourceTest method shouldGetAllLinks.

@Test
@SubjectAware(username = "slarti", password = "secret")
public void shouldGetAllLinks() throws URISyntaxException, IOException {
    mockExistingComments();
    MockHttpRequest request = MockHttpRequest.get("/" + PullRequestRootResource.PULL_REQUESTS_PATH_V2 + "/space/name/1/comments").contentType(MediaType.APPLICATION_JSON);
    dispatcher.invoke(request, response);
    assertEquals(HttpServletResponse.SC_OK, response.getStatus());
    ObjectMapper mapper = new ObjectMapper();
    JsonNode jsonNode = mapper.readValue(response.getContentAsString(), JsonNode.class);
    JsonNode prNode = jsonNode.get("_embedded").get("pullRequestComments");
    JsonNode comment_1 = prNode.path(0);
    JsonNode comment_2 = prNode.path(1);
    assertThat(comment_1.get("_links").get("self").get("href").asText()).isEqualTo("/v2/pull-requests/space/name/1/comments/1");
    assertThat(comment_2.get("_links").get("self").get("href").asText()).isEqualTo("/v2/pull-requests/space/name/1/comments/2");
    assertThat(comment_1.get("_links").get("update").get("href").asText()).isEqualTo("/v2/pull-requests/space/name/1/comments/1");
    assertThat(comment_2.get("_links").get("update").get("href").asText()).isEqualTo("/v2/pull-requests/space/name/1/comments/2");
    assertThat(comment_1.get("_links").get("delete").get("href").asText()).isEqualTo("/v2/pull-requests/space/name/1/comments/1");
    // must not delete comment with responses
    assertThat(comment_2.get("_links").get("delete")).isNull();
    // root comments have reply links
    assertThat(comment_1.get("_links").get("reply").get("href").asText()).isEqualTo("/v2/pull-requests/space/name/1/comments/1/reply");
    assertThat(comment_2.get("_links").get("reply").get("href").asText()).isEqualTo("/v2/pull-requests/space/name/1/comments/2/reply");
    JsonNode replies = comment_2.get("_embedded").get("replies");
    JsonNode reply_1 = replies.path(0);
    JsonNode reply_2 = replies.path(1);
    assertThat(reply_1.get("_links").get("self").get("href").asText()).isEqualTo("/v2/pull-requests/space/name/1/comments/2/replies/2_1");
    assertThat(reply_2.get("_links").get("self").get("href").asText()).isEqualTo("/v2/pull-requests/space/name/1/comments/2/replies/2_2");
    assertThat(reply_1.get("_links").get("update").get("href").asText()).isEqualTo("/v2/pull-requests/space/name/1/comments/2/replies/2_1");
    assertThat(reply_2.get("_links").get("update").get("href").asText()).isEqualTo("/v2/pull-requests/space/name/1/comments/2/replies/2_2");
    assertThat(reply_1.get("_links").get("delete").get("href").asText()).isEqualTo("/v2/pull-requests/space/name/1/comments/2/replies/2_1");
    assertThat(reply_2.get("_links").get("delete").get("href").asText()).isEqualTo("/v2/pull-requests/space/name/1/comments/2/replies/2_2");
    // replies no longer have a reply link
    assertThat(reply_1.get("_links").get("reply")).isNull();
    assertThat(reply_2.get("_links").get("reply")).isNull();
}
Also used : MockHttpRequest(org.jboss.resteasy.mock.MockHttpRequest) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test) SubjectAware(com.github.sdorra.shiro.SubjectAware)

Example 7 with SubjectAware

use of com.github.sdorra.shiro.SubjectAware in project scm-review-plugin by scm-manager.

the class CommentRootResourceTest method shouldThrowConflictExceptionIfTargetRevisionNotEqualPullRequestTargetRevision.

@Test
@SubjectAware(username = "slarti", password = "secret")
public void shouldThrowConflictExceptionIfTargetRevisionNotEqualPullRequestTargetRevision() throws URISyntaxException, UnsupportedEncodingException {
    when(pullRequestService.get(any(), any(), any())).thenReturn(PULL_REQUEST);
    when(branchRevisionResolver.getRevisions(any(), eq(PULL_REQUEST))).thenReturn(new BranchRevisionResolver.RevisionResult("source", "target"));
    byte[] commentJson = "{\"comment\" : \"this is my comment\"}".getBytes();
    MockHttpRequest request = MockHttpRequest.post("/" + PullRequestRootResource.PULL_REQUESTS_PATH_V2 + "/space/name/1/comments?sourceRevision=source&targetRevision=other").content(commentJson).contentType(MediaType.APPLICATION_JSON);
    dispatcher.invoke(request, response);
    assertEquals(HttpServletResponse.SC_CONFLICT, response.getStatus());
    assertThat(response.getContentAsString()).contains("modified concurrently");
}
Also used : MockHttpRequest(org.jboss.resteasy.mock.MockHttpRequest) BranchRevisionResolver(com.cloudogu.scm.review.pullrequest.dto.BranchRevisionResolver) Test(org.junit.Test) SubjectAware(com.github.sdorra.shiro.SubjectAware)

Example 8 with SubjectAware

use of com.github.sdorra.shiro.SubjectAware in project scm-review-plugin by scm-manager.

the class CommentRootResourceTest method shouldCreateNewComment.

@Test
@SubjectAware(username = "slarti", password = "secret")
public void shouldCreateNewComment() throws URISyntaxException {
    when(pullRequestService.get(any(), any(), any())).thenReturn(PULL_REQUEST);
    when(service.add(eq(REPOSITORY_NAMESPACE), eq(REPOSITORY_NAME), eq("1"), argThat(t -> t.getComment().equals("this is my comment")))).thenReturn("1");
    byte[] commentJson = "{\"comment\" : \"this is my comment\"}".getBytes();
    MockHttpRequest request = MockHttpRequest.post("/" + PullRequestRootResource.PULL_REQUESTS_PATH_V2 + "/space/name/1/comments?sourceRevision=source&targetRevision=target").content(commentJson).contentType(MediaType.APPLICATION_JSON);
    dispatcher.invoke(request, response);
    assertEquals(HttpServletResponse.SC_CREATED, response.getStatus());
    assertThat(response.getOutputHeaders().getFirst("Location")).hasToString("/v2/pull-requests/space/name/1/comments/1");
}
Also used : ContextLine.copy(com.cloudogu.scm.review.comment.service.ContextLine.copy) Provider(javax.inject.Provider) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) ShiroRule(com.github.sdorra.shiro.ShiroRule) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) RestDispatcher(sonia.scm.web.RestDispatcher) URISyntaxException(java.net.URISyntaxException) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) BranchRevisionResolver(com.cloudogu.scm.review.pullrequest.dto.BranchRevisionResolver) Repository(sonia.scm.repository.Repository) PullRequest(com.cloudogu.scm.review.pullrequest.service.PullRequest) PullRequestRootResource(com.cloudogu.scm.review.pullrequest.api.PullRequestRootResource) PullRequestMapperImpl(com.cloudogu.scm.review.pullrequest.dto.PullRequestMapperImpl) MediaType(javax.ws.rs.core.MediaType) Comment(com.cloudogu.scm.review.comment.service.Comment) Arrays.asList(java.util.Arrays.asList) JsonNode(com.fasterxml.jackson.databind.JsonNode) UriBuilder(javax.ws.rs.core.UriBuilder) PullRequestService(com.cloudogu.scm.review.pullrequest.service.PullRequestService) RepositoryResolver(com.cloudogu.scm.review.RepositoryResolver) PullRequestResource(com.cloudogu.scm.review.pullrequest.api.PullRequestResource) ExecutedTransition(com.cloudogu.scm.review.comment.service.ExecutedTransition) ChannelRegistry(sonia.scm.sse.ChannelRegistry) MockHttpRequest(org.jboss.resteasy.mock.MockHttpRequest) CommentService(com.cloudogu.scm.review.comment.service.CommentService) UserDisplayManager(sonia.scm.user.UserDisplayManager) InlineContext(com.cloudogu.scm.review.comment.service.InlineContext) Providers(com.google.inject.util.Providers) UriInfo(javax.ws.rs.core.UriInfo) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) MockHttpResponse(org.jboss.resteasy.mock.MockHttpResponse) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) ArrayList(java.util.ArrayList) Reply(com.cloudogu.scm.review.comment.service.Reply) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) Reply.createReply(com.cloudogu.scm.review.comment.service.Reply.createReply) RepositoryServiceFactory(sonia.scm.repository.api.RepositoryServiceFactory) MockedDiffLine(com.cloudogu.scm.review.comment.service.MockedDiffLine) Before(org.junit.Before) InjectMocks(org.mockito.InjectMocks) TestData(com.cloudogu.scm.review.TestData) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HttpServletResponse(javax.servlet.http.HttpServletResponse) Test(org.junit.Test) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) CommentTransition(com.cloudogu.scm.review.comment.service.CommentTransition) SubjectAware(com.github.sdorra.shiro.SubjectAware) Rule(org.junit.Rule) Location(com.cloudogu.scm.review.comment.service.Location) Comment.createComment(com.cloudogu.scm.review.comment.service.Comment.createComment) Assert.assertEquals(org.junit.Assert.assertEquals) MockHttpRequest(org.jboss.resteasy.mock.MockHttpRequest) Test(org.junit.Test) SubjectAware(com.github.sdorra.shiro.SubjectAware)

Example 9 with SubjectAware

use of com.github.sdorra.shiro.SubjectAware in project scm-review-plugin by scm-manager.

the class RepositoryLinkEnricherTest method shouldNotEnrichBecauseOfMissingPermission.

@Test
@SubjectAware(username = "trillian", password = "secret")
public void shouldNotEnrichBecauseOfMissingPermission() {
    enricher = new RepositoryLinkEnricher(scmPathInfoStoreProvider, pullRequestService, configService, globalEngineConfigurator);
    Repository repo = new Repository("id", "type", "space", "name");
    HalEnricherContext context = HalEnricherContext.of(repo);
    enricher.enrich(context, appender);
    verify(appender, never()).appendLink(any(), any());
}
Also used : Repository(sonia.scm.repository.Repository) HalEnricherContext(sonia.scm.api.v2.resources.HalEnricherContext) Test(org.junit.Test) SubjectAware(com.github.sdorra.shiro.SubjectAware)

Example 10 with SubjectAware

use of com.github.sdorra.shiro.SubjectAware in project scm-review-plugin by scm-manager.

the class RepositoryLinkEnricherTest method shouldEnrichWorkflowConfigLink.

@Test
@SubjectAware(username = "dent", password = "secret")
public void shouldEnrichWorkflowConfigLink() {
    when(pullRequestService.supportsPullRequests(any())).thenReturn(true);
    mockGlobalConfig(true);
    enricher = new RepositoryLinkEnricher(scmPathInfoStoreProvider, pullRequestService, configService, globalEngineConfigurator);
    Repository repo = new Repository("id", "type", "space", "name");
    HalEnricherContext context = HalEnricherContext.of(repo);
    enricher.enrich(context, appender);
    verify(appender).appendLink("workflowConfig", "https://scm-manager.org/scm/api/v2/workflow/space/name/config");
}
Also used : Repository(sonia.scm.repository.Repository) HalEnricherContext(sonia.scm.api.v2.resources.HalEnricherContext) Test(org.junit.Test) SubjectAware(com.github.sdorra.shiro.SubjectAware)

Aggregations

SubjectAware (com.github.sdorra.shiro.SubjectAware)91 Test (org.junit.Test)91 MockHttpRequest (org.jboss.resteasy.mock.MockHttpRequest)45 Comment.createComment (com.cloudogu.scm.review.comment.service.Comment.createComment)27 JsonNode (com.fasterxml.jackson.databind.JsonNode)18 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)16 PullRequest (com.cloudogu.scm.review.pullrequest.service.PullRequest)14 Reply.createReply (com.cloudogu.scm.review.comment.service.Reply.createReply)12 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)12 TestData.createPullRequest (com.cloudogu.scm.review.TestData.createPullRequest)11 Repository (sonia.scm.repository.Repository)9 BasicComment (com.cloudogu.scm.review.comment.service.BasicComment)6 BranchRevisionResolver (com.cloudogu.scm.review.pullrequest.dto.BranchRevisionResolver)6 HalEnricherContext (sonia.scm.api.v2.resources.HalEnricherContext)6 NamespaceAndName (sonia.scm.repository.NamespaceAndName)6 DisplayUser (sonia.scm.user.DisplayUser)6 User (sonia.scm.user.User)6 Changeset (sonia.scm.repository.Changeset)5 Comment (com.cloudogu.scm.review.comment.service.Comment)4 ShiroRule (com.github.sdorra.shiro.ShiroRule)3