add: handling of RA Agent Contract
[gigi.git] / tests / club / wpia / gigi / dbObjects / TestContract.java
1 package club.wpia.gigi.dbObjects;
2
3 import static org.junit.Assert.*;
4
5 import org.hamcrest.CoreMatchers;
6 import org.junit.Test;
7
8 import club.wpia.gigi.GigiApiException;
9 import club.wpia.gigi.testUtils.ClientBusinessTest;
10 import club.wpia.gigi.testUtils.TestEmailReceiver.TestMail;
11 import club.wpia.gigi.util.RandomToken;
12
13 public class TestContract extends ClientBusinessTest {
14
15     @Test
16     public void testContract() throws GigiApiException {
17
18         assertEquals(Contract.getContractByUser(u, Contract.ContractType.RA_AGENT_CONTRACT), null);
19
20         assertFalse(Contract.hasSignedContract(u, Contract.ContractType.RA_AGENT_CONTRACT));
21
22         Contract c = Contract.getRAAgentContractByUser(u);
23         assertEquals(c, null);
24
25         c = new Contract(u, Contract.ContractType.RA_AGENT_CONTRACT);
26         TestMail rc = getMailReceiver().receive(u.getEmail());
27
28         assertEquals(u.getEmail(), rc.getTo());
29         assertThat(rc.getMessage(), CoreMatchers.containsString("signed the RA Agent Contract"));
30         assertEquals(u.getPreferredName().toString(), c.getRAAgentName());
31         assertTrue(Contract.hasSignedContract(u, Contract.ContractType.RA_AGENT_CONTRACT));
32
33         Contract c1 = null;
34         try {
35             c1 = new Contract(u, Contract.ContractType.RA_AGENT_CONTRACT);
36             fail("double add contract must fail");
37         } catch (GigiApiException e) {
38             assertEquals("Contract exists", e.getMessage());
39         }
40
41         c1 = Contract.getContractByUser(u, Contract.ContractType.RA_AGENT_CONTRACT);
42         assertEquals(c.getID(), c1.getID());
43
44         c1 = Contract.getRAAgentContractByUser(u);
45         assertEquals(c.getID(), c1.getID());
46
47         c1 = Contract.getRAAgentContractByToken(c.getToken());
48         assertEquals(c.getID(), c1.getID());
49
50         c1 = Contract.getRAAgentContractByToken(RandomToken.generateToken(16));
51         assertEquals(c1, null);
52
53     }
54
55     @Test
56     public void testRevokeContract() throws GigiApiException {
57         Contract c = new Contract(u, Contract.ContractType.RA_AGENT_CONTRACT);
58
59         TestMail rc = getMailReceiver().receive(u.getEmail());
60         assertThat(rc.getMessage(), CoreMatchers.containsString("signed the RA Agent Contract"));
61
62         c.revokeContract();
63
64         rc = getMailReceiver().receive(u.getEmail());
65         assertEquals(u.getEmail(), rc.getTo());
66         assertThat(rc.getMessage(), CoreMatchers.containsString("revoked the RA Agent Contract"));
67         assertFalse(Contract.hasSignedContract(u, Contract.ContractType.RA_AGENT_CONTRACT));
68
69         Contract c1 = new Contract(u, Contract.ContractType.RA_AGENT_CONTRACT);
70         rc = getMailReceiver().receive(u.getEmail());
71
72         assertNotEquals(c.getID(), c1.getID());
73     }
74
75     @Test
76     public void testContractInt() throws GigiApiException {
77         Contract c = new Contract(u, Contract.ContractType.RA_AGENT_CONTRACT);
78
79         TestMail rc = getMailReceiver().receive(u.getEmail());
80         assertThat(rc.getMessage(), CoreMatchers.containsString("signed the RA Agent Contract"));
81
82         Contract c1 = Contract.getById(c.getID());
83
84         assertEquals(c.getID(), c1.getID());
85         assertEquals(c.getContractType(), c1.getContractType());
86
87         c1 = Contract.getById(0);
88         assertEquals(null, c1);
89     }
90
91 }