|
| 1 | +package uk.gov.communities.prsdb.webapp.validation |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Named |
| 4 | +import org.junit.jupiter.api.Nested |
| 5 | +import org.junit.jupiter.api.Test |
| 6 | +import org.junit.jupiter.params.ParameterizedTest |
| 7 | +import org.junit.jupiter.params.provider.MethodSource |
| 8 | +import kotlin.test.assertFalse |
| 9 | +import kotlin.test.assertTrue |
| 10 | + |
| 11 | +class EmailConstraintValidatorTests { |
| 12 | + companion object { |
| 13 | + @JvmStatic |
| 14 | + fun provideValidEmails() = |
| 15 | + arrayOf( |
| 16 | + Named.of("simple address", "test@example.com"), |
| 17 | + Named.of("with dots in local part", "first.last@example.com"), |
| 18 | + Named.of("with plus in local part", "user+tag@example.com"), |
| 19 | + Named.of("with hyphen in domain", "user@my-domain.com"), |
| 20 | + Named.of("with subdomain", "user@mail.example.com"), |
| 21 | + Named.of("with apostrophe in local part", "firstname-o'surname@domain.com"), |
| 22 | + Named.of("with special chars in local part", "user!#\$%&'*+/=?^_`{|}~@example.com"), |
| 23 | + Named.of("single char local part", "a@example.com"), |
| 24 | + Named.of("numeric local part", "123@example.com"), |
| 25 | + Named.of("long TLD", "user@example.museum"), |
| 26 | + Named.of("two letter TLD", "user@example.uk"), |
| 27 | + ) |
| 28 | + |
| 29 | + @JvmStatic |
| 30 | + fun provideInvalidEmails() = |
| 31 | + arrayOf( |
| 32 | + Named.of("null value", null), |
| 33 | + Named.of("miscellaneous whitespace", " \u180e \u200b"), |
| 34 | + Named.of("empty string", ""), |
| 35 | + Named.of("no @ sign", "userexample.com"), |
| 36 | + Named.of("no domain", "user@"), |
| 37 | + Named.of("no local part", "@example.com"), |
| 38 | + Named.of("consecutive dots in local part", "user..name@example.com"), |
| 39 | + Named.of("consecutive dots in domain", "user@example..com"), |
| 40 | + Named.of("domain starts with dot", "user@.example.com"), |
| 41 | + Named.of("no TLD", "user@localhost"), |
| 42 | + Named.of("space in address", "user @example.com"), |
| 43 | + Named.of("obscure whitespace in address", "user\u180e@example.com"), |
| 44 | + Named.of("double quote in local part", "user\"name@example.com"), |
| 45 | + Named.of("semicolon in local part", "user;name@example.com"), |
| 46 | + Named.of("single letter TLD", "user@example.a"), |
| 47 | + Named.of("numeric TLD", "user@example.123"), |
| 48 | + Named.of("multiple @ signs", "user@@example.com"), |
| 49 | + Named.of("domain part over 63 chars", "user@${"a".repeat(64)}.com"), |
| 50 | + ) |
| 51 | + } |
| 52 | + |
| 53 | + @ParameterizedTest(name = "{0}") |
| 54 | + @MethodSource("provideValidEmails") |
| 55 | + fun `isValid returns true for valid email with`(input: String) { |
| 56 | + assertTrue(EmailConstraintValidator().isValid(input)) |
| 57 | + } |
| 58 | + |
| 59 | + @ParameterizedTest(name = "{0}") |
| 60 | + @MethodSource("provideInvalidEmails") |
| 61 | + fun `isValid returns false for invalid email with`(input: String?) { |
| 62 | + assertFalse(EmailConstraintValidator().isValid(input)) |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + fun `isValid returns false for email longer than 320 characters`() { |
| 67 | + val longLocal = "a".repeat(310) |
| 68 | + val email = "$longLocal@example.com" |
| 69 | + assertTrue(email.length > 320) |
| 70 | + assertFalse(EmailConstraintValidator().isValid(email)) |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + fun `isValid returns false for hostname longer than 253 characters`() { |
| 75 | + val longHostname = (1..42).joinToString(".") { "abcde" } |
| 76 | + val email = "user@$longHostname.com" |
| 77 | + assertFalse(EmailConstraintValidator().isValid(email)) |
| 78 | + } |
| 79 | + |
| 80 | + @Nested |
| 81 | + inner class OptionalEmailConstraintValidatorTests { |
| 82 | + @Test |
| 83 | + fun `isValid returns true for null`() { |
| 84 | + assertTrue(OptionalEmailConstraintValidator().isValid(null)) |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + fun `isValid returns true for blank string`() { |
| 89 | + assertTrue(OptionalEmailConstraintValidator().isValid("")) |
| 90 | + assertTrue(OptionalEmailConstraintValidator().isValid(" ")) |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + fun `isValid returns true for valid email`() { |
| 95 | + assertTrue(OptionalEmailConstraintValidator().isValid("user@example.com")) |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + fun `isValid returns false for invalid email`() { |
| 100 | + assertFalse(OptionalEmailConstraintValidator().isValid("not-an-email")) |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments