1 package net.pterodactylus.sone.web.pages
3 import net.pterodactylus.sone.data.Post
4 import net.pterodactylus.sone.data.PostReply
5 import net.pterodactylus.sone.data.Profile
6 import net.pterodactylus.sone.data.Sone
7 import net.pterodactylus.sone.test.isOnPage
8 import net.pterodactylus.sone.test.mock
9 import net.pterodactylus.sone.test.whenever
10 import net.pterodactylus.sone.utils.Pagination
11 import net.pterodactylus.sone.utils.asOptional
12 import org.hamcrest.MatcherAssert.assertThat
13 import org.hamcrest.Matchers.contains
14 import org.hamcrest.Matchers.equalTo
15 import org.hamcrest.Matchers.nullValue
16 import org.junit.Before
20 * Unit test for [ViewSonePage].
22 class ViewSonePageTest: WebPageTest2(::ViewSonePage) {
25 whenever(currentSone.id).thenReturn("sone-id")
28 private val post1 = createPost("post1", "First Post.", 1000, currentSone)
29 private val post2 = createPost("post2", "Second Post.", 2000, currentSone)
30 private val foreignPost1 = createPost("foreign-post1", "First Foreign Post.", 1000, mock<Sone>())
31 private val foreignPost2 = createPost("foreign-post2", "Second Foreign Post.", 2000, mock<Sone>())
32 private val foreignPost3 = createPost("foreign-post3", "Third Foreign Post.", 3000, mock<Sone>())
33 private val directed1 = createPost("post3", "First directed.", 1500, mock<Sone>(), recipient = currentSone)
34 private val directed2 = createPost("post4", "Second directed.", 2500, mock<Sone>(), recipient = currentSone)
38 whenever(currentSone.posts).thenReturn(mutableListOf(post2, post1))
39 whenever(core.getDirectedPosts("sone-id")).thenReturn(setOf(directed1, directed2))
40 core.preferences.postsPerPage = 2
44 fun `page returns correct path`() {
45 assertThat(page.path, equalTo("viewSone.html"))
49 fun `page does not require login`() {
50 assertThat(page.requiresLogin(), equalTo(false))
54 fun `get request without sone parameter stores null in template context`() {
56 assertThat(templateContext["sone"], nullValue())
57 assertThat(templateContext["soneId"], equalTo<Any>(""))
62 fun `get request with invalid sone parameter stores null in template context`() {
63 addHttpRequestParameter("sone", "invalid-sone-id")
65 assertThat(templateContext["sone"], nullValue())
66 assertThat(templateContext["soneId"], equalTo<Any>("invalid-sone-id"))
71 fun `get request with valid sone parameter stores sone in template context`() {
72 whenever(currentSone.posts).thenReturn(mutableListOf())
73 whenever(core.getDirectedPosts("sone-id")).thenReturn(emptyList())
74 addHttpRequestParameter("sone", "sone-id")
75 addSone("sone-id", currentSone)
77 assertThat(templateContext["sone"], equalTo<Any>(currentSone))
78 assertThat(templateContext["soneId"], equalTo<Any>("sone-id"))
82 private fun createPost(id: String, text: String, time: Long, sender: Sone? = null, recipient: Sone? = null) = mock<Post>().apply {
83 whenever(this.id).thenReturn(id)
84 sender?.run { whenever(this@apply.sone).thenReturn(this) }
85 val recipientId = recipient?.id
86 whenever(this.recipientId).thenReturn(recipientId.asOptional())
87 whenever(this.recipient).thenReturn(recipient.asOptional())
88 whenever(this.time).thenReturn(time)
89 whenever(this.text).thenReturn(text)
93 @Suppress("UNCHECKED_CAST")
94 fun `request with valid sone stores posts and directed posts in template context`() {
95 addSone("sone-id", currentSone)
96 addHttpRequestParameter("sone", "sone-id")
98 assertThat(templateContext["posts"] as Iterable<Post>, contains(directed2, post2))
99 assertThat(templateContext["postPagination"] as Pagination<Post>, isOnPage(0).hasPages(2))
104 @Suppress("UNCHECKED_CAST")
105 fun `second page of posts is shown correctly`() {
106 addSone("sone-id", currentSone)
107 addHttpRequestParameter("sone", "sone-id")
108 addHttpRequestParameter("postPage", "1")
110 assertThat(templateContext["posts"] as Iterable<Post>, contains(directed1, post1))
111 assertThat(templateContext["postPagination"] as Pagination<Post>, isOnPage(1).hasPages(2))
115 private fun createReply(text: String, time: Long, post: Post?) = mock<PostReply>().apply {
116 whenever(this.text).thenReturn(text)
117 whenever(this.time).thenReturn(time)
118 whenever(this.post).thenReturn(post.asOptional())
122 @Suppress("UNCHECKED_CAST")
123 fun `replies are shown correctly`() {
124 val reply1 = createReply("First Reply", 1500, foreignPost1)
125 val reply2 = createReply("Second Reply", 2500, foreignPost2)
126 val reply3 = createReply("Third Reply", 1750, post1)
127 val reply4 = createReply("Fourth Reply", 2250, post2)
128 val reply5 = createReply("Fifth Reply", 1600, post1)
129 val reply6 = createReply("Sixth Reply", 2100, directed1)
130 val reply7 = createReply("Seventh Reply", 2200, null)
131 val reply8 = createReply("Eigth Reply", 2300, foreignPost1)
132 val reply9 = createReply("Ninth Reply", 2050, foreignPost3)
133 whenever(currentSone.replies).thenReturn(setOf(reply1, reply2, reply3, reply4, reply5, reply6, reply7, reply8, reply9))
134 whenever(core.getReplies("post1")).thenReturn(listOf(reply3, reply5))
135 whenever(core.getReplies("post2")).thenReturn(listOf(reply4))
136 whenever(core.getReplies("foreign-post1")).thenReturn(listOf(reply8, reply1))
137 whenever(core.getReplies("foreign-post2")).thenReturn(listOf(reply2))
138 whenever(core.getReplies("post3")).thenReturn(listOf(reply6))
139 whenever(core.getReplies("foreign-post3")).thenReturn(listOf(reply9))
140 addSone("sone-id", currentSone)
141 addHttpRequestParameter("sone", "sone-id")
143 assertThat(templateContext["repliedPosts"] as Iterable<Post>, contains(foreignPost2, foreignPost1))
144 assertThat(templateContext["repliedPostPagination"] as Pagination<Post>, isOnPage(0).hasPages(2))
149 @Suppress("UNCHECKED_CAST")
150 fun `second page of replies is shown correctly`() {
151 val reply1 = createReply("First Reply", 1500, foreignPost1)
152 val reply2 = createReply("Second Reply", 2500, foreignPost2)
153 val reply3 = createReply("Third Reply", 1750, post1)
154 val reply4 = createReply("Fourth Reply", 2250, post2)
155 val reply5 = createReply("Fifth Reply", 1600, post1)
156 val reply6 = createReply("Sixth Reply", 2100, directed1)
157 val reply7 = createReply("Seventh Reply", 2200, null)
158 val reply8 = createReply("Eigth Reply", 2300, foreignPost1)
159 val reply9 = createReply("Ninth Reply", 2050, foreignPost3)
160 whenever(currentSone.replies).thenReturn(setOf(reply1, reply2, reply3, reply4, reply5, reply6, reply7, reply8, reply9))
161 whenever(core.getReplies("post1")).thenReturn(listOf(reply3, reply5))
162 whenever(core.getReplies("post2")).thenReturn(listOf(reply4))
163 whenever(core.getReplies("foreign-post1")).thenReturn(listOf(reply8, reply1))
164 whenever(core.getReplies("foreign-post2")).thenReturn(listOf(reply2))
165 whenever(core.getReplies("post3")).thenReturn(listOf(reply6))
166 whenever(core.getReplies("foreign-post3")).thenReturn(listOf(reply9))
167 addSone("sone-id", currentSone)
168 addHttpRequestParameter("sone", "sone-id")
169 addHttpRequestParameter("repliedPostPage", "1")
171 assertThat(templateContext["repliedPosts"] as Iterable<Post>, contains(foreignPost3))
172 assertThat(templateContext["repliedPostPagination"] as Pagination<Post>, isOnPage(1).hasPages(2))
177 fun `page title is default for request without parameters`() {
178 addTranslation("Page.ViewSone.Page.TitleWithoutSone", "view sone page without sone")
179 assertThat(page.getPageTitle(freenetRequest), equalTo("view sone page without sone"))
183 fun `page title is default for request with invalid sone parameters`() {
184 addHttpRequestParameter("sone", "invalid-sone-id")
185 addTranslation("Page.ViewSone.Page.TitleWithoutSone", "view sone page without sone")
186 assertThat(page.getPageTitle(freenetRequest), equalTo("view sone page without sone"))
190 fun `page title contains sone name for request with sone parameters`() {
191 addHttpRequestParameter("sone", "sone-id")
192 addSone("sone-id", currentSone)
193 whenever(currentSone.profile).thenReturn(Profile(currentSone).apply {
198 addTranslation("Page.ViewSone.Title", "view sone page")
199 assertThat(page.getPageTitle(freenetRequest), equalTo("First M. Last - view sone page"))
203 fun `page is link-excepted`() {
204 assertThat(page.isLinkExcepted(null), equalTo(true))