|
1 | 1 | package com.iexec.resultproxy.proxy; |
2 | 2 |
|
3 | | -import static org.assertj.core.api.Assertions.assertThat; |
4 | | -import static org.mockito.ArgumentMatchers.any; |
5 | | -import static org.mockito.Mockito.when; |
6 | | - |
| 3 | +import com.iexec.commons.poco.chain.ChainContribution; |
7 | 4 | import com.iexec.resultproxy.chain.IexecHubService; |
8 | 5 | import com.iexec.resultproxy.ipfs.IpfsResultService; |
9 | | - |
10 | 6 | import org.junit.jupiter.api.BeforeEach; |
11 | 7 | import org.junit.jupiter.api.Test; |
| 8 | +import org.junit.jupiter.api.io.TempDir; |
12 | 9 | import org.mockito.InjectMocks; |
13 | 10 | import org.mockito.Mock; |
14 | 11 | import org.mockito.MockitoAnnotations; |
| 12 | +import org.mockito.Spy; |
| 13 | + |
| 14 | +import java.io.File; |
| 15 | +import java.util.Base64; |
| 16 | +import java.util.Optional; |
| 17 | + |
| 18 | +import static com.iexec.commons.poco.chain.ChainContributionStatus.REVEALED; |
| 19 | +import static org.assertj.core.api.Assertions.assertThat; |
| 20 | +import static org.mockito.Mockito.*; |
15 | 21 |
|
16 | 22 | class ProxyServiceTest { |
| 23 | + private static final String CHAIN_TASK_ID = "0x59d9b6c36d6db89bae058ff55de6e4d6a6f6e0da3f9ea02297fc8d6d5f5cedf1"; |
| 24 | + private static final String WALLET_ADDRESS = "0x123abc"; |
| 25 | + /** |
| 26 | + * Contains a valid result zip, with the following files: |
| 27 | + * <ul> |
| 28 | + * <li>{@literal computed.json}, pointing on `/iexec_out/result.txt` |
| 29 | + * <li>{@literal result.txt} |
| 30 | + * </ul> |
| 31 | + */ |
| 32 | + private static final byte[] RESULT_ZIP = Base64.getDecoder().decode("UEsDBBQACAgIADhKL1cAAAAAAAAAAAAAAAANAAAAY29tcHV0ZWQuanNvbqtWSkktSS3KzczLLC7JTNbNLy0pKC3RLUgsyVCyUlDSz0ytSE2OB4rqF6UWl+aU6JVUlCjVAgBQSwcIv08alTcAAAA2AAAAUEsDBBQACAgIADhKL1cAAAAAAAAAAAAAAAAKAAAAcmVzdWx0LnR4dG2NsQrDMAxEd33FdbOhoL1boUM/wnCE4MFg2qEZOvjjK9tKhhIbW9LTnQQC6G8ET7DX/5CQhnlJHol3FQkGAuOAU9ENPaqZE45k6h0NSC/N0Ff231DzgW3qbWxqDqeCZuDYkfpCehIQoeYHEtMOR4NRcHKantF55JlrfV9xr2XNF5HHsi2fvCFoyd+8srw03iDyA1BLBwgqyEkSkwAAAE0BAABQSwECFAAUAAgICAA4Si9Xv08alTcAAAA2AAAADQAAAAAAAAAAAAAAAAAAAAAAY29tcHV0ZWQuanNvblBLAQIUABQACAgIADhKL1cqyEkSkwAAAE0BAAAKAAAAAAAAAAAAAAAAAHIAAAByZXN1bHQudHh0UEsFBgAAAAACAAIAcwAAAD0BAAAAAA=="); |
| 33 | + /** |
| 34 | + * {@link ChainContribution} with a hash |
| 35 | + * corresponding to {@link ProxyServiceTest#RESULT_ZIP} hash, |
| 36 | + * including a fixed ChainTask ID: {@literal 0x59d9b6c36d6db89bae058ff55de6e4d6a6f6e0da3f9ea02297fc8d6d5f5cedf1}. |
| 37 | + */ |
| 38 | + private static final ChainContribution CHAIN_CONTRIBUTION = ChainContribution.builder() |
| 39 | + .resultHash("0x865e1ebff87de7928040a42383b46690a12a988b278eb880e0e641f5da3cc9d1") |
| 40 | + .build(); |
| 41 | + |
| 42 | + @TempDir |
| 43 | + File tmpFolder; |
17 | 44 |
|
18 | 45 | @Mock |
19 | 46 | private IexecHubService iexecHubService; |
20 | 47 |
|
21 | 48 | @Mock |
22 | 49 | private IpfsResultService ipfsResultService; |
23 | 50 |
|
24 | | - |
| 51 | + @Spy |
25 | 52 | @InjectMocks |
26 | 53 | private ProxyService proxyService; |
27 | 54 |
|
28 | | - private String chainTaskId; |
29 | | - private String walletAddress; |
30 | 55 |
|
31 | 56 | @BeforeEach |
32 | 57 | void init() { |
33 | 58 | MockitoAnnotations.openMocks(this); |
34 | | - chainTaskId = "0x1"; |
35 | | - walletAddress = "0x123abc"; |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + void isNotAbleToUploadSinceNoChainContribution() { |
| 63 | + when(iexecHubService.isTeeTask(CHAIN_TASK_ID)).thenReturn(false); |
| 64 | + when(ipfsResultService.doesResultExist(CHAIN_TASK_ID)).thenReturn(false); |
| 65 | + when(iexecHubService.isStatusTrueOnChain(CHAIN_TASK_ID, WALLET_ADDRESS, REVEALED)).thenReturn(true); |
| 66 | + when(iexecHubService.getChainContribution(CHAIN_TASK_ID, WALLET_ADDRESS)).thenReturn(Optional.empty()); |
| 67 | + |
| 68 | + assertThat(proxyService.canUploadResult(CHAIN_TASK_ID, WALLET_ADDRESS, RESULT_ZIP)).isFalse(); |
| 69 | + |
| 70 | + verify(iexecHubService).isTeeTask(CHAIN_TASK_ID); |
| 71 | + verify(proxyService).isResultFound(CHAIN_TASK_ID); |
| 72 | + verify(iexecHubService).isStatusTrueOnChain(CHAIN_TASK_ID, WALLET_ADDRESS, REVEALED); |
| 73 | + verify(iexecHubService).getChainContribution(CHAIN_TASK_ID, WALLET_ADDRESS); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + void isNotAbleToUploadSinceCannotWriteZip() { |
| 78 | + when(iexecHubService.isTeeTask(CHAIN_TASK_ID)).thenReturn(false); |
| 79 | + when(ipfsResultService.doesResultExist(CHAIN_TASK_ID)).thenReturn(false); |
| 80 | + when(iexecHubService.isStatusTrueOnChain(CHAIN_TASK_ID, WALLET_ADDRESS, REVEALED)).thenReturn(true); |
| 81 | + when(iexecHubService.getChainContribution(CHAIN_TASK_ID, WALLET_ADDRESS)).thenReturn(Optional.of(CHAIN_CONTRIBUTION)); |
| 82 | + when(proxyService.getResultFolderPath(CHAIN_TASK_ID)).thenReturn("/this/path/does/not/exist"); |
| 83 | + |
| 84 | + assertThat(proxyService.canUploadResult(CHAIN_TASK_ID, WALLET_ADDRESS, RESULT_ZIP)).isFalse(); |
| 85 | + |
| 86 | + verify(iexecHubService).isTeeTask(CHAIN_TASK_ID); |
| 87 | + verify(proxyService).isResultFound(CHAIN_TASK_ID); |
| 88 | + verify(iexecHubService).isStatusTrueOnChain(CHAIN_TASK_ID, WALLET_ADDRESS, REVEALED); |
| 89 | + verify(iexecHubService).getChainContribution(CHAIN_TASK_ID, WALLET_ADDRESS); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + void isNotAbleToUploadSinceWrongHash() { |
| 94 | + when(iexecHubService.isTeeTask(CHAIN_TASK_ID)).thenReturn(false); |
| 95 | + when(ipfsResultService.doesResultExist(CHAIN_TASK_ID)).thenReturn(false); |
| 96 | + when(iexecHubService.isStatusTrueOnChain(CHAIN_TASK_ID, WALLET_ADDRESS, REVEALED)).thenReturn(true); |
| 97 | + when(iexecHubService.getChainContribution(CHAIN_TASK_ID, WALLET_ADDRESS)).thenReturn(Optional.of(CHAIN_CONTRIBUTION)); |
| 98 | + when(proxyService.getResultFolderPath(CHAIN_TASK_ID)).thenReturn(tmpFolder.getAbsolutePath()); |
| 99 | + |
| 100 | + assertThat(proxyService.canUploadResult(CHAIN_TASK_ID, WALLET_ADDRESS, new byte[] {})).isFalse(); |
| 101 | + |
| 102 | + verify(iexecHubService).isTeeTask(CHAIN_TASK_ID); |
| 103 | + verify(proxyService).isResultFound(CHAIN_TASK_ID); |
| 104 | + verify(iexecHubService).isStatusTrueOnChain(CHAIN_TASK_ID, WALLET_ADDRESS, REVEALED); |
| 105 | + verify(iexecHubService).getChainContribution(CHAIN_TASK_ID, WALLET_ADDRESS); |
36 | 106 | } |
37 | 107 |
|
38 | 108 | @Test |
39 | 109 | void isNotAbleToUploadSinceResultAlreadyExistsWithIpfs() { |
40 | | - when(iexecHubService.isPublicResult(chainTaskId, 0)).thenReturn(true); |
41 | | - when(ipfsResultService.doesResultExist(chainTaskId)).thenReturn(true); |
| 110 | + when(iexecHubService.isTeeTask(CHAIN_TASK_ID)).thenReturn(false); |
| 111 | + when(ipfsResultService.doesResultExist(CHAIN_TASK_ID)).thenReturn(true); |
| 112 | + when(iexecHubService.getChainContribution(CHAIN_TASK_ID, WALLET_ADDRESS)).thenReturn(Optional.of(CHAIN_CONTRIBUTION)); |
| 113 | + when(proxyService.getResultFolderPath(CHAIN_TASK_ID)).thenReturn(tmpFolder.getAbsolutePath()); |
| 114 | + |
| 115 | + assertThat(proxyService.canUploadResult(CHAIN_TASK_ID, WALLET_ADDRESS, RESULT_ZIP)).isFalse(); |
42 | 116 |
|
43 | | - assertThat(proxyService.canUploadResult(chainTaskId, walletAddress)).isFalse(); |
| 117 | + verify(iexecHubService).isTeeTask(CHAIN_TASK_ID); |
| 118 | + verify(proxyService).isResultFound(CHAIN_TASK_ID); |
| 119 | + verify(iexecHubService, never()).isStatusTrueOnChain(CHAIN_TASK_ID, WALLET_ADDRESS, REVEALED); |
| 120 | + verify(iexecHubService, never()).getChainContribution(CHAIN_TASK_ID, WALLET_ADDRESS); |
44 | 121 | } |
45 | 122 |
|
46 | 123 | @Test |
47 | 124 | void isNotAbleToUploadSinceChainStatusIsNotRevealedWithIpfs() { |
48 | | - when(iexecHubService.isPublicResult(chainTaskId, 0)).thenReturn(true); |
49 | | - when(ipfsResultService.doesResultExist(chainTaskId)).thenReturn(true); |
50 | | - when(iexecHubService.isStatusTrueOnChain(any(), any(), any())).thenReturn(false); |
51 | | - |
52 | | - assertThat(proxyService.canUploadResult(chainTaskId, walletAddress)).isFalse(); |
| 125 | + when(iexecHubService.isTeeTask(CHAIN_TASK_ID)).thenReturn(false); |
| 126 | + when(ipfsResultService.doesResultExist(CHAIN_TASK_ID)).thenReturn(true); |
| 127 | + when(iexecHubService.isStatusTrueOnChain(CHAIN_TASK_ID, WALLET_ADDRESS, REVEALED)).thenReturn(false); |
| 128 | + when(iexecHubService.getChainContribution(CHAIN_TASK_ID, WALLET_ADDRESS)).thenReturn(Optional.of(CHAIN_CONTRIBUTION)); |
| 129 | + when(proxyService.getResultFolderPath(CHAIN_TASK_ID)).thenReturn(tmpFolder.getAbsolutePath()); |
| 130 | + |
| 131 | + assertThat(proxyService.canUploadResult(CHAIN_TASK_ID, WALLET_ADDRESS, RESULT_ZIP)).isFalse(); |
| 132 | + |
| 133 | + verify(iexecHubService).isTeeTask(CHAIN_TASK_ID); |
| 134 | + verify(proxyService).isResultFound(CHAIN_TASK_ID); |
| 135 | + verify(iexecHubService, never()).isStatusTrueOnChain(CHAIN_TASK_ID, WALLET_ADDRESS, REVEALED); |
| 136 | + verify(iexecHubService, never()).getChainContribution(CHAIN_TASK_ID, WALLET_ADDRESS); |
53 | 137 | } |
54 | 138 |
|
55 | 139 | @Test |
56 | 140 | void isAbleToUploadWithIpfs() { |
57 | | - when(iexecHubService.isPublicResult(chainTaskId, 0)).thenReturn(true); |
58 | | - when(ipfsResultService.doesResultExist(chainTaskId)).thenReturn(false); |
59 | | - when(iexecHubService.isStatusTrueOnChain(any(), any(), any())).thenReturn(true); |
60 | | - |
61 | | - assertThat(proxyService.canUploadResult(chainTaskId, walletAddress)).isTrue(); |
| 141 | + when(iexecHubService.isTeeTask(CHAIN_TASK_ID)).thenReturn(false); |
| 142 | + when(ipfsResultService.doesResultExist(CHAIN_TASK_ID)).thenReturn(false); |
| 143 | + when(iexecHubService.isStatusTrueOnChain(CHAIN_TASK_ID, WALLET_ADDRESS, REVEALED)).thenReturn(true); |
| 144 | + when(iexecHubService.getChainContribution(CHAIN_TASK_ID, WALLET_ADDRESS)).thenReturn(Optional.of(CHAIN_CONTRIBUTION)); |
| 145 | + when(proxyService.getResultFolderPath(CHAIN_TASK_ID)).thenReturn(tmpFolder.getAbsolutePath()); |
| 146 | + |
| 147 | + assertThat(proxyService.canUploadResult(CHAIN_TASK_ID, WALLET_ADDRESS, RESULT_ZIP)).isTrue(); |
| 148 | + |
| 149 | + verify(iexecHubService).getChainContribution(CHAIN_TASK_ID, WALLET_ADDRESS); |
| 150 | + verify(iexecHubService).isTeeTask(CHAIN_TASK_ID); |
| 151 | + verify(proxyService).isResultFound(CHAIN_TASK_ID); |
| 152 | + verify(iexecHubService).isStatusTrueOnChain(CHAIN_TASK_ID, WALLET_ADDRESS, REVEALED); |
62 | 153 | } |
63 | 154 | } |
0 commit comments