|
| 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