Skip to content

Commit

Permalink
PP-12355: Unhappy paths for updating a service name (#5255)
Browse files Browse the repository at this point in the history
* PP-12355: Unhappy paths for updating a service name

In line with other endpoints, we're also using a class `UpdateServiceNameRequest` to represent the body of an API request, rather than more inefficient way of passing in a Map and doing manual validation on that.
  • Loading branch information
oswaldquek committed May 20, 2024
1 parent 782cae1 commit 539ef12
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 22 deletions.
10 changes: 5 additions & 5 deletions .secrets.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -188,14 +188,14 @@
"filename": "openapi/connector_spec.yaml",
"hashed_secret": "40a714e4a70aa9b75e73dc27be3a978fa98ee4e3",
"is_verified": false,
"line_number": 4677
"line_number": 4684
},
{
"type": "Hex High Entropy String",
"filename": "openapi/connector_spec.yaml",
"hashed_secret": "ec969b7613112e3df2b11d8f64ce37f0d20bf29d",
"is_verified": false,
"line_number": 4688
"line_number": 4695
}
],
"src/main/java/uk/gov/pay/connector/agreement/resource/AgreementsApiResource.java": [
Expand Down Expand Up @@ -315,14 +315,14 @@
"filename": "src/main/java/uk/gov/pay/connector/gatewayaccount/resource/GatewayAccountResource.java",
"hashed_secret": "5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8",
"is_verified": false,
"line_number": 81
"line_number": 82
},
{
"type": "Hex High Entropy String",
"filename": "src/main/java/uk/gov/pay/connector/gatewayaccount/resource/GatewayAccountResource.java",
"hashed_secret": "48ca4a65970b54002939b02e8d8c1b0167de7e4b",
"is_verified": false,
"line_number": 139
"line_number": 140
}
],
"src/main/java/uk/gov/pay/connector/gatewayaccount/resource/StripeAccountResource.java": [
Expand Down Expand Up @@ -1059,5 +1059,5 @@
}
]
},
"generated_at": "2024-05-20T12:55:59Z"
"generated_at": "2024-05-20T14:38:11Z"
}
7 changes: 7 additions & 0 deletions openapi/connector_spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4648,6 +4648,13 @@ components:
updated:
type: string
example: 2022-06-28T10:41:40.460Z
UpdateServiceNameRequest:
type: object
properties:
service_name:
type: string
required:
- service_name
ValidationResult:
type: object
properties:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package uk.gov.pay.connector.gatewayaccount.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import org.hibernate.validator.constraints.Length;

import javax.validation.constraints.NotNull;

@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
@JsonIgnoreProperties(ignoreUnknown = true)
public class UpdateServiceNameRequest {

@NotNull(message = "Field [service_name] cannot be null")
@Length(max = 50, min = 1, message = "Field [service_name] can have a size between 1 and 50")
private String serviceName;

public String getServiceName() {
return serviceName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import uk.gov.pay.connector.gatewayaccount.model.GatewayAccountType;
import uk.gov.pay.connector.gatewayaccount.model.GatewayAccountWithCredentialsResponse;
import uk.gov.pay.connector.gatewayaccount.model.GatewayAccountsListDTO;
import uk.gov.pay.connector.gatewayaccount.model.UpdateServiceNameRequest;
import uk.gov.pay.connector.gatewayaccount.service.GatewayAccountService;
import uk.gov.pay.connector.gatewayaccount.service.GatewayAccountServicesFactory;
import uk.gov.pay.connector.gatewayaccount.service.GatewayAccountSwitchPaymentProviderService;
Expand Down Expand Up @@ -352,19 +353,17 @@ public Response patchGatewayAccount(@Parameter(example = "1", description = "Gat
public Response updateGatewayAccountServiceNameByServiceId(
@Parameter(example = "46eb1b601348499196c99de90482ee68", description = "Service ID") @PathParam("serviceId") String serviceId,
@Parameter(example = "test", description = "Account type") @PathParam("accountType") GatewayAccountType accountType,
Map<String, String> payload) {
@Valid UpdateServiceNameRequest updateServiceNameRequest) {

String serviceName = payload.get(SERVICE_NAME_FIELD_NAME);

return gatewayAccountService.getGatewayAccountByServiceIdAndAccountType(serviceId, accountType)
.map(gatewayAccount ->
{
gatewayAccount.setServiceName(serviceName);
gatewayAccount.setServiceName(updateServiceNameRequest.getServiceName());
return Response.ok().build();
}
)
.orElseGet(() ->
notFoundResponse(format("The gateway account for service id '%s' and account type '%s' does not exist", serviceId, accountType)));
notFoundResponse(format("A service with service id '%s' and account type '%s' does not exist", serviceId, accountType)));
}

@PATCH
Expand Down Expand Up @@ -406,7 +405,7 @@ public Response updateGatewayAccountServiceNameByGatewayAccountId(@Parameter(exa
.orElseGet(() ->
notFoundResponse(format("The gateway account id '%s' does not exist", gatewayAccountId)));
}

@PATCH
@Path("/v1/frontend/accounts/{accountId}/3ds-toggle")
@Consumes(APPLICATION_JSON)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,15 +368,14 @@ void forInvalidCredentials_shouldReturn200_withResultInvalid() {
"organisational_unit_id", invalidOrgUnitId,
"jwt_mac_key", invalidJwtMacKey));

String gatewayAccountId = app.givenSetup()
app.givenSetup()
.body(toJson(Map.of(
"payment_provider", "worldpay",
"service_id", SERVICE_ID,
"service_name", SERVICE_NAME,
"type", "test"
)))
.post("/v1/api/accounts")
.then().extract().path("gateway_account_id");
.post("/v1/api/accounts");

givenSetup()
.body(payload)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.restassured.response.Response;
import io.restassured.response.ValidatableResponse;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
Expand All @@ -14,6 +15,7 @@
import uk.gov.pay.connector.extension.AppWithPostgresAndSqsExtension;
import uk.gov.pay.connector.gatewayaccount.model.GatewayAccountType;
import uk.gov.pay.connector.it.dao.DatabaseFixtures;
import uk.gov.pay.connector.util.RandomIdGenerator;
import uk.gov.service.payments.commons.model.ErrorIdentifier;

import java.util.Arrays;
Expand Down Expand Up @@ -185,17 +187,24 @@ void shouldReturn404IfGatewayAccountIsNotNumeric() {

@Nested
class UpdateServiceNameByServiceIdAndAccountType {
@Test
void updateGatewayAccountServiceNameSuccessfully() {
String serviceId = "a-service-id";
Map<String, String> payload = Map.of(

private Map<String, String> gatewayAccountRequest;
private String serviceId;

@BeforeEach
void before() {
serviceId = RandomIdGenerator.newId();
gatewayAccountRequest = Map.of(
"payment_provider", "stripe",
"service_id", serviceId,
"service_name", "Service Name",
"type", "test"
);
"type", "test");
}

@Test
void updateGatewayAccountServiceNameSuccessfully() {
app.givenSetup()
.body(toJson(payload))
.body(toJson(gatewayAccountRequest))
.post(ACCOUNTS_API_URL)
.then()
.statusCode(201)
Expand All @@ -215,8 +224,43 @@ void updateGatewayAccountServiceNameSuccessfully() {
.statusCode(200)
.body("service_name", is("New Service Name"));
}

@Test
void assertBadRequestForMissingServiceNameField() {
app.givenSetup().body(toJson(gatewayAccountRequest)).post(ACCOUNTS_API_URL);

app.givenSetup().accept(JSON)
.body(Map.of())
.patch(format("/v1/frontend/service/%s/test/servicename", serviceId))
.then()
.statusCode(422)
.body("message", contains("Field [service_name] cannot be null"))
.body("error_identifier", is(ErrorIdentifier.GENERIC.toString()));
}

//TODO equivalent tests for PreDegatewayedServiceNameEndpoint
@Test
void assertBadRequestForInvalidServiceNameLength() {
app.givenSetup().body(toJson(gatewayAccountRequest)).post(ACCOUNTS_API_URL);

app.givenSetup().accept(JSON)
.body(Map.of("service_name", "service-name-more-than-fifty-chars-service-name-more-than-fifty-chars"))
.patch(format("/v1/frontend/service/%s/test/servicename", serviceId))
.then()
.statusCode(422)
.body("message", contains("Field [service_name] can have a size between 1 and 50"))
.body("error_identifier", is(ErrorIdentifier.GENERIC.toString()));
}

@Test
void assertNotFoundForNonExistentServiceId() {
app.givenSetup().accept(JSON)
.body(Map.of("service_name", "New Service Name"))
.patch("/v1/frontend/service/nexiste-pas/test/servicename")
.then()
.statusCode(404)
.body("message", contains("A service with service id 'nexiste-pas' and account type 'test' does not exist"))
.body("error_identifier", is(ErrorIdentifier.GENERIC.toString()));
}
}

@Nested
Expand Down

0 comments on commit 539ef12

Please sign in to comment.