add: implement to check if user has valid challenges for roles
[gigi.git] / tests / club / wpia / gigi / dbObjects / TestUser.java
1 package club.wpia.gigi.dbObjects;
2
3 import static org.junit.Assert.*;
4
5 import java.io.IOException;
6 import java.security.GeneralSecurityException;
7 import java.sql.Date;
8
9 import org.junit.Test;
10
11 import club.wpia.gigi.GigiApiException;
12 import club.wpia.gigi.dbObjects.CATS.CATSType;
13 import club.wpia.gigi.dbObjects.NamePart.NamePartType;
14 import club.wpia.gigi.testUtils.ClientBusinessTest;
15
16 public class TestUser extends ClientBusinessTest {
17
18     @Test
19     public void testGetInitials() throws GigiApiException {
20         User u0 = User.getById(createVerificationUser("Kurti", "Hansel", createUniqueName() + "@email.com", TEST_PASSWORD));
21
22         assertEquals("KH", u0.getInitials());
23
24         // single name as preferred name
25         Name sName = new Name(u0, new NamePart(NamePartType.SINGLE_NAME, "SingleName"));
26         u0.setPreferredName(sName);
27         assertEquals("S", u0.getInitials());
28
29         // second western style name as preferred name
30         NamePart[] np = {
31                 new NamePart(NamePartType.FIRST_NAME, "John"), new NamePart(NamePartType.FIRST_NAME, "Walker"), new NamePart(NamePartType.LAST_NAME, "Hansel")
32         };
33         sName = new Name(u0, np);
34         u0.setPreferredName(sName);
35         assertEquals("JWH", u0.getInitials());
36         // second western style name as preferred name
37
38         NamePart[] np1 = {
39                 new NamePart(NamePartType.FIRST_NAME, "Dieter"), new NamePart(NamePartType.LAST_NAME, "Hansel"), new NamePart(NamePartType.LAST_NAME, "von"), new NamePart(NamePartType.LAST_NAME, "Hof"), new NamePart(NamePartType.SUFFIX, "Meister")
40         };
41         sName = new Name(u0, np1);
42         u0.setPreferredName(sName);
43         assertEquals("DHVHM", u0.getInitials());
44
45         // western style name with dash as preferred name (Hans-Peter)
46         NamePart[] np2 = {
47                 new NamePart(NamePartType.FIRST_NAME, "Hans-Peter"), new NamePart(NamePartType.LAST_NAME, "Hansel")
48         };
49         sName = new Name(u0, np2);
50         u0.setPreferredName(sName);
51         assertEquals("HH", u0.getInitials());
52
53         // western style name with dash as separate entry as preferred name
54         // (Hans - Peter)
55         NamePart[] np3 = {
56                 new NamePart(NamePartType.FIRST_NAME, "Hans"), new NamePart(NamePartType.FIRST_NAME, "-"), new NamePart(NamePartType.FIRST_NAME, "Joachim"), new NamePart(NamePartType.LAST_NAME, "Hansel")
57         };
58         sName = new Name(u0, np3);
59         u0.setPreferredName(sName);
60         assertEquals("HJH", u0.getInitials());
61
62         // western style name with / as separate entry as preferred name
63         // (Hans / Peter)
64         NamePart[] np4 = {
65                 new NamePart(NamePartType.FIRST_NAME, "Hans"), new NamePart(NamePartType.FIRST_NAME, "/"), new NamePart(NamePartType.FIRST_NAME, "Peter"), new NamePart(NamePartType.LAST_NAME, "Hansel")
66         };
67         sName = new Name(u0, np4);
68         u0.setPreferredName(sName);
69         assertEquals("HPH", u0.getInitials());
70     }
71
72     @Test
73     public void testValidCATS() throws IOException, GeneralSecurityException {
74         Date min11month = new Date(System.currentTimeMillis() - 24L * 60 * 60 * 11 * 31 * 1000L);
75         Date min12month = new Date(System.currentTimeMillis() - 24L * 60 * 60 * 12 * 31 * 1000L);
76
77         assertFalse(u.hasValidRAChallenge());
78         CATS.enterResult(u, CATSType.AGENT_CHALLENGE, min12month, "en_US", "1");
79         assertFalse(u.hasValidRAChallenge());
80         CATS.enterResult(u, CATSType.AGENT_CHALLENGE, min11month, "en_US", "1");
81         assertTrue(u.hasValidRAChallenge());
82
83         assertFalse(u.hasValidSupportChallenge());
84         CATS.enterResult(u, CATSType.SUPPORT_DP_CHALLENGE_NAME, min12month, "en_US", "1");
85         assertFalse(u.hasValidSupportChallenge());
86         CATS.enterResult(u, CATSType.SUPPORT_DP_CHALLENGE_NAME, min11month, "en_US", "1");
87         assertTrue(u.hasValidSupportChallenge());
88
89         assertFalse(u.hasValidOrgAdminChallenge());
90         CATS.enterResult(u, CATSType.ORG_ADMIN_DP_CHALLENGE_NAME, min12month, "en_US", "1");
91         assertFalse(u.hasValidOrgAdminChallenge());
92         CATS.enterResult(u, CATSType.ORG_ADMIN_DP_CHALLENGE_NAME, min11month, "en_US", "1");
93         assertTrue(u.hasValidOrgAdminChallenge());
94
95         assertFalse(u.hasValidOrgAgentChallenge());
96         CATS.enterResult(u, CATSType.ORG_AGENT_CHALLENGE, min12month, "en_US", "1");
97         assertFalse(u.hasValidOrgAgentChallenge());
98         CATS.enterResult(u, CATSType.ORG_AGENT_CHALLENGE, min11month, "en_US", "1");
99         assertTrue(u.hasValidOrgAgentChallenge());
100
101         assertFalse(u.hasValidTTPAgentChallenge());
102         CATS.enterResult(u, CATSType.TTP_AGENT_CHALLENGE, min12month, "en_US", "1");
103         assertFalse(u.hasValidTTPAgentChallenge());
104         CATS.enterResult(u, CATSType.TTP_AGENT_CHALLENGE, min11month, "en_US", "1");
105         assertTrue(u.hasValidTTPAgentChallenge());
106     }
107
108 }