Search in sources :

Example 1 with Account

use of uk.gov.pay.api.auth.Account in project pay-publicapi by alphagov.

the class RateLimiterFilterTest method shouldSendErrorResponse_whenRateLimitExceeded.

@Test
public void shouldSendErrorResponse_whenRateLimitExceeded() throws Exception {
    Account account = new Account(ACCOUNT_ID, TokenPaymentType.CARD, "some-token-link");
    SecurityContext mockSecurityContext = mock(SecurityContext.class);
    when(mockSecurityContext.getUserPrincipal()).thenReturn(account);
    when(mockContainerRequestContext.getSecurityContext()).thenReturn(mockSecurityContext);
    when(mockContainerRequestContext.getMethod()).thenReturn("GET");
    doThrow(RateLimitException.class).when(rateLimiter).checkRateOf(eq("account-id"), any());
    WebApplicationException webApplicationException = assertThrows(WebApplicationException.class, () -> rateLimiterFilter.filter(mockContainerRequestContext));
    Response response = webApplicationException.getResponse();
    assertEquals(429, response.getStatus());
    assertEquals("application/json", response.getHeaderString("Content-Type"));
    assertEquals("utf-8", response.getHeaderString("Content-Encoding"));
    assertEquals("{\"code\":\"P0900\",\"description\":\"Too many requests\"}", response.getEntity());
}
Also used : Response(javax.ws.rs.core.Response) Account(uk.gov.pay.api.auth.Account) WebApplicationException(javax.ws.rs.WebApplicationException) SecurityContext(javax.ws.rs.core.SecurityContext) Test(org.junit.jupiter.api.Test)

Example 2 with Account

use of uk.gov.pay.api.auth.Account in project pay-publicapi by alphagov.

the class RateLimiterFilterTest method shouldCheckRateLimitsWhenFilterIsInvoked.

@Test
public void shouldCheckRateLimitsWhenFilterIsInvoked() throws Exception {
    Account account = new Account(ACCOUNT_ID, TokenPaymentType.CARD, "some-token-link");
    SecurityContext mockSecurityContext = mock(SecurityContext.class);
    when(mockSecurityContext.getUserPrincipal()).thenReturn(account);
    when(mockContainerRequestContext.getSecurityContext()).thenReturn(mockSecurityContext);
    when(mockContainerRequestContext.getMethod()).thenReturn("GET");
    when(mockContainerRequestContext.getMethod()).thenReturn("POST");
    rateLimiterFilter.filter(mockContainerRequestContext);
    verify(rateLimiter).checkRateOf(eq(ACCOUNT_ID), any());
}
Also used : Account(uk.gov.pay.api.auth.Account) SecurityContext(javax.ws.rs.core.SecurityContext) Test(org.junit.jupiter.api.Test)

Example 3 with Account

use of uk.gov.pay.api.auth.Account in project pay-publicapi by alphagov.

the class CachingAuthenticatorIT method setup.

@Before
public void setup() throws Exception {
    String tokenLink = "some-token-link";
    stubPublicAuthV1ApiAuth(publicAuthRule, new Account(accountId, CARD, tokenLink), bearerToken);
    setUpMockForConnector();
}
Also used : Account(uk.gov.pay.api.auth.Account) Before(org.junit.Before)

Example 4 with Account

use of uk.gov.pay.api.auth.Account in project pay-publicapi by alphagov.

the class CreatePaymentServiceTest method shouldThrowExceptionWithIdentifierMissingMandatoryAttribute_whenReturnUrlMissing.

@Test
@PactVerification({ "connector" })
@Pacts(pacts = { "publicapi-connector-create-payment-with-missing-return-url" })
public void shouldThrowExceptionWithIdentifierMissingMandatoryAttribute_whenReturnUrlMissing() {
    Account account = new Account("444", TokenPaymentType.CARD, "a-token-link");
    var requestPayload = CreateCardPaymentRequestBuilder.builder().amount(100).reference("a reference").description("a description").build();
    try {
        createPaymentService.create(account, requestPayload);
        fail("Expected CreateChargeException to be thrown");
    } catch (CreateChargeException e) {
        assertThat(e.getErrorIdentifier(), is(ErrorIdentifier.MISSING_MANDATORY_ATTRIBUTE));
        assertThat(e.getConnectorErrorMessage(), is("Missing mandatory attribute: return_url"));
    }
}
Also used : Account(uk.gov.pay.api.auth.Account) CreateChargeException(uk.gov.pay.api.exception.CreateChargeException) PactVerification(au.com.dius.pact.consumer.PactVerification) Test(org.junit.Test) Pacts(uk.gov.service.payments.commons.testing.pact.consumers.Pacts)

Example 5 with Account

use of uk.gov.pay.api.auth.Account in project pay-publicapi by alphagov.

the class CreatePaymentServiceTest method setup.

@Before
public void setup() {
    // We will actually send real requests here, which will be intercepted by pact
    when(configuration.getConnectorUrl()).thenReturn(connectorRule.getUrl());
    when(configuration.getBaseUrl()).thenReturn("http://publicapi.test.localhost/");
    PublicApiUriGenerator publicApiUriGenerator = new PublicApiUriGenerator(configuration);
    ConnectorUriGenerator connectorUriGenerator = new ConnectorUriGenerator(configuration);
    Client client = RestClientFactory.buildClient(new RestClientConfig(false));
    createPaymentService = new CreatePaymentService(client, publicApiUriGenerator, connectorUriGenerator);
    account = new Account("123456", TokenPaymentType.CARD, "a-token-link");
}
Also used : Account(uk.gov.pay.api.auth.Account) RestClientConfig(uk.gov.pay.api.app.config.RestClientConfig) Client(javax.ws.rs.client.Client) Before(org.junit.Before)

Aggregations

Account (uk.gov.pay.api.auth.Account)47 Test (org.junit.Test)40 PactVerification (au.com.dius.pact.consumer.PactVerification)37 Pacts (uk.gov.service.payments.commons.testing.pact.consumers.Pacts)37 CardPayment (uk.gov.pay.api.model.CardPayment)14 PaymentWithAllLinks (uk.gov.pay.api.model.links.PaymentWithAllLinks)14 Response (javax.ws.rs.core.Response)9 PaymentState (uk.gov.pay.api.model.PaymentState)5 RefundResponse (uk.gov.pay.api.model.RefundResponse)5 PostLink (uk.gov.pay.api.model.links.PostLink)4 SearchRefundsResults (uk.gov.pay.api.model.search.card.SearchRefundsResults)4 Test (org.junit.jupiter.api.Test)3 CreateChargeException (uk.gov.pay.api.exception.CreateChargeException)3 RefundsResponse (uk.gov.pay.api.model.RefundsResponse)3 Client (javax.ws.rs.client.Client)2 SecurityContext (javax.ws.rs.core.SecurityContext)2 Before (org.junit.Before)2 PaymentEventsResponse (uk.gov.pay.api.model.PaymentEventsResponse)2 Timed (com.codahale.metrics.annotation.Timed)1 ImmutableMap (com.google.common.collect.ImmutableMap)1