From: David ‘Bombe’ Roden Date: Sat, 26 Nov 2016 21:58:21 +0000 (+0100) Subject: Add unit test for follow sone page X-Git-Tag: 0.9.7^2~389 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=67903388084f195d6eca7e2b6c8a9d2890a38eda;p=Sone.git Add unit test for follow sone page --- diff --git a/src/test/kotlin/net/pterodactylus/sone/web/FollowSonePageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/FollowSonePageTest.kt new file mode 100644 index 0000000..8eee302 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/web/FollowSonePageTest.kt @@ -0,0 +1,78 @@ +package net.pterodactylus.sone.web + +import net.pterodactylus.sone.data.Sone +import net.pterodactylus.sone.test.mock +import net.pterodactylus.sone.web.WebTestUtils.redirectsTo +import net.pterodactylus.util.web.Method.GET +import net.pterodactylus.util.web.Method.POST +import org.junit.Test +import org.mockito.ArgumentMatchers +import org.mockito.ArgumentMatchers.any +import org.mockito.ArgumentMatchers.anyString +import org.mockito.Mockito.never +import org.mockito.Mockito.verify + +/** + * Unit test for [FollowSonePage]. + */ +class FollowSonePageTest : WebPageTest() { + + private val page = FollowSonePage(template, webInterface) + + @Test + fun `get request does not redirect`() { + request("", GET) + page.handleRequest(freenetRequest, templateContext) + } + + @Test + fun `a single sone can be followed`() { + request("", POST) + val sone = mock() + addSone("sone-id", sone) + addHttpRequestParameter("sone", "sone-id") + addHttpRequestParameter("returnPage", "return.html") + expectedException.expect(redirectsTo("return.html")) + try { + page.handleRequest(freenetRequest, templateContext) + } finally { + verify(core).followSone(currentSone, "sone-id") + verify(core).markSoneKnown(sone) + } + } + + @Test + fun `multiple sones can be followed`() { + request("", POST) + val firstSone = mock() + addSone("sone-id1", firstSone) + val secondSone = mock() + addSone("sone-id2", secondSone) + addHttpRequestParameter("sone", "sone-id1,sone-id2") + addHttpRequestParameter("returnPage", "return.html") + expectedException.expect(redirectsTo("return.html")) + try { + page.handleRequest(freenetRequest, templateContext) + } finally { + verify(core).followSone(currentSone, "sone-id1") + verify(core).followSone(currentSone, "sone-id2") + verify(core).markSoneKnown(firstSone) + verify(core).markSoneKnown(secondSone) + } + } + + @Test + fun `a non-existing sone is not followed`() { + request("", POST) + addHttpRequestParameter("sone", "sone-id") + addHttpRequestParameter("returnPage", "return.html") + expectedException.expect(redirectsTo("return.html")) + try { + page.handleRequest(freenetRequest, templateContext) + } finally { + verify(core, never()).followSone(ArgumentMatchers.eq(currentSone), anyString()) + verify(core, never()).markSoneKnown(any()) + } + } + +}