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  import static org.junit.jupiter.api.Assertions.assertTrue;
26  
27  import java.security.SecureRandom;
28  import java.time.LocalDateTime;
29  import java.util.UUID;
30  import org.apache.syncope.common.lib.SyncopeClientException;
31  import org.apache.syncope.common.lib.to.AuthProfileTO;
32  import org.apache.syncope.common.lib.to.PagedResult;
33  import org.apache.syncope.common.lib.wa.GoogleMfaAuthToken;
34  import org.apache.syncope.fit.AbstractITCase;
35  import org.junit.jupiter.api.BeforeEach;
36  import org.junit.jupiter.api.Test;
37  
38  public class GoogleMfaAuthTokenITCase extends AbstractITCase {
39  
40      private static final SecureRandom SECURE_RANDOM = new SecureRandom();
41  
42      private static GoogleMfaAuthToken createGoogleMfaAuthToken() {
43          int token = SECURE_RANDOM.ints(100_000, 999_999).findFirst().getAsInt();
44          return new GoogleMfaAuthToken.Builder().token(token).issueDate(LocalDateTime.now()).build();
45      }
46  
47      @BeforeEach
48      public void setup() {
49          GOOGLE_MFA_AUTH_TOKEN_SERVICE.delete((LocalDateTime) null);
50      }
51  
52      @Test
53      public void create() {
54          GoogleMfaAuthToken token = createGoogleMfaAuthToken();
55          assertDoesNotThrow(() -> GOOGLE_MFA_AUTH_TOKEN_SERVICE.store(UUID.randomUUID().toString(), token));
56      }
57  
58      @Test
59      public void count() {
60          String owner = UUID.randomUUID().toString();
61          GoogleMfaAuthToken token = createGoogleMfaAuthToken();
62          GOOGLE_MFA_AUTH_TOKEN_SERVICE.store(owner, token);
63          assertEquals(1, GOOGLE_MFA_AUTH_TOKEN_SERVICE.list().getTotalCount());
64          assertEquals(1, GOOGLE_MFA_AUTH_TOKEN_SERVICE.read(owner).getTotalCount());
65      }
66  
67      @Test
68      public void verifyProfile() {
69          String owner = UUID.randomUUID().toString();
70          GoogleMfaAuthToken token = createGoogleMfaAuthToken();
71          GOOGLE_MFA_AUTH_TOKEN_SERVICE.store(owner, token);
72          PagedResult<AuthProfileTO> results = AUTH_PROFILE_SERVICE.list(1, 100);
73          assertFalse(results.getResult().isEmpty());
74          AuthProfileTO profileTO = results.getResult().stream().
75                  filter(p -> owner.equals(p.getOwner())).findFirst().get();
76          assertEquals(profileTO, AUTH_PROFILE_SERVICE.read(profileTO.getKey()));
77          AUTH_PROFILE_SERVICE.delete(profileTO.getKey());
78          assertThrows(SyncopeClientException.class, () -> AUTH_PROFILE_SERVICE.read(profileTO.getKey()));
79      }
80  
81      @Test
82      public void deleteByToken() {
83          String owner = UUID.randomUUID().toString();
84          GoogleMfaAuthToken token = createGoogleMfaAuthToken();
85          GOOGLE_MFA_AUTH_TOKEN_SERVICE.store(owner, token);
86          GOOGLE_MFA_AUTH_TOKEN_SERVICE.delete(token.getOtp());
87          assertTrue(GOOGLE_MFA_AUTH_TOKEN_SERVICE.read(owner).getResult().isEmpty());
88      }
89  
90      @Test
91      public void delete() {
92          String owner = UUID.randomUUID().toString();
93          GoogleMfaAuthToken token = createGoogleMfaAuthToken();
94          GOOGLE_MFA_AUTH_TOKEN_SERVICE.store(owner, token);
95          GOOGLE_MFA_AUTH_TOKEN_SERVICE.delete(owner);
96          assertTrue(GOOGLE_MFA_AUTH_TOKEN_SERVICE.read(owner).getResult().isEmpty());
97      }
98  
99      @Test
100     public void deleteByOwnerAndToken() {
101         String owner = UUID.randomUUID().toString();
102         GoogleMfaAuthToken token = createGoogleMfaAuthToken();
103         GOOGLE_MFA_AUTH_TOKEN_SERVICE.store(owner, token);
104         GOOGLE_MFA_AUTH_TOKEN_SERVICE.delete(owner, token.getOtp());
105         assertTrue(GOOGLE_MFA_AUTH_TOKEN_SERVICE.read(owner).getResult().isEmpty());
106     }
107 
108     @Test
109     public void deleteByDate() {
110         String owner = UUID.randomUUID().toString();
111         createGoogleMfaAuthToken();
112         GOOGLE_MFA_AUTH_TOKEN_SERVICE.delete(LocalDateTime.now().minusDays(1));
113         assertTrue(GOOGLE_MFA_AUTH_TOKEN_SERVICE.read(owner).getResult().isEmpty());
114         assertEquals(0, GOOGLE_MFA_AUTH_TOKEN_SERVICE.read(owner).getTotalCount());
115     }
116 }