22 * ownCloud Android client application
33 *
44 * @author David González Verdugo
5- * Copyright (C) 2020 ownCloud GmbH.
5+ * @author Juan Carlos Garrote Gascón
6+ *
7+ * Copyright (C) 2023 ownCloud GmbH.
68 *
79 * This program is free software: you can redistribute it and/or modify
810 * it under the terms of the GNU General Public License version 2,
@@ -21,8 +23,6 @@ package com.owncloud.android.data.authentication.datasources.implementation
2123
2224import android.accounts.Account
2325import android.accounts.AccountManager
24- import androidx.arch.core.executor.testing.InstantTaskExecutorRule
25- import androidx.test.filters.MediumTest
2626import androidx.test.platform.app.InstrumentationRegistry
2727import com.owncloud.android.data.authentication.KEY_CLIENT_REGISTRATION_CLIENT_EXPIRATION_DATE
2828import com.owncloud.android.data.authentication.KEY_CLIENT_REGISTRATION_CLIENT_ID
@@ -47,6 +47,7 @@ import com.owncloud.android.testutil.OC_ACCOUNT
4747import com.owncloud.android.testutil.OC_ACCOUNT_ID
4848import com.owncloud.android.testutil.OC_ACCOUNT_NAME
4949import com.owncloud.android.testutil.OC_AUTH_TOKEN_TYPE
50+ import com.owncloud.android.testutil.OC_BASIC_PASSWORD
5051import com.owncloud.android.testutil.OC_SECURE_BASE_URL
5152import com.owncloud.android.testutil.OC_BASIC_USERNAME
5253import com.owncloud.android.testutil.OC_OAUTH_SUPPORTED_TRUE
@@ -58,23 +59,17 @@ import com.owncloud.android.testutil.OC_USER_INFO
5859import com.owncloud.android.testutil.oauth.OC_CLIENT_REGISTRATION
5960import io.mockk.every
6061import io.mockk.mockk
61- import io.mockk.spyk
6262import io.mockk.verify
6363import org.junit.Assert.assertEquals
6464import org.junit.Assert.assertTrue
6565import org.junit.Before
66- import org.junit.Rule
6766import org.junit.Test
6867
69- @MediumTest
7068class OCLocalAuthenticationDataSourceTest {
71- @Rule
72- @JvmField
73- val instantExecutorRule = InstantTaskExecutorRule ()
7469
7570 private lateinit var ocLocalAuthenticationDataSource: OCLocalAuthenticationDataSource
7671 private val accountManager = mockk<AccountManager >(relaxUnitFun = true )
77- private val preferencesProvider = spyk <SharedPreferencesProvider >()
72+ private val preferencesProvider = mockk <SharedPreferencesProvider >(relaxUnitFun = true )
7873
7974 @Before
8075 fun setUp () {
@@ -86,13 +81,7 @@ class OCLocalAuthenticationDataSourceTest {
8681 preferencesProvider,
8782 OC_ACCOUNT .type
8883 )
89- getAccountsByType(OC_ACCOUNT .type, arrayOf(OC_ACCOUNT ))
90- }
91-
92- private fun getAccountsByType (accountType : String , accounts : Array <Account >) {
93- every {
94- accountManager.getAccountsByType(accountType)
95- } returns accounts
84+ mockGetAccountsByType(OC_ACCOUNT .type, arrayOf(OC_ACCOUNT ))
9685 }
9786
9887 @Test
@@ -103,21 +92,20 @@ class OCLocalAuthenticationDataSourceTest {
10392 val newAccountName = ocLocalAuthenticationDataSource.addBasicAccount(
10493 OC_ACCOUNT_ID ,
10594 OC_REDIRECTION_PATH .lastPermanentLocation,
106- " password " ,
95+ OC_BASIC_PASSWORD ,
10796 OC_SECURE_SERVER_INFO_BASIC_AUTH ,
10897 OC_USER_INFO ,
10998 null
11099 )
111100
112101 val newAccount = Account (OC_ACCOUNT_NAME , OC_ACCOUNT .type)
113102
103+ assertEquals(newAccount.name, newAccountName)
104+
114105 // One for checking if the account exists and another one for getting the new account
115106 verifyAccountsByTypeAreGot(newAccount.type, 2 )
116-
117- verifyAccountIsExplicitlyAdded(newAccount, " password" , 1 )
107+ verifyAccountIsExplicitlyAdded(newAccount, OC_BASIC_PASSWORD , 1 )
118108 verifyAccountInfoIsUpdated(newAccount, OC_SECURE_SERVER_INFO_BASIC_AUTH , OC_USER_INFO , 1 )
119-
120- assertEquals(newAccount.name, newAccountName)
121109 }
122110
123111 @Test(expected = AccountNotNewException ::class )
@@ -126,7 +114,7 @@ class OCLocalAuthenticationDataSourceTest {
126114 ocLocalAuthenticationDataSource.addBasicAccount(
127115 OC_ACCOUNT_ID ,
128116 OC_REDIRECTION_PATH .lastPermanentLocation,
129- " password " ,
117+ OC_BASIC_PASSWORD ,
130118 OC_SECURE_SERVER_INFO_BASIC_AUTH ,
131119 OC_USER_INFO .copy(id = OC_ACCOUNT_ID ),
132120 null
@@ -138,20 +126,22 @@ class OCLocalAuthenticationDataSourceTest {
138126
139127 mockSelectedAccountNameInPreferences()
140128
141- ocLocalAuthenticationDataSource.addBasicAccount(
129+ val accountName = ocLocalAuthenticationDataSource.addBasicAccount(
142130 OC_ACCOUNT_ID ,
143131 OC_REDIRECTION_PATH .lastPermanentLocation,
144- " password " ,
132+ OC_BASIC_PASSWORD ,
145133 OC_SECURE_SERVER_INFO_BASIC_AUTH ,
146134 OC_USER_INFO .copy(id = OC_ACCOUNT_ID ),
147135 OC_ACCOUNT_NAME
148136 )
149137
138+ assertEquals(OC_ACCOUNT_NAME , accountName)
139+
150140 // One for getting account to update
151141 verifyAccountsByTypeAreGot(OC_ACCOUNT .type, 1 )
152142
153143 // The account already exists so do not create it
154- verifyAccountIsExplicitlyAdded(OC_ACCOUNT , " password " , 0 )
144+ verifyAccountIsExplicitlyAdded(OC_ACCOUNT , OC_BASIC_PASSWORD , 0 )
155145
156146 // The account already exists, so update it
157147 verifyAccountInfoIsUpdated(OC_ACCOUNT , OC_SECURE_SERVER_INFO_BASIC_AUTH , OC_USER_INFO , 1 )
@@ -166,7 +156,7 @@ class OCLocalAuthenticationDataSourceTest {
166156 ocLocalAuthenticationDataSource.addBasicAccount(
167157 OC_BASIC_USERNAME ,
168158 OC_REDIRECTION_PATH .lastPermanentLocation,
169- " password " ,
159+ OC_BASIC_PASSWORD ,
170160 OC_SECURE_SERVER_INFO_BASIC_AUTH ,
171161 OC_USER_INFO ,
172162 " NotTheSameAccount"
@@ -175,7 +165,7 @@ class OCLocalAuthenticationDataSourceTest {
175165 assertTrue(exception is AccountNotTheSameException )
176166 } finally {
177167 // The account already exists so do not create a new one
178- verifyAccountIsExplicitlyAdded(OC_ACCOUNT , " password " , 0 )
168+ verifyAccountIsExplicitlyAdded(OC_ACCOUNT , OC_BASIC_PASSWORD , 0 )
179169
180170 // The account is not the same, so no update needed
181171 verifyAccountInfoIsUpdated(OC_ACCOUNT , OC_SECURE_SERVER_INFO_BASIC_AUTH , OC_USER_INFO , 0 )
@@ -236,7 +226,7 @@ class OCLocalAuthenticationDataSourceTest {
236226
237227 mockSelectedAccountNameInPreferences()
238228
239- ocLocalAuthenticationDataSource.addOAuthAccount(
229+ val accountName = ocLocalAuthenticationDataSource.addOAuthAccount(
240230 OC_ACCOUNT_ID ,
241231 OC_REDIRECTION_PATH .lastPermanentLocation,
242232 OC_AUTH_TOKEN_TYPE ,
@@ -249,11 +239,13 @@ class OCLocalAuthenticationDataSourceTest {
249239 OC_CLIENT_REGISTRATION
250240 )
251241
242+ assertEquals(OC_ACCOUNT_NAME , accountName)
243+
252244 // One for getting account to update
253245 verifyAccountsByTypeAreGot(OC_ACCOUNT .type, 1 )
254246
255247 // The account already exists so do not create it
256- verifyAccountIsExplicitlyAdded(OC_ACCOUNT , " password " , 0 )
248+ verifyAccountIsExplicitlyAdded(OC_ACCOUNT , OC_BASIC_PASSWORD , 0 )
257249
258250 // The account already exists, so update it
259251 verifyAccountInfoIsUpdated(OC_ACCOUNT , OC_SECURE_SERVER_INFO_BASIC_AUTH , OC_USER_INFO , 1 )
@@ -282,7 +274,7 @@ class OCLocalAuthenticationDataSourceTest {
282274 assertTrue(exception is AccountNotTheSameException )
283275 } finally {
284276 // The account already exists so do not create it
285- verifyAccountIsExplicitlyAdded(OC_ACCOUNT , " password " , 0 )
277+ verifyAccountIsExplicitlyAdded(OC_ACCOUNT , OC_BASIC_PASSWORD , 0 )
286278
287279 // The account already exists, so update it
288280 verifyAccountInfoIsUpdated(OC_ACCOUNT , OC_SECURE_SERVER_INFO_BASIC_AUTH , OC_USER_INFO , 0 )
@@ -299,23 +291,23 @@ class OCLocalAuthenticationDataSourceTest {
299291 }
300292
301293 @Test
302- fun supportsOAuthOk () {
294+ fun supportsOAuth2Ok () {
303295
304296 every {
305297 accountManager.getUserData(OC_ACCOUNT , KEY_SUPPORTS_OAUTH2 )
306298 } returns OC_OAUTH_SUPPORTED_TRUE
307299
308300 val supportsOAuth2 = ocLocalAuthenticationDataSource.supportsOAuth2(OC_ACCOUNT .name)
309301
302+ assertTrue(supportsOAuth2)
303+
310304 verifyAccountsByTypeAreGot(OC_ACCOUNT .type, 1 )
311305 verifyUserDataIsGot(OC_ACCOUNT , KEY_SUPPORTS_OAUTH2 , 1 )
312-
313- assertEquals(true , supportsOAuth2)
314306 }
315307
316308 @Test(expected = AccountNotFoundException ::class )
317309 fun supportsOAuthAccountNotFound () {
318- getAccountsByType (OC_ACCOUNT .type, arrayOf())// That account does not exist
310+ mockGetAccountsByType (OC_ACCOUNT .type, arrayOf()) // That account does not exist
319311
320312 ocLocalAuthenticationDataSource.supportsOAuth2(OC_ACCOUNT .name)
321313 }
@@ -329,43 +321,41 @@ class OCLocalAuthenticationDataSourceTest {
329321
330322 val baseUrl = ocLocalAuthenticationDataSource.getBaseUrl(OC_ACCOUNT .name)
331323
324+ assertEquals(OC_SECURE_BASE_URL , baseUrl)
325+
332326 verifyAccountsByTypeAreGot(OC_ACCOUNT .type, 1 )
333327 verifyUserDataIsGot(OC_ACCOUNT , KEY_OC_BASE_URL , 1 )
334-
335- assertEquals(OC_SECURE_BASE_URL , baseUrl)
336328 }
337329
338330 @Test(expected = AccountNotFoundException ::class )
339331 fun getBaseUrlAccountNotFound () {
340- getAccountsByType (OC_ACCOUNT .type, arrayOf()) // That account does not exist
332+ mockGetAccountsByType (OC_ACCOUNT .type, arrayOf()) // That account does not exist
341333
342334 ocLocalAuthenticationDataSource.getBaseUrl(OC_ACCOUNT .name)
343335 }
344336
337+ private fun mockGetAccountsByType (accountType : String , accounts : Array <Account >) {
338+ every {
339+ accountManager.getAccountsByType(accountType)
340+ } returns accounts
341+ }
342+
345343 private fun mockSelectedAccountNameInPreferences (
346344 selectedAccountName : String = OC_ACCOUNT .name
347345 ) {
348346 every {
349347 preferencesProvider.getString(SELECTED_ACCOUNT , any())
350348 } returns selectedAccountName
351- every {
352- preferencesProvider.putString(any(), any())
353- } returns Unit
354349 }
355350
356351 private fun mockRegularAccountCreationFlow () {
357352 // Step 1: Get accounts to know if the current account exists
358- getAccountsByType (OC_ACCOUNT .type, arrayOf()) // There's no accounts yet
353+ mockGetAccountsByType (OC_ACCOUNT .type, arrayOf()) // There's no accounts yet
359354
360355 // Step 2: Add new account
361356 every {
362357 accountManager.addAccountExplicitly(any(), any(), any())
363358 } returns true
364-
365- // Step 3: Update just created account
366- every {
367- accountManager.setUserData(any(), any(), any())
368- } returns Unit
369359 }
370360
371361 private fun verifyAccountsByTypeAreGot (accountType : String , exactly : Int ) {
0 commit comments