Skip to content

Commit 4bf04fd

Browse files
committed
fix codacy
Signed-off-by: alperozturk96 <alper_ozturk@proton.me>
1 parent 5d232fe commit 4bf04fd

File tree

2 files changed

+58
-4
lines changed

2 files changed

+58
-4
lines changed

core/src/main/java/com/nextcloud/android/common/core/utils/ecosystem/EcosystemManager.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,11 @@ class EcosystemManager(
3838
private const val PLAY_STORE_LINK = "https://play.google.com/store/apps/details?id="
3939
private const val PLAY_STORE_MARKET_LINK = "market://details?id="
4040
private const val EXTRA_KEY_ACCOUNT = "KEY_ACCOUNT"
41+
42+
const val ACCOUNT_NAME_PATTERN_REGEX = "[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-z]{2,}"
4143
}
4244

43-
private val accountNamePattern =
44-
Pattern.compile(
45-
"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-z]{2,}"
46-
)
45+
private val accountNamePattern = Pattern.compile(ACCOUNT_NAME_PATTERN_REGEX)
4746

4847
/**
4948
* Opens an ecosystem app with the given account information.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Nextcloud Android Common Library
3+
*
4+
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
5+
* SPDX-License-Identifier: MIT
6+
*/
7+
8+
package com.nextcloud.android.common.core.utils
9+
10+
import com.nextcloud.android.common.core.utils.ecosystem.EcosystemManager
11+
import org.junit.Assert.assertFalse
12+
import org.junit.Assert.assertTrue
13+
import org.junit.Test
14+
import java.util.regex.Pattern
15+
16+
/**
17+
* Unit tests for validating account name format using the same
18+
* regular expression as {@link EcosystemManager}.
19+
*
20+
* These tests verify that:
21+
* - Valid account names (e.g. "abc@example.cloud.com") are accepted
22+
* - Invalid or malformed account names are rejected
23+
* - Edge cases such as empty or blank strings do not match
24+
*
25+
* The account name is expected to follow an email-like format and is
26+
* used when passing account information between ecosystem apps.
27+
*/
28+
class AccountNamePatternTest {
29+
private val pattern = Pattern.compile(EcosystemManager.ACCOUNT_NAME_PATTERN_REGEX)
30+
31+
@Test
32+
fun `valid account names should match`() {
33+
assertTrue(pattern.matcher("abc@example.cloud.com").matches())
34+
assertTrue(pattern.matcher("user.name+test@sub.domain.org").matches())
35+
assertTrue(pattern.matcher("user_123@test.co").matches())
36+
}
37+
38+
@Test
39+
fun `invalid account names should not match`() {
40+
assertFalse(pattern.matcher("abc").matches())
41+
assertFalse(pattern.matcher("abc@").matches())
42+
assertFalse(pattern.matcher("abc@example").matches())
43+
assertFalse(pattern.matcher("abc@example.").matches())
44+
assertFalse(pattern.matcher("abc@.com").matches())
45+
assertFalse(pattern.matcher("@example.com").matches())
46+
assertFalse(pattern.matcher("abc@example.c").matches())
47+
assertFalse(pattern.matcher("abc example@test.com").matches())
48+
}
49+
50+
@Test
51+
fun `empty or blank account names should not match`() {
52+
assertFalse(pattern.matcher("").matches())
53+
assertFalse(pattern.matcher(" ").matches())
54+
}
55+
}

0 commit comments

Comments
 (0)