chg: adjust ticket number handling according to current number scheme
[gigi.git] / tests / club / wpia / gigi / pages / admin / TestSEAdminTicketSetting.java
1 package club.wpia.gigi.pages.admin;
2
3 import static org.hamcrest.CoreMatchers.*;
4 import static org.hamcrest.MatcherAssert.assertThat;
5 import static org.junit.Assert.*;
6
7 import java.io.IOException;
8 import java.io.UnsupportedEncodingException;
9 import java.net.MalformedURLException;
10 import java.util.Random;
11
12 import org.junit.Test;
13
14 import club.wpia.gigi.GigiApiException;
15 import club.wpia.gigi.dbObjects.Group;
16 import club.wpia.gigi.pages.admin.support.FindUserByDomainPage;
17 import club.wpia.gigi.pages.admin.support.FindUserByEmailPage;
18 import club.wpia.gigi.pages.admin.support.SupportEnterTicketForm;
19 import club.wpia.gigi.pages.admin.support.SupportEnterTicketPage;
20 import club.wpia.gigi.testUtils.ClientTest;
21 import club.wpia.gigi.testUtils.IOUtils;
22
23 public class TestSEAdminTicketSetting extends ClientTest {
24
25     public TestSEAdminTicketSetting() throws IOException, GigiApiException {
26         grant(u, Group.SUPPORTER);
27         cookie = login(email, TEST_PASSWORD);
28     }
29
30     @Test
31     public void testFulltextMailSearch() throws MalformedURLException, UnsupportedEncodingException, IOException {
32         assertEquals(403, get(FindUserByEmailPage.PATH).getResponseCode());
33         assertEquals(302, post(cookie, SupportEnterTicketPage.PATH, "ticketno=a20140808.8&setTicket=action", 0).getResponseCode());
34         assertEquals(200, get(FindUserByEmailPage.PATH).getResponseCode());
35         assertEquals(200, get(FindUserByDomainPage.PATH).getResponseCode());
36         assertEquals(302, post(cookie, SupportEnterTicketPage.PATH, "ticketno=a20140808.8&deleteTicket=action", 0).getResponseCode());
37         assertEquals(403, get(FindUserByEmailPage.PATH).getResponseCode());
38     }
39
40     @Test
41     public void testSetTicketNumberCharacter() throws MalformedURLException, UnsupportedEncodingException, IOException {
42         String ticket;
43         String alphabet = "abcdefghijklmnopqrstuvwxyz";
44
45         // test allowed character
46         for (char ch : SupportEnterTicketForm.TICKET_PREFIX.toCharArray()) {
47             ticket = ch + "20171212.1";
48             assertEquals(302, post(cookie, SupportEnterTicketPage.PATH, "ticketno=" + ticket + "&setTicket=action", 0).getResponseCode());
49             ticket = Character.toUpperCase(ch) + "20171212.1";
50             assertEquals(302, post(cookie, SupportEnterTicketPage.PATH, "ticketno=" + ticket + "&setTicket=action", 0).getResponseCode());
51             alphabet = alphabet.replaceAll(Character.toString(ch), "");
52         }
53
54         // test not allowed character
55         Random rnd = new Random();
56         char ch = alphabet.charAt(rnd.nextInt(alphabet.length()));
57         assertWrongTicketNumber(ch + "20171212.1");
58     }
59
60     @Test
61     public void testSetTicketNumberDatepart() throws MalformedURLException, UnsupportedEncodingException, IOException {
62         char ch = getValidCharacter();
63
64         assertWrongTicketNumber(ch + "220171212.1");
65
66         assertWrongTicketNumber(ch + "0171212.1");
67
68         assertWrongTicketNumber(ch + "20171512.1");
69
70         assertWrongTicketNumber(ch + "20170229.1");
71
72         assertWrongTicketNumber(ch + ch + "20171212.1");
73
74         assertWrongTicketNumber("20171212.1");
75
76         assertWrongTicketNumber(ch + "20171212" + ch + ".1");
77
78         assertWrongTicketNumber(ch + "201721" + ch + "21.1");
79     }
80
81     @Test
82     public void testSetTicketNumberNumberpart() throws MalformedURLException, UnsupportedEncodingException, IOException {
83         char ch = getValidCharacter();
84
85         assertWrongTicketNumber(ch + "20171212.");
86
87         assertWrongTicketNumber(ch + "20171212");
88
89         assertWrongTicketNumber(ch + "20171212.1" + ch);
90
91     }
92
93     private char getValidCharacter() {
94         Random rnd = new Random();
95         return SupportEnterTicketForm.TICKET_PREFIX.charAt(rnd.nextInt(SupportEnterTicketForm.TICKET_PREFIX.length()));
96     }
97
98     private void assertWrongTicketNumber(String ticket) throws IOException {
99         String res = IOUtils.readURL(post(SupportEnterTicketPage.PATH, "ticketno=" + ticket + "&setTicket=action"));
100         assertThat(res, containsString("Ticket format malformed"));
101     }
102 }