Skip to content

Commit a0c3f41

Browse files
committed
add new tests
Signed-off-by: alperozturk96 <alper_ozturk@proton.me>
1 parent 3704807 commit a0c3f41

1 file changed

Lines changed: 89 additions & 0 deletions

File tree

app/src/androidTest/java/com/owncloud/android/utils/FileUploadHelperTest.kt

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import com.nextcloud.client.database.entity.UploadEntity
1111
import com.nextcloud.client.database.entity.toOCUpload
1212
import com.nextcloud.client.database.entity.toUploadEntity
1313
import com.nextcloud.client.jobs.upload.FileUploadHelper
14+
import com.nextcloud.utils.extensions.checkWCFRestrictions
1415
import com.owncloud.android.datamodel.UploadsStorageManager.UploadStatus
1516
import com.owncloud.android.db.OCUpload
1617
import com.owncloud.android.db.UploadResult
@@ -35,6 +36,18 @@ class FileUploadHelperTest {
3536
private val accountName = "test@nextcloud.example.com"
3637
private val localPath = "/sdcard/DCIM/photo.jpg"
3738

39+
private fun toggleWCFRestrictions(value: Boolean) {
40+
val mockCapability = mockk<OCCapability>(relaxed = true)
41+
every { mockCapability.checkWCFRestrictions() } returns value
42+
43+
val mockFileStorageManager = mockk<com.owncloud.android.datamodel.FileDataStorageManager>(relaxed = true)
44+
every { mockFileStorageManager.getCapability(any<com.nextcloud.client.account.User>()) } returns mockCapability
45+
46+
val field = FileUploadHelper::class.java.getDeclaredField("fileStorageManager")
47+
field.isAccessible = true
48+
field.set(fileUploadHelper, mockFileStorageManager)
49+
}
50+
3851
@Suppress("LongParameterList")
3952
private fun buildEntity(
4053
id: Int = 1,
@@ -124,6 +137,8 @@ class FileUploadHelperTest {
124137

125138
@Test
126139
fun getUploadByPathsCaseInsensitiveExtensionFallback() {
140+
toggleWCFRestrictions(true)
141+
127142
// DB stores "/a/b/1.TXT", caller searches with "/a/b/1.txt"
128143
val searchPath = "/a/b/AAA/b/1.txt"
129144
val storedPath = "/a/b/AAA/b/1.TXT"
@@ -142,6 +157,8 @@ class FileUploadHelperTest {
142157

143158
@Test
144159
fun getUploadByPathsFindsRecordWhenDBHasLowercaseExtensionButSearchUsesUppercase() {
160+
toggleWCFRestrictions(true)
161+
145162
// DB stores "/a/b/1.txt", caller searches with "/a/b/1.TXT"
146163
val searchPath = "/a/b/AAA/b/1.TXT"
147164
val storedPath = "/a/b/AAA/b/1.txt"
@@ -189,6 +206,8 @@ class FileUploadHelperTest {
189206

190207
@Test
191208
fun getUploadByPathsHandlesDeepNestedPathWithUppercaseExtension() {
209+
toggleWCFRestrictions(true)
210+
192211
val searchPath = "/a/b/c/d/e/file.PNG"
193212
val storedPath = "/a/b/c/d/e/file.png"
194213
val entity = buildEntity(remotePath = storedPath)
@@ -204,6 +223,8 @@ class FileUploadHelperTest {
204223

205224
@Test
206225
fun getUploadByPathsOnlyTogglesExtensionNotRestOfFilenameOrPath() {
226+
toggleWCFRestrictions(true)
227+
207228
val searchPath = "/a/b/AAA/b/1.txt"
208229
val wrongPath = "/a/b/aaa/b/1.TXT"
209230
val storedPath = "/a/b/AAA/b/1.TXT"
@@ -220,6 +241,74 @@ class FileUploadHelperTest {
220241
verify(exactly = 0) { uploadDao.getUploadByAccountAndPaths(accountName, localPath, wrongPath) }
221242
}
222243

244+
@Test
245+
fun getUploadByPathsCaseInsensitiveExtensionFallbackWCFDisabled() {
246+
toggleWCFRestrictions(false)
247+
248+
val searchPath = "/a/b/AAA/b/1.txt"
249+
val storedPath = "/a/b/AAA/b/1.TXT"
250+
251+
every { uploadDao.getUploadByAccountAndPaths(accountName, localPath, searchPath) } returns null
252+
253+
val result = fileUploadHelper.getUploadByPaths(accountName, localPath, searchPath)
254+
255+
assertNull(result)
256+
257+
// counts getUploadByAccountAndPaths call times based on fileUploadHelper.getUploadByPaths call
258+
verify(exactly = 1) { uploadDao.getUploadByAccountAndPaths(accountName, localPath, searchPath) }
259+
verify(exactly = 0) { uploadDao.getUploadByAccountAndPaths(accountName, localPath, storedPath) }
260+
}
261+
262+
@Test
263+
fun getUploadByPathsFindsRecordWhenDBHasLowercaseExtensionButSearchUsesUppercaseWCFDisabled() {
264+
toggleWCFRestrictions(false)
265+
266+
val searchPath = "/a/b/AAA/b/1.TXT"
267+
val storedPath = "/a/b/AAA/b/1.txt"
268+
269+
every { uploadDao.getUploadByAccountAndPaths(accountName, localPath, searchPath) } returns null
270+
271+
val result = fileUploadHelper.getUploadByPaths(accountName, localPath, searchPath)
272+
273+
assertNull(result)
274+
verify(exactly = 1) { uploadDao.getUploadByAccountAndPaths(accountName, localPath, searchPath) }
275+
verify(exactly = 0) { uploadDao.getUploadByAccountAndPaths(accountName, localPath, storedPath) }
276+
}
277+
278+
@Test
279+
fun getUploadByPathsHandlesDeepNestedPathWithUppercaseExtensionWCFDisabled() {
280+
toggleWCFRestrictions(false)
281+
282+
val searchPath = "/a/b/c/d/e/file.PNG"
283+
val storedPath = "/a/b/c/d/e/file.png"
284+
285+
every { uploadDao.getUploadByAccountAndPaths(accountName, localPath, searchPath) } returns null
286+
287+
val result = fileUploadHelper.getUploadByPaths(accountName, localPath, searchPath)
288+
289+
assertNull(result)
290+
verify(exactly = 1) { uploadDao.getUploadByAccountAndPaths(accountName, localPath, searchPath) }
291+
verify(exactly = 0) { uploadDao.getUploadByAccountAndPaths(accountName, localPath, storedPath) }
292+
}
293+
294+
@Test
295+
fun getUploadByPathsOnlyTogglesExtensionNotRestOfFilenameOrPathWCFDisabled() {
296+
toggleWCFRestrictions(false)
297+
298+
val searchPath = "/a/b/AAA/b/1.txt"
299+
val wrongPath = "/a/b/aaa/b/1.TXT"
300+
val storedPath = "/a/b/AAA/b/1.TXT"
301+
302+
every { uploadDao.getUploadByAccountAndPaths(accountName, localPath, searchPath) } returns null
303+
304+
val result = fileUploadHelper.getUploadByPaths(accountName, localPath, searchPath)
305+
306+
assertNull(result)
307+
verify(exactly = 1) { uploadDao.getUploadByAccountAndPaths(accountName, localPath, searchPath) }
308+
verify(exactly = 0) { uploadDao.getUploadByAccountAndPaths(accountName, localPath, storedPath) }
309+
verify(exactly = 0) { uploadDao.getUploadByAccountAndPaths(accountName, localPath, wrongPath) }
310+
}
311+
223312
@Test
224313
fun toOCUploadMapsAllFieldsCorrectlyWithoutCapability() {
225314
val entity = buildEntity(

0 commit comments

Comments
 (0)