Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
Signed-off-by: alperozturk <[email protected]>
  • Loading branch information
alperozturk96 committed Sep 18, 2024
1 parent 7c56ae8 commit 4f91c97
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 11 deletions.
91 changes: 82 additions & 9 deletions app/src/androidTest/java/com/nextcloud/utils/AutoRenameTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import com.owncloud.android.lib.resources.status.OCCapability
import org.junit.Before
import org.junit.Test

class AutoRenameTests: AbstractOnServerIT() {
class AutoRenameTests : AbstractOnServerIT() {

private var capability: OCCapability = fileDataStorageManager.getCapability(account.name)

Expand All @@ -28,7 +28,7 @@ class AutoRenameTests: AbstractOnServerIT() {
}

@Test
fun testRenameWhenInvalidCharacterIncludedShouldReturnValidFilename() {
fun testInvalidChar() {
val forbiddenFilenameCharacters = capability.forbiddenFilenameCharacters()
val randomInvalidChar = forbiddenFilenameCharacters.random()

Expand All @@ -39,48 +39,121 @@ class AutoRenameTests: AbstractOnServerIT() {
}

@Test
fun testRenameWhenInvalidFilenameExtensionIncludedShouldReturnValidFilename() {
fun testInvalidExtension() {
val forbiddenFilenameExtension = capability.forbiddenFilenameExtension()
val randomInvalidFilenameExtension = forbiddenFilenameExtension.random()

val filename = "file${randomInvalidFilenameExtension}"
val filename = "file$randomInvalidFilenameExtension"
val result = AutoRename.rename(filename, capability)
val expectedFilename = "file_"
assert(result == expectedFilename) { "Expected $expectedFilename but got $result" }
}

@Test
fun testRenameWhenMultipleInvalidCharactersShouldReturnValidFilename() {
fun testMultipleInvalidChars() {
val filename = "file|name?<>.txt"
val result = AutoRename.rename(filename, capability)
val expectedFilename = "file_name___.txt"
assert(result == expectedFilename) { "Expected $expectedFilename but got $result" }
}

@Test
fun testRenameWhenFilenameStartsAndEndsWithInvalidExtensionsShouldReturnValidFilename() {
fun testStartEndInvalidExtensions() {
val filename = " .file.part "
val result = AutoRename.rename(filename, capability)
val expectedFilename = "_file_part"
assert(result == expectedFilename) { "Expected $expectedFilename but got $result" }
}

@Test
fun testRenameWhenNonPrintableCharactersArePresentShouldRemoveThem() {
fun testStartInvalidExtension() {
val filename = " .file.part"
val result = AutoRename.rename(filename, capability)
val expectedFilename = "_file_part"
assert(result == expectedFilename) { "Expected $expectedFilename but got $result" }
}

@Test
fun testEndInvalidExtension() {
val filename = ".file.part "
val result = AutoRename.rename(filename, capability)
val expectedFilename = "_file_part"
assert(result == expectedFilename) { "Expected $expectedFilename but got $result" }
}

@Test
fun testMiddleNonPrintableChar() {
val filename = "file\u0001name.txt"
val result = AutoRename.rename(filename, capability)
val expectedFilename = "filename.txt"
assert(result == expectedFilename) { "Expected $expectedFilename but got $result" }
}

@Test
fun testRenameWhenInvalidFolderPathGivenShouldReturnValidFolderPath() {
fun testStartNonPrintableChar() {
val filename = "\u0001filename.txt"
val result = AutoRename.rename(filename, capability)
val expectedFilename = "filename.txt"
assert(result == expectedFilename) { "Expected $expectedFilename but got $result" }
}

@Test
fun testEndNonPrintableChar() {
val filename = "filename.txt\u0001"
val result = AutoRename.rename(filename, capability)
val expectedFilename = "filename.txt"
assert(result == expectedFilename) { "Expected $expectedFilename but got $result" }
}

@Test
fun testExtensionNonPrintableChar() {
val filename = "filename.t\u0001xt"
val result = AutoRename.rename(filename, capability)
val expectedFilename = "filename.txt"
assert(result == expectedFilename) { "Expected $expectedFilename but got $result" }
}

@Test
fun testMiddleInvalidFolderChar() {
val forbiddenFilenameCharacters = capability.forbiddenFilenameCharacters()
val randomInvalidChar = forbiddenFilenameCharacters.random()

val folderPath = "abc/def/kg${randomInvalidChar}/lmo/pp"
val folderPath = "abc/def/kg$randomInvalidChar/lmo/pp"
val result = AutoRename.rename(folderPath, capability, true)
val expectedFolderName = "abc/def/kg_/lmo/pp"
assert(result == expectedFolderName) { "Expected $expectedFolderName but got $result" }
}

@Test
fun testEndInvalidFolderChar() {
val forbiddenFilenameCharacters = capability.forbiddenFilenameCharacters()
val randomInvalidChar = forbiddenFilenameCharacters.random()

val folderPath = "abc/def/kg/lmo/pp$randomInvalidChar"
val result = AutoRename.rename(folderPath, capability, true)
val expectedFolderName = "abc/def/kg/lmo/pp_"
assert(result == expectedFolderName) { "Expected $expectedFolderName but got $result" }
}

@Test
fun testStartInvalidFolderChar() {
val forbiddenFilenameCharacters = capability.forbiddenFilenameCharacters()
val randomInvalidChar = forbiddenFilenameCharacters.random()

val folderPath = "${randomInvalidChar}abc/def/kg/lmo/pp"
val result = AutoRename.rename(folderPath, capability, true)
val expectedFolderName = "_abc/def/kg/lmo/pp"
assert(result == expectedFolderName) { "Expected $expectedFolderName but got $result" }
}

@Test
fun testMixedInvalidChar() {
val forbiddenFilenameCharacters = capability.forbiddenFilenameCharacters()
val randomInvalidChar = forbiddenFilenameCharacters.random()

val filename = " file\u0001na${randomInvalidChar}me.txt "
val result = AutoRename.rename(filename, capability)
val expectedFilename = "filena_me.txt"
assert(result == expectedFilename) { "Expected $expectedFilename but got $result" }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ object AutoRename {
}

return if (capability.shouldRemoveNonPrintableUnicodeCharacters()) {
val nonPrintableUnicodeVersion = removeNonPrintableUnicodeCharacters(result)
convertToUTF8(nonPrintableUnicodeVersion)
result = convertToUTF8(result)
removeNonPrintableUnicodeCharacters(result)
} else {
result
}
Expand Down

0 comments on commit 4f91c97

Please sign in to comment.