1 package net.pterodactylus.sone.web;
3 import static org.hamcrest.MatcherAssert.assertThat;
4 import static org.hamcrest.Matchers.is;
5 import static org.mockito.Matchers.anyInt;
6 import static org.mockito.Matchers.anyString;
7 import static org.mockito.Mockito.mock;
8 import static org.mockito.Mockito.verify;
9 import static org.mockito.Mockito.when;
13 import net.pterodactylus.sone.core.Core;
14 import net.pterodactylus.sone.data.Album;
15 import net.pterodactylus.sone.data.IdBuilder;
16 import net.pterodactylus.sone.data.Sone;
17 import net.pterodactylus.sone.web.page.FreenetRequest;
18 import net.pterodactylus.sone.web.page.FreenetTemplatePage.RedirectException;
19 import net.pterodactylus.util.template.Template;
20 import net.pterodactylus.util.template.TemplateContext;
21 import net.pterodactylus.util.web.Method;
23 import freenet.support.api.HTTPRequest;
25 import com.google.common.base.Optional;
26 import com.google.common.collect.ImmutableMap;
27 import org.apache.commons.lang.StringUtils;
28 import org.hamcrest.Matchers;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.mockito.invocation.InvocationOnMock;
32 import org.mockito.stubbing.Answer;
35 * Unit test for {@link DeleteAlbumPageTest}.
37 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
39 public class DeleteAlbumPageTest {
41 private static final String SONE_ID = StringUtils.repeat("s", 43);
42 private static final String ALBUM_ID = StringUtils.repeat("a", IdBuilder.ID_STRING_LENGTH);
43 private static final String PARENT_ALBUM_ID = StringUtils.repeat("b", IdBuilder.ID_STRING_LENGTH);
44 private final Template template = mock(Template.class);
45 private final Core core = mock(Core.class);
46 private final WebInterface webInterface = mock(WebInterface.class);
47 private final DeleteAlbumPage deleteAlbumPage = new DeleteAlbumPage(template, webInterface);
48 private final TemplateContext templateContext = new TemplateContext();
49 private final Album parentAlbum = mock(Album.class);
50 private final Album album = mock(Album.class);
51 private final Sone sone = mock(Sone.class);
54 public void setupCore() {
55 when(core.getAlbum(anyString())).thenAnswer(new Answer<Optional<Album>>() {
57 public Optional<Album> answer(InvocationOnMock invocation) throws Throwable {
58 return ALBUM_ID.equals(invocation.getArguments()[0]) ? Optional.of(album) : Optional.<Album>absent();
64 public void setupWebInterface() {
65 when(webInterface.getCore()).thenReturn(core);
69 public void setupAlbums() {
70 when(album.getId()).thenReturn(ALBUM_ID);
71 when(album.getSone()).thenReturn(sone);
72 when(album.getParent()).thenReturn(parentAlbum);
73 when(parentAlbum.getId()).thenReturn(PARENT_ALBUM_ID);
77 public void setupSone() {
78 when(sone.getId()).thenReturn(SONE_ID);
79 when(sone.getRootAlbum()).thenReturn(parentAlbum);
83 public void gettingAValidAlbumStoresAlbumInTemplateContext() throws RedirectException {
84 FreenetRequest request = createFreenetRequest(Method.GET, ImmutableMap.of("album", ALBUM_ID));
85 deleteAlbumPage.processSonePage(request, templateContext);
86 assertThat(templateContext.get("album"), Matchers.<Object>is(album));
89 @Test(expected = RedirectException.class)
90 public void gettingAnInvalidAlbumThrowsRedirectException() throws RedirectException {
92 FreenetRequest request = createFreenetRequest(Method.GET, ImmutableMap.of("album", "foo"));
93 deleteAlbumPage.processSonePage(request, templateContext);
94 } catch (RedirectException e) {
95 assertThat(e.getTarget(), is("invalid.html"));
100 @Test(expected = RedirectException.class)
101 public void postingAnInvalidAlbumIdWillRedirect() throws RedirectException {
103 FreenetRequest request = createFreenetRequest(Method.POST, ImmutableMap.of("album", "foo"));
104 deleteAlbumPage.processSonePage(request, templateContext);
105 } catch (RedirectException e) {
106 assertThat(e.getTarget(), is("invalid.html"));
111 @Test(expected = RedirectException.class)
112 public void deletingAnAlbumOfARemoteSoneWillRedirect() throws RedirectException {
114 FreenetRequest request = createFreenetRequest(Method.POST, ImmutableMap.of("album", ALBUM_ID));
115 deleteAlbumPage.processSonePage(request, templateContext);
116 } catch (RedirectException e) {
117 assertThat(e.getTarget(), is("noPermission.html"));
122 @Test(expected = RedirectException.class)
123 public void abortingTheDeleteWillRedirect() throws RedirectException {
125 when(sone.isLocal()).thenReturn(true);
126 FreenetRequest request = createFreenetRequest(Method.POST, ImmutableMap.of("album", ALBUM_ID, "abortDelete", "true"));
127 deleteAlbumPage.processSonePage(request, templateContext);
128 } catch (RedirectException e) {
129 assertThat(e.getTarget(), is(String.format("imageBrowser.html?album=%s", ALBUM_ID)));
134 @Test(expected = RedirectException.class)
135 public void deletingAnAlbumFromTheRootAlbumRedirectsToSoneImageBrowser() throws RedirectException {
137 when(sone.isLocal()).thenReturn(true);
138 FreenetRequest request = createFreenetRequest(Method.POST, ImmutableMap.of("album", ALBUM_ID));
139 deleteAlbumPage.processSonePage(request, templateContext);
140 } catch (RedirectException e) {
141 assertThat(e.getTarget(), is(String.format("imageBrowser.html?sone=%s", SONE_ID)));
142 verify(core).deleteAlbum(album);
147 @Test(expected = RedirectException.class)
148 public void deletingAnAlbumNotFromTheRootAlbumRedirectsToParentAlbumImageBrowser() throws RedirectException {
150 when(sone.isLocal()).thenReturn(true);
151 when(sone.getRootAlbum()).thenReturn(mock(Album.class));
152 FreenetRequest request = createFreenetRequest(Method.POST, ImmutableMap.of("album", ALBUM_ID));
153 deleteAlbumPage.processSonePage(request, templateContext);
154 } catch (RedirectException e) {
155 assertThat(e.getTarget(), is(String.format("imageBrowser.html?album=%s", PARENT_ALBUM_ID)));
156 verify(core).deleteAlbum(album);
161 private static FreenetRequest createFreenetRequest(Method method, final Map<String, String> parameters) {
162 HTTPRequest httpRequest = mock(HTTPRequest.class);
163 when(httpRequest.getParam(anyString())).thenAnswer(new Answer<String>() {
165 public String answer(InvocationOnMock invocation) throws Throwable {
166 if (parameters.containsKey(invocation.getArguments()[0])) {
167 return parameters.get(invocation.getArguments()[0]);
172 when(httpRequest.getParam(anyString(), anyString())).thenAnswer(new Answer<String>() {
174 public String answer(InvocationOnMock invocation) throws Throwable {
175 if (parameters.containsKey(invocation.getArguments()[0])) {
176 return parameters.get(invocation.getArguments()[0]);
178 return (String) invocation.getArguments()[1];
181 when(httpRequest.getPartAsStringFailsafe(anyString(), anyInt())).thenAnswer(new Answer<String>() {
183 public String answer(InvocationOnMock invocation) throws Throwable {
184 if (parameters.containsKey(invocation.getArguments()[0])) {
185 return StringUtils.left(parameters.get(invocation.getArguments()[0]), (Integer) invocation.getArguments()[1]);
190 when(httpRequest.isPartSet(anyString())).thenAnswer(new Answer<Boolean>() {
192 public Boolean answer(InvocationOnMock invocation) throws Throwable {
193 return parameters.containsKey(invocation.getArguments()[0]);
196 return new FreenetRequest(null, method, httpRequest, null);