From: David ‘Bombe’ Roden Date: Tue, 25 Oct 2016 18:50:08 +0000 (+0200) Subject: Add test for CreateSonePage X-Git-Tag: 0.9.6^2~2 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=5183f00192da53deea6e847c3c05eb4c668dc814;p=Sone.git Add test for CreateSonePage --- diff --git a/src/test/java/net/pterodactylus/sone/web/CreateSonePageTest.java b/src/test/java/net/pterodactylus/sone/web/CreateSonePageTest.java new file mode 100644 index 0000000..aab40c9 --- /dev/null +++ b/src/test/java/net/pterodactylus/sone/web/CreateSonePageTest.java @@ -0,0 +1,167 @@ +package net.pterodactylus.sone.web; + +import static net.pterodactylus.sone.web.WebTestUtils.redirectsTo; +import static net.pterodactylus.util.web.Method.POST; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.contains; +import static org.hamcrest.Matchers.is; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.Arrays; +import java.util.Collection; +import java.util.HashSet; + +import net.pterodactylus.sone.data.Profile; +import net.pterodactylus.sone.data.Sone; +import net.pterodactylus.sone.freenet.wot.OwnIdentity; + +import org.junit.Test; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; + +/** + * Unit test for {@link CreateSonePage}. + * + * @author David ‘Bombe’ Roden + */ +public class CreateSonePageTest extends WebPageTest { + + private final CreateSonePage page = new CreateSonePage(template, webInterface); + private final Sone[] localSones = { createSone("local-sone1"), createSone("local-sone2"), createSone("local-sone3") }; + private final OwnIdentity[] ownIdentities = { + createOwnIdentity("own-id-1", "Sone"), + createOwnIdentity("own-id-2", "Test", "Foo"), + createOwnIdentity("own-id-3"), + createOwnIdentity("own-id-4", "Sone") + }; + + @Test + public void pageReturnsCorrectPath() { + assertThat(page.getPath(), is("createSone.html")); + } + + @Test + @SuppressWarnings("unchecked") + public void getRequestStoresListOfIdentitiesInTemplateContext() throws Exception { + addDefaultLocalSones(); + addDefaultOwnIdentities(); + page.processTemplate(freenetRequest, templateContext); + assertThat((Collection) templateContext.get("sones"), contains(localSones[0], localSones[1], localSones[2])); + assertThat((Collection) templateContext.get("identitiesWithoutSone"), contains(ownIdentities[1], ownIdentities[2])); + } + + private void addDefaultLocalSones() { + addLocalSone("local-sone3", localSones[2]); + addLocalSone("local-sone1", localSones[0]); + addLocalSone("local-sone2", localSones[1]); + } + + private void addDefaultOwnIdentities() { + addOwnIdentity(ownIdentities[2]); + addOwnIdentity(ownIdentities[0]); + addOwnIdentity(ownIdentities[3]); + addOwnIdentity(ownIdentities[1]); + } + + private Sone createSone(String id) { + Sone sone = mock(Sone.class); + when(sone.getId()).thenReturn(id); + when(sone.getProfile()).thenReturn(new Profile(sone)); + return sone; + } + + private OwnIdentity createOwnIdentity(String id, final String... contexts) { + OwnIdentity ownIdentity = mock(OwnIdentity.class); + when(ownIdentity.getId()).thenReturn(id); + when(ownIdentity.getNickname()).thenReturn(id); + when(ownIdentity.getContexts()).thenReturn(new HashSet<>(Arrays.asList(contexts))); + when(ownIdentity.hasContext(anyString())).thenAnswer(new Answer() { + @Override + public Boolean answer(InvocationOnMock invocation) throws Throwable { + return Arrays.asList(contexts).contains(invocation.getArgument(0)); + } + }); + return ownIdentity; + } + + @Test + public void soneIsCreatedAndLoggedIn() throws Exception { + addDefaultLocalSones(); + addDefaultOwnIdentities(); + addHttpRequestParameter("identity", "own-id-3"); + request("", POST); + Sone newSone = mock(Sone.class); + when(core.createSone(ownIdentities[2])).thenReturn(newSone); + expectedException.expect(redirectsTo("index.html")); + try { + page.processTemplate(freenetRequest, templateContext); + } finally { + verify(core).createSone(ownIdentities[2]); + verify(webInterface).setCurrentSone(toadletContext, newSone); + } + } + + @Test + public void onInvalidIdentityIdFlagIsStoredInTemplateContext() throws Exception { + addDefaultLocalSones(); + addDefaultOwnIdentities(); + addHttpRequestParameter("identity", "own-id-invalid"); + request("", POST); + page.processTemplate(freenetRequest, templateContext); + assertThat(((Boolean) templateContext.get("errorNoIdentity")), is(true)); + } + + @Test + public void ifSoneIsNotCreatedUserIsStillRedirectedToIndex() throws Exception { + addDefaultLocalSones(); + addDefaultOwnIdentities(); + addHttpRequestParameter("identity", "own-id-3"); + request("", POST); + when(core.createSone(ownIdentities[2])).thenReturn(null); + expectedException.expect(redirectsTo("index.html")); + try { + page.processTemplate(freenetRequest, templateContext); + } finally { + verify(core).createSone(ownIdentities[2]); + verify(webInterface).setCurrentSone(toadletContext, null); + } + } + + @Test + public void doNotShowCreateSoneInMenuIfFullAccessRequiredButClientHasNoFullAccess() { + when(core.getPreferences().isRequireFullAccess()).thenReturn(true); + when(toadletContext.isAllowedFullAccess()).thenReturn(false); + assertThat(page.isEnabled(toadletContext), is(false)); + } + + @Test + public void showCreateSoneInMenuIfNotLoggedInAndClientHasFullAccess() { + when(core.getPreferences().isRequireFullAccess()).thenReturn(true); + when(toadletContext.isAllowedFullAccess()).thenReturn(true); + unsetCurrentSone(); + assertThat(page.isEnabled(toadletContext), is(true)); + } + + @Test + public void showCreateSoneInMenuIfNotLoggedIn() { + unsetCurrentSone(); + assertThat(page.isEnabled(toadletContext), is(true)); + } + + @Test + public void showCreateSoneInMenuIfLoggedInAndASingleSoneExists() { + addLocalSone("local-sone", mock(Sone.class)); + assertThat(page.isEnabled(toadletContext), is(true)); + } + + @Test + public void doNotShowCreateSoneInMenuIfLoggedInAndMoreLocalSonesExists() { + addLocalSone("local-sone1", mock(Sone.class)); + addLocalSone("local-sone2", mock(Sone.class)); + assertThat(page.isEnabled(toadletContext), is(false)); + } + +} diff --git a/src/test/java/net/pterodactylus/sone/web/WebPageTest.java b/src/test/java/net/pterodactylus/sone/web/WebPageTest.java index bb6282c..3e2df88 100644 --- a/src/test/java/net/pterodactylus/sone/web/WebPageTest.java +++ b/src/test/java/net/pterodactylus/sone/web/WebPageTest.java @@ -11,11 +11,15 @@ import static org.mockito.Mockito.when; import java.net.URI; import java.net.URISyntaxException; import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; import net.pterodactylus.sone.core.Core; import net.pterodactylus.sone.core.UpdateChecker; import net.pterodactylus.sone.data.Post; import net.pterodactylus.sone.data.Sone; +import net.pterodactylus.sone.freenet.wot.OwnIdentity; import net.pterodactylus.sone.web.page.FreenetRequest; import net.pterodactylus.util.notify.Notification; import net.pterodactylus.util.template.Template; @@ -53,6 +57,8 @@ public abstract class WebPageTest { protected final FreenetRequest freenetRequest = mock(FreenetRequest.class); protected final ToadletContext toadletContext = mock(ToadletContext.class); + private final Set ownIdentities = new HashSet<>(); + private final List localSones = new ArrayList<>(); @Before public final void setupFreenetRequest() { @@ -71,17 +77,28 @@ public abstract class WebPageTest { UpdateChecker updateChecker = mock(UpdateChecker.class); when(core.getUpdateChecker()).thenReturn(updateChecker); when(core.getLocalSone(anyString())).thenReturn(null); + when(core.getLocalSones()).thenReturn(localSones); when(core.getSone(anyString())).thenReturn(Optional.absent()); when(core.getPost(anyString())).thenReturn(Optional.absent()); } @Before + public final void setupIdentityManager() { + when(core.getIdentityManager().getAllOwnIdentities()).thenReturn(ownIdentities); + } + + @Before public final void setupWebInterface() { when(webInterface.getCurrentSone(toadletContext)).thenReturn(currentSone); when(webInterface.getCurrentSone(eq(toadletContext), anyBoolean())).thenReturn(currentSone); when(webInterface.getNotifications(currentSone)).thenReturn(new ArrayList()); } + protected void unsetCurrentSone() { + when(webInterface.getCurrentSone(toadletContext)).thenReturn(null); + when(webInterface.getCurrentSone(eq(toadletContext), anyBoolean())).thenReturn(null); + } + protected void request(String uri, Method method) { try { when(freenetRequest.getUri()).thenReturn(new URI(uri)); @@ -111,6 +128,11 @@ public abstract class WebPageTest { protected void addLocalSone(String soneId, Sone sone) { when(core.getLocalSone(eq(soneId))).thenReturn(sone); + localSones.add(sone); + } + + protected void addOwnIdentity(OwnIdentity ownIdentity) { + ownIdentities.add(ownIdentity); } }