Fix bug where an empty image title would corrupt a Sone’s albums’ state
[Sone.git] / src / test / java / net / pterodactylus / sone / web / UploadImagePageTest.java
1 package net.pterodactylus.sone.web;
2
3 import static org.mockito.Matchers.any;
4 import static org.mockito.Matchers.anyInt;
5 import static org.mockito.Matchers.eq;
6 import static org.mockito.Mockito.mock;
7 import static org.mockito.Mockito.when;
8
9 import java.net.URI;
10
11 import net.pterodactylus.sone.core.Core;
12 import net.pterodactylus.sone.core.UpdateChecker;
13 import net.pterodactylus.sone.data.Album;
14 import net.pterodactylus.sone.data.Sone;
15 import net.pterodactylus.sone.web.page.FreenetRequest;
16 import net.pterodactylus.sone.web.page.FreenetTemplatePage.RedirectException;
17 import net.pterodactylus.util.template.Template;
18 import net.pterodactylus.util.template.TemplateContext;
19 import net.pterodactylus.util.web.Method;
20
21 import freenet.clients.http.ToadletContext;
22 import freenet.support.api.HTTPRequest;
23
24 import org.hamcrest.Description;
25 import org.hamcrest.Matcher;
26 import org.hamcrest.TypeSafeDiagnosingMatcher;
27 import org.junit.Before;
28 import org.junit.Rule;
29 import org.junit.Test;
30 import org.junit.rules.ExpectedException;
31
32 /**
33  * Unit test for {@link UploadImagePageTest}.
34  *
35  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
36  */
37 public class UploadImagePageTest {
38
39         @Rule
40         public final ExpectedException expectedException = ExpectedException.none();
41
42         private final Template template = new Template();
43         private final WebInterface webInterface = mock(WebInterface.class);
44         private final UploadImagePage uploadImagePage = new UploadImagePage(template, webInterface);
45
46         private final TemplateContext templateContext = new TemplateContext();
47         private final HTTPRequest httpRequest = mock(HTTPRequest.class);
48         private final ToadletContext toadletContext = mock(ToadletContext.class);
49         private final Core core = mock(Core.class);
50         private final Sone currentSone = mock(Sone.class);
51         private final Album parentAlbum = mock(Album.class);
52
53         @Before
54         public void setupWebInterface() {
55                 UpdateChecker updateChecker = mock(UpdateChecker.class);
56                 when(core.getUpdateChecker()).thenReturn(updateChecker);
57                 when(webInterface.getCore()).thenReturn(core);
58                 when(webInterface.getCurrentSone(any(ToadletContext.class))).thenReturn(currentSone);
59         }
60
61         @Before
62         public void setupParentAlbum() {
63                 when(core.getAlbum("parent-id")).thenReturn(parentAlbum);
64                 when(parentAlbum.getSone()).thenReturn(currentSone);
65         }
66
67         @Test
68         public void uploadingAnImageWithoutTitleRedirectsToEmptyImageTitlePage() throws Exception {
69                 FreenetRequest request = new FreenetRequest(new URI(""), Method.POST, httpRequest, toadletContext);
70                 when(httpRequest.getPartAsStringFailsafe(eq("parent"), anyInt())).thenReturn("parent-id");
71                 when(httpRequest.getPartAsStringFailsafe(eq("title"), anyInt())).thenReturn("  ");
72                 expectedException.expect(redirectsTo("emptyImageTitle.html"));
73                 uploadImagePage.processTemplate(request, templateContext);
74         }
75
76         private Matcher<RedirectException> redirectsTo(final String page) {
77                 return new TypeSafeDiagnosingMatcher<RedirectException>() {
78                         @Override
79                         protected boolean matchesSafely(RedirectException exception, Description mismatchDescription) {
80                                 if (!exception.getTarget().equals(page)) {
81                                         mismatchDescription.appendText("target is ").appendValue(exception.getTarget());
82                                         return false;
83                                 }
84                                 return true;
85                         }
86
87                         @Override
88                         public void describeTo(Description description) {
89                                 description.appendText("target is ").appendValue(page);
90                         }
91                 };
92         }
93
94 }