]> WPIA git - gigi.git/blob - tests/club/wpia/gigi/dbObjects/TestUser.java
Merge "upd: remove 'browser install'"
[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.hamcrest.CoreMatchers;
10 import org.junit.Test;
11
12 import club.wpia.gigi.GigiApiException;
13 import club.wpia.gigi.dbObjects.CATS.CATSType;
14 import club.wpia.gigi.dbObjects.NamePart.NamePartType;
15 import club.wpia.gigi.testUtils.ArrayContains;
16 import club.wpia.gigi.testUtils.ClientBusinessTest;
17
18 public class TestUser extends ClientBusinessTest {
19
20     @Test
21     public void testGetInitials() throws GigiApiException {
22         User u0 = User.getById(createVerificationUser("Kurti", "Hansel", createUniqueName() + "@email.com", TEST_PASSWORD));
23
24         assertEquals("KH", u0.getInitials());
25
26         // single name as preferred name
27         Name sName = new Name(u0, new NamePart(NamePartType.SINGLE_NAME, "SingleName"));
28         u0.setPreferredName(sName);
29         assertEquals("S", u0.getInitials());
30
31         // second western style name as preferred name
32         NamePart[] np = {
33                 new NamePart(NamePartType.FIRST_NAME, "John"), new NamePart(NamePartType.FIRST_NAME, "Walker"), new NamePart(NamePartType.LAST_NAME, "Hansel")
34         };
35         sName = new Name(u0, np);
36         u0.setPreferredName(sName);
37         assertEquals("JWH", u0.getInitials());
38         // second western style name as preferred name
39
40         NamePart[] np1 = {
41                 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")
42         };
43         sName = new Name(u0, np1);
44         u0.setPreferredName(sName);
45         assertEquals("DHVHM", u0.getInitials());
46
47         // western style name with dash as preferred name (Hans-Peter)
48         NamePart[] np2 = {
49                 new NamePart(NamePartType.FIRST_NAME, "Hans-Peter"), new NamePart(NamePartType.LAST_NAME, "Hansel")
50         };
51         sName = new Name(u0, np2);
52         u0.setPreferredName(sName);
53         assertEquals("HH", u0.getInitials());
54
55         // western style name with dash as separate entry as preferred name
56         // (Hans - Peter)
57         NamePart[] np3 = {
58                 new NamePart(NamePartType.FIRST_NAME, "Hans"), new NamePart(NamePartType.FIRST_NAME, "-"), new NamePart(NamePartType.FIRST_NAME, "Joachim"), new NamePart(NamePartType.LAST_NAME, "Hansel")
59         };
60         sName = new Name(u0, np3);
61         u0.setPreferredName(sName);
62         assertEquals("HJH", u0.getInitials());
63
64         // western style name with / as separate entry as preferred name
65         // (Hans / Peter)
66         NamePart[] np4 = {
67                 new NamePart(NamePartType.FIRST_NAME, "Hans"), new NamePart(NamePartType.FIRST_NAME, "/"), new NamePart(NamePartType.FIRST_NAME, "Peter"), new NamePart(NamePartType.LAST_NAME, "Hansel")
68         };
69         sName = new Name(u0, np4);
70         u0.setPreferredName(sName);
71         assertEquals("HPH", u0.getInitials());
72     }
73
74     @Test
75     public void testValidCATS() throws IOException, GeneralSecurityException {
76         Date min11month = new Date(System.currentTimeMillis() - 24L * 60 * 60 * 11 * 31 * 1000L);
77         Date min12month = new Date(System.currentTimeMillis() - 24L * 60 * 60 * 12 * 31 * 1000L);
78
79         assertFalse(u.hasValidRAChallenge());
80         CATS.enterResult(u, CATSType.AGENT_CHALLENGE, min12month, "en_US", "1");
81         assertFalse(u.hasValidRAChallenge());
82         CATS.enterResult(u, CATSType.AGENT_CHALLENGE, min11month, "en_US", "1");
83         assertTrue(u.hasValidRAChallenge());
84
85         assertFalse(u.hasValidSupportChallenge());
86         CATS.enterResult(u, CATSType.SUPPORT_DP_CHALLENGE_NAME, min12month, "en_US", "1");
87         assertFalse(u.hasValidSupportChallenge());
88         CATS.enterResult(u, CATSType.SUPPORT_DP_CHALLENGE_NAME, min11month, "en_US", "1");
89         assertTrue(u.hasValidSupportChallenge());
90
91         assertFalse(u.hasValidOrgAdminChallenge());
92         CATS.enterResult(u, CATSType.ORG_ADMIN_DP_CHALLENGE_NAME, min12month, "en_US", "1");
93         assertFalse(u.hasValidOrgAdminChallenge());
94         CATS.enterResult(u, CATSType.ORG_ADMIN_DP_CHALLENGE_NAME, min11month, "en_US", "1");
95         assertTrue(u.hasValidOrgAdminChallenge());
96
97         assertFalse(u.hasValidOrgAgentChallenge());
98         CATS.enterResult(u, CATSType.ORG_AGENT_CHALLENGE, min12month, "en_US", "1");
99         assertFalse(u.hasValidOrgAgentChallenge());
100         CATS.enterResult(u, CATSType.ORG_AGENT_CHALLENGE, min11month, "en_US", "1");
101         assertTrue(u.hasValidOrgAgentChallenge());
102
103         assertFalse(u.hasValidTTPAgentChallenge());
104         CATS.enterResult(u, CATSType.TTP_AGENT_CHALLENGE, min12month, "en_US", "1");
105         assertFalse(u.hasValidTTPAgentChallenge());
106         CATS.enterResult(u, CATSType.TTP_AGENT_CHALLENGE, min11month, "en_US", "1");
107         assertTrue(u.hasValidTTPAgentChallenge());
108     }
109
110     @Test
111     public void testHasContract() throws GigiApiException {
112         assertEquals(false, Contract.hasSignedContract(u, Contract.ContractType.RA_AGENT_CONTRACT));
113
114         Contract c = new Contract(u, Contract.ContractType.RA_AGENT_CONTRACT);
115         getMailReceiver().receive(u.getEmail());
116         assertEquals(true, Contract.hasSignedContract(u, Contract.ContractType.RA_AGENT_CONTRACT));
117
118         c.revokeContract();
119         getMailReceiver().receive(u.getEmail());
120         assertEquals(false, Contract.hasSignedContract(u, Contract.ContractType.RA_AGENT_CONTRACT));
121     }
122
123     @Test
124     public void testWriteUserLog() throws GigiApiException {
125         String type = "Log test";
126         u.writeUserLog(u, type);
127         String[] result = u.getAdminLog();
128         assertThat(result, ArrayContains.contains(CoreMatchers.equalTo(type)));
129
130         String type1 = "Log test1";
131         u.writeUserLog(u, type1);
132         result = u.getAdminLog();
133         assertThat(result, ArrayContains.contains(CoreMatchers.equalTo(type1)));
134         assertThat(result, ArrayContains.contains(CoreMatchers.equalTo(type)));
135     }
136
137     @Test
138     public void testValidVerification() throws GigiApiException {
139         User u0 = User.getById(createVerifiedUser("f", "l", createUniqueName() + "@email.com", TEST_PASSWORD));
140         assertFalse(u0.isValidNameVerification(u0.getPreferredName().toString()));
141
142         add100Points(u0.getId());
143         assertTrue(u0.isValidNameVerification(u0.getPreferredName().toString()));
144
145         setVerificationDateToPast(u0.getPreferredName());
146         assertFalse(u0.isValidNameVerification(u0.getPreferredName().toString()));
147
148         add100Points(u0.getId());
149         assertTrue(u0.isValidNameVerification(u0.getPreferredName().toString()));
150     }
151
152 }