From 90fe357a30c0daa81e169e779bbc2e00461133f1 Mon Sep 17 00:00:00 2001 From: Rohith Gilla Date: Thu, 19 Sep 2024 10:15:48 +0530 Subject: [PATCH] =?UTF-8?q?feat(auth):=20=F0=9F=94=90=20Add=20UserProfile?= =?UTF-8?q?=20serialization=20and=20include=20in=20Credentials?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add userProfile to Credentials.toMap() method - Implement toMap() method in UserProfile class - Update existing tests and add new UserProfile tests --- .../lib/src/credentials.dart | 1 + .../lib/src/user_profile.dart | 25 ++++ .../test/credentials_test.dart | 16 ++ ...thod_channel_credentials_manager_test.dart | 4 +- .../test/user_profile_test.dart | 137 ++++++++++++++++++ 5 files changed, 181 insertions(+), 2 deletions(-) create mode 100644 auth0_flutter_platform_interface/test/user_profile_test.dart diff --git a/auth0_flutter_platform_interface/lib/src/credentials.dart b/auth0_flutter_platform_interface/lib/src/credentials.dart index 156bd870..b187e931 100644 --- a/auth0_flutter_platform_interface/lib/src/credentials.dart +++ b/auth0_flutter_platform_interface/lib/src/credentials.dart @@ -76,6 +76,7 @@ class Credentials { 'refreshToken': refreshToken, 'expiresAt': expiresAt.toUtc().toIso8601String(), 'scopes': scopes.toList(), + 'userProfile': user.toMap(), 'tokenType': tokenType, }; } diff --git a/auth0_flutter_platform_interface/lib/src/user_profile.dart b/auth0_flutter_platform_interface/lib/src/user_profile.dart index 5f5ae4fb..3a17943c 100644 --- a/auth0_flutter_platform_interface/lib/src/user_profile.dart +++ b/auth0_flutter_platform_interface/lib/src/user_profile.dart @@ -163,4 +163,29 @@ class UserProfile { result['custom_claims'] as Map) : null, ); + + + Map toMap() => { + 'sub': sub, + 'name': name, + 'given_name': givenName, + 'family_name': familyName, + 'middle_name': middleName, + 'nickname': nickname, + 'preferred_username': preferredUsername, + 'profile': profileUrl?.toString(), + 'picture': pictureUrl?.toString(), + 'website': websiteUrl?.toString(), + 'email': email, + 'email_verified': isEmailVerified, + 'gender': gender, + 'birthdate': birthdate, + 'zoneinfo': zoneinfo, + 'locale': locale, + 'phone_number': phoneNumber, + 'phone_number_verified': isPhoneNumberVerified, + 'address': address, + 'updated_at': updatedAt?.toIso8601String(), + 'custom_claims': customClaims, + }; } diff --git a/auth0_flutter_platform_interface/test/credentials_test.dart b/auth0_flutter_platform_interface/test/credentials_test.dart index c9a63930..f16cac91 100644 --- a/auth0_flutter_platform_interface/test/credentials_test.dart +++ b/auth0_flutter_platform_interface/test/credentials_test.dart @@ -20,6 +20,15 @@ void main() { }); expect(credentials.expiresAt.isUtc, true); + expect( + credentials.user.toMap(), + UserProfile.fromMap( + { + 'sub': '123', + 'name': 'John Doe', + }, + ).toMap(), + ); }); test('Credentials throws when expiresAt Locale set to ar', () async { @@ -74,6 +83,13 @@ void main() { tokenType: 'Bearer'); expect(credentials.toMap()['expiresAt'], '2023-11-01T22:16:35.760Z'); + expect( + credentials.toMap()['userProfile'], + { + 'sub': '123', + 'name': 'John Doe', + }, + ); }); }); } diff --git a/auth0_flutter_platform_interface/test/method_channel_credentials_manager_test.dart b/auth0_flutter_platform_interface/test/method_channel_credentials_manager_test.dart index eecbad68..89cf3d0b 100644 --- a/auth0_flutter_platform_interface/test/method_channel_credentials_manager_test.dart +++ b/auth0_flutter_platform_interface/test/method_channel_credentials_manager_test.dart @@ -266,8 +266,8 @@ void main() { expect(verificationResult.arguments['credentials']['scopes'], ['a']); expect( verificationResult.arguments['credentials']['tokenType'], 'Bearer'); - expect( - verificationResult.arguments['credentials']['userProfile'], isNull); + expect(verificationResult.arguments['credentials']['userProfile'], + isNotNull); }); test( diff --git a/auth0_flutter_platform_interface/test/user_profile_test.dart b/auth0_flutter_platform_interface/test/user_profile_test.dart new file mode 100644 index 00000000..8061ac83 --- /dev/null +++ b/auth0_flutter_platform_interface/test/user_profile_test.dart @@ -0,0 +1,137 @@ +import 'package:auth0_flutter_platform_interface/auth0_flutter_platform_interface.dart'; +import 'package:flutter_test/flutter_test.dart'; + +void main() { + group('UserProfile', () { + test('fromMap correctly maps all properties', () { + final map = { + 'sub': 'user123', + 'name': 'John Doe', + 'given_name': 'John', + 'family_name': 'Doe', + 'middle_name': 'Smith', + 'nickname': 'Johnny', + 'preferred_username': 'john.doe', + 'profile': 'https://example.com/profile', + 'picture': 'https://example.com/picture.jpg', + 'website': 'https://johndoe.com', + 'email': 'john@example.com', + 'email_verified': true, + 'gender': 'male', + 'birthdate': '1990-01-01', + 'zoneinfo': 'America/New_York', + 'locale': 'en-US', + 'phone_number': '+1234567890', + 'phone_number_verified': true, + 'address': {'country': 'USA', 'postal_code': '12345'}, + 'updated_at': '2023-01-01T00:00:00.000Z', + 'custom_claims': {'role': 'admin'} + }; + + final userProfile = UserProfile.fromMap(map); + + expect(userProfile.sub, 'user123'); + expect(userProfile.name, 'John Doe'); + expect(userProfile.givenName, 'John'); + expect(userProfile.familyName, 'Doe'); + expect(userProfile.middleName, 'Smith'); + expect(userProfile.nickname, 'Johnny'); + expect(userProfile.preferredUsername, 'john.doe'); + expect(userProfile.profileUrl, Uri.parse('https://example.com/profile')); + expect( + userProfile.pictureUrl, Uri.parse('https://example.com/picture.jpg')); + expect(userProfile.websiteUrl, Uri.parse('https://johndoe.com')); + expect(userProfile.email, 'john@example.com'); + expect(userProfile.isEmailVerified, true); + expect(userProfile.gender, 'male'); + expect(userProfile.birthdate, '1990-01-01'); + expect(userProfile.zoneinfo, 'America/New_York'); + expect(userProfile.locale, 'en-US'); + expect(userProfile.phoneNumber, '+1234567890'); + expect(userProfile.isPhoneNumberVerified, true); + expect(userProfile.address, {'country': 'USA', 'postal_code': '12345'}); + expect(userProfile.updatedAt, DateTime.parse('2023-01-01T00:00:00.000Z')); + expect(userProfile.customClaims, {'role': 'admin'}); + }); + + test('fromMap handles missing optional properties', () { + final map = { + 'sub': 'user123', + 'email': 'john@example.com', + }; + + final userProfile = UserProfile.fromMap(map); + + expect(userProfile.sub, 'user123'); + expect(userProfile.email, 'john@example.com'); + expect(userProfile.name, isNull); + expect(userProfile.givenName, isNull); + expect(userProfile.familyName, isNull); + expect(userProfile.middleName, isNull); + expect(userProfile.nickname, isNull); + expect(userProfile.preferredUsername, isNull); + expect(userProfile.profileUrl, isNull); + expect(userProfile.pictureUrl, isNull); + expect(userProfile.websiteUrl, isNull); + expect(userProfile.isEmailVerified, isNull); + expect(userProfile.gender, isNull); + expect(userProfile.birthdate, isNull); + expect(userProfile.zoneinfo, isNull); + expect(userProfile.locale, isNull); + expect(userProfile.phoneNumber, isNull); + expect(userProfile.isPhoneNumberVerified, isNull); + expect(userProfile.address, isNull); + expect(userProfile.updatedAt, isNull); + expect(userProfile.customClaims, isNull); + }); + + test('toMap correctly converts all properties', () { + final userProfile = UserProfile( + sub: 'user123', + name: 'John Doe', + givenName: 'John', + familyName: 'Doe', + middleName: 'Smith', + nickname: 'Johnny', + preferredUsername: 'john.doe', + profileUrl: Uri.parse('https://example.com/profile'), + pictureUrl: Uri.parse('https://example.com/picture.jpg'), + websiteUrl: Uri.parse('https://johndoe.com'), + email: 'john@example.com', + isEmailVerified: true, + gender: 'male', + birthdate: '1990-01-01', + zoneinfo: 'America/New_York', + locale: 'en-US', + phoneNumber: '+1234567890', + isPhoneNumberVerified: true, + address: {'country': 'USA', 'postal_code': '12345'}, + updatedAt: DateTime.parse('2023-01-01T00:00:00.000Z'), + customClaims: {'role': 'admin'}); + + final map = userProfile.toMap(); + + expect(map['sub'], 'user123'); + expect(map['name'], 'John Doe'); + expect(map['given_name'], 'John'); + expect(map['family_name'], 'Doe'); + expect(map['middle_name'], 'Smith'); + expect(map['nickname'], 'Johnny'); + expect(map['preferred_username'], 'john.doe'); + expect(map['profile'], 'https://example.com/profile'); + expect(map['picture'], 'https://example.com/picture.jpg'); + expect(map['website'], 'https://johndoe.com'); + expect(map['email'], 'john@example.com'); + expect(map['email_verified'], true); + expect(map['gender'], 'male'); + expect(map['birthdate'], '1990-01-01'); + expect(map['zoneinfo'], 'America/New_York'); + expect(map['locale'], 'en-US'); + expect(map['phone_number'], '+1234567890'); + expect(map['phone_number_verified'], true); + expect(map['address'], {'country': 'USA', 'postal_code': '12345'}); + expect(map['updated_at'], '2023-01-01T00:00:00.000Z'); + expect(map['custom_claims'], {'role': 'admin'}); + }); + }); +}