@@ -18,9 +18,13 @@ package com.duckduckgo.app.email
1818
1919import androidx.arch.core.executor.testing.InstantTaskExecutorRule
2020import com.duckduckgo.app.CoroutineTestRule
21+ import com.duckduckgo.app.email.AppEmailManager.WaitlistState.*
2122import com.duckduckgo.app.email.AppEmailManager.Companion.DUCK_EMAIL_DOMAIN
2223import com.duckduckgo.app.email.api.EmailAlias
24+ import com.duckduckgo.app.email.api.EmailInviteCodeResponse
2325import com.duckduckgo.app.email.api.EmailService
26+ import com.duckduckgo.app.email.api.WaitlistResponse
27+ import com.duckduckgo.app.email.api.WaitlistStatusResponse
2428import com.duckduckgo.app.email.db.EmailDataStore
2529import com.duckduckgo.app.runBlocking
2630import com.nhaarman.mockitokotlin2.*
@@ -184,7 +188,172 @@ class AppEmailManagerTest {
184188 assertEquals(" username$DUCK_EMAIL_DOMAIN " , testee.getEmailAddress())
185189 }
186190
191+ @Test
192+ fun whenWaitlistStateIfTimestampExistsCodeDoesNotExistAndSendNotificationIsTrueThenReturnJoinedQueueWithTrue () {
193+ whenever(mockEmailDataStore.waitlistTimestamp).thenReturn(1234 )
194+ whenever(mockEmailDataStore.sendNotification).thenReturn(true )
195+
196+ assertEquals(JoinedQueue (true ), testee.waitlistState())
197+ }
198+
199+ @Test
200+ fun whenWaitlistStateIfTimestampExistsCodeDoesNotExistAndSendNotificationIsFalseThenReturnJoinedQueueWithFalse () {
201+ whenever(mockEmailDataStore.waitlistTimestamp).thenReturn(1234 )
202+ whenever(mockEmailDataStore.sendNotification).thenReturn(false )
203+
204+ assertEquals(JoinedQueue (false ), testee.waitlistState())
205+ }
206+
207+ @Test
208+ fun whenWaitlistStateIfTimestampExistsAndCodeExistsThenReturnInBeta () {
209+ whenever(mockEmailDataStore.waitlistTimestamp).thenReturn(1234 )
210+ whenever(mockEmailDataStore.inviteCode).thenReturn(" abcde" )
211+
212+ assertEquals(InBeta , testee.waitlistState())
213+ }
214+
215+ @Test
216+ fun whenWaitlistStateIfTimestampAndCodeDoesNotExistThenReturnNotJoinedQueue () {
217+ whenever(mockEmailDataStore.waitlistTimestamp).thenReturn(- 1 )
218+ whenever(mockEmailDataStore.inviteCode).thenReturn(null )
219+
220+ assertEquals(NotJoinedQueue , testee.waitlistState())
221+ }
222+
223+ @Test
224+ fun whenJoinWaitlistIfTimestampAndTokenDidNotExistThenStoreTimestampAndToken () {
225+ whenever(mockEmailDataStore.waitlistTimestamp).thenReturn(- 1 )
226+ whenever(mockEmailDataStore.waitlistToken).thenReturn(null )
227+
228+ testee.joinWaitlist(1234 , " abcde" )
229+
230+ verify(mockEmailDataStore).waitlistTimestamp = 1234
231+ verify(mockEmailDataStore).waitlistToken = " abcde"
232+ }
233+ @Test
234+ fun whenJoinWaitlistIfTimestampAndTokenDidExistThenStoreTimestampAndTokenAreNotStored () {
235+ whenever(mockEmailDataStore.waitlistTimestamp).thenReturn(1234 )
236+ whenever(mockEmailDataStore.waitlistToken).thenReturn(" abcde" )
237+
238+ testee.joinWaitlist(4321 , " edcba" )
239+
240+ verify(mockEmailDataStore, never()).waitlistTimestamp = 4321
241+ verify(mockEmailDataStore, never()).waitlistToken = " edcba"
242+ }
243+
244+ @Test
245+ fun whenGetInviteCodeIfCodeExistsThenReturnCode () {
246+ whenever(mockEmailDataStore.inviteCode).thenReturn(" abcde" )
247+ assertEquals(" abcde" , testee.getInviteCode())
248+ }
249+
250+ @Test
251+ fun whenGetInviteCodeIfCodeDoesNotExistThenReturnEmpty () {
252+ whenever(mockEmailDataStore.inviteCode).thenReturn(null )
253+ assertEquals(" " , testee.getInviteCode())
254+ }
255+
256+ @Test
257+ fun whenDoesCodeAlreadyExistIfCodeExistsThenReturnTrue () {
258+ whenever(mockEmailDataStore.inviteCode).thenReturn(" inviteCode" )
259+
260+ assertTrue(testee.doesCodeAlreadyExist())
261+ }
262+
263+ @Test
264+ fun whenDoesCodeAlreadyExistIfCodeIsNullThenReturnFalse () {
265+ whenever(mockEmailDataStore.inviteCode).thenReturn(null )
266+
267+ assertFalse(testee.doesCodeAlreadyExist())
268+ }
269+
270+ @Test
271+ fun whenFetchInviteCodeIfCodeAlreadyExistsThenReturnCodeExisted () = coroutineRule.runBlocking {
272+ whenever(mockEmailDataStore.inviteCode).thenReturn(" inviteCode" )
273+
274+ assertEquals(AppEmailManager .FetchCodeResult .CodeExisted , testee.fetchInviteCode())
275+ }
276+
277+ @Test
278+ fun whenFetchInviteCodeIfTimestampIsSmallerThanQueueTimestampThenCallGetCode () = coroutineRule.runBlocking {
279+ givenUserIsInWaitlist()
280+ whenever(mockEmailService.waitlistStatus()).thenReturn(WaitlistStatusResponse (12345 ))
281+
282+ testee.fetchInviteCode()
283+
284+ verify(mockEmailService).getCode(" token" )
285+ }
286+
287+ @Test
288+ fun whenFetchInviteCodeIfTimestampIsEqualsThanQueueTimestampThenCallGetCode () = coroutineRule.runBlocking {
289+ givenUserIsInWaitlist()
290+ whenever(mockEmailService.waitlistStatus()).thenReturn(WaitlistStatusResponse (1234 ))
291+
292+ testee.fetchInviteCode()
293+
294+ verify(mockEmailService).getCode(" token" )
295+ }
296+
297+ @Test
298+ fun whenFetchInviteCodeIfUserIsTopOfQueueAndCodeAvailableThenReturnCode () = coroutineRule.runBlocking {
299+ givenUserIsTopOfTheQueue()
300+ whenever(mockEmailService.getCode(any())).thenReturn(EmailInviteCodeResponse (" code" ))
301+
302+ assertEquals(AppEmailManager .FetchCodeResult .Code , testee.fetchInviteCode())
303+ }
304+
305+ @Test
306+ fun whenFetchInviteCodeIfUserIsTopOfQueueAndCodeNotAvailableThenReturnNoCode () = coroutineRule.runBlocking {
307+ givenUserIsTopOfTheQueue()
308+ whenever(mockEmailService.getCode(any())).thenReturn(EmailInviteCodeResponse (" " ))
309+
310+ assertEquals(AppEmailManager .FetchCodeResult .NoCode , testee.fetchInviteCode())
311+ }
312+
313+ @Test
314+ fun whenFetchInviteCodeIfUserIsTopOfQueueAndCodeServiceNotAvailableThenReturnNoCode () = coroutineRule.runBlocking {
315+ testee = AppEmailManager (TestEmailService (), mockEmailDataStore, coroutineRule.testDispatcherProvider, TestCoroutineScope ())
316+ givenUserIsTopOfTheQueue()
317+
318+ assertEquals(AppEmailManager .FetchCodeResult .NoCode , testee.fetchInviteCode())
319+ }
320+
321+ @Test
322+ fun whenFetchInviteCodeIfUserInTheQueueAndStatusServiceNotAvailableThenReturnNoCode () = coroutineRule.runBlocking {
323+ testee = AppEmailManager (TestEmailService (), mockEmailDataStore, coroutineRule.testDispatcherProvider, TestCoroutineScope ())
324+ givenUserIsInWaitlist()
325+
326+ assertEquals(AppEmailManager .FetchCodeResult .NoCode , testee.fetchInviteCode())
327+ }
328+
329+ @Test
330+ fun whenNotifyOnJoinedWaitlistThenSendNotificationSetToTrue () {
331+ testee.notifyOnJoinedWaitlist()
332+ verify(mockEmailDataStore).sendNotification = true
333+ }
334+
335+ private fun givenUserIsInWaitlist () {
336+ whenever(mockEmailDataStore.waitlistTimestamp).thenReturn(1234 )
337+ whenever(mockEmailDataStore.waitlistToken).thenReturn(" token" )
338+ }
339+
340+ private fun givenUserIsTopOfTheQueue () = coroutineRule.runBlocking {
341+ givenUserIsInWaitlist()
342+ whenever(mockEmailService.waitlistStatus()).thenReturn(WaitlistStatusResponse (1234 ))
343+ }
344+
187345 private suspend fun givenNextAliasExists () {
188346 aliasSharedFlow.emit(" alias" )
189347 }
348+
349+ class TestEmailService : EmailService {
350+ override suspend fun newAlias (authorization : String ): EmailAlias = EmailAlias (" alias" )
351+ override suspend fun joinWaitlist (): WaitlistResponse = WaitlistResponse (" token" , 12345 )
352+ override suspend fun waitlistStatus (): WaitlistStatusResponse {
353+ throw Exception ()
354+ }
355+ override suspend fun getCode (token : String ): EmailInviteCodeResponse {
356+ throw Exception ()
357+ }
358+ }
190359}
0 commit comments