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.assertTrue;
24  
25  import java.time.OffsetDateTime;
26  import java.time.ZonedDateTime;
27  import java.util.List;
28  import java.util.UUID;
29  import org.apache.syncope.common.lib.wa.MfaTrustedDevice;
30  import org.apache.syncope.common.rest.api.beans.MfaTrustedDeviceQuery;
31  import org.apache.syncope.fit.AbstractITCase;
32  import org.junit.jupiter.api.BeforeEach;
33  import org.junit.jupiter.api.Test;
34  
35  public class MfaTrustStorageTCase extends AbstractITCase {
36  
37      private static MfaTrustedDevice createDeviceRegistration() {
38          MfaTrustedDevice device = new MfaTrustedDevice();
39          device.setId(System.currentTimeMillis());
40          device.setDeviceFingerprint(UUID.randomUUID().toString());
41          device.setName(UUID.randomUUID().toString());
42          device.setRecordKey(UUID.randomUUID().toString());
43          device.setRecordDate(ZonedDateTime.now());
44          device.setExpirationDate(ZonedDateTime.now().plusDays(30));
45          return device;
46      }
47  
48      @BeforeEach
49      public void setup() {
50          MFA_TRUST_STORAGE_SERVICE.delete(new MfaTrustedDeviceQuery.Builder().build());
51      }
52  
53      @Test
54      public void create() {
55          assertDoesNotThrow(() -> MFA_TRUST_STORAGE_SERVICE.create(
56                  UUID.randomUUID().toString(), createDeviceRegistration()));
57      }
58  
59      @Test
60      public void count() {
61          String owner = UUID.randomUUID().toString();
62          MfaTrustedDevice device = createDeviceRegistration();
63          MFA_TRUST_STORAGE_SERVICE.create(owner, device);
64  
65          List<MfaTrustedDevice> devices = MFA_TRUST_STORAGE_SERVICE.search(new MfaTrustedDeviceQuery.Builder().
66                  principal(owner).build()).getResult();
67          assertEquals(1, devices.size());
68  
69          MFA_TRUST_STORAGE_SERVICE.delete(new MfaTrustedDeviceQuery.Builder().recordKey(device.getRecordKey()).build());
70  
71          devices = MFA_TRUST_STORAGE_SERVICE.search(new MfaTrustedDeviceQuery.Builder().build()).getResult();
72          assertTrue(devices.isEmpty());
73      }
74  
75      @Test
76      public void delete() {
77          MfaTrustedDevice device = createDeviceRegistration();
78          String owner = UUID.randomUUID().toString();
79          MFA_TRUST_STORAGE_SERVICE.create(owner, device);
80  
81          MFA_TRUST_STORAGE_SERVICE.delete(new MfaTrustedDeviceQuery.Builder().recordKey(device.getRecordKey()).build());
82          assertTrue(MFA_TRUST_STORAGE_SERVICE.search(
83                  new MfaTrustedDeviceQuery.Builder().id(device.getId()).build()).getResult().isEmpty());
84  
85          OffsetDateTime date = OffsetDateTime.now().plusDays(1);
86  
87          MFA_TRUST_STORAGE_SERVICE.delete(new MfaTrustedDeviceQuery.Builder().expirationDate(date).build());
88  
89          assertTrue(MFA_TRUST_STORAGE_SERVICE.search(
90                  new MfaTrustedDeviceQuery.Builder().id(device.getId()).build()).getResult().isEmpty());
91      }
92  }