1 package net.pterodactylus.sone.web;
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;
13 import java.util.Arrays;
14 import java.util.Collection;
15 import java.util.HashSet;
17 import net.pterodactylus.sone.data.Profile;
18 import net.pterodactylus.sone.data.Sone;
19 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
21 import org.junit.Test;
22 import org.mockito.invocation.InvocationOnMock;
23 import org.mockito.stubbing.Answer;
26 * Unit test for {@link CreateSonePage}.
28 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
30 public class CreateSonePageTest extends WebPageTest {
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")
42 public void pageReturnsCorrectPath() {
43 assertThat(page.getPath(), is("createSone.html"));
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]));
56 private void addDefaultLocalSones() {
57 addLocalSone("local-sone3", localSones[2]);
58 addLocalSone("local-sone1", localSones[0]);
59 addLocalSone("local-sone2", localSones[1]);
62 private void addDefaultOwnIdentities() {
63 addOwnIdentity(ownIdentities[2]);
64 addOwnIdentity(ownIdentities[0]);
65 addOwnIdentity(ownIdentities[3]);
66 addOwnIdentity(ownIdentities[1]);
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));
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>() {
83 public Boolean answer(InvocationOnMock invocation) throws Throwable {
84 return Arrays.asList(contexts).contains(invocation.<String>getArgument(0));
91 public void soneIsCreatedAndLoggedIn() throws Exception {
92 addDefaultLocalSones();
93 addDefaultOwnIdentities();
94 addHttpRequestParameter("identity", "own-id-3");
96 Sone newSone = mock(Sone.class);
97 when(core.createSone(ownIdentities[2])).thenReturn(newSone);
98 expectedException.expect(redirectsTo("index.html"));
100 page.processTemplate(freenetRequest, templateContext);
102 verify(core).createSone(ownIdentities[2]);
103 verify(webInterface).setCurrentSone(toadletContext, newSone);
108 public void onInvalidIdentityIdFlagIsStoredInTemplateContext() throws Exception {
109 addDefaultLocalSones();
110 addDefaultOwnIdentities();
111 addHttpRequestParameter("identity", "own-id-invalid");
113 page.processTemplate(freenetRequest, templateContext);
114 assertThat(((Boolean) templateContext.get("errorNoIdentity")), is(true));
118 public void ifSoneIsNotCreatedUserIsStillRedirectedToIndex() throws Exception {
119 addDefaultLocalSones();
120 addDefaultOwnIdentities();
121 addHttpRequestParameter("identity", "own-id-3");
123 when(core.createSone(ownIdentities[2])).thenReturn(null);
124 expectedException.expect(redirectsTo("index.html"));
126 page.processTemplate(freenetRequest, templateContext);
128 verify(core).createSone(ownIdentities[2]);
129 verify(webInterface).setCurrentSone(toadletContext, null);
134 public void doNotShowCreateSoneInMenuIfFullAccessRequiredButClientHasNoFullAccess() {
135 when(core.getPreferences().isRequireFullAccess()).thenReturn(true);
136 when(toadletContext.isAllowedFullAccess()).thenReturn(false);
137 assertThat(page.isEnabled(toadletContext), is(false));
141 public void showCreateSoneInMenuIfNotLoggedInAndClientHasFullAccess() {
142 when(core.getPreferences().isRequireFullAccess()).thenReturn(true);
143 when(toadletContext.isAllowedFullAccess()).thenReturn(true);
145 assertThat(page.isEnabled(toadletContext), is(true));
149 public void showCreateSoneInMenuIfNotLoggedIn() {
151 assertThat(page.isEnabled(toadletContext), is(true));
155 public void showCreateSoneInMenuIfLoggedInAndASingleSoneExists() {
156 addLocalSone("local-sone", mock(Sone.class));
157 assertThat(page.isEnabled(toadletContext), is(true));
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));