1 package org.cacert.gigi;
3 import static org.junit.Assert.*;
5 import java.io.IOException;
6 import java.sql.SQLException;
7 import java.util.Arrays;
8 import java.util.Locale;
10 import org.cacert.gigi.dbObjects.Assurance;
11 import org.cacert.gigi.dbObjects.Assurance.AssuranceType;
12 import org.cacert.gigi.dbObjects.Domain;
13 import org.cacert.gigi.dbObjects.EmailAddress;
14 import org.cacert.gigi.dbObjects.Name;
15 import org.cacert.gigi.dbObjects.NamePart;
16 import org.cacert.gigi.dbObjects.NamePart.NamePartType;
17 import org.cacert.gigi.dbObjects.User;
18 import org.cacert.gigi.testUtils.BusinessTest;
19 import org.cacert.gigi.util.DayDate;
20 import org.cacert.gigi.util.Notary;
21 import org.hamcrest.CoreMatchers;
22 import org.junit.Test;
24 public class TestUser extends BusinessTest {
27 public void testStoreAndLoad() throws SQLException, GigiApiException {
28 long dob = System.currentTimeMillis();
29 dob -= dob % (1000 * 60 * 60 * 24);
30 User u = createUser("f", "l", createUniqueName() + "a@email.org", TEST_PASSWORD);
32 User u2 = User.getById(id);
33 assertEquals(u.getNames()[0], u2.getNames()[0]);
34 assertEquals(u.getDoB().toString(), u2.getDoB().toString());
35 assertEquals(u.getEmail(), u2.getEmail());
39 public void testWebStoreAndLoad() throws SQLException, GigiApiException {
40 int id = createVerifiedUser("aä", "b", createUniqueName() + "a@email.org", TEST_PASSWORD);
42 Name u = User.getById(id).getNames()[0];
44 assertThat(Arrays.asList(u.getParts()), CoreMatchers.hasItem(new NamePart(NamePartType.FIRST_NAME, "aä")));
45 assertThat(Arrays.asList(u.getParts()), CoreMatchers.hasItem(new NamePart(NamePartType.LAST_NAME, "b")));
46 assertEquals(2, u.getParts().length);
50 public void testAssurerUtilMethods() throws SQLException, GigiApiException {
51 int id = createAssuranceUser("aä", "b", createUniqueName() + "a@email.org", TEST_PASSWORD);
53 User u = User.getById(id);
54 assertTrue(u.canAssure());
55 int assurancePoints = u.getAssurancePoints();
56 int expPoints = u.getExperiencePoints();
57 assertEquals(100, assurancePoints);
58 assertEquals(User.EXPERIENCE_POINTS, expPoints);
59 assertTrue(u.hasPassedCATS());
60 assertEquals(10, u.getMaxAssurePoints());
64 public void testMatcherMethodsDomain() throws SQLException, GigiApiException, IOException {
65 String uq = createUniqueName();
66 int id = createVerifiedUser("aä", "b", uq + "a@email.org", TEST_PASSWORD);
68 User u = User.getById(id);
69 verify(new Domain(u, u, uq + "a-testdomain.org"));
70 verify(new Domain(u, u, uq + "b-testdomain.org"));
71 verify(new Domain(u, u, uq + "c-testdomain.org"));
72 assertEquals(3, u.getDomains().length);
73 assertTrue(u.isValidDomain(uq + "a-testdomain.org"));
74 assertTrue(u.isValidDomain(uq + "b-testdomain.org"));
75 assertTrue(u.isValidDomain(uq + "c-testdomain.org"));
76 assertTrue(u.isValidDomain("a." + uq + "a-testdomain.org"));
77 assertTrue(u.isValidDomain("*." + uq + "a-testdomain.org"));
78 assertFalse(u.isValidDomain("a" + uq + "a-testdomain.org"));
79 assertFalse(u.isValidDomain("b" + uq + "a-testdomain.org"));
83 public void testMatcherMethodsEmail() throws SQLException, GigiApiException, IOException {
84 String uq = createUniqueName();
85 int id = createVerifiedUser("aä", "b", uq + "a@email.org", TEST_PASSWORD);
87 User u = User.getById(id);
89 new EmailAddress(u, uq + "b@email.org", Locale.ENGLISH);
90 getMailReceiver().receive().verify();
91 new EmailAddress(u, uq + "c@email.org", Locale.ENGLISH);
92 getMailReceiver().receive();// no-verify
93 assertEquals(3, u.getEmails().length);
95 assertTrue(u.isValidEmail(uq + "a@email.org"));
96 assertTrue(u.isValidEmail(uq + "b@email.org"));
97 assertFalse(u.isValidEmail(uq + "b+6@email.org"));
98 assertFalse(u.isValidEmail(uq + "b*@email.org"));
99 assertFalse(u.isValidEmail(uq + "c@email.org"));
103 public void testMatcherMethodsName() throws SQLException, GigiApiException, IOException {
104 String uq = createUniqueName();
105 int id = createVerifiedUser("aä", "b", uq + "a@email.org", TEST_PASSWORD);
107 User u = User.getById(id);
109 User[] us = new User[5];
110 for (int i = 0; i < us.length; i++) {
111 us[i] = User.getById(createAssuranceUser("f", "l", createUniqueName() + "@email.com", TEST_PASSWORD));
112 Notary.assure(us[i], u, u.getPreferredName(), u.getDoB(), 10, "here", "2000-01-01", AssuranceType.FACE_TO_FACE);
115 assertTrue(u.isValidName("aä b"));
116 assertFalse(u.isValidName("aä c"));
117 assertFalse(u.isValidName("aä d b"));
122 public void testDoubleInsert() throws GigiApiException {
123 long d = System.currentTimeMillis();
124 d -= d % DayDate.MILLI_DAY;
125 User u = createUser("f", "l", createUniqueName() + "@example.org", TEST_PASSWORD);
126 Assurance[] ma = u.getMadeAssurances();
127 Assurance[] ma2 = u.getMadeAssurances();
128 Assurance[] ra = u.getReceivedAssurances();
129 Assurance[] ra2 = u.getReceivedAssurances();
130 assertEquals(0, u.getCertificates(false).length);
131 assertEquals(0, ma.length);
132 assertEquals(0, ma2.length);
133 assertEquals(0, ra.length);
134 assertEquals(0, ra2.length);
140 public void testGetByMail() throws GigiApiException {
141 String email = createUniqueName() + "a@email.org";
142 int id = createVerifiedUser("aä", "b", email, TEST_PASSWORD);
143 User emailUser = User.getByEmail(email);
144 User u = User.getById(id);
145 assertSame(u, emailUser);
149 public void testNoCats() throws GigiApiException {
150 String email = createUniqueName() + "a@email.org";
151 createVerifiedUser("aä", "b", email, TEST_PASSWORD);
152 User emailUser = User.getByEmail(email);
153 assertFalse(emailUser.hasPassedCATS());
157 public void testGetByMailFail() {
158 String email = createUniqueName() + "d@email.org";
159 User emailUser = User.getByEmail(email);
160 assertNull(emailUser);