Skip to content

Commit 5a397e4

Browse files
authored
Stop using TestUtils in AuthorizationServiceTests (#300)
1 parent d05cb46 commit 5a397e4

File tree

2 files changed

+44
-15
lines changed

2 files changed

+44
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ All notable changes to this project will be documented in this file.
2020
- Rename `blockchain` package to `chain` and `BlockchainConfig` class to `ChainConfig`. (#294)
2121
- Fix several SonarQube Cloud issues. (#295)
2222
- Harmonize YML internal variables to proper case. (#299)
23+
- Stop using `TestUtils` in `AuthorizationServiceTests.java`. (#300)
2324

2425
### Breaking API changes
2526

src/test/java/com/iexec/sms/authorization/AuthorizationServiceTests.java

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@
1818

1919
import com.iexec.commons.poco.chain.ChainDeal;
2020
import com.iexec.commons.poco.chain.ChainTask;
21+
import com.iexec.commons.poco.chain.ChainTaskStatus;
2122
import com.iexec.commons.poco.chain.WorkerpoolAuthorization;
2223
import com.iexec.commons.poco.security.Signature;
2324
import com.iexec.commons.poco.tee.TeeUtils;
2425
import com.iexec.commons.poco.utils.BytesUtils;
25-
import com.iexec.commons.poco.utils.TestUtils;
26+
import com.iexec.commons.poco.utils.HashUtils;
27+
import com.iexec.commons.poco.utils.SignatureUtils;
2628
import com.iexec.sms.chain.IexecHubService;
2729
import org.junit.jupiter.api.Assertions;
2830
import org.junit.jupiter.api.Test;
@@ -42,6 +44,13 @@
4244
@ExtendWith(MockitoExtension.class)
4345
class AuthorizationServiceTests {
4446

47+
private static final String ENCLAVE_ADDRESS = "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee";
48+
private static final String DEAL_ID = "0x2222222222222222222222222222222222222222222222222222222222222222";
49+
private static final String WORKER_ADDRESS = "0x87ae2b87b5db23830572988fb1f51242fbc471ce";
50+
private static final String CHAIN_TASK_ID = "0x1111111111111111111111111111111111111111111111111111111111111111";
51+
private static final String POOL_PRIVATE = "0xe2a973b083fae8043543f15313955aecee9de809a318656c1cfb22d3a6d52de1";
52+
private static final String POOL_WRONG_SIGNATURE = "0xf869daaca2407b7eabd27c3c4c5a3f3565172ca7211ac1d8bfacea2beb511a4029446a07cccc0884c2193b269dfb341461db8c680a8898bb53862d6e48340c2e1b";
53+
4554
@Mock
4655
IexecHubService iexecHubService;
4756

@@ -52,8 +61,8 @@ class AuthorizationServiceTests {
5261
@Test
5362
void shouldBeAuthorizedOnExecutionOfTeeTaskWithDetails() {
5463
final ChainDeal chainDeal = getChainDeal();
55-
final ChainTask chainTask = TestUtils.getChainTask(ACTIVE);
56-
final WorkerpoolAuthorization auth = TestUtils.getTeeWorkerpoolAuth();
64+
final ChainTask chainTask = getChainTask(ACTIVE);
65+
final WorkerpoolAuthorization auth = getTeeWorkerpoolAuth();
5766
when(iexecHubService.getChainTask(auth.getChainTaskId())).thenReturn(Optional.of(chainTask));
5867
when(iexecHubService.getChainDeal(chainTask.getDealid())).thenReturn(Optional.of(chainDeal));
5968

@@ -81,8 +90,8 @@ void shouldNotBeAuthorizedOnExecutionOfTeeTaskWhenTaskTypeNotTeeOnchainWithDetai
8190
.poolOwner("0xc911f9345717ba7c8ec862ce002af3e058df84e4")
8291
.tag(BytesUtils.EMPTY_HEX_STRING_32)
8392
.build();
84-
final ChainTask chainTask = TestUtils.getChainTask(ACTIVE);
85-
final WorkerpoolAuthorization auth = TestUtils.getTeeWorkerpoolAuth();
93+
final ChainTask chainTask = getChainTask(ACTIVE);
94+
final WorkerpoolAuthorization auth = getTeeWorkerpoolAuth();
8695
when(iexecHubService.getChainTask(auth.getChainTaskId())).thenReturn(Optional.of(chainTask));
8796
when(iexecHubService.getChainDeal(chainTask.getDealid())).thenReturn(Optional.of(chainDeal));
8897

@@ -93,7 +102,7 @@ void shouldNotBeAuthorizedOnExecutionOfTeeTaskWhenTaskTypeNotTeeOnchainWithDetai
93102

94103
@Test
95104
void shouldNotBeAuthorizedOnExecutionOfTeeTaskWhenGetTaskFailedWithDetails() {
96-
final WorkerpoolAuthorization auth = TestUtils.getTeeWorkerpoolAuth();
105+
final WorkerpoolAuthorization auth = getTeeWorkerpoolAuth();
97106
when(iexecHubService.getChainTask(auth.getChainTaskId())).thenReturn(Optional.empty());
98107

99108
final Optional<AuthorizationError> isAuth = authorizationService.isAuthorizedOnExecutionWithDetailedIssue(auth);
@@ -103,8 +112,8 @@ void shouldNotBeAuthorizedOnExecutionOfTeeTaskWhenGetTaskFailedWithDetails() {
103112

104113
@Test
105114
void shouldNotBeAuthorizedOnExecutionOfTeeTaskWhenTaskNotActiveWithDetails() {
106-
final WorkerpoolAuthorization auth = TestUtils.getTeeWorkerpoolAuth();
107-
final ChainTask chainTask = TestUtils.getChainTask(UNSET);
115+
final WorkerpoolAuthorization auth = getTeeWorkerpoolAuth();
116+
final ChainTask chainTask = getChainTask(UNSET);
108117
when(iexecHubService.getChainTask(auth.getChainTaskId())).thenReturn(Optional.of(chainTask));
109118

110119
final Optional<AuthorizationError> isAuth = authorizationService.isAuthorizedOnExecutionWithDetailedIssue(auth);
@@ -114,9 +123,8 @@ void shouldNotBeAuthorizedOnExecutionOfTeeTaskWhenTaskNotActiveWithDetails() {
114123

115124
@Test
116125
void shouldNotBeAuthorizedOnExecutionOfTeeTaskWhenGetDealFailedWithDetails() {
117-
final ChainTask chainTask = TestUtils.getChainTask(ACTIVE);
118-
final WorkerpoolAuthorization auth = TestUtils.getTeeWorkerpoolAuth();
119-
auth.setSignature(new Signature(TestUtils.POOL_WRONG_SIGNATURE));
126+
final ChainTask chainTask = getChainTask(ACTIVE);
127+
final WorkerpoolAuthorization auth = getTeeWorkerpoolAuth();
120128

121129
when(iexecHubService.getChainTask(auth.getChainTaskId())).thenReturn(Optional.of(chainTask));
122130
when(iexecHubService.getChainDeal(chainTask.getDealid())).thenReturn(Optional.empty());
@@ -129,9 +137,14 @@ void shouldNotBeAuthorizedOnExecutionOfTeeTaskWhenGetDealFailedWithDetails() {
129137
@Test
130138
void shouldNotBeAuthorizedOnExecutionOfTeeTaskWhenPoolSignatureIsNotValidWithDetails() {
131139
final ChainDeal chainDeal = getChainDeal();
132-
final ChainTask chainTask = TestUtils.getChainTask(ACTIVE);
133-
final WorkerpoolAuthorization auth = TestUtils.getTeeWorkerpoolAuth();
134-
auth.setSignature(new Signature(TestUtils.POOL_WRONG_SIGNATURE));
140+
final ChainTask chainTask = getChainTask(ACTIVE);
141+
final Signature signature = new Signature(POOL_WRONG_SIGNATURE);
142+
final WorkerpoolAuthorization auth = WorkerpoolAuthorization.builder()
143+
.chainTaskId(CHAIN_TASK_ID)
144+
.workerWallet(WORKER_ADDRESS)
145+
.enclaveChallenge(ENCLAVE_ADDRESS)
146+
.signature(signature)
147+
.build();
135148

136149
when(iexecHubService.getChainTask(auth.getChainTaskId())).thenReturn(Optional.of(chainTask));
137150
when(iexecHubService.getChainDeal(chainTask.getDealid())).thenReturn(Optional.of(chainDeal));
@@ -163,11 +176,26 @@ void getChallengeForSetWeb3Secret() {
163176
// endregion
164177

165178
// region utils
166-
ChainDeal getChainDeal() {
179+
private ChainDeal getChainDeal() {
167180
return ChainDeal.builder()
168181
.poolOwner("0xc911f9345717ba7c8ec862ce002af3e058df84e4")
169182
.tag(TeeUtils.TEE_SCONE_ONLY_TAG)
170183
.build();
171184
}
185+
186+
private ChainTask getChainTask(ChainTaskStatus status) {
187+
return ChainTask.builder().dealid(DEAL_ID).status(status).build();
188+
}
189+
190+
private WorkerpoolAuthorization getTeeWorkerpoolAuth() {
191+
final String hash = HashUtils.concatenateAndHash(WORKER_ADDRESS, CHAIN_TASK_ID, ENCLAVE_ADDRESS);
192+
final Signature signature = SignatureUtils.signMessageHashAndGetSignature(hash, POOL_PRIVATE);
193+
return WorkerpoolAuthorization.builder()
194+
.chainTaskId(CHAIN_TASK_ID)
195+
.workerWallet(WORKER_ADDRESS)
196+
.enclaveChallenge(ENCLAVE_ADDRESS)
197+
.signature(signature)
198+
.build();
199+
}
172200
// endregion
173201
}

0 commit comments

Comments
 (0)