Skip to content

Commit

Permalink
Renamed names in tests class and added relaxing mock in LocalAuthenti…
Browse files Browse the repository at this point in the history
…cationDataSource
  • Loading branch information
Aitorbp committed Oct 11, 2023
1 parent 435e25e commit aa47e83
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class OCLocalAuthenticationDataSourceTest {
val instantExecutorRule = InstantTaskExecutorRule()

private lateinit var ocLocalAuthenticationDataSource: OCLocalAuthenticationDataSource
private val accountManager = mockk<AccountManager>()
private val accountManager = mockk<AccountManager>(relaxUnitFun = true)
private val preferencesProvider = spyk<SharedPreferencesProvider>()

@Before
Expand All @@ -86,6 +86,13 @@ class OCLocalAuthenticationDataSourceTest {
preferencesProvider,
OC_ACCOUNT.type
)
getAccountsByType(OC_ACCOUNT.type, arrayOf(OC_ACCOUNT))
}

private fun getAccountsByType(accountType: String, accounts: Array<Account>) {
every {
accountManager.getAccountsByType(accountType)
} returns accounts
}

@Test
Expand All @@ -102,7 +109,7 @@ class OCLocalAuthenticationDataSourceTest {
null
)

val newAccount = Account(OC_ACCOUNT_NAME, "owncloud")
val newAccount = Account(OC_ACCOUNT_NAME, OC_ACCOUNT.type)

// One for checking if the account exists and another one for getting the new account
verifyAccountsByTypeAreGot(newAccount.type, 2)
Expand All @@ -115,9 +122,6 @@ class OCLocalAuthenticationDataSourceTest {

@Test(expected = AccountNotNewException::class)
fun addBasicAccountAlreadyExistsNoUpdate() {
every {
accountManager.getAccountsByType(OC_ACCOUNT.type)
} returns arrayOf(OC_ACCOUNT) // The account is already there

ocLocalAuthenticationDataSource.addBasicAccount(
OC_ACCOUNT_ID,
Expand All @@ -131,17 +135,6 @@ class OCLocalAuthenticationDataSourceTest {

@Test
fun addBasicAccountAlreadyExistsUpdateSameUsername() {
every {
accountManager.getAccountsByType(OC_ACCOUNT.type)
} returns arrayOf(OC_ACCOUNT) // The account is already there

every {
accountManager.setPassword(any(), any())
} returns Unit

every {
accountManager.setUserData(any(), any(), any())
} returns Unit

mockSelectedAccountNameInPreferences()

Expand All @@ -167,10 +160,6 @@ class OCLocalAuthenticationDataSourceTest {
@Test
fun addBasicAccountAlreadyExistsUpdateDifferentUsername() {

every {
accountManager.setUserData(any(), any(), any())
} returns Unit

mockSelectedAccountNameInPreferences()

try {
Expand Down Expand Up @@ -198,10 +187,6 @@ class OCLocalAuthenticationDataSourceTest {
mockRegularAccountCreationFlow()
mockSelectedAccountNameInPreferences()

every {
accountManager.setAuthToken(any(), any(), any())
} returns Unit

val newAccountName = ocLocalAuthenticationDataSource.addOAuthAccount(
OC_ACCOUNT_ID,
OC_REDIRECTION_PATH.lastPermanentLocation,
Expand All @@ -215,7 +200,7 @@ class OCLocalAuthenticationDataSourceTest {
OC_CLIENT_REGISTRATION
)

val newAccount = Account(OC_ACCOUNT_NAME, "owncloud")
val newAccount = Account(OC_ACCOUNT_NAME, OC_ACCOUNT.type)

// One for checking if the account exists and another one for getting the new account
verifyAccountsByTypeAreGot(newAccount.type, 2)
Expand All @@ -231,9 +216,6 @@ class OCLocalAuthenticationDataSourceTest {

@Test(expected = AccountNotNewException::class)
fun addOAuthAccountAlreadyExistsNoUpdate() {
every {
accountManager.getAccountsByType(OC_ACCOUNT.type)
} returns arrayOf(OC_ACCOUNT) // The account is already there

ocLocalAuthenticationDataSource.addOAuthAccount(
OC_ACCOUNT_ID,
Expand All @@ -251,17 +233,6 @@ class OCLocalAuthenticationDataSourceTest {

@Test
fun addOAuthAccountAlreadyExistsUpdateSameUsername() {
every {
accountManager.getAccountsByType(OC_ACCOUNT.type)
} returns arrayOf(OC_ACCOUNT) // The account is already there

every {
accountManager.setUserData(any(), any(), any())
} returns Unit

every {
accountManager.setAuthToken(any(), any(), any())
} returns Unit

mockSelectedAccountNameInPreferences()

Expand Down Expand Up @@ -292,14 +263,6 @@ class OCLocalAuthenticationDataSourceTest {
@Test
fun addOAuthAccountAlreadyExistsUpdateDifferentUsername() {

every {
accountManager.setUserData(any(), any(), any())
} returns Unit

every {
accountManager.setAuthToken(any(), any(), any())
} returns Unit

mockSelectedAccountNameInPreferences()

try {
Expand Down Expand Up @@ -337,9 +300,6 @@ class OCLocalAuthenticationDataSourceTest {

@Test
fun supportsOAuthOk() {
every {
accountManager.getAccountsByType(OC_ACCOUNT.type)
} returns arrayOf(OC_ACCOUNT)

every {
accountManager.getUserData(OC_ACCOUNT, KEY_SUPPORTS_OAUTH2)
Expand All @@ -355,18 +315,13 @@ class OCLocalAuthenticationDataSourceTest {

@Test(expected = AccountNotFoundException::class)
fun supportsOAuthAccountNotFound() {
every {
accountManager.getAccountsByType(OC_ACCOUNT.type)
} returns arrayOf() // That account does not exist
getAccountsByType(OC_ACCOUNT.type, arrayOf())// That account does not exist

ocLocalAuthenticationDataSource.supportsOAuth2(OC_ACCOUNT.name)
}

@Test
fun getBaseUrlOk() {
every {
accountManager.getAccountsByType(OC_ACCOUNT.type)
} returns arrayOf(OC_ACCOUNT)

every {
accountManager.getUserData(OC_ACCOUNT, KEY_OC_BASE_URL)
Expand All @@ -382,9 +337,7 @@ class OCLocalAuthenticationDataSourceTest {

@Test(expected = AccountNotFoundException::class)
fun getBaseUrlAccountNotFound() {
every {
accountManager.getAccountsByType(OC_ACCOUNT.type)
} returns arrayOf() // That account does not exist
getAccountsByType(OC_ACCOUNT.type, arrayOf()) // That account does not exist

ocLocalAuthenticationDataSource.getBaseUrl(OC_ACCOUNT.name)
}
Expand All @@ -402,9 +355,7 @@ class OCLocalAuthenticationDataSourceTest {

private fun mockRegularAccountCreationFlow() {
// Step 1: Get accounts to know if the current account exists
every {
accountManager.getAccountsByType("owncloud")
} returns arrayOf() // There's no accounts yet
getAccountsByType(OC_ACCOUNT.type, arrayOf()) // There's no accounts yet

// Step 2: Add new account
every {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ class OCLocalShareDataSourceTest {
**************************************************************************************************************/

@Test
fun `replaceShares renewal shares related to a list of files`() {
fun `replaceShares renewal shares related to a list of shares`() {
val expectedValues = listOf<Long>(1, 2)
every { ocSharesDao.replaceShares(publicShares) } returns expectedValues

Expand All @@ -313,7 +313,7 @@ class OCLocalShareDataSourceTest {
}

@Test
fun `deleteShare removes a share related to a file`() {
fun `deleteShare removes a share correctly`() {
every { ocSharesDao.deleteShare(OC_SHARE.remoteId) } returns 1

val deletedRows = ocLocalSharesDataSource.deleteShare(OC_SHARE.remoteId)
Expand All @@ -323,7 +323,7 @@ class OCLocalShareDataSourceTest {
verify(exactly = 1) { ocSharesDao.deleteShare(OC_SHARE.remoteId) }
}
@Test
fun `deleteSharesForAccount removes shares related to a file`() {
fun `deleteSharesForAccount removes shares related to an account`() {

ocLocalSharesDataSource.deleteSharesForAccount(OC_SHARE.accountOwner)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ class OCRemoteShareDataSourceTest {
}

@Test
fun `updateShare update a public share returns OCShare`() {
fun `updateShare for public share returns OCShare`() {
val updateRemoteShareOperationResult = createRemoteOperationResultMock(
ShareResponse(
listOf(
Expand Down Expand Up @@ -463,7 +463,7 @@ class OCRemoteShareDataSourceTest {
}

@Test
fun `deleteShare remove a share correctly`() {
fun `deleteShare removes a share correctly`() {
val removeRemoteShareOperationResult = createRemoteOperationResultMock(
Unit,
isSuccess = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,17 +204,16 @@ class OCLocalTransferDataSourceTest {
@Test
fun `getAllTransfersAsStream returns a flow of list of OCTransfer ordered by status`() = runBlocking {

val transferEntityInProgress: OCTransferEntity = ocTransfer.toEntity().copy(status = 0)
val transferEntityInProgress: OCTransferEntity = ocTransfer.copy(status = TransferStatus.TRANSFER_IN_PROGRESS).toEntity()

val transferEntityQueue: OCTransferEntity = ocTransfer.toEntity().copy(status = 1)
val transferEntityQueue: OCTransferEntity = ocTransfer.copy(status = TransferStatus.TRANSFER_QUEUED).toEntity()

val transferEntityFailed: OCTransferEntity = ocTransfer.toEntity().copy(status = 2)
val transferEntityFailed: OCTransferEntity = ocTransfer.copy(status = TransferStatus.TRANSFER_FAILED).toEntity()

val transferEntitySucceeded: OCTransferEntity = ocTransfer.toEntity().copy(status = 3)
val transferEntitySucceeded: OCTransferEntity = ocTransfer.copy(status = TransferStatus.TRANSFER_SUCCEEDED).toEntity()

val transferListRandom = listOf(transferEntityQueue, transferEntityFailed, transferEntityInProgress, transferEntitySucceeded)


val transferQueue = ocTransfer.copy()
transferQueue.status = TransferStatus.TRANSFER_QUEUED

Expand All @@ -230,7 +229,7 @@ class OCLocalTransferDataSourceTest {
transferDao.getAllTransfersAsStream()
} returns flowOf(transferListRandom)

val actualResult = ocLocalTransferDataSource.getAllTransfersAsStream().first().map { it }
val actualResult = ocLocalTransferDataSource.getAllTransfersAsStream().first()

assertEquals(transferListOrdered, actualResult)

Expand Down Expand Up @@ -305,7 +304,7 @@ class OCLocalTransferDataSourceTest {
}

@Test
fun `clearFailedTransfers clears it correctly`() {
fun `clearFailedTransfers removes transfers correctly`() {

ocLocalTransferDataSource.clearFailedTransfers()

Expand All @@ -315,7 +314,7 @@ class OCLocalTransferDataSourceTest {
}

@Test
fun `clearSuccessfulTransfers clears it correctly`() {
fun `clearSuccessfulTransfers removes transfers correctly`() {

ocLocalTransferDataSource.clearSuccessfulTransfers()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class OCRemoteWebFingerDatasourceTest {
private val clientManager: ClientManager = mockk(relaxed = true)
private val ownCloudClient: OwnCloudClient = mockk(relaxed = true)
private val ocWebFingerService: OCWebFingerService = mockk()
private val listString: List<String> = listOf(
private val urls: List<String> = listOf(
"http://webfinger.owncloud/tests/server-instance1",
"http://webfinger.owncloud/tests/server-instance2",
"http://webfinger.owncloud/tests/server-instance3",
Expand All @@ -65,7 +65,7 @@ class OCRemoteWebFingerDatasourceTest {
fun `getInstancesFromWebFinger returns a list of web finger`() {

val getInstancesFromWebFingerResult: RemoteOperationResult<List<String>> =
createRemoteOperationResultMock(data = listString, isSuccess = true)
createRemoteOperationResultMock(data = urls, isSuccess = true)

every {
ocWebFingerService.getInstancesFromWebFinger(
Expand Down Expand Up @@ -99,7 +99,7 @@ class OCRemoteWebFingerDatasourceTest {
fun `getInstancesFromAuthenticatedWebFinger returns a list of web finger`() {

val getInstancesFromAuthenticatedWebFingerResult: RemoteOperationResult<List<String>> =
createRemoteOperationResultMock(data = listString, isSuccess = true)
createRemoteOperationResultMock(data = urls, isSuccess = true)

every {
ocWebFingerService.getInstancesFromWebFinger(
Expand Down

0 comments on commit aa47e83

Please sign in to comment.