Skip to content

Commit

Permalink
feat: created pared-down version of User object (#2499)
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanoltman authored Sep 27, 2024
1 parent 20f2641 commit 9c6d652
Show file tree
Hide file tree
Showing 13 changed files with 112 additions and 30 deletions.
2 changes: 1 addition & 1 deletion packages/shorebird_cli/test/src/auth/auth_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ void main() {
authProvider: AuthProvider.google,
);
const email = '[email protected]';
const user = User(
const user = PrivateUser(
id: 42,
email: email,
jwtIssuer: googleJwtIssuer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class CodePushClient {
Uri get _v1 => Uri.parse('$hostedUri/api/v1');

/// Fetches the currently logged-in user.
Future<User?> getCurrentUser() async {
Future<PrivateUser?> getCurrentUser() async {
final uri = Uri.parse('$_v1/users/me');
final response = await _httpClient.get(uri);

Expand All @@ -122,7 +122,7 @@ class CodePushClient {
}

final json = jsonDecode(response.body) as Map<String, dynamic>;
return User.fromJson(json);
return PrivateUser.fromJson(json);
}

/// Create a new artifact for a specific [patchId].
Expand Down Expand Up @@ -331,7 +331,7 @@ class CodePushClient {
/// Create a new Shorebird user with the provided [name].
///
/// The email associated with the user's JWT will be used as the user's email.
Future<User> createUser({
Future<PrivateUser> createUser({
required String name,
}) async {
final response = await _httpClient.post(
Expand All @@ -344,7 +344,7 @@ class CodePushClient {
}

final body = json.decode(response.body) as Json;
return User.fromJson(body);
return PrivateUser.fromJson(body);
}

/// Delete the app with the provided [appId].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ void main() {
});

group('getCurrentUser', () {
const user = User(
const user = PrivateUser(
id: 123,
email: '[email protected]',
jwtIssuer: 'https://accounts.google.com',
Expand Down Expand Up @@ -1221,7 +1221,7 @@ void main() {

group('createUser', () {
const userName = 'Jane Doe';
final user = User(
final user = PrivateUser(
id: 1,
email: '[email protected]',
displayName: userName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ export 'organization_user.dart';
export 'patch.dart';
export 'patch_artifact.dart';
export 'plan.dart';
export 'private_user.dart';
export 'public_user.dart';
export 'release.dart';
export 'release_artifact.dart';
export 'release_patch.dart';
export 'release_platform.dart';
export 'release_status.dart';
export 'user.dart';
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class OrganizationUser {
Map<String, dynamic> toJson() => _$OrganizationUserToJson(this);

/// The user that is a member of the organization.
final User user;
final PublicUser user;

/// The role [user] has in the organization.
final OrganizationRole role;
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import 'package:json_annotation/json_annotation.dart';
import 'package:meta/meta.dart';
import 'package:shorebird_code_push_protocol/shorebird_code_push_protocol.dart';

part 'user.g.dart';
part 'private_user.g.dart';

/// {@template user}
/// A user account which contains zero or more apps.
/// A fully-detailed user object, possibly including sensitive information.
/// This should only be used when querying the user's own information. For other
/// users, use [PublicUser].
/// {@endtemplate}
@JsonSerializable()
class User {
class PrivateUser {
/// {@macro user}
const User({
const PrivateUser({
required this.id,
required this.email,
required this.jwtIssuer,
Expand All @@ -19,13 +22,14 @@ class User {
this.patchOverageLimit,
});

/// Converts a Map<String, dynamic> to a [User]
factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json);
/// Converts a Map<String, dynamic> to a [PrivateUser]
factory PrivateUser.fromJson(Map<String, dynamic> json) =>
_$PrivateUserFromJson(json);

// coverage:ignore-start
/// Constructs a user with arbitrary default values for testing.
@visibleForTesting
factory User.forTest({
factory PrivateUser.forTest({
int id = 42,
String email = '[email protected]',
String jwtIssuer = 'https://accounts.google.com',
Expand All @@ -34,7 +38,7 @@ class User {
String? stripeCustomerId,
int? patchOverageLimit = 0,
}) =>
User(
PrivateUser(
id: id,
email: email,
jwtIssuer: jwtIssuer,
Expand All @@ -45,8 +49,8 @@ class User {
);
// coverage:ignore-end

/// Converts a [User] to a Map<String, dynamic>
Map<String, dynamic> toJson() => _$UserToJson(this);
/// Converts a [PrivateUser] to a Map<String, dynamic>
Map<String, dynamic> toJson() => _$PrivateUserToJson(this);

/// The unique user identifier.
final int id;
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import 'package:json_annotation/json_annotation.dart';
import 'package:shorebird_code_push_protocol/shorebird_code_push_protocol.dart';

part 'public_user.g.dart';

/// {@template user}
/// A Shorebird user. This is a pared down version of [PrivateUser].
/// {@endtemplate}
@JsonSerializable()
class PublicUser {
/// {@macro user}
PublicUser({
required this.id,
required this.email,
required this.displayName,
});

/// Converts a Map<String, dynamic> to a [PublicUser]
factory PublicUser.fromJson(Map<String, dynamic> json) =>
_$PublicUserFromJson(json);

/// Constructs a [PublicUser] from a [PrivateUser], removing sensitive
/// information.
factory PublicUser.fromPrivateUser(PrivateUser fullUser) {
return PublicUser(
id: fullUser.id,
email: fullUser.email,
displayName: fullUser.displayName,
);
}

/// Converts a [PublicUser] to a JSON map.
Map<String, dynamic> toJson() => _$PublicUserToJson(this);

/// The user's unique identifier.
final int id;

/// The user's email address.
final String email;

/// The user's name.
final String? displayName;
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ void main() {
test('can be (de)serialized', () {
final getOrganizationUsersRequest = GetOrganizationUsersResponse(
users: [
OrganizationUser(user: User.forTest(), role: OrganizationRole.owner),
OrganizationUser(
user: PublicUser.fromPrivateUser(PrivateUser.forTest()),
role: OrganizationRole.owner,
),
],
);
expect(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ void main() {
group(OrganizationUser, () {
test('can be (de)serialized', () {
final organizationUser = OrganizationUser(
user: User.forTest(),
user: PublicUser.fromPrivateUser(PrivateUser.forTest()),
role: OrganizationRole.member,
);
expect(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import 'package:shorebird_code_push_protocol/shorebird_code_push_protocol.dart';
import 'package:test/test.dart';

void main() {
group(User, () {
group(PrivateUser, () {
test('can be (de)serialized', () {
const user = User(
const user = PrivateUser(
id: 1,
email: '[email protected]',
stripeCustomerId: 'test-customer-id',
Expand All @@ -13,7 +13,7 @@ void main() {
patchOverageLimit: 123,
);
expect(
User.fromJson(user.toJson()).toJson(),
PrivateUser.fromJson(user.toJson()).toJson(),
equals(user.toJson()),
);
});
Expand Down

0 comments on commit 9c6d652

Please sign in to comment.