Search in sources :

Example 6 with MockHttpRequest

use of org.jboss.resteasy.mock.MockHttpRequest in project candlepin by candlepin.

the class CloudRegistrationAuthTest method testGetPrincipalAbortsIfAuthenticationIsWrongType.

@Test
public void testGetPrincipalAbortsIfAuthenticationIsWrongType() {
    CloudRegistrationInfo cloudRegInfo = this.buildCloudRegistrationInfo("test_type", "metadata", "sig");
    CloudRegistrationAuth provider = this.buildAuthProvider();
    String token = provider.generateRegistrationToken(mock(Principal.class), cloudRegInfo);
    MockHttpRequest request = this.buildHttpRequest();
    request.header("Authorization", "Potato " + token);
    Principal principal = provider.getPrincipal(request);
    assertNull(principal);
}
Also used : CloudRegistrationInfo(org.candlepin.service.model.CloudRegistrationInfo) MockHttpRequest(org.jboss.resteasy.mock.MockHttpRequest) Test(org.junit.jupiter.api.Test)

Example 7 with MockHttpRequest

use of org.jboss.resteasy.mock.MockHttpRequest in project candlepin by candlepin.

the class CloudRegistrationAuthTest method testGetPrincipalIgnoresExpiredTokens.

@Test
public void testGetPrincipalIgnoresExpiredTokens() {
    int ctSeconds = this.getCurrentSeconds();
    String token = this.buildMalformedToken(new JsonWebToken().type(TOKEN_TYPE).subject("test_subject").audience("test_org").issuedAt(ctSeconds - 15).notBefore(ctSeconds - 15).expiration(ctSeconds - 10));
    MockHttpRequest request = this.buildHttpRequest();
    request.header("Authorization", "Bearer " + token);
    Principal principal = this.buildAuthProvider().getPrincipal(request);
    assertNull(principal);
}
Also used : MockHttpRequest(org.jboss.resteasy.mock.MockHttpRequest) JsonWebToken(org.keycloak.representations.JsonWebToken) Test(org.junit.jupiter.api.Test)

Example 8 with MockHttpRequest

use of org.jboss.resteasy.mock.MockHttpRequest in project candlepin by candlepin.

the class CloudRegistrationAuthTest method testGetPrincipalIgnoresInactiveTokens.

@Test
public void testGetPrincipalIgnoresInactiveTokens() {
    int ctSeconds = this.getCurrentSeconds();
    String token = this.buildMalformedToken(new JsonWebToken().type(TOKEN_TYPE).subject("test_subject").audience("test_org").issuedAt(ctSeconds + 15).notBefore(ctSeconds + 15).expiration(ctSeconds + 25));
    MockHttpRequest request = this.buildHttpRequest();
    request.header("Authorization", "Bearer " + token);
    Principal principal = this.buildAuthProvider().getPrincipal(request);
    assertNull(principal);
}
Also used : MockHttpRequest(org.jboss.resteasy.mock.MockHttpRequest) JsonWebToken(org.keycloak.representations.JsonWebToken) Test(org.junit.jupiter.api.Test)

Example 9 with MockHttpRequest

use of org.jboss.resteasy.mock.MockHttpRequest in project candlepin by candlepin.

the class CloudRegistrationAuthTest method testGetPrincipal.

@Test
public void testGetPrincipal() {
    CloudRegistrationInfo cloudRegInfo = this.buildCloudRegistrationInfo("test_type", "metadata", "sig");
    CloudRegistrationAuth provider = this.buildAuthProvider();
    String token = provider.generateRegistrationToken(mock(Principal.class), cloudRegInfo);
    MockHttpRequest request = this.buildHttpRequest();
    request.header("Authorization", "Bearer " + token);
    Principal principal = provider.getPrincipal(request);
    assertNotNull(principal);
    assertTrue(principal instanceof UserPrincipal);
    // Test note:
    // The owner key resolves to the cloud registration type due to the mock adapter implementation
    // we've defined in our test init, which *always* uses the type as the owner key. In the real
    // implementation, this isn't guaranteed.
    String ownerKey = cloudRegInfo.getType();
    Owner owner = this.mockOwnerCurator.getByKey(ownerKey);
    assertTrue(principal.canAccess(owner, SubResource.CONSUMERS, Access.CREATE));
}
Also used : CloudRegistrationInfo(org.candlepin.service.model.CloudRegistrationInfo) MockHttpRequest(org.jboss.resteasy.mock.MockHttpRequest) Owner(org.candlepin.model.Owner) Test(org.junit.jupiter.api.Test)

Example 10 with MockHttpRequest

use of org.jboss.resteasy.mock.MockHttpRequest in project scm-review-plugin by scm-manager.

the class CommentRootResourceTest method shouldNotThrowConflictExceptionIfNoExpectationsInUrl.

@Test
@SubjectAware(username = "slarti", password = "secret")
public void shouldNotThrowConflictExceptionIfNoExpectationsInUrl() 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").content(commentJson).contentType(MediaType.APPLICATION_JSON);
    dispatcher.invoke(request, response);
    assertEquals(HttpServletResponse.SC_CREATED, response.getStatus());
}
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)

Aggregations

MockHttpRequest (org.jboss.resteasy.mock.MockHttpRequest)108 Test (org.junit.Test)64 SubjectAware (com.github.sdorra.shiro.SubjectAware)46 Test (org.junit.jupiter.api.Test)41 JsonNode (com.fasterxml.jackson.databind.JsonNode)29 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)24 PullRequest (com.cloudogu.scm.review.pullrequest.service.PullRequest)17 TestData.createPullRequest (com.cloudogu.scm.review.TestData.createPullRequest)16 DisplayUser (sonia.scm.user.DisplayUser)16 User (sonia.scm.user.User)16 MockHttpResponse (org.jboss.resteasy.mock.MockHttpResponse)14 NamespaceAndName (sonia.scm.repository.NamespaceAndName)13 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)9 Repository (sonia.scm.repository.Repository)9 Comment (com.cloudogu.scm.review.comment.service.Comment)8 BranchRevisionResolver (com.cloudogu.scm.review.pullrequest.dto.BranchRevisionResolver)8 UnsupportedEncodingException (java.io.UnsupportedEncodingException)8 URISyntaxException (java.net.URISyntaxException)8 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)8 ArgumentMatchers.argThat (org.mockito.ArgumentMatchers.argThat)8