TF-2667 Write unit test for Domain & MailAddress class
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
|
||||
import 'package:core/utils/mail/domain.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
void main() {
|
||||
test('Domain.of(args) should not be case sensitive', () {
|
||||
expect(Domain.of('Domain'), equals(Domain.of('domain')));
|
||||
});
|
||||
|
||||
group('Domain.of(arg) should throw an AssertionError with a list of invalid domains', () {
|
||||
final listDomainInValid = [
|
||||
'domain\$bad.com',
|
||||
'',
|
||||
'aab..ddd',
|
||||
'aab.cc.1com',
|
||||
'abc.abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcd.com',
|
||||
'domain\$bad.com',
|
||||
'domain/bad.com',
|
||||
'domain\\bad.com',
|
||||
'domain@bad.com',
|
||||
'domain@bad.com',
|
||||
'domain%bad.com',
|
||||
'#domain.com',
|
||||
'bad-.com',
|
||||
'bad_.com',
|
||||
'-bad.com',
|
||||
'bad_.com',
|
||||
'[domain.tld',
|
||||
'domain.tld]',
|
||||
'a[aaa]a',
|
||||
'[aaa]a',
|
||||
'a[aaa]',
|
||||
'[]'
|
||||
];
|
||||
for (var arg in listDomainInValid) {
|
||||
test(arg, () {
|
||||
expect(() => Domain.of(arg), throwsA(const TypeMatcher<AssertionError>()));
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
group('Domain.of(arg) should not throw any exceptions with the list of valid domains', () {
|
||||
final listDomainValid = [
|
||||
'127.0.0.1',
|
||||
'domain.tld',
|
||||
'do-main.tld',
|
||||
'do_main.tld',
|
||||
'ab.dc.de.fr',
|
||||
'123.456.789.a23',
|
||||
'acv.abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabc.fr',
|
||||
'ab--cv.fr',
|
||||
'ab__cd.fr',
|
||||
'domain',
|
||||
'[domain]',
|
||||
'127.0.0.1'
|
||||
];
|
||||
for (var arg in listDomainValid) {
|
||||
test(arg, () {
|
||||
expect(() => Domain.of(arg), returnsNormally);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
test('Domain.of(args) should remove brackets', () {
|
||||
expect(Domain.of('[domain]'), equals(Domain.of('domain')));
|
||||
});
|
||||
|
||||
test('Domain.of(args) should throw AssertionError when args is null', () {
|
||||
expect(() => Domain.of(null), throwsA(const TypeMatcher<AssertionError>()));
|
||||
});
|
||||
|
||||
test('Domain.of(args) should allow 253 long domain', () {
|
||||
expect(Domain.of('${'aaaaaaaaa.' * 25}aaa').domainName.length, 253);
|
||||
});
|
||||
|
||||
test('Domain.of(args) should throw AssertionError when too long', () {
|
||||
expect(() => Domain.of('a' * 254), throwsA(const TypeMatcher<AssertionError>()));
|
||||
});
|
||||
}
|
||||
@@ -1,20 +1,85 @@
|
||||
import 'package:core/domain/exceptions/address_exception.dart';
|
||||
import 'package:core/utils/mail/domain.dart';
|
||||
import 'package:core/utils/mail/mail_address.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
void main() {
|
||||
group('MailAddress test', () {
|
||||
test('validate method should be return MailAddress when address valid', () {
|
||||
String validAddress = 'user@example.com';
|
||||
MailAddress mailAddress = MailAddress.validate(validAddress);
|
||||
const String GOOD_LOCAL_PART = "\"quoted@local part\"";
|
||||
const String GOOD_QUOTED_LOCAL_PART = "\"quoted@local part\"@james.apache.org";
|
||||
const String GOOD_ADDRESS = "server-dev@james.apache.org";
|
||||
final Domain GOOD_DOMAIN = Domain.of("james.apache.org");
|
||||
|
||||
final List<String> goodAddresses = [
|
||||
GOOD_ADDRESS,
|
||||
GOOD_QUOTED_LOCAL_PART,
|
||||
"server-dev@james-apache.org",
|
||||
"server-dev@[127.0.0.1]",
|
||||
"server.dev@james.apache.org",
|
||||
"\\.server-dev@james.apache.org",
|
||||
"Abc@10.42.0.1",
|
||||
"Abc.123@example.com",
|
||||
"user+mailbox/department=shipping@example.com",
|
||||
"user+mailbox@example.com",
|
||||
"\"Abc@def\"@example.com",
|
||||
"\"Fred Bloggs\"@example.com",
|
||||
"\"Joe.\\Blow\"@example.com",
|
||||
"!#\$%&'*+-/=?^_`.{|}~@example.com"
|
||||
];
|
||||
|
||||
final List<String> badAddresses = [
|
||||
"",
|
||||
"server-dev",
|
||||
"server-dev@",
|
||||
"[]",
|
||||
"server-dev@[]",
|
||||
"server-dev@#",
|
||||
"quoted local-part@james.apache.org",
|
||||
"quoted@local-part@james.apache.org",
|
||||
"local-part.@james.apache.org",
|
||||
".local-part@james.apache.org",
|
||||
"local-part@.james.apache.org",
|
||||
"local-part@james.apache.org.",
|
||||
"local-part@james.apache..org",
|
||||
"server-dev@-james.apache.org",
|
||||
"server-dev@james.apache.org-",
|
||||
"server-dev@#james.apache.org",
|
||||
"server-dev@#123james.apache.org",
|
||||
"server-dev@#-123.james.apache.org",
|
||||
"server-dev@james. apache.org",
|
||||
"server-dev@james\\.apache.org",
|
||||
"server-dev@[300.0.0.1]",
|
||||
"server-dev@[127.0.1]",
|
||||
"server-dev@[0127.0.0.1]",
|
||||
"server-dev@[127.0.1.1a]",
|
||||
"server-dev@[127\\.0.1.1]",
|
||||
"server-dev@#123",
|
||||
"server-dev@#123.apache.org",
|
||||
"server-dev@[127.0.1.1.1]",
|
||||
"server-dev@[127.0.1.-1]",
|
||||
"\"a..b\"@domain.com", // jakarta.mail is unable to handle this so we better reject it
|
||||
"server-dev\\.@james.apache.org", // jakarta.mail is unable to handle this so we better reject it
|
||||
"a..b@domain.com",
|
||||
// According to wikipedia these addresses are valid but as jakarta.mail is unable
|
||||
// to work with them we shall rather reject them (note that this is not breaking retro-compatibility)
|
||||
"Loïc.Accentué@voilà.fr8",
|
||||
"pelé@exemple.com",
|
||||
"δοκιμή@παράδειγμα.δοκιμή",
|
||||
"我買@屋企.香港",
|
||||
"二ノ宮@黒川.日本",
|
||||
"медведь@с-балалайкой.рф",
|
||||
"संपर्क@डाटामेल.भारत"
|
||||
];
|
||||
|
||||
group('MailAddress simple test', () {
|
||||
test('MailAddress.validateAddress() should be return MailAddress when address valid', () {
|
||||
MailAddress mailAddress = MailAddress.validateAddress('user@example.com');
|
||||
expect(mailAddress.localPart, equals('user'));
|
||||
expect(mailAddress.domain.name(), equals('example.com'));
|
||||
});
|
||||
|
||||
test('validate method should be throw AddressException when address missing @', () {
|
||||
String invalidAddress = 'userexample.com';
|
||||
test('MailAddress.validateAddress() should be throw AddressException when address missing @', () {
|
||||
expect(
|
||||
() => MailAddress.validate(invalidAddress),
|
||||
() => MailAddress.validateAddress('userexample.com'),
|
||||
throwsA(isA<AddressException>().having(
|
||||
(e) => e.message,
|
||||
'message',
|
||||
@@ -23,10 +88,9 @@ void main() {
|
||||
);
|
||||
});
|
||||
|
||||
test('validate method should be throw AddressException when address empty local-part', () {
|
||||
String invalidAddress = '@example.com';
|
||||
test('MailAddress.validateAddress() should be throw AddressException when address empty local-part', () {
|
||||
expect(
|
||||
() => MailAddress.validate(invalidAddress),
|
||||
() => MailAddress.validateAddress('@example.com'),
|
||||
throwsA(isA<AddressException>().having(
|
||||
(e) => e.message,
|
||||
'message',
|
||||
@@ -35,10 +99,9 @@ void main() {
|
||||
);
|
||||
});
|
||||
|
||||
test('validate method should be throw AddressException when address empty domain', () {
|
||||
String invalidAddress = 'user@';
|
||||
test('MailAddress.validateAddress() should be throw AddressException when address empty domain', () {
|
||||
expect(
|
||||
() => MailAddress.validate(invalidAddress),
|
||||
() => MailAddress.validateAddress('user@'),
|
||||
throwsA(isA<AddressException>().having(
|
||||
(e) => e.message,
|
||||
'message',
|
||||
@@ -47,10 +110,9 @@ void main() {
|
||||
);
|
||||
});
|
||||
|
||||
test('validate method should be throw AddressException when address with a hyphen "-"', () {
|
||||
String invalidAddress = 'user@-example.com';
|
||||
test('MailAddress.validateAddress() should be throw AddressException when address with a hyphen "-"', () {
|
||||
expect(
|
||||
() => MailAddress.validate(invalidAddress),
|
||||
() => MailAddress.validateAddress('user@-example.com'),
|
||||
throwsA(isA<AddressException>().having(
|
||||
(e) => e.message,
|
||||
'message',
|
||||
@@ -59,4 +121,49 @@ void main() {
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
group('MailAddress advanced test', () {
|
||||
group('MailAddress.validateAddress() should not throw any exceptions with the list of good address', () {
|
||||
for (var arg in goodAddresses) {
|
||||
test(arg, () {
|
||||
expect(() => MailAddress.validateAddress(arg), returnsNormally);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
group('MailAddress.validateAddress() should throw an AddressException with a list of bad address', () {
|
||||
for (var arg in badAddresses) {
|
||||
test(arg, () {
|
||||
expect(() => MailAddress.validateAddress(arg), throwsA(const TypeMatcher<AddressException>()));
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
test('MailAddress.validateLocalPartAndDomain() should not throw any exceptions with good address have LocalPart and Domain', () {
|
||||
expect(() => MailAddress.validateLocalPartAndDomain(localPart: 'local-part', domain: 'domain'), returnsNormally);
|
||||
});
|
||||
|
||||
test('MailAddress.validateLocalPartAndDomain() should throw an AddressException with bad address have LocalPart and Domain', () {
|
||||
expect(() => MailAddress.validateLocalPartAndDomain(localPart: 'local-part', domain: '-domain'), throwsA(const TypeMatcher<AddressException>()));
|
||||
});
|
||||
|
||||
test('MailAddress.validateAddress() should not throw any exceptions with mail address is GOOD_QUOTED_LOCAL_PART', () {
|
||||
expect(() => MailAddress.validateAddress(GOOD_QUOTED_LOCAL_PART), returnsNormally);
|
||||
});
|
||||
|
||||
test('MailAddress.getDomain() should return GOOD_DOMAIN with address is GOOD_ADDRESS', () {
|
||||
final mailAddress = MailAddress.validateAddress(GOOD_ADDRESS);
|
||||
expect(mailAddress.getDomain(), equals(GOOD_DOMAIN));
|
||||
});
|
||||
|
||||
test('MailAddress.getLocalPart() should return GOOD_LOCAL_PART with address is GOOD_QUOTED_LOCAL_PART', () {
|
||||
final mailAddress = MailAddress.validateAddress(GOOD_QUOTED_LOCAL_PART);
|
||||
expect(mailAddress.getLocalPart(), equals(GOOD_LOCAL_PART));
|
||||
});
|
||||
|
||||
test('MailAddress.toString() should return GOOD_ADDRESS with address is GOOD_ADDRESS', () {
|
||||
final mailAddress = MailAddress.validateAddress(GOOD_ADDRESS);
|
||||
expect(mailAddress.toString(), equals(GOOD_ADDRESS));
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user