Add test for album deletion page
[Sone.git] / src / test / java / net / pterodactylus / sone / web / DeleteAlbumPageTest.java
1 package net.pterodactylus.sone.web;
2
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;
10
11 import java.util.Map;
12
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;
22
23 import freenet.support.api.HTTPRequest;
24
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;
33
34 /**
35  * Unit test for {@link DeleteAlbumPageTest}.
36  *
37  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
38  */
39 public class DeleteAlbumPageTest {
40
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);
52
53         @Before
54         public void setupCore() {
55                 when(core.getAlbum(anyString())).thenAnswer(new Answer<Optional<Album>>() {
56                         @Override
57                         public Optional<Album> answer(InvocationOnMock invocation) throws Throwable {
58                                 return ALBUM_ID.equals(invocation.getArguments()[0]) ? Optional.of(album) : Optional.<Album>absent();
59                         }
60                 });
61         }
62
63         @Before
64         public void setupWebInterface() {
65                 when(webInterface.getCore()).thenReturn(core);
66         }
67
68         @Before
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);
74         }
75
76         @Before
77         public void setupSone() {
78                 when(sone.getId()).thenReturn(SONE_ID);
79                 when(sone.getRootAlbum()).thenReturn(parentAlbum);
80         }
81
82         @Test
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));
87         }
88
89         @Test(expected = RedirectException.class)
90         public void gettingAnInvalidAlbumThrowsRedirectException() throws RedirectException {
91                 try {
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"));
96                         throw e;
97                 }
98         }
99
100         @Test(expected = RedirectException.class)
101         public void postingAnInvalidAlbumIdWillRedirect() throws RedirectException {
102                 try {
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"));
107                         throw e;
108                 }
109         }
110
111         @Test(expected = RedirectException.class)
112         public void deletingAnAlbumOfARemoteSoneWillRedirect() throws RedirectException {
113                 try {
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"));
118                         throw e;
119                 }
120         }
121
122         @Test(expected = RedirectException.class)
123         public void abortingTheDeleteWillRedirect() throws RedirectException {
124                 try {
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)));
130                         throw e;
131                 }
132         }
133
134         @Test(expected = RedirectException.class)
135         public void deletingAnAlbumFromTheRootAlbumRedirectsToSoneImageBrowser() throws RedirectException {
136                 try {
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);
143                         throw e;
144                 }
145         }
146
147         @Test(expected = RedirectException.class)
148         public void deletingAnAlbumNotFromTheRootAlbumRedirectsToParentAlbumImageBrowser() throws RedirectException {
149                 try {
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);
157                         throw e;
158                 }
159         }
160
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>() {
164                         @Override
165                         public String answer(InvocationOnMock invocation) throws Throwable {
166                                 if (parameters.containsKey(invocation.getArguments()[0])) {
167                                         return parameters.get(invocation.getArguments()[0]);
168                                 }
169                                 return "";
170                         }
171                 });
172                 when(httpRequest.getParam(anyString(), anyString())).thenAnswer(new Answer<String>() {
173                         @Override
174                         public String answer(InvocationOnMock invocation) throws Throwable {
175                                 if (parameters.containsKey(invocation.getArguments()[0])) {
176                                         return parameters.get(invocation.getArguments()[0]);
177                                 }
178                                 return (String) invocation.getArguments()[1];
179                         }
180                 });
181                 when(httpRequest.getPartAsStringFailsafe(anyString(), anyInt())).thenAnswer(new Answer<String>() {
182                         @Override
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]);
186                                 }
187                                 return "";
188                         }
189                 });
190                 when(httpRequest.isPartSet(anyString())).thenAnswer(new Answer<Boolean>() {
191                         @Override
192                         public Boolean answer(InvocationOnMock invocation) throws Throwable {
193                                 return parameters.containsKey(invocation.getArguments()[0]);
194                         }
195                 });
196                 return new FreenetRequest(null, method, httpRequest, null);
197         }
198
199 }