TF-2305 Session::getOwnEmailAddress method to fetch the user email address from urn:ietf:params:jmap:principals imip mailto property if not present in the session username, which is the case for Cyrus
This commit is contained in:
committed by
Dat H. Pham
parent
327af38fb0
commit
6720709a2e
@@ -0,0 +1 @@
|
||||
class UnknownAddressException implements Exception {}
|
||||
@@ -2,15 +2,20 @@
|
||||
import 'dart:ui';
|
||||
|
||||
import 'package:core/presentation/extensions/uri_extension.dart';
|
||||
import 'package:get/get_utils/src/extensions/string_extensions.dart';
|
||||
import 'package:jmap_dart_client/jmap/account_id.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/capability/calendar_event_capability.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/capability/capability_identifier.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/capability/capability_properties.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/capability/default_capability.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/capability/empty_capability.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/id.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/session/session.dart';
|
||||
import 'package:model/error_type_handler/account_exception.dart';
|
||||
import 'package:model/error_type_handler/unknown_address_exception.dart';
|
||||
import 'package:model/error_type_handler/unknown_uri_exception.dart';
|
||||
import 'package:model/model.dart';
|
||||
import 'package:model/principals/capability_principals.dart';
|
||||
import 'package:uri/uri.dart';
|
||||
|
||||
extension SessionExtension on Session {
|
||||
@@ -66,6 +71,38 @@ extension SessionExtension on Session {
|
||||
}
|
||||
}
|
||||
|
||||
String getOwnEmailAddress() {
|
||||
return username.value.isEmail ? username.value
|
||||
: _getOwnEmailAddressFromPersonalAccount()
|
||||
?? _getOwnEmailAddressFromPrincipalsCapability()
|
||||
?? (throw UnknownAddressException());
|
||||
}
|
||||
|
||||
String? _getOwnEmailAddressFromPersonalAccount() {
|
||||
try {
|
||||
return personalAccount.name.value.isEmail ? personalAccount.name.value : null;
|
||||
} catch (_) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
String? _getOwnEmailAddressFromPrincipalsCapability() {
|
||||
try {
|
||||
var principalsCapability = getCapabilityProperties<DefaultCapability>(AccountId(Id(username.value)), capabilityPrincipals);
|
||||
final sendTo = principalsCapability?.properties?['urn:ietf:params:jmap:calendars']?['sendTo'];
|
||||
if (sendTo is Map<String, dynamic>) {
|
||||
final wrappedAddress = sendTo['imip'];
|
||||
if (wrappedAddress is String && wrappedAddress.startsWith('mailto:')) {
|
||||
String address = wrappedAddress.substring("mailto:".length);
|
||||
return address.isEmail ? address : null;
|
||||
}
|
||||
}
|
||||
} catch (_) {
|
||||
return null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
JmapAccount get personalAccount {
|
||||
if (accounts.isNotEmpty) {
|
||||
final listPersonalAccount = accounts.entries
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
import 'package:jmap_dart_client/jmap/core/capability/capability_identifier.dart';
|
||||
|
||||
final capabilityPrincipals = CapabilityIdentifier(Uri.parse('urn:ietf:params:jmap:principals'));
|
||||
@@ -5,12 +5,15 @@ import 'package:jmap_dart_client/jmap/account_id.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/account/account.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/capability/calendar_event_capability.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/capability/capability_identifier.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/capability/default_capability.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/capability/empty_capability.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/id.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/session/session.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/state.dart';
|
||||
import 'package:jmap_dart_client/jmap/core/user_name.dart';
|
||||
import 'package:model/error_type_handler/unknown_address_exception.dart';
|
||||
import 'package:model/extensions/session_extension.dart';
|
||||
import 'package:model/principals/capability_principals.dart';
|
||||
|
||||
void main() {
|
||||
final accountId = AccountId(Id('123abc'));
|
||||
@@ -80,7 +83,7 @@ void main() {
|
||||
|
||||
test(
|
||||
'should return null '
|
||||
'when calendar event capability supportes no language',
|
||||
'when calendar event capability supports no language',
|
||||
() {
|
||||
// arrange
|
||||
final calendarEventCapability = CalendarEventCapability();
|
||||
@@ -165,4 +168,122 @@ void main() {
|
||||
expect(calendarEventLanguage, 'vi');
|
||||
});
|
||||
});
|
||||
|
||||
group('getOwnEmailAddress test:', () {
|
||||
test(
|
||||
'should return username.value '
|
||||
'when it is a valid email and principals capability is absent',
|
||||
() {
|
||||
// arrange
|
||||
final session = Session({}, {}, {}, UserName('test@example.com'), Uri(), Uri(), Uri(), Uri(), State(''),);
|
||||
|
||||
// act
|
||||
final emailAddress = session.getOwnEmailAddress();
|
||||
|
||||
// assert
|
||||
expect(emailAddress, 'test@example.com');
|
||||
});
|
||||
|
||||
test(
|
||||
'should return username.value'
|
||||
'when it is a valid email and principals capability is present but has no address',
|
||||
() {
|
||||
// arrange
|
||||
final session = Session(
|
||||
{capabilityPrincipals: DefaultCapability({})},
|
||||
{}, {}, UserName('test@example.com'), Uri(), Uri(), Uri(), Uri(), State(''),
|
||||
);
|
||||
|
||||
// act
|
||||
final emailAddress = session.getOwnEmailAddress();
|
||||
|
||||
// assert
|
||||
expect(emailAddress, 'test@example.com');
|
||||
});
|
||||
|
||||
test(
|
||||
'should return username.value'
|
||||
'when it is a valid email and principals capability is present with an address',
|
||||
() {
|
||||
// arrange
|
||||
final session = Session(
|
||||
{capabilityPrincipals: DefaultCapability({'key': {'subKey': 'mailto:another@example.com'}})},
|
||||
{}, {}, UserName('test@example.com'), Uri(), Uri(), Uri(), Uri(), State(''),
|
||||
);
|
||||
|
||||
// act
|
||||
final emailAddress = session.getOwnEmailAddress();
|
||||
|
||||
// assert
|
||||
expect(emailAddress, 'test@example.com');
|
||||
});
|
||||
|
||||
test(
|
||||
'should throw exception'
|
||||
'when username is not a valid email and principals capability is absent',
|
||||
() {
|
||||
// arrange
|
||||
final session = Session({}, {}, {}, UserName('notAnEmail'), Uri(), Uri(), Uri(), Uri(), State(''));
|
||||
|
||||
// assert
|
||||
expect(() => session.getOwnEmailAddress(), throwsA(isA<UnknownAddressException>()));
|
||||
});
|
||||
|
||||
test(
|
||||
'should return null'
|
||||
'when username is not a valid email and principals capability is present but has no address',
|
||||
() {
|
||||
// arrange
|
||||
final session = Session(
|
||||
{capabilityPrincipals: DefaultCapability({})},
|
||||
{}, {}, UserName('notAnEmail'), Uri(), Uri(), Uri(), Uri(), State(''),
|
||||
);
|
||||
|
||||
// assert
|
||||
expect(() => session.getOwnEmailAddress(), throwsA(isA<UnknownAddressException>()));
|
||||
});
|
||||
|
||||
test(
|
||||
'should return null'
|
||||
'when username is not a valid email and principals capability is present but has no valid address',
|
||||
() {
|
||||
// arrange
|
||||
final session = Session(
|
||||
{capabilityPrincipals: DefaultCapability({'key': {'subKey': 'test@example.com'}})},
|
||||
{}, {}, UserName('notAnEmail'), Uri(), Uri(), Uri(), Uri(), State(''),
|
||||
);
|
||||
|
||||
// assert
|
||||
expect(() => session.getOwnEmailAddress(), throwsA(isA<UnknownAddressException>()));
|
||||
});
|
||||
|
||||
test(
|
||||
'should return email address from principals capability'
|
||||
'when username is not a valid email and principals capability is present with an address',
|
||||
() {
|
||||
// arrange
|
||||
final capability = DefaultCapability({
|
||||
"currentUserPrincipalId": "bob",
|
||||
"urn:ietf:params:jmap:calendars": {
|
||||
"accountId": "bob",
|
||||
"account": null,
|
||||
"mayGetAvailability": true,
|
||||
"sendTo": {
|
||||
"imip": "mailto:bob@example.com"
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
final session = Session(
|
||||
{capabilityPrincipals: capability},
|
||||
{}, {}, UserName('notAnEmail'), Uri(), Uri(), Uri(), Uri(), State(''),
|
||||
);
|
||||
|
||||
// act
|
||||
final emailAddress = session.getOwnEmailAddress();
|
||||
|
||||
// assert
|
||||
expect(emailAddress, 'bob@example.com');
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user