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.util.List;
27  import java.util.UUID;
28  import org.apache.syncope.common.lib.wa.U2FDevice;
29  import org.apache.syncope.common.rest.api.beans.U2FDeviceQuery;
30  import org.apache.syncope.fit.AbstractITCase;
31  import org.junit.jupiter.api.BeforeEach;
32  import org.junit.jupiter.api.Test;
33  
34  public class U2FRegistrationITCase extends AbstractITCase {
35  
36      private static U2FDevice createDeviceRegistration() {
37          return new U2FDevice.Builder()
38                  .issueDate(OffsetDateTime.now())
39                  .id(System.currentTimeMillis())
40                  .record("{\"keyHandle\":\"2_QYgDSPYcOgYBGBe8c9PVCunjigbD-3o5HcliXhu-Up_GKckYMxxVF6AgSPWubqfWy8WmJNDYQE"
41                          + "J1QKZe343Q\","
42                          + "\"publicKey\":\"BMj46cH-lHkRMovZhrusmm_fYL_sFausDPJIDZfx4pIiRqRNtasd4vU3yJyrTXXbdxyD36GZLx1"
43                          + "WKLHGmApv7Nk\""
44                          + ",\"counter\":-1,\"compromised\":false}")
45                  .build();
46      }
47  
48      @BeforeEach
49      public void setup() {
50          U2F_REGISTRATION_SERVICE.delete(new U2FDeviceQuery.Builder().build());
51      }
52  
53      @Test
54      public void create() {
55          assertDoesNotThrow(() -> U2F_REGISTRATION_SERVICE.create(
56                  UUID.randomUUID().toString(), createDeviceRegistration()));
57      }
58  
59      @Test
60      public void count() {
61          String owner = UUID.randomUUID().toString();
62          U2FDevice device = createDeviceRegistration();
63          U2F_REGISTRATION_SERVICE.create(owner, device);
64  
65          List<U2FDevice> devices = U2F_REGISTRATION_SERVICE.search(new U2FDeviceQuery.Builder().
66                  owner(owner).
67                  expirationDate(OffsetDateTime.now().minusDays(1)).
68                  build()).getResult();
69          assertEquals(1, devices.size());
70  
71          U2F_REGISTRATION_SERVICE.delete(new U2FDeviceQuery.Builder().id(device.getId()).build());
72  
73          devices = U2F_REGISTRATION_SERVICE.search(new U2FDeviceQuery.Builder().build()).getResult();
74          assertTrue(devices.isEmpty());
75      }
76  
77      @Test
78      public void delete() {
79          U2FDevice device = createDeviceRegistration();
80          String owner = UUID.randomUUID().toString();
81          U2F_REGISTRATION_SERVICE.create(owner, device);
82  
83          U2F_REGISTRATION_SERVICE.delete(new U2FDeviceQuery.Builder().owner(owner).build());
84          assertTrue(U2F_REGISTRATION_SERVICE.search(
85                  new U2FDeviceQuery.Builder().owner(owner).build()).getResult().isEmpty());
86  
87          OffsetDateTime date = OffsetDateTime.now().plusDays(1);
88  
89          U2F_REGISTRATION_SERVICE.delete(new U2FDeviceQuery.Builder().expirationDate(date).build());
90  
91          assertTrue(U2F_REGISTRATION_SERVICE.search(
92                  new U2FDeviceQuery.Builder().expirationDate(date).build()).getResult().isEmpty());
93      }
94  }