diff --git a/src/main/java/com/iexec/sms/secret/SecretController.java b/src/main/java/com/iexec/sms/secret/SecretController.java index a8c593a8..c0d9a662 100644 --- a/src/main/java/com/iexec/sms/secret/SecretController.java +++ b/src/main/java/com/iexec/sms/secret/SecretController.java @@ -1,5 +1,5 @@ /* - * Copyright 2020-2024 IEXEC BLOCKCHAIN TECH + * Copyright 2020-2026 IEXEC BLOCKCHAIN TECH * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,7 +18,6 @@ import com.iexec.sms.authorization.AuthorizationService; import com.iexec.sms.secret.web2.NotAnExistingSecretException; -import com.iexec.sms.secret.web2.SameSecretException; import com.iexec.sms.secret.web2.Web2SecretService; import com.iexec.sms.secret.web3.Web3SecretService; import lombok.extern.slf4j.Slf4j; @@ -115,7 +114,7 @@ public ResponseEntity updateWeb2Secret(@RequestHeader String authorizati @RequestParam String ownerAddress, @RequestParam String secretName, @RequestBody String newSecretValue) { - String challenge = authorizationService.getChallengeForSetWeb2Secret(ownerAddress, secretName, newSecretValue); + final String challenge = authorizationService.getChallengeForSetWeb2Secret(ownerAddress, secretName, newSecretValue); if (!authorizationService.isSignedByHimself(challenge, authorization, ownerAddress)) { log.error("Unauthorized to updateWeb2Secret [expectedChallenge:{}]", challenge); @@ -129,8 +128,6 @@ public ResponseEntity updateWeb2Secret(@RequestHeader String authorizati try { web2SecretService.updateSecret(ownerAddress, secretName, newSecretValue); return ResponseEntity.noContent().build(); - } catch (SameSecretException ignored) { - return ResponseEntity.noContent().build(); } catch (NoSuchElementException | NotAnExistingSecretException e) { return ResponseEntity.notFound().build(); } diff --git a/src/main/java/com/iexec/sms/secret/web2/SameSecretException.java b/src/main/java/com/iexec/sms/secret/web2/SameSecretException.java deleted file mode 100644 index af06678b..00000000 --- a/src/main/java/com/iexec/sms/secret/web2/SameSecretException.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.iexec.sms.secret.web2; - -import lombok.Getter; - -@Getter -public class SameSecretException extends Exception { - private final String ownerAddress; - private final String secretAddress; - - public SameSecretException(String ownerAddress, String secretAddress) { - this.ownerAddress = ownerAddress; - this.secretAddress = secretAddress; - } -} diff --git a/src/main/java/com/iexec/sms/secret/web2/Web2SecretService.java b/src/main/java/com/iexec/sms/secret/web2/Web2SecretService.java index 5931dd43..878b6310 100644 --- a/src/main/java/com/iexec/sms/secret/web2/Web2SecretService.java +++ b/src/main/java/com/iexec/sms/secret/web2/Web2SecretService.java @@ -128,11 +128,9 @@ public boolean addSecret(String ownerAddress, String secretAddress, String secre * @param ownerAddress Address of the secret owner. * @param secretAddress Address of the secret. * @param newSecretValue New, unencrypted value of the secret. - * @return The {@link Web2Secret} that has been saved. * @throws NotAnExistingSecretException thrown when the requested secret does not exist. - * @throws SameSecretException thrown when the requested secret already contains the encrypted value. */ - public Web2Secret updateSecret(String ownerAddress, String secretAddress, String newSecretValue) throws NotAnExistingSecretException, SameSecretException { + public void updateSecret(String ownerAddress, String secretAddress, String newSecretValue) throws NotAnExistingSecretException { final Optional oSecret = getSecret(ownerAddress, secretAddress); if (oSecret.isEmpty()) { log.error("Secret does not exist, can't update it [ownerAddress:{}, secretAddress:{}]", @@ -145,12 +143,11 @@ public Web2Secret updateSecret(String ownerAddress, String secretAddress, String if (Objects.equals(secret.getValue(), encryptedValue)) { log.info("No need to update secret [ownerAddress:{}, secretAddress:{}]", ownerAddress, secretAddress); - throw new SameSecretException(ownerAddress, secretAddress); + return; } final Web2Secret newSecret = secret.withValue(encryptedValue); final Web2Secret savedSecret = web2SecretRepository.save(newSecret); cacheSecretService.putSecretExistenceInCache(savedSecret.getHeader(), true); - return savedSecret; } } diff --git a/src/test/java/com/iexec/sms/secret/SecretControllerTests.java b/src/test/java/com/iexec/sms/secret/SecretControllerTests.java index 92dfeb14..e66dfcbf 100644 --- a/src/test/java/com/iexec/sms/secret/SecretControllerTests.java +++ b/src/test/java/com/iexec/sms/secret/SecretControllerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2022-2024 IEXEC BLOCKCHAIN TECH + * Copyright 2022-2026 IEXEC BLOCKCHAIN TECH * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,14 +18,13 @@ import com.iexec.sms.authorization.AuthorizationService; import com.iexec.sms.secret.web2.NotAnExistingSecretException; -import com.iexec.sms.secret.web2.SameSecretException; import com.iexec.sms.secret.web2.Web2SecretService; import com.iexec.sms.secret.web3.Web3SecretService; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; import org.mockito.Mock; -import org.mockito.MockitoAnnotations; +import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -35,6 +34,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.*; +@ExtendWith(MockitoExtension.class) class SecretControllerTests { private static final String AUTHORIZATION = "AUTHORIZATION"; @@ -59,11 +59,6 @@ class SecretControllerTests { private static final SecureRandom seed = new SecureRandom(); - @BeforeEach - public void init() { - MockitoAnnotations.openMocks(this); - } - //region isWeb3SecretSet @Test void shouldReturnNoContentWhenWeb3SecretExists() { @@ -233,7 +228,7 @@ void failToUpdateWeb2SecretWhenPayloadTooLarge() { } @Test - void failToUpdateWeb2SecretWhenSecretIsMissing() throws NotAnExistingSecretException, SameSecretException { + void failToUpdateWeb2SecretWhenSecretIsMissing() throws NotAnExistingSecretException { when(authorizationService.getChallengeForSetWeb2Secret(WEB2_OWNER_ADDRESS, WEB2_SECRET_NAME, WEB2_SECRET_VALUE)) .thenReturn(CHALLENGE); when(authorizationService.isSignedByHimself(CHALLENGE, AUTHORIZATION, WEB2_OWNER_ADDRESS)) diff --git a/src/test/java/com/iexec/sms/secret/web2/Web2SecretServiceTests.java b/src/test/java/com/iexec/sms/secret/web2/Web2SecretServiceTests.java index 14fc6eeb..fbde1dc2 100644 --- a/src/test/java/com/iexec/sms/secret/web2/Web2SecretServiceTests.java +++ b/src/test/java/com/iexec/sms/secret/web2/Web2SecretServiceTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2022-2024 IEXEC BLOCKCHAIN TECH + * Copyright 2022-2026 IEXEC BLOCKCHAIN TECH * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,8 +25,9 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestInstance; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; -import org.mockito.MockitoAnnotations; +import org.mockito.junit.jupiter.MockitoExtension; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; @@ -39,6 +40,7 @@ import static org.mockito.Mockito.*; @DataJpaTest +@ExtendWith(MockitoExtension.class) @TestInstance(TestInstance.Lifecycle.PER_CLASS) class Web2SecretServiceTests { private static final String OWNER_ADDRESS = "ownerAddress"; @@ -73,7 +75,6 @@ void initLog() { @BeforeEach void beforeEach() { - MockitoAnnotations.openMocks(this); memoryLogAppender.reset(); web2SecretRepository.deleteAll(); web2CacheSecretService.clear(); @@ -215,7 +216,7 @@ void shouldNotAddSecretWhenNull() { // region updateSecret @Test - void shouldUpdateSecret() throws NotAnExistingSecretException, SameSecretException { + void shouldUpdateSecret() throws NotAnExistingSecretException { final Web2Secret encryptedSecret = new Web2Secret(OWNER_ADDRESS, SECRET_ADDRESS, ENCRYPTED_SECRET_VALUE); final String newSecretValue = "newSecretValue"; final String newEncryptedSecretValue = "newEncryptedSecretValue"; @@ -223,7 +224,8 @@ void shouldUpdateSecret() throws NotAnExistingSecretException, SameSecretExcepti when(encryptionService.encrypt(newSecretValue)) .thenReturn(newEncryptedSecretValue); - final Web2Secret newSecret = web2SecretService.updateSecret(OWNER_ADDRESS, SECRET_ADDRESS, newSecretValue); + web2SecretService.updateSecret(OWNER_ADDRESS, SECRET_ADDRESS, newSecretValue); + final Web2Secret newSecret = web2SecretRepository.findById(encryptedSecret.getHeader()).orElseThrow(); assertAll( () -> assertThat(newSecret).extracting(Web2Secret::getHeader).usingRecursiveComparison().isEqualTo(new Web2SecretHeader(OWNER_ADDRESS, SECRET_ADDRESS)), () -> assertThat(newSecret).extracting(Web2Secret::getValue).isEqualTo(newEncryptedSecretValue), @@ -243,17 +245,17 @@ void shouldNotUpdateSecretIfMissing() { } @Test - void shouldNotUpdateSecretIfSameValue() { + void shouldNotUpdateSecretIfSameValue() throws NotAnExistingSecretException { final Web2Secret encryptedSecret = new Web2Secret(OWNER_ADDRESS, SECRET_ADDRESS, ENCRYPTED_SECRET_VALUE); web2SecretRepository.save(encryptedSecret); when(encryptionService.encrypt(PLAIN_SECRET_VALUE)) .thenReturn(ENCRYPTED_SECRET_VALUE); - final SameSecretException exception = assertThrows(SameSecretException.class, - () -> web2SecretService.updateSecret(OWNER_ADDRESS, SECRET_ADDRESS, PLAIN_SECRET_VALUE)); + web2SecretService.updateSecret(OWNER_ADDRESS, SECRET_ADDRESS, PLAIN_SECRET_VALUE); + final Web2Secret newSecret = web2SecretRepository.findById(encryptedSecret.getHeader()).orElseThrow(); assertAll( - () -> assertEquals(OWNER_ADDRESS, exception.getOwnerAddress()), - () -> assertEquals(SECRET_ADDRESS, exception.getSecretAddress()), + () -> assertThat(newSecret).extracting(Web2Secret::getHeader).usingRecursiveComparison().isEqualTo(new Web2SecretHeader(OWNER_ADDRESS, SECRET_ADDRESS)), + () -> assertThat(newSecret).extracting(Web2Secret::getValue).isEqualTo(ENCRYPTED_SECRET_VALUE), () -> assertThat(web2SecretRepository.count()).isOne() ); }