Merge branch 'release-0.9.6'
[Sone.git] / src / test / java / net / pterodactylus / sone / web / CreatePostPageTest.java
1 package net.pterodactylus.sone.web;
2
3 import static net.pterodactylus.util.web.Method.POST;
4 import static org.hamcrest.MatcherAssert.assertThat;
5 import static org.hamcrest.Matchers.is;
6 import static org.mockito.Mockito.mock;
7 import static org.mockito.Mockito.verify;
8
9 import net.pterodactylus.sone.data.Sone;
10
11 import com.google.common.base.Optional;
12 import org.junit.Test;
13
14 /**
15  * Unit test for {@link CreatePostPage}.
16  *
17  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
18  */
19 public class CreatePostPageTest extends WebPageTest {
20
21         private final CreatePostPage page = new CreatePostPage(template, webInterface);
22
23         @Test
24         public void pageReturnsCorrectPath() {
25                 assertThat(page.getPath(), is("createPost.html"));
26         }
27
28         @Test
29         public void returnPageIsSetInTemplateContext() throws Exception {
30                 addHttpRequestParameter("returnPage", "returnPage.html");
31                 page.processTemplate(freenetRequest, templateContext);
32                 assertThat(templateContext.get("returnPage"), is((Object) "returnPage.html"));
33         }
34
35         @Test
36         public void postIsCreatedCorrectly() throws Exception {
37                 addHttpRequestParameter("returnPage", "returnPage.html");
38                 addHttpRequestParameter("text", "post text");
39                 request("", POST);
40                 expectedException.expect(WebTestUtils.redirectsTo("returnPage.html"));
41                 try {
42                         page.processTemplate(freenetRequest, templateContext);
43                 } finally {
44                         verify(core).createPost(currentSone, Optional.<Sone>absent(), "post text");
45                 }
46         }
47
48         @Test
49         public void creatingAnEmptyPostIsDenied() throws Exception {
50                 addHttpRequestParameter("returnPage", "returnPage.html");
51                 addHttpRequestParameter("text", "   ");
52                 request("", POST);
53                 page.processTemplate(freenetRequest, templateContext);
54                 assertThat(templateContext.get("errorTextEmpty"), is((Object) true));
55         }
56
57         @Test
58         public void aSenderCanBeSelected() throws Exception {
59                 addHttpRequestParameter("returnPage", "returnPage.html");
60                 addHttpRequestParameter("text", "post text");
61                 addHttpRequestParameter("sender", "sender-id");
62                 Sone sender = mock(Sone.class);
63                 addLocalSone("sender-id", sender);
64                 request("", POST);
65                 expectedException.expect(WebTestUtils.redirectsTo("returnPage.html"));
66                 try {
67                         page.processTemplate(freenetRequest, templateContext);
68                 } finally {
69                         verify(core).createPost(sender, Optional.<Sone>absent(), "post text");
70                 }
71         }
72
73         @Test
74         public void aRecipientCanBeSelected() throws Exception {
75                 addHttpRequestParameter("returnPage", "returnPage.html");
76                 addHttpRequestParameter("text", "post text");
77                 addHttpRequestParameter("recipient", "recipient-id");
78                 Sone recipient = mock(Sone.class);
79                 addSone("recipient-id", recipient);
80                 request("", POST);
81                 expectedException.expect(WebTestUtils.redirectsTo("returnPage.html"));
82                 try {
83                         page.processTemplate(freenetRequest, templateContext);
84                 } finally {
85                         verify(core).createPost(currentSone, Optional.of(recipient), "post text");
86                 }
87         }
88
89 }