/
MfaOptionImplTest.java
316 lines (267 loc) · 16.2 KB
/
MfaOptionImplTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*******************************************************************************
* Copyright (c) 2021, 2022 Eurotech and/or its affiliates and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Eurotech - initial API and implementation
*******************************************************************************/
package org.eclipse.kapua.service.authentication.credential.mfa.shiro;
import org.eclipse.kapua.KapuaException;
import org.eclipse.kapua.commons.model.id.KapuaEid;
import org.eclipse.kapua.model.id.KapuaId;
import org.eclipse.kapua.qa.markers.junit.JUnitTests;
import org.eclipse.kapua.service.authentication.credential.mfa.MfaOption;
import org.eclipse.kapua.service.authentication.credential.mfa.ScratchCode;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.mockito.Mockito;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
@Category(JUnitTests.class)
public class MfaOptionImplTest {
KapuaId[] scopeIds;
KapuaEid[] userIds;
String[] mfaSecretKeys;
MfaOption mfaOption;
Date trustExpirationDate, modifiedOn;
@Before
public void initialize() {
scopeIds = new KapuaId[]{null, KapuaId.ONE};
userIds = new KapuaEid[]{null, new KapuaEid()};
mfaSecretKeys = new String[]{null, "", "!!mfaSecretKey-1", "#1(newMfa.,/SecretKey)9--99", "!$$ 1-2 key//", "mfa_key(....)<00>"};
mfaOption = Mockito.mock(MfaOption.class);
trustExpirationDate = new Date();
modifiedOn = new Date();
}
@Test
public void mfaOptionImplWithoutParametersTest() {
MfaOptionImpl mfaOptionImpl = new MfaOptionImpl();
Assert.assertNull("Null expected.", mfaOptionImpl.getScopeId());
Assert.assertNull("Null expected.", mfaOptionImpl.getUserId());
Assert.assertNull("Null expected.", mfaOptionImpl.getMfaSecretKey());
Assert.assertNull("Null expected.", mfaOptionImpl.getTrustKey());
Assert.assertNull("Null expected.", mfaOptionImpl.getTrustExpirationDate());
}
@Test
public void mfaOptionImplScopeIdParameterTest() {
for (KapuaId scopeId : scopeIds) {
MfaOptionImpl mfaOptionImpl = new MfaOptionImpl(scopeId);
Assert.assertEquals("Expected and actual values should be the same.", scopeId, mfaOptionImpl.getScopeId());
Assert.assertNull("Null expected.", mfaOptionImpl.getUserId());
Assert.assertNull("Null expected.", mfaOptionImpl.getMfaSecretKey());
Assert.assertNull("Null expected.", mfaOptionImpl.getTrustKey());
Assert.assertNull("Null expected.", mfaOptionImpl.getTrustExpirationDate());
}
}
@Test
public void mfaOptionImplScopeIdUserIdMfaSecretKeyParametersTest() {
for (KapuaId scopeId : scopeIds) {
for (KapuaId userId : userIds) {
for (String mfaSecretKey : mfaSecretKeys) {
MfaOptionImpl mfaOptionImpl = new MfaOptionImpl(scopeId, userId, mfaSecretKey);
Assert.assertEquals("Expected and actual values should be the same.", scopeId, mfaOptionImpl.getScopeId());
Assert.assertEquals("Expected and actual values should be the same.", userId, mfaOptionImpl.getUserId());
Assert.assertEquals("Expected and actual values should be the same.", mfaSecretKey, mfaOptionImpl.getMfaSecretKey());
Assert.assertNull("Null expected.", mfaOptionImpl.getTrustKey());
Assert.assertNull("Null expected.", mfaOptionImpl.getTrustExpirationDate());
}
}
}
}
@Test
public void mfaOptionImplMfaOptionParameterTest() throws KapuaException {
Mockito.when(mfaOption.getScopeId()).thenReturn(KapuaId.ONE);
Mockito.when(mfaOption.getUserId()).thenReturn(KapuaId.ONE);
Mockito.when(mfaOption.getMfaSecretKey()).thenReturn("mfa secret key");
Mockito.when(mfaOption.getTrustKey()).thenReturn("trust key");
Mockito.when(mfaOption.getTrustExpirationDate()).thenReturn(trustExpirationDate);
Mockito.when(mfaOption.getModifiedOn()).thenReturn(modifiedOn);
Mockito.when(mfaOption.getModifiedBy()).thenReturn(KapuaId.ONE);
Mockito.when(mfaOption.getOptlock()).thenReturn(10);
MfaOptionImpl mfaOptionImpl = new MfaOptionImpl(mfaOption);
Assert.assertEquals("Expected and actual values should be the same.", KapuaId.ONE, mfaOptionImpl.getScopeId());
Assert.assertEquals("Expected and actual values should be the same.", KapuaId.ONE, mfaOptionImpl.getUserId());
Assert.assertEquals("Expected and actual values should be the same.", "mfa secret key", mfaOptionImpl.getMfaSecretKey());
Assert.assertEquals("Expected and actual values should be the same.", "trust key", mfaOptionImpl.getTrustKey());
Assert.assertEquals("Expected and actual values should be the same.", trustExpirationDate, mfaOptionImpl.getTrustExpirationDate());
Assert.assertEquals("Expected and actual values should be the same.", modifiedOn, mfaOptionImpl.getModifiedOn());
Assert.assertEquals("Expected and actual values should be the same.", KapuaId.ONE, mfaOptionImpl.getModifiedBy());
Assert.assertEquals("Expected and actual values should be the same.", 10, mfaOptionImpl.getOptlock());
}
@Test(expected = NullPointerException.class)
public void mfaOptionImplNullMfaOptionParameterTest() throws KapuaException {
new MfaOptionImpl((MfaOption) null);
}
@Test
public void setAndGetUserIdTest() throws KapuaException {
KapuaId[] newUserIds = {null, KapuaId.ANY};
MfaOptionImpl mfaOptionImpl1 = new MfaOptionImpl();
for (KapuaId newUserId : newUserIds) {
mfaOptionImpl1.setUserId(newUserId);
Assert.assertEquals("Expected and actual values should be the same.", newUserId, mfaOptionImpl1.getUserId());
}
MfaOptionImpl mfaOptionImpl2 = new MfaOptionImpl(KapuaId.ONE);
for (KapuaId newUserId : newUserIds) {
mfaOptionImpl2.setUserId(newUserId);
Assert.assertEquals("Expected and actual values should be the same.", newUserId, mfaOptionImpl2.getUserId());
}
MfaOptionImpl mfaOptionImpl3 = new MfaOptionImpl(KapuaId.ONE, new KapuaEid(), "mfa secret key");
for (KapuaId newUserId : newUserIds) {
mfaOptionImpl3.setUserId(newUserId);
Assert.assertEquals("Expected and actual values should be the same.", newUserId, mfaOptionImpl3.getUserId());
}
MfaOptionImpl mfaOptionImpl4 = new MfaOptionImpl(mfaOption);
for (KapuaId newUserId : newUserIds) {
mfaOptionImpl4.setUserId(newUserId);
Assert.assertEquals("Expected and actual values should be the same.", newUserId, mfaOptionImpl4.getUserId());
}
}
@Test
public void setAndGetMfaSecretKeyTest() throws KapuaException {
String[] newSecretKeys = {null, "new secret key"};
MfaOptionImpl mfaOptionImpl1 = new MfaOptionImpl();
for (String newSecretKey : newSecretKeys) {
mfaOptionImpl1.setMfaSecretKey(newSecretKey);
Assert.assertEquals("Expected and actual values should be the same.", newSecretKey, mfaOptionImpl1.getMfaSecretKey());
}
MfaOptionImpl mfaOptionImpl2 = new MfaOptionImpl(KapuaId.ONE);
for (String newSecretKey : newSecretKeys) {
mfaOptionImpl2.setMfaSecretKey(newSecretKey);
Assert.assertEquals("Expected and actual values should be the same.", newSecretKey, mfaOptionImpl2.getMfaSecretKey());
}
MfaOptionImpl mfaOptionImpl3 = new MfaOptionImpl(KapuaId.ONE, new KapuaEid(), "mfa secret key");
for (String newSecretKey : newSecretKeys) {
mfaOptionImpl3.setMfaSecretKey(newSecretKey);
Assert.assertEquals("Expected and actual values should be the same.", newSecretKey, mfaOptionImpl3.getMfaSecretKey());
}
MfaOptionImpl mfaOptionImpl4 = new MfaOptionImpl(mfaOption);
for (String newSecretKey : newSecretKeys) {
mfaOptionImpl4.setMfaSecretKey(newSecretKey);
Assert.assertEquals("Expected and actual values should be the same.", newSecretKey, mfaOptionImpl4.getMfaSecretKey());
}
}
@Test
public void setAndGetTrustKeyTest() throws KapuaException {
String[] newTrustKeys = {null, "new trust key"};
MfaOptionImpl mfaOptionImpl1 = new MfaOptionImpl();
for (String newTrustKey : newTrustKeys) {
mfaOptionImpl1.setTrustKey(newTrustKey);
Assert.assertEquals("Expected and actual values should be the same.", newTrustKey, mfaOptionImpl1.getTrustKey());
}
MfaOptionImpl mfaOptionImpl2 = new MfaOptionImpl(KapuaId.ONE);
for (String newTrustKey : newTrustKeys) {
mfaOptionImpl2.setTrustKey(newTrustKey);
Assert.assertEquals("Expected and actual values should be the same.", newTrustKey, mfaOptionImpl2.getTrustKey());
}
MfaOptionImpl mfaOptionImpl3 = new MfaOptionImpl(KapuaId.ONE, new KapuaEid(), "mfa secret key");
for (String newTrustKey : newTrustKeys) {
mfaOptionImpl3.setTrustKey(newTrustKey);
Assert.assertEquals("Expected and actual values should be the same.", newTrustKey, mfaOptionImpl3.getTrustKey());
}
MfaOptionImpl mfaOptionImpl4 = new MfaOptionImpl(mfaOption);
for (String newTrustKey : newTrustKeys) {
mfaOptionImpl4.setTrustKey(newTrustKey);
Assert.assertEquals("Expected and actual values should be the same.", newTrustKey, mfaOptionImpl4.getTrustKey());
}
}
@Test
public void setAndGetTrustExpirationDateTest() throws KapuaException {
Date[] newTrustExpirationDates = {null, new Date()};
MfaOptionImpl mfaOptionImpl1 = new MfaOptionImpl();
for (Date newTrustExpirationDate : newTrustExpirationDates) {
mfaOptionImpl1.setTrustExpirationDate(newTrustExpirationDate);
Assert.assertEquals("Expected and actual values should be the same.", newTrustExpirationDate, mfaOptionImpl1.getTrustExpirationDate());
}
MfaOptionImpl mfaOptionImpl2 = new MfaOptionImpl(KapuaId.ONE);
for (Date newTrustExpirationDate : newTrustExpirationDates) {
mfaOptionImpl2.setTrustExpirationDate(newTrustExpirationDate);
Assert.assertEquals("Expected and actual values should be the same.", newTrustExpirationDate, mfaOptionImpl2.getTrustExpirationDate());
}
MfaOptionImpl mfaOptionImpl3 = new MfaOptionImpl(KapuaId.ONE, new KapuaEid(), "mfa secret key");
for (Date newTrustExpirationDate : newTrustExpirationDates) {
mfaOptionImpl3.setTrustExpirationDate(newTrustExpirationDate);
Assert.assertEquals("Expected and actual values should be the same.", newTrustExpirationDate, mfaOptionImpl3.getTrustExpirationDate());
}
MfaOptionImpl mfaOptionImpl4 = new MfaOptionImpl(mfaOption);
for (Date newTrustExpirationDate : newTrustExpirationDates) {
mfaOptionImpl4.setTrustExpirationDate(newTrustExpirationDate);
Assert.assertEquals("Expected and actual values should be the same.", newTrustExpirationDate, mfaOptionImpl4.getTrustExpirationDate());
}
}
@Test
public void setAndGetQRCodeImageTest() throws KapuaException {
String[] newQrCodeImages = {null, "new qr Code Image"};
MfaOptionImpl mfaOptionImpl1 = new MfaOptionImpl();
for (String newQrCodeImage : newQrCodeImages) {
mfaOptionImpl1.setQRCodeImage(newQrCodeImage);
Assert.assertEquals("Expected and actual values should be the same.", newQrCodeImage, mfaOptionImpl1.getQRCodeImage());
}
MfaOptionImpl mfaOptionImpl2 = new MfaOptionImpl(KapuaId.ONE);
for (String newQrCodeImage : newQrCodeImages) {
mfaOptionImpl2.setQRCodeImage(newQrCodeImage);
Assert.assertEquals("Expected and actual values should be the same.", newQrCodeImage, mfaOptionImpl2.getQRCodeImage());
}
MfaOptionImpl mfaOptionImpl3 = new MfaOptionImpl(KapuaId.ONE, new KapuaEid(), "mfa secret key");
for (String newQrCodeImage : newQrCodeImages) {
mfaOptionImpl3.setQRCodeImage(newQrCodeImage);
Assert.assertEquals("Expected and actual values should be the same.", newQrCodeImage, mfaOptionImpl3.getQRCodeImage());
}
MfaOptionImpl mfaOptionImpl4 = new MfaOptionImpl(mfaOption);
for (String newQrCodeImage : newQrCodeImages) {
mfaOptionImpl4.setQRCodeImage(newQrCodeImage);
Assert.assertEquals("Expected and actual values should be the same.", newQrCodeImage, mfaOptionImpl4.getQRCodeImage());
}
}
@Test
public void setAndGetScratchCodesEmptyListTest() throws KapuaException {
List<ScratchCode> scratchCodeList = new LinkedList<>();
MfaOptionImpl mfaOptionImpl1 = new MfaOptionImpl();
mfaOptionImpl1.setScratchCodes(scratchCodeList);
Assert.assertEquals("Expected and actual values should be the same.", scratchCodeList, mfaOptionImpl1.getScratchCodes());
Assert.assertTrue("True expected.", mfaOptionImpl1.getScratchCodes().isEmpty());
MfaOptionImpl mfaOptionImpl2 = new MfaOptionImpl(KapuaId.ONE);
mfaOptionImpl2.setScratchCodes(scratchCodeList);
Assert.assertEquals("Expected and actual values should be the same.", scratchCodeList, mfaOptionImpl2.getScratchCodes());
Assert.assertTrue("True expected.", mfaOptionImpl2.getScratchCodes().isEmpty());
MfaOptionImpl mfaOptionImpl3 = new MfaOptionImpl(KapuaId.ONE, new KapuaEid(), "mfa secret key");
mfaOptionImpl3.setScratchCodes(scratchCodeList);
Assert.assertEquals("Expected and actual values should be the same.", scratchCodeList, mfaOptionImpl3.getScratchCodes());
Assert.assertTrue("True expected.", mfaOptionImpl3.getScratchCodes().isEmpty());
MfaOptionImpl mfaOptionImpl4 = new MfaOptionImpl(mfaOption);
mfaOptionImpl4.setScratchCodes(scratchCodeList);
Assert.assertEquals("Expected and actual values should be the same.", scratchCodeList, mfaOptionImpl4.getScratchCodes());
Assert.assertTrue("True expected.", mfaOptionImpl4.getScratchCodes().isEmpty());
}
@Test
public void setAndGetScratchCodesTest() throws KapuaException {
List<ScratchCode> scratchCodeList = new LinkedList<>();
ScratchCode scratchCode1 = Mockito.mock(ScratchCode.class);
ScratchCode scratchCode2 = Mockito.mock(ScratchCode.class);
scratchCodeList.add(scratchCode1);
scratchCodeList.add(scratchCode2);
MfaOptionImpl mfaOptionImpl1 = new MfaOptionImpl();
mfaOptionImpl1.setScratchCodes(scratchCodeList);
Assert.assertEquals("Expected and actual values should be the same.", scratchCodeList, mfaOptionImpl1.getScratchCodes());
Assert.assertFalse("False expected.", mfaOptionImpl1.getScratchCodes().isEmpty());
MfaOptionImpl mfaOptionImpl2 = new MfaOptionImpl(KapuaId.ONE);
mfaOptionImpl2.setScratchCodes(scratchCodeList);
Assert.assertEquals("Expected and actual values should be the same.", scratchCodeList, mfaOptionImpl2.getScratchCodes());
Assert.assertFalse("False expected.", mfaOptionImpl2.getScratchCodes().isEmpty());
MfaOptionImpl mfaOptionImpl3 = new MfaOptionImpl(KapuaId.ONE, new KapuaEid(), "mfa secret key");
mfaOptionImpl3.setScratchCodes(scratchCodeList);
Assert.assertEquals("Expected and actual values should be the same.", scratchCodeList, mfaOptionImpl3.getScratchCodes());
Assert.assertFalse("False expected.", mfaOptionImpl3.getScratchCodes().isEmpty());
MfaOptionImpl mfaOptionImpl4 = new MfaOptionImpl(mfaOption);
mfaOptionImpl4.setScratchCodes(scratchCodeList);
Assert.assertEquals("Expected and actual values should be the same.", scratchCodeList, mfaOptionImpl4.getScratchCodes());
Assert.assertFalse("False expected.", mfaOptionImpl4.getScratchCodes().isEmpty());
}
}