f6e15af9e2febb2b11697ed45c84ecc27877b4ce
[Sone.git] / src / test / java / net / pterodactylus / sone / web / DeleteReplyPageTest.java
1 package net.pterodactylus.sone.web;
2
3 import static net.pterodactylus.sone.web.WebTestUtils.redirectsTo;
4 import static org.mockito.Matchers.anyInt;
5 import static org.mockito.Matchers.eq;
6 import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
7 import static org.mockito.Mockito.mock;
8 import static org.mockito.Mockito.when;
9
10 import java.util.Collections;
11
12 import net.pterodactylus.sone.data.PostReply;
13 import net.pterodactylus.sone.data.Sone;
14 import net.pterodactylus.sone.web.page.FreenetRequest;
15 import net.pterodactylus.util.notify.Notification;
16 import net.pterodactylus.util.template.Template;
17 import net.pterodactylus.util.template.TemplateContext;
18 import net.pterodactylus.util.web.Method;
19
20 import freenet.support.api.HTTPRequest;
21
22 import com.google.common.base.Optional;
23 import org.junit.Before;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.junit.rules.ExpectedException;
27
28 /**
29  * Unit test for {@link DeleteReplyPage}.
30  *
31  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
32  */
33 public class DeleteReplyPageTest {
34
35         @Rule
36         public final ExpectedException expectedException = ExpectedException.none();
37
38         private final Template template = new Template();
39         private final WebInterface webInterface = mock(WebInterface.class, RETURNS_DEEP_STUBS);
40         private final DeleteReplyPage page = new DeleteReplyPage(template, webInterface);
41         private final TemplateContext templateContext = new TemplateContext();
42         private final FreenetRequest freenetRequest = mock(FreenetRequest.class);
43         private final HTTPRequest httpRequest = mock(HTTPRequest.class);
44
45         @Before
46         public void setupWebInterface() {
47                 when(webInterface.getNotifications(any(Sone.class))).thenReturn(Collections.<Notification>emptyList());
48         }
49
50         @Before
51         public void setupHttpRequest() {
52                 when(freenetRequest.getHttpRequest()).thenReturn(httpRequest);
53         }
54
55         @Test
56         public void tryingToDeleteAReplyWithAnInvalidIdResultsInNoPermissionPage() throws Exception {
57                 when(freenetRequest.getMethod()).thenReturn(Method.POST);
58                 when(httpRequest.getPartAsStringFailsafe(eq("reply"), anyInt())).thenReturn("id");
59                 when(webInterface.getCore().getPostReply("id")).thenReturn(Optional.<PostReply>absent());
60                 expectedException.expect(redirectsTo("noPermission.html"));
61                 page.processTemplate(freenetRequest, templateContext);
62         }
63
64 }