aab40c9a98897c0a63ad920f6c771246fa920adc
[Sone.git] / src / test / java / net / pterodactylus / sone / web / CreateSonePageTest.java
1 package net.pterodactylus.sone.web;
2
3 import static net.pterodactylus.sone.web.WebTestUtils.redirectsTo;
4 import static net.pterodactylus.util.web.Method.POST;
5 import static org.hamcrest.MatcherAssert.assertThat;
6 import static org.hamcrest.Matchers.contains;
7 import static org.hamcrest.Matchers.is;
8 import static org.mockito.ArgumentMatchers.anyString;
9 import static org.mockito.Mockito.mock;
10 import static org.mockito.Mockito.verify;
11 import static org.mockito.Mockito.when;
12
13 import java.util.Arrays;
14 import java.util.Collection;
15 import java.util.HashSet;
16
17 import net.pterodactylus.sone.data.Profile;
18 import net.pterodactylus.sone.data.Sone;
19 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
20
21 import org.junit.Test;
22 import org.mockito.invocation.InvocationOnMock;
23 import org.mockito.stubbing.Answer;
24
25 /**
26  * Unit test for {@link CreateSonePage}.
27  *
28  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
29  */
30 public class CreateSonePageTest extends WebPageTest {
31
32         private final CreateSonePage page = new CreateSonePage(template, webInterface);
33         private final Sone[] localSones = { createSone("local-sone1"), createSone("local-sone2"), createSone("local-sone3") };
34         private final OwnIdentity[] ownIdentities = {
35                         createOwnIdentity("own-id-1", "Sone"),
36                         createOwnIdentity("own-id-2", "Test", "Foo"),
37                         createOwnIdentity("own-id-3"),
38                         createOwnIdentity("own-id-4", "Sone")
39         };
40
41         @Test
42         public void pageReturnsCorrectPath() {
43                 assertThat(page.getPath(), is("createSone.html"));
44         }
45
46         @Test
47         @SuppressWarnings("unchecked")
48         public void getRequestStoresListOfIdentitiesInTemplateContext() throws Exception {
49                 addDefaultLocalSones();
50                 addDefaultOwnIdentities();
51                 page.processTemplate(freenetRequest, templateContext);
52                 assertThat((Collection<Sone>) templateContext.get("sones"), contains(localSones[0], localSones[1], localSones[2]));
53                 assertThat((Collection<OwnIdentity>) templateContext.get("identitiesWithoutSone"), contains(ownIdentities[1], ownIdentities[2]));
54         }
55
56         private void addDefaultLocalSones() {
57                 addLocalSone("local-sone3", localSones[2]);
58                 addLocalSone("local-sone1", localSones[0]);
59                 addLocalSone("local-sone2", localSones[1]);
60         }
61
62         private void addDefaultOwnIdentities() {
63                 addOwnIdentity(ownIdentities[2]);
64                 addOwnIdentity(ownIdentities[0]);
65                 addOwnIdentity(ownIdentities[3]);
66                 addOwnIdentity(ownIdentities[1]);
67         }
68
69         private Sone createSone(String id) {
70                 Sone sone = mock(Sone.class);
71                 when(sone.getId()).thenReturn(id);
72                 when(sone.getProfile()).thenReturn(new Profile(sone));
73                 return sone;
74         }
75
76         private OwnIdentity createOwnIdentity(String id, final String... contexts) {
77                 OwnIdentity ownIdentity = mock(OwnIdentity.class);
78                 when(ownIdentity.getId()).thenReturn(id);
79                 when(ownIdentity.getNickname()).thenReturn(id);
80                 when(ownIdentity.getContexts()).thenReturn(new HashSet<>(Arrays.asList(contexts)));
81                 when(ownIdentity.hasContext(anyString())).thenAnswer(new Answer<Boolean>() {
82                         @Override
83                         public Boolean answer(InvocationOnMock invocation) throws Throwable {
84                                 return Arrays.asList(contexts).contains(invocation.<String>getArgument(0));
85                         }
86                 });
87                 return ownIdentity;
88         }
89
90         @Test
91         public void soneIsCreatedAndLoggedIn() throws Exception {
92                 addDefaultLocalSones();
93                 addDefaultOwnIdentities();
94                 addHttpRequestParameter("identity", "own-id-3");
95                 request("", POST);
96                 Sone newSone = mock(Sone.class);
97                 when(core.createSone(ownIdentities[2])).thenReturn(newSone);
98                 expectedException.expect(redirectsTo("index.html"));
99                 try {
100                         page.processTemplate(freenetRequest, templateContext);
101                 } finally {
102                         verify(core).createSone(ownIdentities[2]);
103                         verify(webInterface).setCurrentSone(toadletContext, newSone);
104                 }
105         }
106
107         @Test
108         public void onInvalidIdentityIdFlagIsStoredInTemplateContext() throws Exception {
109                 addDefaultLocalSones();
110                 addDefaultOwnIdentities();
111                 addHttpRequestParameter("identity", "own-id-invalid");
112                 request("", POST);
113                 page.processTemplate(freenetRequest, templateContext);
114                 assertThat(((Boolean) templateContext.get("errorNoIdentity")), is(true));
115         }
116
117         @Test
118         public void ifSoneIsNotCreatedUserIsStillRedirectedToIndex() throws Exception {
119                 addDefaultLocalSones();
120                 addDefaultOwnIdentities();
121                 addHttpRequestParameter("identity", "own-id-3");
122                 request("", POST);
123                 when(core.createSone(ownIdentities[2])).thenReturn(null);
124                 expectedException.expect(redirectsTo("index.html"));
125                 try {
126                         page.processTemplate(freenetRequest, templateContext);
127                 } finally {
128                         verify(core).createSone(ownIdentities[2]);
129                         verify(webInterface).setCurrentSone(toadletContext, null);
130                 }
131         }
132
133         @Test
134         public void doNotShowCreateSoneInMenuIfFullAccessRequiredButClientHasNoFullAccess() {
135                 when(core.getPreferences().isRequireFullAccess()).thenReturn(true);
136                 when(toadletContext.isAllowedFullAccess()).thenReturn(false);
137                 assertThat(page.isEnabled(toadletContext), is(false));
138         }
139
140         @Test
141         public void showCreateSoneInMenuIfNotLoggedInAndClientHasFullAccess() {
142                 when(core.getPreferences().isRequireFullAccess()).thenReturn(true);
143                 when(toadletContext.isAllowedFullAccess()).thenReturn(true);
144                 unsetCurrentSone();
145                 assertThat(page.isEnabled(toadletContext), is(true));
146         }
147
148         @Test
149         public void showCreateSoneInMenuIfNotLoggedIn() {
150                 unsetCurrentSone();
151                 assertThat(page.isEnabled(toadletContext), is(true));
152         }
153
154         @Test
155         public void showCreateSoneInMenuIfLoggedInAndASingleSoneExists() {
156                 addLocalSone("local-sone", mock(Sone.class));
157                 assertThat(page.isEnabled(toadletContext), is(true));
158         }
159
160         @Test
161         public void doNotShowCreateSoneInMenuIfLoggedInAndMoreLocalSonesExists() {
162                 addLocalSone("local-sone1", mock(Sone.class));
163                 addLocalSone("local-sone2", mock(Sone.class));
164                 assertThat(page.isEnabled(toadletContext), is(false));
165         }
166
167 }