1 package net.pterodactylus.sone.web.pages
3 import net.pterodactylus.sone.data.Post
4 import net.pterodactylus.sone.test.capture
5 import net.pterodactylus.sone.test.mock
6 import net.pterodactylus.sone.test.whenever
7 import net.pterodactylus.util.web.Method.POST
8 import org.hamcrest.MatcherAssert.assertThat
9 import org.hamcrest.Matchers.contains
10 import org.hamcrest.Matchers.equalTo
12 import org.mockito.Mockito.any
13 import org.mockito.Mockito.never
14 import org.mockito.Mockito.times
15 import org.mockito.Mockito.verify
18 * Unit test for [UnbookmarkPage].
20 class UnbookmarkPageTest: WebPageTest(::UnbookmarkPage) {
23 fun `page returns correct path`() {
24 assertThat(page.path, equalTo("unbookmark.html"))
28 fun `page does not require login`() {
29 assertThat(page.requiresLogin(), equalTo(false))
33 fun `page returns correct title`() {
34 addTranslation("Page.Unbookmark.Title", "unbookmark page title")
35 assertThat(page.getPageTitle(freenetRequest), equalTo("unbookmark page title"))
39 fun `get request does not redirect`() {
44 fun `get request with all-not-loaded parameter unloads all not loaded posts and redirects to bookmarks`() {
45 addHttpRequestParameter("post", "allNotLoaded")
46 val loadedPost1 = mock<Post>().apply { whenever(isLoaded).thenReturn(true) }
47 val loadedPost2 = mock<Post>().apply { whenever(isLoaded).thenReturn(true) }
48 val notLoadedPost1 = mock<Post>()
49 val notLoadedPost2 = mock<Post>()
50 whenever(core.bookmarkedPosts).thenReturn(setOf(loadedPost1, loadedPost2, notLoadedPost1, notLoadedPost2))
51 verifyRedirect("bookmarks.html") {
52 val postCaptor = capture<Post>()
53 verify(core, times(2)).unbookmarkPost(postCaptor.capture())
54 assertThat(postCaptor.allValues, contains(notLoadedPost1, notLoadedPost2))
59 fun `post request does not unbookmark not-present post but redirects to return page`() {
61 addHttpRequestPart("post", "post-id")
62 addHttpRequestPart("returnPage", "return.html")
63 verifyRedirect("return.html") {
64 verify(core, never()).unbookmarkPost(any())
69 fun `post request unbookmarks present post and redirects to return page`() {
71 addHttpRequestPart("post", "post-id")
72 addHttpRequestPart("returnPage", "return.html")
73 val post = mock<Post>().apply { whenever(isLoaded).thenReturn(true) }
74 addPost("post-id", post)
75 verifyRedirect("return.html") {
76 verify(core).unbookmarkPost(post)