🔥 Remove unnecessary imports
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / template / ProfileAccessorTest.kt
1 package net.pterodactylus.sone.template
2
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
22 import org.junit.Test
23 import org.mockito.ArgumentMatchers.anyBoolean
24 import org.mockito.ArgumentMatchers.eq
25
26 /**
27  * Unit test for [ProfileAccessor].
28  */
29 class ProfileAccessorTest {
30
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>()
37
38         @Before
39         fun setupTemplateContext() {
40                 whenever(templateContext.get("currentSone")).thenReturn(currentSone)
41         }
42
43         @Before
44         fun setupProfile() {
45                 whenever(profile.sone).thenReturn(remoteSone)
46                 whenever(profile.avatar).thenReturn("avatar-id")
47         }
48
49         @Before
50         fun setupSones() {
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)
59         }
60
61         @Before
62         fun setupCore() {
63                 whenever(core.getImage(eq("avatar-id"), anyBoolean())).thenReturn(mock())
64         }
65
66
67         @Test
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())
71         }
72
73         @Test
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())
77         }
78
79         @Test
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())
83         }
84
85         @Test
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"))
89         }
90
91         @Test
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())
95         }
96
97         @Test
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"))
101         }
102
103         @Test
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"))
108         }
109
110         @Test
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())
114         }
115
116         @Test
117         fun `avatar is null if sone is configure to show avatars based on trust but there is no trust`() {
118                 setTrust(null)
119                 currentSone.options.showCustomAvatars = MANUALLY_TRUSTED
120                 assertThat(accessor.get(templateContext, profile, "avatar"), nullValue())
121         }
122
123         private fun setTrust(trust: Trust?) {
124                 whenever(remoteSone.identity.getTrust(currentSone.identity as OwnIdentity)).thenReturn(trust)
125         }
126
127         @Test
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())
131         }
132
133         @Test
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())
138         }
139
140         @Test
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"))
145         }
146
147         @Test
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())
152         }
153
154         @Test
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"))
159         }
160
161         @Test
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())
166         }
167
168         @Test
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())
173         }
174
175         @Test
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"))
180         }
181
182         @Test
183         fun `accessing other members uses reflection accessor`() {
184                 assertThat(accessor.get(templateContext, profile, "hashCode"), equalTo<Any>(profile.hashCode()))
185         }
186
187 }