1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.syncope.fit.core.wa;
20
21 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
22 import static org.junit.jupiter.api.Assertions.assertEquals;
23 import static org.junit.jupiter.api.Assertions.assertFalse;
24 import static org.junit.jupiter.api.Assertions.assertThrows;
25
26 import java.time.OffsetDateTime;
27 import java.util.List;
28 import java.util.UUID;
29 import org.apache.syncope.common.lib.SyncopeClientException;
30 import org.apache.syncope.common.lib.to.PagedResult;
31 import org.apache.syncope.common.lib.wa.GoogleMfaAuthAccount;
32 import org.apache.syncope.core.spring.security.SecureRandomUtils;
33 import org.apache.syncope.fit.AbstractITCase;
34 import org.junit.jupiter.api.BeforeEach;
35 import org.junit.jupiter.api.Test;
36
37 public class GoogleMfaAuthAccountITCase extends AbstractITCase {
38
39 private static GoogleMfaAuthAccount createGoogleMfaAuthAccount() {
40 return new GoogleMfaAuthAccount.Builder()
41 .registrationDate(OffsetDateTime.now())
42 .scratchCodes(List.of(1, 2, 3, 4, 5))
43 .secretKey(SecureRandomUtils.generateRandomUUID().toString())
44 .validationCode(123456)
45 .name(SecureRandomUtils.generateRandomUUID().toString())
46 .build();
47 }
48
49 @BeforeEach
50 public void setup() {
51 GOOGLE_MFA_AUTH_ACCOUNT_SERVICE.deleteAll();
52 }
53
54 @Test
55 public void create() {
56 GoogleMfaAuthAccount acct = createGoogleMfaAuthAccount();
57 assertDoesNotThrow(() -> GOOGLE_MFA_AUTH_ACCOUNT_SERVICE.create(UUID.randomUUID().toString(), acct));
58 }
59
60 @Test
61 public void count() {
62 String owner = UUID.randomUUID().toString();
63 GoogleMfaAuthAccount acct = createGoogleMfaAuthAccount();
64 GOOGLE_MFA_AUTH_ACCOUNT_SERVICE.create(owner, acct);
65 PagedResult<GoogleMfaAuthAccount> list = GOOGLE_MFA_AUTH_ACCOUNT_SERVICE.list();
66 assertFalse(list.getResult().isEmpty());
67 assertEquals(1, list.getTotalCount());
68
69 PagedResult<GoogleMfaAuthAccount> read = GOOGLE_MFA_AUTH_ACCOUNT_SERVICE.read(owner);
70 assertEquals(1, read.getTotalCount());
71 assertFalse(read.getResult().isEmpty());
72 }
73
74 @Test
75 public void delete() {
76 String owner = UUID.randomUUID().toString();
77 GoogleMfaAuthAccount acct = createGoogleMfaAuthAccount();
78 GOOGLE_MFA_AUTH_ACCOUNT_SERVICE.create(owner, acct);
79 GOOGLE_MFA_AUTH_ACCOUNT_SERVICE.delete(owner);
80 assertThrows(SyncopeClientException.class, () -> GOOGLE_MFA_AUTH_ACCOUNT_SERVICE.read(owner));
81 }
82
83 @Test
84 public void update() {
85 String owner = UUID.randomUUID().toString();
86 GoogleMfaAuthAccount acct = createGoogleMfaAuthAccount();
87 GOOGLE_MFA_AUTH_ACCOUNT_SERVICE.create(owner, acct);
88 acct = GOOGLE_MFA_AUTH_ACCOUNT_SERVICE.read(acct.getId());
89 acct.setSecretKey("NewSecret");
90 acct.setScratchCodes(List.of(9, 8, 7, 6, 5));
91 GOOGLE_MFA_AUTH_ACCOUNT_SERVICE.update(owner, acct);
92 assertEquals(1, GOOGLE_MFA_AUTH_ACCOUNT_SERVICE.list().getTotalCount());
93 acct = GOOGLE_MFA_AUTH_ACCOUNT_SERVICE.read(owner).getResult().get(0);
94 assertEquals(acct.getSecretKey(), acct.getSecretKey());
95 GOOGLE_MFA_AUTH_ACCOUNT_SERVICE.delete(owner);
96 }
97 }