1 package net.pterodactylus.sone.template
3 import net.pterodactylus.sone.core.Core
4 import net.pterodactylus.sone.data.Profile
5 import net.pterodactylus.sone.data.Sone
6 import net.pterodactylus.sone.data.SoneOptions.DefaultSoneOptions
7 import net.pterodactylus.sone.data.SoneOptions.LoadExternalContent.ALWAYS
8 import net.pterodactylus.sone.data.SoneOptions.LoadExternalContent.FOLLOWED
9 import net.pterodactylus.sone.data.SoneOptions.LoadExternalContent.MANUALLY_TRUSTED
10 import net.pterodactylus.sone.data.SoneOptions.LoadExternalContent.NEVER
11 import net.pterodactylus.sone.data.SoneOptions.LoadExternalContent.TRUSTED
12 import net.pterodactylus.sone.freenet.wot.Identity
13 import net.pterodactylus.sone.freenet.wot.OwnIdentity
14 import net.pterodactylus.sone.freenet.wot.Trust
15 import net.pterodactylus.sone.test.mock
16 import net.pterodactylus.sone.test.whenever
17 import net.pterodactylus.util.template.TemplateContext
18 import org.hamcrest.MatcherAssert.assertThat
19 import org.hamcrest.Matchers.equalTo
20 import org.hamcrest.Matchers.nullValue
21 import org.junit.Before
23 import org.mockito.ArgumentMatchers.anyBoolean
24 import org.mockito.ArgumentMatchers.eq
27 * Unit test for [ProfileAccessor].
29 class ProfileAccessorTest {
31 private val core = mock<Core>()
32 private val accessor = ProfileAccessor(core)
33 private val profile = mock<Profile>()
34 private val templateContext = mock<TemplateContext>()
35 private val currentSone = mock<Sone>()
36 private val remoteSone = mock<Sone>()
39 fun setupTemplateContext() {
40 whenever(templateContext.get("currentSone")).thenReturn(currentSone)
45 whenever(profile.sone).thenReturn(remoteSone)
46 whenever(profile.avatar).thenReturn("avatar-id")
51 val currentIdentity = mock<OwnIdentity>()
52 whenever(currentSone.options).thenReturn(DefaultSoneOptions())
53 whenever(currentSone.identity).thenReturn(currentIdentity)
54 whenever(remoteSone.id).thenReturn("remote-sone")
55 val identity = mock<Identity>()
56 val trust = Trust(null, null, null)
57 whenever(remoteSone.identity).thenReturn(identity)
58 whenever(identity.getTrust(currentIdentity)).thenReturn(trust)
63 whenever(core.getImage(eq("avatar-id"), anyBoolean())).thenReturn(mock())
68 fun `avatar is null if there is no current sone`() {
69 whenever(templateContext.get("currentSone")).thenReturn(null)
70 assertThat(accessor.get(templateContext, profile, "avatar"), nullValue())
74 fun `avatar is null if profile has no avatar id`() {
75 whenever(profile.avatar).thenReturn(null)
76 assertThat(accessor.get(templateContext, profile, "avatar"), nullValue())
80 fun `avatar is null if core has no image with avatar ID`() {
81 whenever(core.getImage(eq("avatar-id"), anyBoolean())).thenReturn(null)
82 assertThat(accessor.get(templateContext, profile, "avatar"), nullValue())
86 fun `avatar ID is returned if profile belongs to local sone`() {
87 whenever(remoteSone.isLocal).thenReturn(true)
88 assertThat(accessor.get(templateContext, profile, "avatar"), equalTo<Any>("avatar-id"))
92 fun `avatar is null if sone is configure to never show avatars`() {
93 currentSone.options.showCustomAvatars = NEVER
94 assertThat(accessor.get(templateContext, profile, "avatar"), nullValue())
98 fun `avatar ID is returned if sone is configure to always show avatars`() {
99 currentSone.options.showCustomAvatars = ALWAYS
100 assertThat(accessor.get(templateContext, profile, "avatar"), equalTo<Any>("avatar-id"))
104 fun `avatar ID is returned if sone is configure to show avatars of followed sones and remote sone is followed`() {
105 currentSone.options.showCustomAvatars = FOLLOWED
106 whenever(currentSone.hasFriend("remote-sone")).thenReturn(true)
107 assertThat(accessor.get(templateContext, profile, "avatar"), equalTo<Any>("avatar-id"))
111 fun `avatar is null if sone is configure to show avatars of followed sones but remote sone is not followed`() {
112 currentSone.options.showCustomAvatars = FOLLOWED
113 assertThat(accessor.get(templateContext, profile, "avatar"), nullValue())
117 fun `avatar is null if sone is configure to show avatars based on trust but there is no trust`() {
119 currentSone.options.showCustomAvatars = MANUALLY_TRUSTED
120 assertThat(accessor.get(templateContext, profile, "avatar"), nullValue())
123 private fun setTrust(trust: Trust?) {
124 whenever(remoteSone.identity.getTrust(currentSone.identity as OwnIdentity)).thenReturn(trust)
128 fun `avatar is null if sone is configure to show avatars based on manual trust but there is no explicit trust`() {
129 currentSone.options.showCustomAvatars = MANUALLY_TRUSTED
130 assertThat(accessor.get(templateContext, profile, "avatar"), nullValue())
134 fun `avatar is null if sone is configure to show avatars based on manual trust but explicit trust is zero`() {
135 currentSone.options.showCustomAvatars = MANUALLY_TRUSTED
136 setTrust(Trust(0, null, null))
137 assertThat(accessor.get(templateContext, profile, "avatar"), nullValue())
141 fun `avatar ID is returned if sone is configure to show avatars based on manual trust and explicit trust is one`() {
142 currentSone.options.showCustomAvatars = MANUALLY_TRUSTED
143 setTrust(Trust(1, null, null))
144 assertThat(accessor.get(templateContext, profile, "avatar"), equalTo<Any>("avatar-id"))
148 fun `avatar is null if sone is configure to show avatars based on trust but explicit trust is zero`() {
149 currentSone.options.showCustomAvatars = TRUSTED
150 setTrust(Trust(0, null, null))
151 assertThat(accessor.get(templateContext, profile, "avatar"), nullValue())
155 fun `avatar ID is returned if sone is configure to show avatars based on trust and explicit trust is one`() {
156 currentSone.options.showCustomAvatars = TRUSTED
157 setTrust(Trust(1, null, null))
158 assertThat(accessor.get(templateContext, profile, "avatar"), equalTo<Any>("avatar-id"))
162 fun `avatar is null if sone is configure to show avatars based on trust but both explicit and implicit trust are null`() {
163 currentSone.options.showCustomAvatars = TRUSTED
164 setTrust(Trust(null, null, null))
165 assertThat(accessor.get(templateContext, profile, "avatar"), nullValue())
169 fun `avatar is null if sone is configure to show avatars based on trust but both implicit trust is zero`() {
170 currentSone.options.showCustomAvatars = TRUSTED
171 setTrust(Trust(null, 0, null))
172 assertThat(accessor.get(templateContext, profile, "avatar"), nullValue())
176 fun `avatar ID is returned if sone is configure to show avatars based on trust and implicit trust is one`() {
177 currentSone.options.showCustomAvatars = TRUSTED
178 setTrust(Trust(0, 1, null))
179 assertThat(accessor.get(templateContext, profile, "avatar"), equalTo<Any>("avatar-id"))
183 fun `accessing other members uses reflection accessor`() {
184 assertThat(accessor.get(templateContext, profile, "hashCode"), equalTo<Any>(profile.hashCode()))