From 66b09949053f25c4f0f56579c5017e769a446f9c Mon Sep 17 00:00:00 2001 From: Dat PHAM HOANG Date: Thu, 15 Aug 2024 18:03:54 +0700 Subject: [PATCH] Add more test for saveBaseUrl and toQualifiedUrl --- .../extensions/uri_extension_test.dart | 10 +++ .../credential_repository_imp_test.dart | 63 +++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 test/features/login/data/repository/credential_repository_imp_test.dart diff --git a/core/test/presentation/extensions/uri_extension_test.dart b/core/test/presentation/extensions/uri_extension_test.dart index 80691a3c62..06d24c50a7 100644 --- a/core/test/presentation/extensions/uri_extension_test.dart +++ b/core/test/presentation/extensions/uri_extension_test.dart @@ -85,5 +85,15 @@ void main() { expect(qualifiedUrlResult, equals(qualifiedUrlExpected)); }); + + test('convertToQualifiedUrl() should return qualified url when baseUrl is localhost and sourceUrl is `https://domain.com:2001/basiauth/jmap`', () async { + final baseUrl = Uri.parse('https://localhost:9080/basicauth'); + final sourceUrl = Uri.parse('https://domain.com:2001/basicauth/jmap'); + + final qualifiedUrlExpected = Uri.parse('https://domain.com:2001/basicauth/jmap'); + final qualifiedUrlResult = sourceUrl.toQualifiedUrl(baseUrl: baseUrl); + + expect(qualifiedUrlResult, equals(qualifiedUrlExpected)); + }); }); } \ No newline at end of file diff --git a/test/features/login/data/repository/credential_repository_imp_test.dart b/test/features/login/data/repository/credential_repository_imp_test.dart new file mode 100644 index 0000000000..a24d0cb097 --- /dev/null +++ b/test/features/login/data/repository/credential_repository_imp_test.dart @@ -0,0 +1,63 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:mockito/annotations.dart'; +import 'package:shared_preferences/shared_preferences.dart'; +import 'package:tmail_ui_user/features/login/data/local/authentication_info_cache_manager.dart'; +import 'package:tmail_ui_user/features/login/data/repository/credential_repository_impl.dart'; + +import 'credential_repository_imp_test.mocks.dart'; + +@GenerateMocks([AuthenticationInfoCacheManager]) +void main() { + late SharedPreferences sharedPreferences; + late AuthenticationInfoCacheManager authenticationInfoCacheManager; + late CredentialRepositoryImpl credentialRepositoryImpl; + + setUp(() async { + SharedPreferences.setMockInitialValues({}); + sharedPreferences = await SharedPreferences.getInstance(); + authenticationInfoCacheManager = MockAuthenticationInfoCacheManager(); + credentialRepositoryImpl = CredentialRepositoryImpl(sharedPreferences, authenticationInfoCacheManager); + }); + + group('test saveBaseUrl', () { + test('baseUrl should be save exactly when url has no sub path', () async { + final url = Uri.parse('https://example.com'); + credentialRepositoryImpl.saveBaseUrl(url); + + final storedUrl = await credentialRepositoryImpl.getBaseUrl(); + expect(storedUrl, url); + }); + + test('baseUrl should be save exactly when url has sub path', () async { + final url = Uri.parse('https://example.com/basicauth'); + credentialRepositoryImpl.saveBaseUrl(url); + + final storedUrl = await credentialRepositoryImpl.getBaseUrl(); + expect(storedUrl, url); + }); + + test('baseUrl should be save exactly when url has sub port', () async { + final url = Uri.parse('https://example.com:8080/'); + credentialRepositoryImpl.saveBaseUrl(url); + + final storedUrl = await credentialRepositoryImpl.getBaseUrl(); + expect(storedUrl, url); + }); + + test('baseUrl should be save exactly when url has port and sub path', () async { + final url = Uri.parse('https://example.com:8080/basicauth'); + credentialRepositoryImpl.saveBaseUrl(url); + + final storedUrl = await credentialRepositoryImpl.getBaseUrl(); + expect(storedUrl, url); + }); + + test('baseUrl should be save exactly when url has multiple sub paths', () async { + final url = Uri.parse('https://example.com:8080/basicauth/jmap'); + credentialRepositoryImpl.saveBaseUrl(url); + + final storedUrl = await credentialRepositoryImpl.getBaseUrl(); + expect(storedUrl, url); + }); + }); +} \ No newline at end of file