TF-2678 Create server settings API & exception
This commit is contained in:
@@ -0,0 +1,101 @@
|
|||||||
|
import 'package:jmap_dart_client/http/http_client.dart';
|
||||||
|
import 'package:jmap_dart_client/jmap/account_id.dart';
|
||||||
|
import 'package:jmap_dart_client/jmap/core/error/method/exception/error_method_response_exception.dart';
|
||||||
|
import 'package:jmap_dart_client/jmap/jmap_request.dart';
|
||||||
|
import 'package:server_settings/server_settings/get/get_server_settings_method.dart';
|
||||||
|
import 'package:server_settings/server_settings/get/get_server_settings_response.dart';
|
||||||
|
import 'package:server_settings/server_settings/server_settings_id.dart';
|
||||||
|
import 'package:server_settings/server_settings/set/set_server_settings_method.dart';
|
||||||
|
import 'package:server_settings/server_settings/set/set_server_settings_response.dart';
|
||||||
|
import 'package:server_settings/server_settings/tmail_server_settings.dart';
|
||||||
|
import 'package:tmail_ui_user/features/base/mixin/handle_error_mixin.dart';
|
||||||
|
import 'package:tmail_ui_user/features/server_settings/domain/exceptions/server_settings_exception.dart';
|
||||||
|
|
||||||
|
class ServerSettingsAPI with HandleSetErrorMixin {
|
||||||
|
final HttpClient httpClient;
|
||||||
|
|
||||||
|
ServerSettingsAPI(this.httpClient);
|
||||||
|
|
||||||
|
Future<TMailServerSettings> getServerSettings(AccountId accountId) async {
|
||||||
|
final processingInvocation = ProcessingInvocation();
|
||||||
|
|
||||||
|
final jmapRequestBuilder = JmapRequestBuilder(httpClient, processingInvocation);
|
||||||
|
|
||||||
|
final getServerSettingsMethod = GetServerSettingsMethod(accountId);
|
||||||
|
|
||||||
|
final queryInvocation = jmapRequestBuilder.invocation(getServerSettingsMethod);
|
||||||
|
|
||||||
|
final capabilities = getServerSettingsMethod.requiredCapabilities;
|
||||||
|
|
||||||
|
final result = await (jmapRequestBuilder..usings(capabilities))
|
||||||
|
.build()
|
||||||
|
.execute();
|
||||||
|
|
||||||
|
final serverSettings = result.parse<GetServerSettingsResponse>(
|
||||||
|
queryInvocation.methodCallId,
|
||||||
|
GetServerSettingsResponse.deserialize
|
||||||
|
)?.list.firstOrNull;
|
||||||
|
|
||||||
|
if (serverSettings == null) {
|
||||||
|
throw NotFoundServerSettingsException();
|
||||||
|
}
|
||||||
|
|
||||||
|
return serverSettings;
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<TMailServerSettings> updateServerSettings(
|
||||||
|
AccountId accountId,
|
||||||
|
TMailServerSettings serverSettings
|
||||||
|
) async {
|
||||||
|
final processingInvocation = ProcessingInvocation();
|
||||||
|
|
||||||
|
final setServerSettingsMethod = SetServerSettingsMethod(accountId)
|
||||||
|
..addUpdatesSingleton({
|
||||||
|
ServerSettingsIdExtension.serverSettingsIdSingleton.id: serverSettings
|
||||||
|
});
|
||||||
|
|
||||||
|
final jmapRequestBuilder = JmapRequestBuilder(httpClient, processingInvocation);
|
||||||
|
final setServerSettigsInvocation = jmapRequestBuilder.invocation(
|
||||||
|
setServerSettingsMethod);
|
||||||
|
|
||||||
|
final getServerSettingsMethod = GetServerSettingsMethod(accountId)
|
||||||
|
..addIds({ServerSettingsIdExtension.serverSettingsIdSingleton.id});
|
||||||
|
|
||||||
|
final getServerSettingsInvocation = jmapRequestBuilder.invocation(
|
||||||
|
getServerSettingsMethod);
|
||||||
|
|
||||||
|
final capabilities = setServerSettingsMethod.requiredCapabilities;
|
||||||
|
|
||||||
|
final result = await (jmapRequestBuilder..usings(capabilities))
|
||||||
|
.build()
|
||||||
|
.execute();
|
||||||
|
|
||||||
|
try {
|
||||||
|
final updateServerSettingsResult = result.parse<SetServerSettingsResponse>(
|
||||||
|
setServerSettigsInvocation.methodCallId,
|
||||||
|
SetServerSettingsResponse.deserialize
|
||||||
|
)?.updated ?? {};
|
||||||
|
|
||||||
|
if (updateServerSettingsResult.isEmpty) {
|
||||||
|
throw CanNotUpdateServerSettingsException();
|
||||||
|
}
|
||||||
|
} on ErrorMethodResponseException catch (_) {
|
||||||
|
throw CanNotUpdateServerSettingsException();
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
final updatedServerSettings = result.parse<GetServerSettingsResponse>(
|
||||||
|
getServerSettingsInvocation.methodCallId,
|
||||||
|
GetServerSettingsResponse.deserialize
|
||||||
|
)?.list.firstOrNull;
|
||||||
|
|
||||||
|
if (updatedServerSettings == null) {
|
||||||
|
throw CanNotUpdateServerSettingsException();
|
||||||
|
}
|
||||||
|
|
||||||
|
return updatedServerSettings;
|
||||||
|
} on ErrorMethodResponseException catch (_) {
|
||||||
|
throw CanNotUpdateServerSettingsException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
class NotFoundServerSettingsException implements Exception {}
|
||||||
|
|
||||||
|
class CanNotUpdateServerSettingsException implements Exception {}
|
||||||
@@ -0,0 +1,104 @@
|
|||||||
|
import 'package:dio/dio.dart';
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:http_mock_adapter/http_mock_adapter.dart';
|
||||||
|
import 'package:jmap_dart_client/http/http_client.dart';
|
||||||
|
import 'package:jmap_dart_client/jmap/account_id.dart';
|
||||||
|
import 'package:jmap_dart_client/jmap/core/id.dart';
|
||||||
|
import 'package:server_settings/server_settings/server_settings_id.dart';
|
||||||
|
import 'package:server_settings/server_settings/tmail_server_settings.dart';
|
||||||
|
import 'package:tmail_ui_user/features/server_settings/data/network/server_settings_api.dart';
|
||||||
|
import 'package:tmail_ui_user/features/server_settings/domain/exceptions/server_settings_exception.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
final newServerSettings = TMailServerSettings(
|
||||||
|
id: ServerSettingsIdExtension.serverSettingsIdSingleton,
|
||||||
|
settings: TMailServerSettingOptions(alwaysReadReceipts: true),
|
||||||
|
);
|
||||||
|
group('server settings api', () {
|
||||||
|
test('should throw CanNotUpdateServerSettingsException '
|
||||||
|
'when module throw exception on parse on update settings', () async {
|
||||||
|
// arrange
|
||||||
|
final baseOption = BaseOptions(method: 'POST');
|
||||||
|
final dio = Dio(baseOption)..options.baseUrl = 'http://domain.com/jmap';
|
||||||
|
final dioAdapter = DioAdapter(dio: dio);
|
||||||
|
dioAdapter.onPost(
|
||||||
|
'',
|
||||||
|
(server) => server.reply(
|
||||||
|
200,
|
||||||
|
{
|
||||||
|
"sessionState": "abcdefghij",
|
||||||
|
"methodResponses": [
|
||||||
|
[
|
||||||
|
"error",
|
||||||
|
{
|
||||||
|
"type": "invalidArguments",
|
||||||
|
"description": "'/accountId' property is not valid: Predicate failed: '' contains some invalid characters. Should be [#a-zA-Z0-9-_] and no longer than 255 chars."
|
||||||
|
},
|
||||||
|
"c0"
|
||||||
|
],
|
||||||
|
[
|
||||||
|
"Settings/get",
|
||||||
|
{
|
||||||
|
"accountId": "123",
|
||||||
|
"notFound": [],
|
||||||
|
"state": "1",
|
||||||
|
"list": [
|
||||||
|
{
|
||||||
|
"id": "singleton",
|
||||||
|
"settings": {"read.receipts.always": "true"}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"c1"
|
||||||
|
]
|
||||||
|
]
|
||||||
|
},
|
||||||
|
),
|
||||||
|
data: {
|
||||||
|
"using": [
|
||||||
|
"urn:ietf:params:jmap:core",
|
||||||
|
"com:linagora:params:jmap:settings"
|
||||||
|
],
|
||||||
|
"methodCalls": [
|
||||||
|
[
|
||||||
|
"Settings/set",
|
||||||
|
{
|
||||||
|
"accountId": "123",
|
||||||
|
"update": {
|
||||||
|
"singleton": {
|
||||||
|
"id": "singleton",
|
||||||
|
"settings": {"read.receipts.always": "true"}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"c0"
|
||||||
|
],
|
||||||
|
[
|
||||||
|
"Settings/get",
|
||||||
|
{
|
||||||
|
"accountId": "123",
|
||||||
|
"ids": ["singleton"]
|
||||||
|
},
|
||||||
|
"c1"
|
||||||
|
]
|
||||||
|
]
|
||||||
|
},
|
||||||
|
headers: {
|
||||||
|
"accept": "application/json;jmapVersion=rfc-8621",
|
||||||
|
"content-length": 503
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
final httpClient = HttpClient(dio);
|
||||||
|
final serverSettingsAPI = ServerSettingsAPI(httpClient);
|
||||||
|
final accountId = AccountId(Id('123'));
|
||||||
|
|
||||||
|
// assert
|
||||||
|
expect(
|
||||||
|
serverSettingsAPI.updateServerSettings(
|
||||||
|
accountId,
|
||||||
|
newServerSettings),
|
||||||
|
throwsA(isA<CanNotUpdateServerSettingsException>()));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user