Skip to content
This repository was archived by the owner on May 16, 2023. It is now read-only.

Commit 8fee024

Browse files
authored
Merge pull request #50 from corona-warn-app/fix/search-and-delete
Fix: Search Pending DCC and Cleanup
2 parents 05f9914 + 479976a commit 8fee024

File tree

5 files changed

+54
-33
lines changed

5 files changed

+54
-33
lines changed

pom.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
<liquibase.version>4.6.2</liquibase.version>
4242
<springdoc.version>1.6.4</springdoc.version>
4343
<cbor.version>4.5</cbor.version>
44-
<hibernate-validator.version>7.0.2.Final</hibernate-validator.version>
4544
<shedlock.version>4.30.0</shedlock.version>
4645
<!-- plugins -->
4746
<plugin.checkstyle.version>3.1.2</plugin.checkstyle.version>
@@ -216,11 +215,6 @@
216215
<version>${spring-security.version}</version>
217216
<type>jar</type>
218217
</dependency>
219-
<dependency>
220-
<groupId>org.hibernate.validator</groupId>
221-
<artifactId>hibernate-validator</artifactId>
222-
<version>${hibernate-validator.version}</version>
223-
</dependency>
224218
<dependency>
225219
<groupId>io.reactivex</groupId>
226220
<artifactId>rxjava</artifactId>

src/main/java/app/coronawarn/dcc/controller/ExternalPublicKeyController.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ public ResponseEntity<Void> uploadPublicKey(
9797
} else if (e.getReason()
9898
== DccRegistrationService.DccRegistrationException.Reason.REGISTRATION_TOKEN_ALREADY_EXISTS) {
9999
throw new DccServerException(HttpStatus.CONFLICT, "RegistrationToken is already assigned with a PublicKey.");
100+
} else if (e.getReason()
101+
== DccRegistrationService.DccRegistrationException.Reason.NO_LAB_ID) {
102+
throw new DccServerException(HttpStatus.FORBIDDEN, "Lab has not provided any LabId for this TestResult.");
100103
} else {
101104
throw new DccServerException(HttpStatus.INTERNAL_SERVER_ERROR, "Unexpected error");
102105
}

src/main/java/app/coronawarn/dcc/repository/DccRegistrationRepository.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public interface DccRegistrationRepository extends JpaRepository<DccRegistration
3737

3838
Optional<DccRegistration> findByRegistrationToken(String registrationToken);
3939

40-
List<DccRegistration> findByLabIdAndDccHashIsNull(String labId);
40+
List<DccRegistration> findByLabIdAndDccHashIsNullAndPublicKeyIsNotNull(String labId);
4141

4242
Optional<DccRegistration> findByHashedGuid(String hashedGuid);
4343

@@ -48,11 +48,12 @@ public interface DccRegistrationRepository extends JpaRepository<DccRegistration
4848
@Modifying(clearAutomatically = true, flushAutomatically = true)
4949
@Query("UPDATE DccRegistration d SET d.dcc = NULL, d.publicKey = NULL, d.encryptedDataEncryptionKey = NULL,"
5050
+ " d.error = NULL, d.hashedGuid = NULL, d.dccEncryptedPayload = NULL"
51-
+ " WHERE d.updatedAt < :threshold AND d.dcc IS NOT NULL")
51+
+ " WHERE d.updatedAt < :threshold AND d.publicKey IS NOT NULL")
5252
int removeDccDataByUpdatedAtBefore(@Param("threshold") LocalDateTime threshold);
5353

5454
@Modifying(clearAutomatically = true, flushAutomatically = true)
55-
@Query("UPDATE DccRegistration d SET d.registrationToken = NULL"
55+
@Query("UPDATE DccRegistration d SET d.registrationToken = NULL, d.dcc = NULL, d.publicKey = NULL,"
56+
+ " d.encryptedDataEncryptionKey = NULL, d.error = NULL, d.hashedGuid = NULL, d.dccEncryptedPayload = NULL"
5657
+ " WHERE d.createdAt < :threshold AND d.registrationToken IS NOT NULL")
5758
int removeRegistrationTokenByCreatedAtBefore(@Param("threshold") LocalDateTime threshold);
5859
}

src/main/java/app/coronawarn/dcc/service/DccRegistrationService.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public DccRegistration createDccRegistration(String registrationToken, PublicKey
8989
* @return List of matching DCC Registrations.
9090
*/
9191
public List<DccRegistration> findPendingDccByLabId(String labId) {
92-
return dccRegistrationRepository.findByLabIdAndDccHashIsNull(labId);
92+
return dccRegistrationRepository.findByLabIdAndDccHashIsNullAndPublicKeyIsNotNull(labId);
9393
}
9494

9595
/**
@@ -194,6 +194,10 @@ private InternalTestResult checkRegistrationTokenIsValid(String registrationToke
194194
throw new DccRegistrationException(DccRegistrationException.Reason.INVALID_REGISTRATION_TOKEN_FORBIDDEN);
195195
}
196196

197+
if (testResult.getLabId() == null) {
198+
throw new DccRegistrationException(DccRegistrationException.Reason.NO_LAB_ID);
199+
}
200+
197201
return testResult;
198202
}
199203

@@ -247,7 +251,8 @@ public enum Reason {
247251
REGISTRATION_TOKEN_ALREADY_EXISTS,
248252
INVALID_REGISTRATION_TOKEN_NOT_FOUND,
249253
INVALID_REGISTRATION_TOKEN_FORBIDDEN,
250-
VERIFICATION_SERVER_ERROR
254+
VERIFICATION_SERVER_ERROR,
255+
NO_LAB_ID
251256
}
252257
}
253258

src/test/java/app/coronawarn/dcc/controller/ExternalPublicKeyControllerTest.java

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import static app.coronawarn.dcc.utils.TestValues.registrationTokenValue;
2828
import static app.coronawarn.dcc.utils.TestValues.testId;
2929
import static org.mockito.ArgumentMatchers.eq;
30-
import static org.mockito.Mockito.doReturn;
3130
import static org.mockito.Mockito.doThrow;
3231
import static org.mockito.Mockito.reset;
3332
import static org.mockito.Mockito.when;
@@ -100,9 +99,9 @@ void testUploadPublicKey() throws Exception {
10099
registrationTokenValue, publicKeyBase64, "");
101100

102101
mockMvc.perform(post("/version/v1/publicKey")
103-
.contentType(MediaType.APPLICATION_JSON_VALUE)
104-
.content(new ObjectMapper().writeValueAsString(uploadPublicKeyRequest))
105-
)
102+
.contentType(MediaType.APPLICATION_JSON_VALUE)
103+
.content(new ObjectMapper().writeValueAsString(uploadPublicKeyRequest))
104+
)
106105
.andExpect(status().isCreated());
107106

108107

@@ -120,9 +119,9 @@ void testUploadPublicKeyFailedInvalidPublicKey() throws Exception {
120119
registrationTokenValue, publicKeyBase64, "");
121120

122121
mockMvc.perform(post("/version/v1/publicKey")
123-
.contentType(MediaType.APPLICATION_JSON_VALUE)
124-
.content(new ObjectMapper().writeValueAsString(uploadPublicKeyRequest))
125-
)
122+
.contentType(MediaType.APPLICATION_JSON_VALUE)
123+
.content(new ObjectMapper().writeValueAsString(uploadPublicKeyRequest))
124+
)
126125
.andExpect(status().isBadRequest());
127126
}
128127

@@ -140,12 +139,31 @@ void testUploadPublicKeyInvalidRegistrationTokenForbidden() throws Exception {
140139
.when(verificationServerClientMock).result(eq(registrationToken));
141140

142141
mockMvc.perform(post("/version/v1/publicKey")
143-
.contentType(MediaType.APPLICATION_JSON_VALUE)
144-
.content(new ObjectMapper().writeValueAsString(uploadPublicKeyRequest))
145-
)
142+
.contentType(MediaType.APPLICATION_JSON_VALUE)
143+
.content(new ObjectMapper().writeValueAsString(uploadPublicKeyRequest))
144+
)
146145
.andExpect(status().isForbidden());
147146
}
148147

148+
@Test
149+
void testUploadPublicKeyInvalidRegistrationTokenNoLabId() throws Exception {
150+
KeyPair keyPair = TestUtils.generateKeyPair();
151+
String publicKeyBase64 = Base64.getEncoder().encodeToString(keyPair.getPublic().getEncoded());
152+
153+
UploadPublicKeyRequest uploadPublicKeyRequest = new UploadPublicKeyRequest(
154+
registrationTokenValue, publicKeyBase64, "");
155+
156+
reset(verificationServerClientMock);
157+
158+
when(verificationServerClientMock.result(eq(registrationToken)))
159+
.thenReturn(new InternalTestResult(6, null, testId, 0));
160+
161+
mockMvc.perform(post("/version/v1/publicKey")
162+
.contentType(MediaType.APPLICATION_JSON_VALUE)
163+
.content(new ObjectMapper().writeValueAsString(uploadPublicKeyRequest))
164+
)
165+
.andExpect(status().isForbidden());
166+
}
149167

150168
@Test
151169
void testUploadPublicKeyInvalidRegistrationTokenNotFound() throws Exception {
@@ -161,9 +179,9 @@ void testUploadPublicKeyInvalidRegistrationTokenNotFound() throws Exception {
161179
.when(verificationServerClientMock).result(eq(registrationToken));
162180

163181
mockMvc.perform(post("/version/v1/publicKey")
164-
.contentType(MediaType.APPLICATION_JSON_VALUE)
165-
.content(new ObjectMapper().writeValueAsString(uploadPublicKeyRequest))
166-
)
182+
.contentType(MediaType.APPLICATION_JSON_VALUE)
183+
.content(new ObjectMapper().writeValueAsString(uploadPublicKeyRequest))
184+
)
167185
.andExpect(status().isNotFound());
168186
}
169187

@@ -182,9 +200,9 @@ void testUploadPublicKeyInvalidRegistrationTokenInternalServerError() throws Exc
182200
.when(verificationServerClientMock).result(eq(registrationToken));
183201

184202
mockMvc.perform(post("/version/v1/publicKey")
185-
.contentType(MediaType.APPLICATION_JSON_VALUE)
186-
.content(new ObjectMapper().writeValueAsString(uploadPublicKeyRequest))
187-
)
203+
.contentType(MediaType.APPLICATION_JSON_VALUE)
204+
.content(new ObjectMapper().writeValueAsString(uploadPublicKeyRequest))
205+
)
188206
.andExpect(status().isInternalServerError());
189207
}
190208

@@ -199,9 +217,9 @@ void testUploadPublicKeyFailedAlreadyExists() throws Exception {
199217
registrationTokenValue, publicKeyBase64, "");
200218

201219
mockMvc.perform(post("/version/v1/publicKey")
202-
.contentType(MediaType.APPLICATION_JSON_VALUE)
203-
.content(new ObjectMapper().writeValueAsString(uploadPublicKeyRequest))
204-
)
220+
.contentType(MediaType.APPLICATION_JSON_VALUE)
221+
.content(new ObjectMapper().writeValueAsString(uploadPublicKeyRequest))
222+
)
205223
.andExpect(status().isConflict());
206224
}
207225

@@ -219,9 +237,9 @@ void testUploadPublicKeyFailedTestResultPending() throws Exception {
219237
.thenReturn(new InternalTestResult(0, labId, testId, 0));
220238

221239
mockMvc.perform(post("/version/v1/publicKey")
222-
.contentType(MediaType.APPLICATION_JSON_VALUE)
223-
.content(new ObjectMapper().writeValueAsString(uploadPublicKeyRequest))
224-
)
240+
.contentType(MediaType.APPLICATION_JSON_VALUE)
241+
.content(new ObjectMapper().writeValueAsString(uploadPublicKeyRequest))
242+
)
225243
.andExpect(status().isForbidden());
226244
}
227245

0 commit comments

Comments
 (0)