View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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  }