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.mock
8 import net.pterodactylus.sone.test.whenever
9 import net.pterodactylus.sone.utils.asOptional
10 import org.hamcrest.MatcherAssert.assertThat
11 import org.hamcrest.Matchers.contains
12 import org.hamcrest.Matchers.equalTo
13 import org.hamcrest.Matchers.nullValue
14 import org.junit.Before
18 * Unit test for [ViewSonePage].
20 class ViewSonePageTest : WebPageTest() {
23 whenever(currentSone.id).thenReturn("sone-id")
26 private val page = ViewSonePage(template, webInterface)
27 private val post1 = createPost("post1", "First Post.", 1000, currentSone)
28 private val post2 = createPost("post2", "Second Post.", 2000, currentSone)
29 private val foreignPost1 = createPost("foreign-post1", "First Foreign Post.", 1000, mock<Sone>())
30 private val foreignPost2 = createPost("foreign-post2", "Second Foreign Post.", 2000, mock<Sone>())
31 private val directed1 = createPost("post3", "First directed.", 1500, mock<Sone>(), recipient = currentSone)
32 private val directed2 = createPost("post4", "Second directed.", 2500, mock<Sone>(), recipient = currentSone)
34 override fun getPage() = page
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 `get request without sone parameter stores null in template context`() {
46 assertThat(templateContext["sone"], nullValue())
47 assertThat(templateContext["soneId"], equalTo<Any>(""))
52 fun `get request with invalid sone parameter stores null in template context`() {
53 addHttpRequestParameter("sone", "invalid-sone-id")
55 assertThat(templateContext["sone"], nullValue())
56 assertThat(templateContext["soneId"], equalTo<Any>("invalid-sone-id"))
61 fun `get request with valid sone parameter stores sone in template context`() {
62 whenever(currentSone.posts).thenReturn(mutableListOf())
63 whenever(core.getDirectedPosts("sone-id")).thenReturn(emptyList())
64 addHttpRequestParameter("sone", "sone-id")
65 addSone("sone-id", currentSone)
67 assertThat(templateContext["sone"], equalTo<Any>(currentSone))
68 assertThat(templateContext["soneId"], equalTo<Any>("sone-id"))
72 private fun createPost(id: String, text: String, time: Long, sender: Sone? = null, recipient: Sone? = null) = mock<Post>().apply {
73 whenever(this.id).thenReturn(id)
74 sender?.run { whenever(this@apply.sone).thenReturn(this) }
75 val recipientId = recipient?.id
76 whenever(this.recipientId).thenReturn(recipientId.asOptional())
77 whenever(this.recipient).thenReturn(recipient.asOptional())
78 whenever(this.time).thenReturn(time)
79 whenever(this.text).thenReturn(text)
83 @Suppress("UNCHECKED_CAST")
84 fun `request with valid sone stores posts and directed posts in template context`() {
85 addSone("sone-id", currentSone)
86 addHttpRequestParameter("sone", "sone-id")
88 assertThat(templateContext["posts"] as Iterable<Post>, contains(directed2, post2))
93 @Suppress("UNCHECKED_CAST")
94 fun `second page of posts is shown correctly`() {
95 addSone("sone-id", currentSone)
96 addHttpRequestParameter("sone", "sone-id")
97 addHttpRequestParameter("postPage", "1")
99 assertThat(templateContext["posts"] as Iterable<Post>, contains(directed1, post1))
103 private fun createReply(text: String, time: Long, post: Post?) = mock<PostReply>().apply {
104 whenever(this.text).thenReturn(text)
105 whenever(this.time).thenReturn(time)
106 whenever(this.post).thenReturn(post.asOptional())
110 @Suppress("UNCHECKED_CAST")
111 fun `replies are shown correctly`() {
112 val reply1 = createReply("First Reply", 1500, foreignPost1)
113 val reply2 = createReply("Second Reply", 2500, foreignPost2)
114 val reply3 = createReply("Third Reply", 1750, post1)
115 val reply4 = createReply("Fourth Reply", 2250, post2)
116 val reply5 = createReply("Fifth Reply", 1600, post1)
117 val reply6 = createReply("Sixth Reply", 2100, directed1)
118 val reply7 = createReply("Seventh Reply", 2200, null)
119 val reply8 = createReply("Eigth Reply", 2300, foreignPost1)
120 whenever(currentSone.replies).thenReturn(setOf(reply1, reply2, reply3, reply4, reply5, reply6, reply7, reply8))
121 whenever(core.getReplies("post1")).thenReturn(listOf(reply3, reply5))
122 whenever(core.getReplies("post2")).thenReturn(listOf(reply4))
123 whenever(core.getReplies("foreign-post1")).thenReturn(listOf(reply1))
124 whenever(core.getReplies("foreign-post2")).thenReturn(listOf(reply2))
125 whenever(core.getReplies("post3")).thenReturn(listOf(reply6))
126 addSone("sone-id", currentSone)
127 addHttpRequestParameter("sone", "sone-id")
129 assertThat(templateContext["repliedPosts"] as Iterable<Post>, contains(foreignPost2, foreignPost1))
134 fun `page title is default for request without parameters`() {
135 assertThat(page.getPageTitle(freenetRequest), equalTo("Page.ViewSone.Page.TitleWithoutSone"))
139 fun `page title is default for request with invalid sone parameters`() {
140 addHttpRequestParameter("sone", "invalid-sone-id")
141 assertThat(page.getPageTitle(freenetRequest), equalTo("Page.ViewSone.Page.TitleWithoutSone"))
145 fun `page title contains sone name for request with sone parameters`() {
146 addHttpRequestParameter("sone", "sone-id")
147 addSone("sone-id", currentSone)
148 whenever(currentSone.profile).thenReturn(Profile(currentSone).apply {
153 assertThat(page.getPageTitle(freenetRequest), equalTo("First M. Last - Page.ViewSone.Title"))
157 fun `page is link-excepted`() {
158 assertThat(page.isLinkExcepted(null), equalTo(true))