Skip to content

Commit

Permalink
removed unique device name per owner constraint
Browse files Browse the repository at this point in the history
  • Loading branch information
tobihagemann committed Sep 8, 2023
1 parent 20f72bc commit dd90976
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 23 deletions.
13 changes: 3 additions & 10 deletions backend/src/main/java/org/cryptomator/hub/api/DeviceResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.ws.rs.ClientErrorException;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.DELETE;
import jakarta.ws.rs.GET;
Expand All @@ -32,7 +31,6 @@
import org.eclipse.microprofile.jwt.JsonWebToken;
import org.eclipse.microprofile.openapi.annotations.Operation;
import org.eclipse.microprofile.openapi.annotations.responses.APIResponse;
import org.hibernate.exception.ConstraintViolationException;
import org.jboss.logging.Logger;
import org.jboss.resteasy.reactive.NoCache;

Expand Down Expand Up @@ -68,7 +66,6 @@ public List<DeviceDto> getSome(@QueryParam("ids") List<String> deviceIds) {
@Transactional
@Operation(summary = "creates or updates a device", description = "the device will be owned by the currently logged-in user")
@APIResponse(responseCode = "201", description = "Device created or updated")
@APIResponse(responseCode = "409", description = "Conflicting device name")
public Response createOrUpdate(@Valid @NotNull DeviceDto dto, @PathParam("deviceId") @ValidId String deviceId) {
Device device;
try {
Expand All @@ -88,13 +85,9 @@ public Response createOrUpdate(@Valid @NotNull DeviceDto dto, @PathParam("device
device.name = dto.name;
device.publickey = dto.publicKey;
device.userPrivateKey = dto.userPrivateKey;
try {
device.persistAndFlush();
AuditEventDeviceRegister.log(jwt.getSubject(), deviceId, device.name, device.type);
return Response.created(URI.create(".")).build();
} catch (ConstraintViolationException e) {
throw new ClientErrorException(Response.Status.CONFLICT, e);
}
device.persistAndFlush();
AuditEventDeviceRegister.log(jwt.getSubject(), deviceId, device.name, device.type);
return Response.created(URI.create(".")).build();
}

@GET
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ CREATE TABLE "device"
"user_privatekey" VARCHAR(2000) NOT NULL UNIQUE, -- private key, encrypted using device's public key (JWE ECDH-ES)
"creation_time" TIMESTAMP WITH TIME ZONE NOT NULL,
CONSTRAINT "DEVICE_PK" PRIMARY KEY ("id"),
CONSTRAINT "DEVICE_FK_USER" FOREIGN KEY ("owner_id") REFERENCES "user_details" ("id") ON DELETE CASCADE,
CONSTRAINT "DEVICE_UNIQUE_NAME_PER_OWNER" UNIQUE ("owner_id", "name")
CONSTRAINT "DEVICE_FK_USER" FOREIGN KEY ("owner_id") REFERENCES "user_details" ("id") ON DELETE CASCADE
);

-- new access tokens will be issued for users (not devices):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,6 @@ public void testCreateNoDeviceId() {
.then().statusCode(400);
}

@Test
@Order(1)
@DisplayName("PUT /devices/deviceX returns 409 due to non-unique name")
public void testCreateX() {
var deviceDto = new DeviceResource.DeviceDto("deviceX", "Computer 1", Device.Type.DESKTOP, "publickey1", "jwe.jwe.jwe.user1.deviceX", "user1", Instant.parse("2020-02-20T20:20:20Z"));

given().contentType(ContentType.JSON).body(deviceDto)
.when().put("/devices/{deviceId}", "deviceX")
.then().statusCode(409);
}

@Test
@Order(1)
@DisplayName("GET /devices/device1 returns 200")
Expand Down Expand Up @@ -131,6 +120,17 @@ public void testCreate999() throws SQLException {
}
}

@Test
@Order(2)
@DisplayName("PUT /devices/deviceX returns 201 (creating new device with same name as device1)")
public void testCreateX() {
var deviceDto = new DeviceResource.DeviceDto("deviceX", "Computer 1", Device.Type.DESKTOP, "publickey1", "jwe.jwe.jwe.user1.deviceX", "user1", Instant.parse("2020-02-20T20:20:20Z"));

given().contentType(ContentType.JSON).body(deviceDto)
.when().put("/devices/{deviceId}", "deviceX")
.then().statusCode(201);
}

@Test
@Order(3)
@DisplayName("GET /devices/device999 returns 200")
Expand Down

0 comments on commit dd90976

Please sign in to comment.