import static net.pterodactylus.sone.test.GuiceKt.supply;
import static net.pterodactylus.sone.web.WebTestUtils.redirectsTo;
+import static net.pterodactylus.util.web.Method.GET;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyInt;
@Before
public final void setupFreenetRequest() throws SizeLimitExceededException {
+ setMethod(GET);
when(freenetRequest.getToadletContext()).thenReturn(toadletContext);
when(freenetRequest.getHttpRequest()).thenReturn(httpRequest);
when(httpRequest.getMultipleParam(anyString())).thenAnswer(new Answer<String[]>() {
when(httpRequest.getMethod()).thenReturn(method.name());
}
- protected void request(String uri, Method method) {
+ protected void request(String uri) {
try {
when(httpRequest.getPath()).thenReturn(uri);
when(freenetRequest.getUri()).thenReturn(new URI(uri));
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
- when(freenetRequest.getMethod()).thenReturn(method);
- when(httpRequest.getMethod()).thenReturn(method.name());
}
protected void addHttpRequestHeader(@Nonnull String name, String value) {
}
private fun setupBookmarkRequest() {
- request("", POST)
+ setMethod(POST)
addHttpRequestPart("returnPage", "return-page.html")
addHttpRequestPart("post", "post-id")
}
@Test
fun `missing name results in attribute being set in template context`() {
- request("", POST)
+ setMethod(POST)
page.processTemplate(freenetRequest, templateContext)
assertThat(templateContext["nameMissing"], equalTo<Any>(true))
}
@Test
fun `title and description are set correctly on the album`() {
- request("", POST)
+ setMethod(POST)
addAlbum("parent-id", parentAlbum)
addHttpRequestPart("name", "new name")
addHttpRequestPart("description", "new description")
@Test
fun `root album is used if no parent is specified`() {
- request("", POST)
+ setMethod(POST)
addHttpRequestPart("name", "new name")
addHttpRequestPart("description", "new description")
verifyRedirect("imageBrowser.html?album=album-id")
@Test
fun `empty album title redirects to error page`() {
- request("", POST)
+ setMethod(POST)
whenever(newAlbum.modify().update()).thenThrow(AlbumTitleMustNotBeEmpty::class.java)
addHttpRequestPart("name", "new name")
addHttpRequestPart("description", "new description")
@Test
fun `album description is filtered`() {
- request("", POST)
+ setMethod(POST)
addHttpRequestPart("name", "new name")
addHttpRequestPart("description", "new http://localhost:12345/KSK@foo description")
addHttpRequestHeader("Host", "localhost:12345")
@Test
fun `post is created correctly`() {
- request("", POST)
+ setMethod(POST)
addHttpRequestPart("returnPage", "return.html")
addHttpRequestPart("text", "post text")
verifyRedirect("return.html") {
@Test
fun `creating an empty post is denied`() {
- request("", POST)
+ setMethod(POST)
addHttpRequestPart("returnPage", "return.html")
addHttpRequestPart("text", " ")
page.processTemplate(freenetRequest, templateContext)
@Test
fun `a sender can be selected`() {
- request("", POST)
+ setMethod(POST)
addHttpRequestPart("returnPage", "return.html")
addHttpRequestPart("text", "post text")
addHttpRequestPart("sender", "sender-id")
@Test
fun `a recipient can be selected`() {
- request("", POST)
+ setMethod(POST)
addHttpRequestPart("returnPage", "return.html")
addHttpRequestPart("text", "post text")
addHttpRequestPart("recipient", "recipient-id")
@Test
fun `text is filtered correctly`() {
- request("", POST)
+ setMethod(POST)
addHttpRequestPart("returnPage", "return.html")
addHttpRequestPart("text", "post http://localhost:12345/KSK@foo text")
addHttpRequestHeader("Host", "localhost:12345")
@Test
fun `reply is created correctly`() {
- request("", POST)
+ setMethod(POST)
addHttpRequestPart("returnPage", "return.html")
addHttpRequestPart("post", "post-id")
addHttpRequestPart("text", "new text")
@Test
fun `reply is filtered`() {
- request("", POST)
+ setMethod(POST)
addHttpRequestPart("returnPage", "return.html")
addHttpRequestPart("post", "post-id")
addHttpRequestPart("text", "new http://localhost:12345/KSK@foo text")
@Test
fun `reply is created with correct sender`() {
- request("", POST)
+ setMethod(POST)
addHttpRequestPart("returnPage", "return.html")
addHttpRequestPart("post", "post-id")
addHttpRequestPart("text", "new text")
@Test
fun `empty text sets parameters in template contexty`() {
- request("", POST)
+ setMethod(POST)
addHttpRequestPart("returnPage", "return.html")
addHttpRequestPart("post", "post-id")
addHttpRequestPart("text", " ")
@Test
fun `user is redirected to no permissions page if post does not exist`() {
- request("", POST)
+ setMethod(POST)
addHttpRequestPart("returnPage", "return.html")
addHttpRequestPart("post", "post-id")
addHttpRequestPart("text", "new text")
@Test
fun `sone is created and logged in`() {
addExistingOwnIdentities()
- request("", POST)
+ setMethod(POST)
addHttpRequestPart("identity", "own-id-3")
val newSone = mock<Sone>()
whenever(core.createSone(ownIdentities_[2])).thenReturn(newSone)
@Test
fun `on invalid identity id a flag is set in the template context`() {
- request("", POST)
+ setMethod(POST)
addHttpRequestParameter("identity", "own-id-3")
page.processTemplate(freenetRequest, templateContext)
assertThat(templateContext["errorNoIdentity"], equalTo<Any>(true))
@Test
fun `if sone is not created user is still redirected to index`() {
addExistingOwnIdentities()
- request("", POST)
+ setMethod(POST)
addHttpRequestPart("identity", "own-id-3")
whenever(core.createSone(ownIdentities_[2])).thenReturn(null)
verifyRedirect("index.html") {
import net.pterodactylus.sone.test.whenever
import net.pterodactylus.sone.web.pages.WebPageTest
import net.pterodactylus.sone.web.pages.DeleteAlbumPage
-import net.pterodactylus.util.web.Method.GET
import net.pterodactylus.util.web.Method.POST
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.equalTo
@Test
fun `get request with invalid album ID results in redirect to invalid page`() {
- request("", GET)
whenever(core.getAlbum(anyString())).thenReturn(null)
verifyRedirect("invalid.html")
}
@Test
fun `get request with valid album ID sets album in template context`() {
- request("", GET)
val album = mock<Album>()
addAlbum("album-id", album)
addHttpRequestParameter("album", "album-id")
@Test
fun `post request redirects to invalid page if album is invalid`() {
- request("", POST)
+ setMethod(POST)
verifyRedirect("invalid.html")
}
@Test
fun `post request redirects to no permissions page if album is not local`() {
- request("", POST)
+ setMethod(POST)
whenever(sone.isLocal).thenReturn(false)
addAlbum("album-id", album)
addHttpRequestPart("album", "album-id")
@Test
fun `post request with abort delete parameter set redirects to album browser`() {
- request("", POST)
+ setMethod(POST)
addAlbum("album-id", album)
addHttpRequestPart("album", "album-id")
addHttpRequestPart("abortDelete", "true")
@Test
fun `album is deleted and page redirects to sone if parent album is root album`() {
- request("", POST)
+ setMethod(POST)
addAlbum("album-id", album)
addHttpRequestPart("album", "album-id")
verifyRedirect("imageBrowser.html?sone=sone-id") {
@Test
fun `album is deleted and page redirects to album if parent album is not root album`() {
- request("", POST)
+ setMethod(POST)
whenever(parentAlbum.isRoot).thenReturn(false)
whenever(sone.rootAlbum).thenReturn(mock<Album>())
addAlbum("album-id", album)
import net.pterodactylus.sone.test.whenever
import net.pterodactylus.sone.web.pages.WebPageTest
import net.pterodactylus.sone.web.pages.DeleteImagePage
-import net.pterodactylus.util.web.Method.GET
import net.pterodactylus.util.web.Method.POST
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.equalTo
@Test
fun `get request with invalid image redirects to invalid page`() {
- request("", GET)
verifyRedirect("invalid.html")
}
@Test
fun `get request with image from non-local sone redirects to no permissions page`() {
- request("", GET)
whenever(sone.isLocal).thenReturn(false)
addImage("image-id", image)
addHttpRequestParameter("image", "image-id")
@Test
fun `get request with image from local sone sets image in template context`() {
- request("", GET)
addImage("image-id", image)
addHttpRequestParameter("image", "image-id")
page.processTemplate(freenetRequest, templateContext)
@Test
fun `post request with abort delete flag set redirects to image browser`() {
- request("", POST)
+ setMethod(POST)
addImage("image-id", image)
addHttpRequestPart("image", "image-id")
addHttpRequestPart("abortDelete", "true")
@Test
fun `post request deletes image and redirects to image browser`() {
- request("", POST)
+ setMethod(POST)
addImage("image-id", image)
addHttpRequestPart("image", "image-id")
verifyRedirect("imageBrowser.html?album=album-id") {
import net.pterodactylus.sone.test.whenever
import net.pterodactylus.sone.web.pages.WebPageTest
import net.pterodactylus.sone.web.pages.DeletePostPage
-import net.pterodactylus.util.web.Method.GET
import net.pterodactylus.util.web.Method.POST
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.equalTo
@Test
fun `get request with invalid post redirects to no permission page`() {
- request("", GET)
verifyRedirect("noPermission.html")
}
@Test
fun `get request with valid post sets post and return page in template context`() {
- request("", GET)
addPost("post-id", post)
addHttpRequestParameter("post", "post-id")
addHttpRequestParameter("returnPage", "return.html")
@Test
fun `post request with invalid post redirects to no permission page`() {
- request("", POST)
+ setMethod(POST)
verifyRedirect("noPermission.html")
}
@Test
fun `post request with post from non-local sone redirects to no permission page`() {
- request("", POST)
+ setMethod(POST)
whenever(sone.isLocal).thenReturn(false)
addPost("post-id", post)
addHttpRequestPart("post", "post-id")
@Test
fun `post request with confirmation deletes post and redirects to return page`() {
- request("", POST)
+ setMethod(POST)
addPost("post-id", post)
addHttpRequestPart("post", "post-id")
addHttpRequestPart("returnPage", "return.html")
@Test
fun `post request with abort delete does not delete post and redirects to return page`() {
- request("", POST)
+ setMethod(POST)
addPost("post-id", post)
addHttpRequestPart("post", "post-id")
addHttpRequestPart("returnPage", "return.html")
@Test
fun `post request without delete or abort sets post in template context`() {
- request("", POST)
+ setMethod(POST)
addPost("post-id", post)
addHttpRequestPart("post", "post-id")
addHttpRequestPart("returnPage", "return.html")
import net.pterodactylus.sone.test.whenever
import net.pterodactylus.sone.web.pages.WebPageTest
import net.pterodactylus.sone.web.pages.DeleteProfileFieldPage
-import net.pterodactylus.util.web.Method.GET
import net.pterodactylus.util.web.Method.POST
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.equalTo
@Test
fun `get request with invalid field name redirects to invalid page`() {
- request("", GET)
verifyRedirect("invalid.html")
}
@Test
fun `post request with invalid field name redirects to invalid page`() {
- request("", POST)
+ setMethod(POST)
addHttpRequestPart("field", "wrong-id")
verifyRedirect("invalid.html")
}
@Test
fun `get request with valid field name sets field in template context`() {
- request("", GET)
addHttpRequestParameter("field", field.id)
page.processTemplate(freenetRequest, templateContext)
assertThat(templateContext["field"], equalTo<Any>(field))
@Test
fun `post request without confirm redirects to edit profile page`() {
- request("", POST)
+ setMethod(POST)
addHttpRequestPart("field", field.id)
verifyRedirect("editProfile.html#profile-fields") {
verify(currentSone, never()).profile = any()
@Test
fun `post request with confirm removes field and redirects to edit profile page`() {
- request("", POST)
+ setMethod(POST)
addHttpRequestPart("field", field.id)
addHttpRequestPart("confirm", "true")
verifyRedirect("editProfile.html#profile-fields") {
import net.pterodactylus.sone.test.whenever
import net.pterodactylus.sone.web.pages.WebPageTest
import net.pterodactylus.sone.web.pages.DeleteReplyPage
-import net.pterodactylus.util.web.Method.GET
import net.pterodactylus.util.web.Method.POST
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.equalTo
@Test
fun `get request sets reply ID and return page in template context`() {
- request("", GET)
addHttpRequestParameter("reply", "reply-id")
addHttpRequestParameter("returnPage", "return.html")
page.processTemplate(freenetRequest, templateContext)
@Test
fun `post request without any action sets reply ID and return page in template context`() {
- request("", POST)
+ setMethod(POST)
addPostReply("reply-id", reply)
addHttpRequestPart("reply", "reply-id")
addHttpRequestPart("returnPage", "return.html")
@Test
fun `trying to delete a reply with an invalid ID results in no permission page`() {
- request("", POST)
+ setMethod(POST)
verifyRedirect("noPermission.html")
}
@Test
fun `trying to delete a reply from a non-local sone results in no permission page`() {
- request("", POST)
+ setMethod(POST)
addHttpRequestPart("reply", "reply-id")
whenever(sone.isLocal).thenReturn(false)
addPostReply("reply-id", reply)
@Test
fun `confirming deletion of reply deletes the reply and redirects to return page`() {
- request("", POST)
+ setMethod(POST)
addPostReply("reply-id", reply)
addHttpRequestPart("reply", "reply-id")
addHttpRequestPart("returnPage", "return.html")
@Test
fun `aborting deletion of reply redirects to return page`() {
- request("", POST)
+ setMethod(POST)
addPostReply("reply-id", reply)
addHttpRequestPart("reply", "reply-id")
addHttpRequestPart("returnPage", "return.html")
import net.pterodactylus.sone.test.whenever
import net.pterodactylus.sone.web.pages.WebPageTest
import net.pterodactylus.sone.web.pages.DeleteSonePage
-import net.pterodactylus.util.web.Method.GET
import net.pterodactylus.util.web.Method.POST
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.equalTo
@Test
fun `get request does not redirect`() {
- request("", GET)
page.processTemplate(freenetRequest, templateContext)
}
@Test
fun `post request without delete confirmation redirects to index`() {
- request("", POST)
+ setMethod(POST)
verifyRedirect("index.html") {
verify(core, never()).deleteSone(any())
}
@Test
fun `post request with delete confirmation deletes sone and redirects to index`() {
- request("", POST)
+ setMethod(POST)
addHttpRequestPart("deleteSone", "true")
verifyRedirect("index.html") {
verify(core).deleteSone(currentSone)
@Test
fun `get request with invalid notification ID redirects to return page`() {
- request("", POST)
+ setMethod(POST)
addHttpRequestPart("returnPage", "return.html")
verifyRedirect("return.html")
}
@Test
fun `get request with non-dismissible notification never dismisses the notification but redirects to return page`() {
- request("", POST)
+ setMethod(POST)
addNotification("notification-id", notification)
addHttpRequestPart("notification", "notification-id")
addHttpRequestPart("returnPage", "return.html")
@Test
fun `post request with dismissible notification dismisses the notification and redirects to return page`() {
- request("", POST)
+ setMethod(POST)
whenever(notification.isDismissable).thenReturn(true)
addNotification("notification-id", notification)
addHttpRequestPart("notification", "notification-id")
import net.pterodactylus.sone.test.whenever
import net.pterodactylus.sone.web.pages.WebPageTest
import net.pterodactylus.sone.web.pages.DistrustPage
-import net.pterodactylus.util.web.Method.GET
import net.pterodactylus.util.web.Method.POST
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.equalTo
@Test
fun `get request does not redirect`() {
- request("", GET)
page.processTemplate(freenetRequest, templateContext)
}
@Test
fun `post request with invalid sone redirects to return page`() {
- request("", POST)
+ setMethod(POST)
addHttpRequestPart("returnPage", "return.html")
verifyRedirect("return.html")
}
@Test
fun `post request with valid sone distrusts sone and redirects to return page`() {
- request("", POST)
+ setMethod(POST)
val remoteSone = mock<Sone>()
addSone("remote-sone-id", remoteSone)
addHttpRequestPart("returnPage", "return.html")
import net.pterodactylus.sone.test.whenever
import net.pterodactylus.sone.web.pages.WebPageTest
import net.pterodactylus.sone.web.pages.EditAlbumPage
-import net.pterodactylus.util.web.Method.GET
import net.pterodactylus.util.web.Method.POST
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.equalTo
@Test
fun `get request does not redirect`() {
- request("", GET)
page.processTemplate(freenetRequest, templateContext)
}
@Test
fun `post request with invalid album redirects to invalid page`() {
- request("", POST)
+ setMethod(POST)
verifyRedirect("invalid.html")
}
@Test
fun `post request with album of non-local sone redirects to no permissions page`() {
- request("", POST)
+ setMethod(POST)
whenever(sone.isLocal).thenReturn(false)
addAlbum("album-id", album)
addHttpRequestPart("album", "album-id")
@Test
fun `post request with move left requested moves album to the left and redirects to album browser`() {
- request("", POST)
+ setMethod(POST)
addAlbum("album-id", album)
addHttpRequestPart("album", "album-id")
addHttpRequestPart("moveLeft", "true")
@Test
fun `post request with move right requested moves album to the left and redirects to album browser`() {
- request("", POST)
+ setMethod(POST)
addAlbum("album-id", album)
addHttpRequestPart("album", "album-id")
addHttpRequestPart("moveRight", "true")
@Test
fun `post request with empty album title redirects to empty album title page`() {
- request("", POST)
+ setMethod(POST)
addAlbum("album-id", album)
addHttpRequestPart("album", "album-id")
whenever(modifier.setTitle("")).thenThrow(AlbumTitleMustNotBeEmpty())
@Test
fun `post request with non-empty album title and description redirects to album browser`() {
- request("", POST)
+ setMethod(POST)
addAlbum("album-id", album)
addHttpRequestPart("album", "album-id")
addHttpRequestPart("title", "title")
import net.pterodactylus.sone.test.mock
import net.pterodactylus.sone.test.mockBuilder
import net.pterodactylus.sone.test.whenever
-import net.pterodactylus.util.web.Method.GET
import net.pterodactylus.util.web.Method.POST
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.equalTo
@Test
fun `get request does not redirect`() {
- request("", GET)
page.processTemplate(freenetRequest, templateContext)
}
@Test
fun `post request with invalid image redirects to invalid page`() {
- request("", POST)
+ setMethod(POST)
verifyRedirect("invalid.html")
}
@Test
fun `post request with valid image from non-local sone redirects to no permission page`() {
- request("", POST)
+ setMethod(POST)
whenever(sone.isLocal).thenReturn(false)
addImage("image-id", image)
addHttpRequestPart("image", "image-id")
@Test
fun `post request with valid image and move left requested moves image left and redirects to return page`() {
- request("", POST)
+ setMethod(POST)
addImage("image-id", image)
addHttpRequestPart("image", "image-id")
addHttpRequestPart("returnPage", "return.html")
@Test
fun `post request with valid image and move right requested moves image right and redirects to return page`() {
- request("", POST)
+ setMethod(POST)
addImage("image-id", image)
addHttpRequestPart("image", "image-id")
addHttpRequestPart("returnPage", "return.html")
@Test
fun `post request with valid image but only whitespace in the title redirects to empty image title page`() {
- request("", POST)
+ setMethod(POST)
addImage("image-id", image)
addHttpRequestPart("image", "image-id")
addHttpRequestPart("returnPage", "return.html")
@Test
fun `post request with valid image title and description modifies image and redirects to reutrn page`() {
- request("", POST)
+ setMethod(POST)
addImage("image-id", image)
addHttpRequestPart("image", "image-id")
addHttpRequestPart("returnPage", "return.html")
@Test
fun `post request with image title and description modifies image with filtered description and redirects to return page`() {
- request("", POST)
+ setMethod(POST)
addImage("image-id", image)
addHttpRequestPart("image", "image-id")
addHttpRequestPart("returnPage", "return.html")
import net.pterodactylus.sone.test.whenever
import net.pterodactylus.sone.web.pages.EditProfileFieldPage
import net.pterodactylus.sone.web.pages.WebPageTest
-import net.pterodactylus.util.web.Method.GET
import net.pterodactylus.util.web.Method.POST
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.equalTo
@Test
fun `get request with invalid field redirects to invalid page`() {
- request("", GET)
verifyRedirect("invalid.html")
}
@Test
fun `get request with valid field stores field in template context`() {
- request("", GET)
addHttpRequestParameter("field", field.id)
page.processTemplate(freenetRequest, templateContext)
assertThat(templateContext["field"], equalTo<Any>(field))
@Test
fun `post request with cancel set redirects to profile edit page`() {
- request("", POST)
+ setMethod(POST)
addHttpRequestPart("field", field.id)
addHttpRequestPart("cancel", "true")
verifyRedirect("editProfile.html#profile-fields")
@Test
fun `post request with new name renames field and redirects to profile edit page`() {
- request("", POST)
+ setMethod(POST)
addHttpRequestPart("field", field.id)
addHttpRequestPart("name", "New Name")
verifyRedirect("editProfile.html#profile-fields") {
@Test
fun `post request with same name does not modify field and redirects to profile edit page`() {
- request("", POST)
+ setMethod(POST)
addHttpRequestPart("field", field.id)
addHttpRequestPart("name", "Name")
verifyRedirect("editProfile.html#profile-fields") {
@Test
fun `post request with same name as different field sets error condition in template`() {
- request("", POST)
+ setMethod(POST)
profile.addField("New Name")
addHttpRequestPart("field", field.id)
addHttpRequestPart("name", "New Name")
import net.pterodactylus.sone.test.whenever
import net.pterodactylus.sone.web.pages.EditProfilePage
import net.pterodactylus.sone.web.pages.WebPageTest
-import net.pterodactylus.util.web.Method.GET
import net.pterodactylus.util.web.Method.POST
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.contains
@Test
fun `get request stores fields of current sone’s profile in template context`() {
- request("", GET)
page.processTemplate(freenetRequest, templateContext)
assertThat(templateContext["firstName"], equalTo<Any>("First"))
assertThat(templateContext["middleName"], equalTo<Any>("Middle"))
@Test
fun `post request without any command stores fields of current sone’s profile in template context`() {
- request("", POST)
+ setMethod(POST)
page.processTemplate(freenetRequest, templateContext)
assertThat(templateContext["firstName"], equalTo<Any>("First"))
assertThat(templateContext["middleName"], equalTo<Any>("Middle"))
}
private fun <T> verifySingleFieldCanBeChanged(fieldName: String, newValue: T, expectedValue: T = newValue, fieldAccessor: () -> T) {
- request("", POST)
+ setMethod(POST)
addHttpRequestPart("save-profile", "true")
addHttpRequestPart(fieldName, newValue.toString())
verifyRedirect("editProfile.html") {
@Test
fun `adding a field with a duplicate name sets error in template context`() {
- request("", POST)
+ setMethod(POST)
profile.addField("new-field")
addHttpRequestPart("add-field", "true")
addHttpRequestPart("field-name", "new-field")
@Test
fun `adding a field with a new name sets adds field to profile and redirects to profile edit page`() {
- request("", POST)
+ setMethod(POST)
addHttpRequestPart("add-field", "true")
addHttpRequestPart("field-name", "new-field")
verifyRedirect("editProfile.html#profile-fields") {
@Test
fun `deleting a field redirects to delete field page`() {
- request("", POST)
+ setMethod(POST)
addHttpRequestPart("delete-field-${firstField.id}", "true")
verifyRedirect("deleteProfileField.html?field=${firstField.id}")
}
@Test
fun `moving a field up moves the field up and redirects to the edit profile page`() {
- request("", POST)
+ setMethod(POST)
addHttpRequestPart("move-up-field-${secondField.id}", "true")
verifyRedirect("editProfile.html#profile-fields") {
assertThat(profile.fields, contains(secondField, firstField))
@Test
fun `moving an invalid field up does not redirect`() {
- request("", POST)
+ setMethod(POST)
addHttpRequestPart("move-up-field-foo", "true")
page.processTemplate(freenetRequest, templateContext)
}
@Test
fun `moving a field down moves the field down and redirects to the edit profile page`() {
- request("", POST)
+ setMethod(POST)
addHttpRequestPart("move-down-field-${firstField.id}", "true")
verifyRedirect("editProfile.html#profile-fields") {
assertThat(profile.fields, contains(secondField, firstField))
@Test
fun `moving an invalid field down does not redirect`() {
- request("", POST)
+ setMethod(POST)
addHttpRequestPart("move-down-field-foo", "true")
page.processTemplate(freenetRequest, templateContext)
}
@Test
fun `editing a field redirects to the edit profile page`() {
- request("", POST)
+ setMethod(POST)
addHttpRequestPart("edit-field-${firstField.id}", "true")
verifyRedirect("editProfileField.html?field=${firstField.id}")
}
import net.pterodactylus.sone.data.Sone
import net.pterodactylus.sone.test.mock
import net.pterodactylus.sone.test.whenever
-import net.pterodactylus.util.web.Method.GET
import net.pterodactylus.util.web.Method.POST
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.equalTo
@Test
fun `get request does not redirect`() {
- request("", GET)
page.processTemplate(freenetRequest, templateContext)
}
@Test
fun `a single sone can be followed`() {
- request("", POST)
+ setMethod(POST)
val sone = mock<Sone>()
addSone("sone-id", sone)
addHttpRequestPart("sone", "sone-id")
@Test
fun `multiple sones can be followed`() {
- request("", POST)
+ setMethod(POST)
val firstSone = mock<Sone>()
addSone("sone-id1", firstSone)
val secondSone = mock<Sone>()
@Test
fun `a non-existing sone is not followed`() {
- request("", POST)
+ setMethod(POST)
addHttpRequestPart("sone", "sone-id")
addHttpRequestPart("returnPage", "return.html")
verifyRedirect("return.html") {
import net.pterodactylus.sone.data.Sone
import net.pterodactylus.sone.test.mock
import net.pterodactylus.sone.test.whenever
-import net.pterodactylus.util.web.Method.GET
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.contains
import org.hamcrest.Matchers.equalTo
@Test
fun `get request with album sets album and page in template context`() {
- request("", GET)
val album = mock<Album>()
addAlbum("album-id", album)
addHttpRequestParameter("album", "album-id")
@Test
fun `get request with image sets image in template context`() {
- request("", GET)
val image = mock<Image>()
addImage("image-id", image)
addHttpRequestParameter("image", "image-id")
@Test
fun `get request with sone sets sone in template context`() {
- request("", GET)
val sone = mock<Sone>()
addSone("sone-id", sone)
addHttpRequestParameter("sone", "sone-id")
@Test
fun `get request with mode of gallery sets albums and page in template context`() {
- request("", GET)
val firstSone = createSone("first album", "second album")
addSone("sone1", firstSone)
val secondSone = createSone("third album", "fourth album")
@Test
fun `requesting nothing will show the albums of the current sone`() {
- request("", GET)
page.processTemplate(freenetRequest, templateContext)
assertThat(templateContext["soneRequested"], equalTo<Any>(true))
assertThat(templateContext["sone"], equalTo<Any>(currentSone))
import net.pterodactylus.sone.test.mock
import net.pterodactylus.sone.test.whenever
import net.pterodactylus.sone.utils.Pagination
-import net.pterodactylus.util.web.Method.GET
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.contains
import org.hamcrest.Matchers.emptyIterable
fun `index page shows all posts of current sone`() {
val posts = listOf(createPost(3000), createPost(2000), createPost(1000))
whenever(currentSone.posts).thenReturn(posts)
- request("", GET)
page.processTemplate(freenetRequest, templateContext)
@Suppress("UNCHECKED_CAST")
assertThat(templateContext["posts"] as Iterable<Post>, contains(*posts.toTypedArray()))
val notFollowedPosts = listOf(createPost(2500, true), createPost(1500))
whenever(notFollowedSone.posts).thenReturn(notFollowedPosts)
addSone("notfollowed1", notFollowedSone)
- request("", GET)
whenever(core.getDirectedPosts("current")).thenReturn(listOf(notFollowedPosts[0]))
page.processTemplate(freenetRequest, templateContext)
@Suppress("UNCHECKED_CAST")
whenever(followedSone.posts).thenReturn(followedPosts)
whenever(currentSone.friends).thenReturn(listOf("followed1", "followed2"))
addSone("followed1", followedSone)
- request("", GET)
page.processTemplate(freenetRequest, templateContext)
@Suppress("UNCHECKED_CAST")
assertThat(templateContext["posts"] as Iterable<Post>, contains(
whenever(currentSone.friends).thenReturn(listOf("followed1", "followed2"))
whenever(postVisibilityFilter.isVisible(ArgumentMatchers.eq(currentSone))).thenReturn(Predicate<Post> { (it?.time ?: 10000) < 2500 })
addSone("followed1", followedSone)
- request("", GET)
page.processTemplate(freenetRequest, templateContext)
@Suppress("UNCHECKED_CAST")
assertThat(templateContext["posts"] as Iterable<Post>, contains(
fun `index page sets pagination correctly`() {
val posts = listOf(createPost(3000), createPost(2000), createPost(1000))
whenever(currentSone.posts).thenReturn(posts)
- request("", GET)
page.processTemplate(freenetRequest, templateContext)
@Suppress("UNCHECKED_CAST")
assertThat((templateContext["pagination"] as Pagination<Post>).items, contains(
fun `index page sets page correctly`() {
val posts = listOf(createPost(3000), createPost(2000), createPost(1000))
whenever(currentSone.posts).thenReturn(posts)
- request("", GET)
core.preferences.postsPerPage = 1
addHttpRequestParameter("page", "2")
page.processTemplate(freenetRequest, templateContext)
@Test
fun `index page without posts sets correct pagination`() {
- request("", GET)
core.preferences.postsPerPage = 1
page.processTemplate(freenetRequest, templateContext)
@Suppress("UNCHECKED_CAST")
import net.pterodactylus.sone.test.mock
import net.pterodactylus.sone.test.whenever
import net.pterodactylus.sone.utils.Pagination
-import net.pterodactylus.util.web.Method.GET
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.contains
import org.hamcrest.Matchers.equalTo
addSone("sone4", sones[3])
}
- @Before
- fun setupGetRequests() {
- setMethod(GET)
- }
-
private fun createSone(time: Long, posts: Int, replies: Int, images: Int, name: String, local: Boolean, new: Boolean) = mock<Sone>().apply {
whenever(identity).thenReturn(if (local) mock<OwnIdentity>() else mock<Identity>())
whenever(this.isLocal).thenReturn(local)
package net.pterodactylus.sone.web.pages
-import net.pterodactylus.util.web.Method.GET
import net.pterodactylus.util.web.Method.POST
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.equalTo
@Test
fun `get request does not redirect`() {
- request("", GET)
verifyNoRedirect {}
}
@Test
fun `post request with post id likes post and redirects to return page`() {
- request("", POST)
+ setMethod(POST)
addHttpRequestPart("type", "post")
addHttpRequestPart("post", "post-id")
addHttpRequestPart("returnPage", "return.html")
@Test
fun `post request with reply id likes post and redirects to return page`() {
- request("", POST)
+ setMethod(POST)
addHttpRequestPart("type", "reply")
addHttpRequestPart("reply", "reply-id")
addHttpRequestPart("returnPage", "return.html")
@Test
fun `post request with invalid likes redirects to return page`() {
- request("", POST)
+ setMethod(POST)
addHttpRequestPart("type", "foo")
addHttpRequestPart("returnPage", "return.html")
verifyRedirect("return.html") {
import net.pterodactylus.sone.test.whenever
import net.pterodactylus.sone.web.pages.WebPageTest
import net.pterodactylus.sone.web.pages.LoginPage
-import net.pterodactylus.util.web.Method.GET
import net.pterodactylus.util.web.Method.POST
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.contains
@Test
@Suppress("UNCHECKED_CAST")
fun `get request stores sones in template context`() {
- request("", GET)
page.processTemplate(freenetRequest, templateContext)
assertThat(templateContext["sones"] as Iterable<Sone>, containsInAnyOrder(sones[0], sones[1], sones[2]))
}
@Test
@Suppress("UNCHECKED_CAST")
fun `get request stores identities without sones in template context`() {
- request("", GET)
page.processTemplate(freenetRequest, templateContext)
assertThat(templateContext["identitiesWithoutSone"] as Iterable<Identity>, contains(sones[1].identity))
}
@Test
@Suppress("UNCHECKED_CAST")
fun `post request with invalid sone sets sones and identities without sone in template context`() {
- request("", POST)
+ setMethod(POST)
page.processTemplate(freenetRequest, templateContext)
assertThat(templateContext["sones"] as Iterable<Sone>, containsInAnyOrder(sones[0], sones[1], sones[2]))
assertThat(templateContext["identitiesWithoutSone"] as Iterable<Identity>, contains(sones[1].identity))
@Test
fun `post request with valid sone logs in the sone and redirects to index page`() {
- request("", POST)
+ setMethod(POST)
addHttpRequestPart("sone-id", "sone2")
verifyRedirect("index.html") {
verify(webInterface).setCurrentSone(toadletContext, sones[1])
@Test
fun `post request with valid sone and target redirects to target page`() {
- request("", POST)
+ setMethod(POST)
addHttpRequestPart("sone-id", "sone2")
addHttpRequestParameter("target", "foo.html")
verifyRedirect("foo.html") {
import net.pterodactylus.sone.test.mock
import net.pterodactylus.sone.test.whenever
import net.pterodactylus.sone.utils.Pagination
-import net.pterodactylus.util.web.Method.GET
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.contains
import org.hamcrest.Matchers.containsInAnyOrder
@Test
fun `posts are not duplicated when they come from both new posts and new replies notifications`() {
- setMethod(GET)
val extraPost = mock<Post>().withTime(2000)
val posts = asList(mock<Post>().withTime(1000), mock<Post>().withTime(3000))
val postReplies = asList(mock<PostReply>(), mock<PostReply>())
@Test
@Suppress("UNCHECKED_CAST")
fun `posts are paginated properly`() {
- setMethod(GET)
webInterface.core.preferences.postsPerPage = 2
val posts = listOf(mock<Post>().withTime(2000), mock<Post>().withTime(3000), mock<Post>().withTime(1000))
whenever(webInterface.getNewPosts(currentSone)).thenReturn(posts)
@Test
@Suppress("UNCHECKED_CAST")
fun `posts are paginated properly on second page`() {
- setMethod(GET)
webInterface.core.preferences.postsPerPage = 2
addHttpRequestParameter("page", "1")
val posts = listOf(mock<Post>().withTime(2000), mock<Post>().withTime(3000), mock<Post>().withTime(1000))
import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired.WRITING
import net.pterodactylus.sone.test.whenever
import net.pterodactylus.sone.web.pages.OptionsPage
-import net.pterodactylus.util.web.Method.GET
import net.pterodactylus.util.web.Method.POST
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.equalTo
@Test
fun `get request stores all preferences in the template context`() {
- request("", GET)
page.handleRequest(freenetRequest, templateContext)
assertThat(templateContext["auto-follow"], equalTo<Any>(true))
assertThat(templateContext["show-notification-new-sones"], equalTo<Any>(true))
@Test
fun `get request without sone does not store sone-specific preferences in the template context`() {
- request("", GET)
unsetCurrentSone()
page.handleRequest(freenetRequest, templateContext)
assertThat(templateContext["auto-follow"], nullValue())
}
private fun <T> verifyThatOptionCanBeSet(option: String, setValue: Any?, expectedValue: T, getter: () -> T) {
- request("", POST)
+ setMethod(POST)
addHttpRequestPart("show-custom-avatars", "ALWAYS")
addHttpRequestPart("load-linked-images", "ALWAYS")
addHttpRequestPart(option, setValue.toString())
private fun verifyThatWrongValueForPreferenceIsDetected(name: String, value: String) {
unsetCurrentSone()
- request("", POST)
+ setMethod(POST)
addHttpRequestPart(name, value)
page.handleRequest(freenetRequest, templateContext)
assertThat(templateContext["fieldErrors"] as Iterable<*>, hasItem(name))
private fun <T> verifyThatPreferencesCanBeSet(name: String, setValue: String?, expectedValue: T, getter: () -> T) {
unsetCurrentSone()
- request("", POST)
+ setMethod(POST)
addHttpRequestPart(name, setValue)
verifyRedirect("options.html") {
assertThat(getter(), equalTo(expectedValue))
import net.pterodactylus.sone.web.pages.ReloadingPage
import net.pterodactylus.sone.web.page.FreenetRequest
-import net.pterodactylus.util.web.Method.GET
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.equalTo
import org.junit.Rule
@Test
fun `requesting invalid file results in 404`() {
- request("/prefix/path/file.txt", GET)
+ request("/prefix/path/file.txt")
page.handleRequest(freenetRequest, response)
assertThat(response.statusCode, equalTo(404))
}
@Test
fun `requesting valid file results in 200 and delivers file`() {
Files.write(Paths.get(folder.path, "file.txt"), listOf("Hello", "World"), UTF_8)
- request("/prefix/path/file.txt", GET)
+ request("/prefix/path/file.txt")
page.handleRequest(freenetRequest, response)
assertThat(response.statusCode, equalTo(200))
assertThat(response.contentType, equalTo("text/plain"))
import net.pterodactylus.sone.test.mock
import net.pterodactylus.sone.test.whenever
import net.pterodactylus.sone.web.pages.RescuePage
-import net.pterodactylus.util.web.Method.GET
import net.pterodactylus.util.web.Method.POST
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.equalTo
@Test
fun `get request sets rescuer in template context`() {
- request("", GET)
page.handleRequest(freenetRequest, templateContext)
assertThat(templateContext["soneRescuer"], equalTo<Any>(soneRescuer))
}
@Test
fun `post request redirects to rescue page`() {
- request("", POST)
+ setMethod(POST)
verifyRedirect("rescue.html")
}
@Test
fun `post request with fetch and invalid edition starts next fetch`() {
- request("", POST)
+ setMethod(POST)
addHttpRequestPart("fetch", "true")
verifyRedirect("rescue.html") {
verify(soneRescuer, never()).setEdition(anyLong())
@Test
fun `post request with fetch and valid edition sets edition and starts next fetch`() {
- request("", POST)
+ setMethod(POST)
addHttpRequestPart("fetch", "true")
addHttpRequestPart("edition", "123")
verifyRedirect("rescue.html") {
import net.pterodactylus.util.notify.Notification
import net.pterodactylus.util.template.TemplateContext
import net.pterodactylus.util.version.Version
-import net.pterodactylus.util.web.Method.GET
import org.hamcrest.Matcher
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.anyOf
@Test
fun `redirect does happen if sone is not logged in`() {
unsetCurrentSone()
- request("index.html", GET)
+ request("index.html")
assertThat(page.getRedirectTarget(freenetRequest), equalTo("login.html?target=index.html"))
}
@Test
fun `redirect does happen with parameters encoded correctly if sone is not logged in`() {
unsetCurrentSone()
- request("index.html", GET)
+ request("index.html")
addHttpRequestParameter("foo", "b=r")
addHttpRequestParameter("baz", "q&o")
assertThat(page.getRedirectTarget(freenetRequest), anyOf(
@Test
fun `post request with missing sone redirects to return page`() {
- request("", POST)
+ setMethod(POST)
addHttpRequestPart("returnPage", "return.html")
addHttpRequestPart("sone", "sone-id")
verifyRedirect("return.html") {
@Test
fun `post request with existing sone trusts the identity and redirects to return page`() {
- request("", POST)
+ setMethod(POST)
addHttpRequestPart("returnPage", "return.html")
addHttpRequestPart("sone", "sone-id")
val sone = mock<Sone>()
@Test
fun `post request does not unbookmark not-present post but redirects to return page`() {
- request("", POST)
+ setMethod(POST)
addHttpRequestPart("post", "post-id")
addHttpRequestPart("returnPage", "return.html")
verifyRedirect("return.html") {
@Test
fun `post request unbookmarks present post and redirects to return page`() {
- request("", POST)
+ setMethod(POST)
addHttpRequestPart("post", "post-id")
addHttpRequestPart("returnPage", "return.html")
val post = mock<Post>().apply { whenever(isLoaded).thenReturn(true) }
@Test
fun `post request unfollows a single sone and redirects to return page`() {
- request("", POST)
+ setMethod(POST)
addHttpRequestPart("returnPage", "return.html")
addHttpRequestPart("sone", "sone-id")
verifyRedirect("return.html") {
@Test
fun `post request unfollows two sones and redirects to return page`() {
- request("", POST)
+ setMethod(POST)
addHttpRequestPart("returnPage", "return.html")
addHttpRequestPart("sone", "sone-id1, sone-id2")
verifyRedirect("return.html") {
@Test
fun `post request does not remove any likes but redirects`() {
- request("", POST)
+ setMethod(POST)
addHttpRequestPart("returnPage", "return.html")
verifyRedirect("return.html") {
verify(currentSone, never()).removeLikedPostId(any())
@Test
fun `post request removes post like and redirects`() {
- request("", POST)
+ setMethod(POST)
addHttpRequestPart("returnPage", "return.html")
addHttpRequestPart("type", "post")
addHttpRequestPart("id", "post-id")
@Test
fun `post request removes reply like and redirects`() {
- request("", POST)
+ setMethod(POST)
addHttpRequestPart("returnPage", "return.html")
addHttpRequestPart("type", "reply")
addHttpRequestPart("id", "reply-id")
@Test
fun `post request without sone redirects to return page`() {
- request("", POST)
+ setMethod(POST)
addHttpRequestPart("returnPage", "return.html")
verifyRedirect("return.html") {
verify(core, never()).unlockSone(any())
@Test
fun `post request without invalid local sone does not unlock any sone and redirects to return page`() {
- request("", POST)
+ setMethod(POST)
addHttpRequestPart("returnPage", "return.html")
addHttpRequestPart("sone", "invalid-sone")
verifyRedirect("return.html") {
@Test
fun `post request without remote sone does not unlock any sone and redirects to return page`() {
- request("", POST)
+ setMethod(POST)
addHttpRequestPart("returnPage", "return.html")
addHttpRequestPart("sone", "remote-sone")
addSone("remote-sone", mock<Sone>())
@Test
fun `post request with local sone unlocks sone and redirects to return page`() {
- request("", POST)
+ setMethod(POST)
addHttpRequestPart("returnPage", "return.html")
addHttpRequestPart("sone", "local-sone")
val sone = mock<Sone>().apply { whenever(isLocal).thenReturn(true) }
@Test
fun `post request without sone parameter does not untrust but redirects`() {
- request("", POST)
+ setMethod(POST)
addHttpRequestPart("returnPage", "return.html")
verifyRedirect("return.html") {
verify(core, never()).untrustSone(eq(currentSone), any())
@Test
fun `post request with invalid sone parameter does not untrust but redirects`() {
- request("", POST)
+ setMethod(POST)
addHttpRequestPart("returnPage", "return.html")
addHttpRequestPart("sone", "no-sone")
verifyRedirect("return.html") {
@Test
fun `post request with valid sone parameter untrusts and redirects`() {
- request("", POST)
+ setMethod(POST)
addHttpRequestPart("returnPage", "return.html")
addHttpRequestPart("sone", "sone-id")
val sone = mock<Sone>()
@Test
fun `post request without parent results in no permission error page`() {
- request("", POST)
+ setMethod(POST)
verifyRedirect("noPermission.html")
}
@Test
fun `post request with parent that is not the current sone results in no permission error page`() {
- request("", POST)
+ setMethod(POST)
addHttpRequestPart("parent", "parent-id")
whenever(parentAlbum.sone).thenReturn(mock<Sone>())
addAlbum("parent-id", parentAlbum)
@Test
fun `post request with empty name redirects to error page`() {
- request("", POST)
+ setMethod(POST)
addAlbum("parent-id", parentAlbum)
addHttpRequestPart("parent", "parent-id")
addHttpRequestPart("title", " ")
@Test
fun `uploading an invalid image results in no redirect and message set in template context`() {
- request("", POST)
+ setMethod(POST)
addAlbum("parent-id", parentAlbum)
addHttpRequestPart("parent", "parent-id")
addHttpRequestPart("title", "title")
@Test
fun `uploading a valid image uploads image and redirects to album browser`() {
- request("", POST)
+ setMethod(POST)
addAlbum("parent-id", parentAlbum)
addHttpRequestPart("parent", "parent-id")
addHttpRequestPart("title", "Title")