feat(shorebird_code_push_client): add support for fetching organization memberships (#2507)

This commit is contained in:
Bryan Oltman
2024-10-07 15:58:48 -04:00
committed by GitHub
parent afb1dea7a5
commit 2cab805dea
6 changed files with 179 additions and 3 deletions
@@ -477,6 +477,22 @@ class CodePushClient {
}
}
/// Gets the list of organizations the user is a member of, along with the
/// user's role in each organization.
Future<List<OrganizationMembership>> getOrganizationMemberships() async {
final response = await _httpClient.get(
Uri.parse('$_v1/organizations'),
);
if (!response.isSuccess) {
throw _parseErrorResponse(response.statusCode, response.body);
}
return GetOrganizationsResponse.fromJson(
json.decode(response.body) as Map<String, dynamic>,
).organizations;
}
/// Closes the client.
void close() => _httpClient.close();
@@ -1913,6 +1913,56 @@ void main() {
});
});
group('getOrganizationMemberships', () {
group('when response is not success', () {
setUp(() {
when(() => httpClient.send(any())).thenAnswer(
(_) async => http.StreamedResponse(
const Stream.empty(),
HttpStatus.failedDependency,
),
);
});
test('throws exception', () async {
expect(
() async => codePushClient.getOrganizationMemberships(),
throwsA(
isA<CodePushException>().having(
(e) => e.message,
'message',
CodePushClient.unknownErrorMessage,
),
),
);
});
});
group('when response is successful', () {
late GetOrganizationsResponse response;
late OrganizationMembership membership;
setUp(() {
membership = OrganizationMembership(
role: OrganizationRole.admin,
organization: Organization.forTest(),
);
response = GetOrganizationsResponse(organizations: [membership]);
when(() => httpClient.send(any())).thenAnswer(
(_) async => http.StreamedResponse(
Stream.value(utf8.encode(json.encode(response))),
HttpStatus.ok,
),
);
});
test('deserializes GetOrganizationMembershipsResponse', () async {
final memberships = await codePushClient.getOrganizationMemberships();
expect(memberships, equals([membership]));
});
});
});
group('close', () {
test('closes the underlying client', () {
codePushClient.close();
@@ -1,3 +1,4 @@
import 'package:equatable/equatable.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:meta/meta.dart';
@@ -35,7 +36,7 @@ enum OrganizationType {
/// [stripeCustomerId] may have a subscription.
/// {@endtemplate}
@JsonSerializable()
class Organization {
class Organization extends Equatable {
/// {@macro organization}
const Organization({
required this.id,
@@ -91,4 +92,14 @@ class Organization {
/// When this organization was last updated.
final DateTime updatedAt;
@override
List<Object?> get props => [
id,
name,
organizationType,
stripeCustomerId,
createdAt,
updatedAt,
];
}
@@ -1,3 +1,4 @@
import 'package:equatable/equatable.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:shorebird_code_push_protocol/src/models/organization.dart';
@@ -7,9 +8,9 @@ part 'organization_membership.g.dart';
/// An organization and the current user's role in that organization.
/// {@endtemplate}
@JsonSerializable()
class OrganizationMembership {
class OrganizationMembership extends Equatable {
/// {@macro organization_membership}
OrganizationMembership({
const OrganizationMembership({
required this.organization,
required this.role,
});
@@ -26,4 +27,10 @@ class OrganizationMembership {
/// The user's role in the organization.
final OrganizationRole role;
@override
List<Object?> get props => [
organization,
role,
];
}
@@ -13,5 +13,49 @@ void main() {
equals(membership.toJson()),
);
});
group('Equatable', () {
final date = DateTime.now();
group('when two instances are equal', () {
test('returns true', () {
final membership = OrganizationMembership(
organization: Organization.forTest(
createdAt: date,
updatedAt: date,
),
role: OrganizationRole.member,
);
final otherMembership = OrganizationMembership(
organization: Organization.forTest(
createdAt: date,
updatedAt: date,
),
role: OrganizationRole.member,
);
expect(membership, equals(otherMembership));
});
});
group('when two instances are not equal', () {
test('returns false', () {
final membership = OrganizationMembership(
organization: Organization.forTest(
createdAt: date,
updatedAt: date,
),
role: OrganizationRole.member,
);
final otherMembership = OrganizationMembership(
organization: Organization.forTest(
createdAt: date,
updatedAt: date,
),
role: OrganizationRole.admin,
);
expect(membership, isNot(equals(otherMembership)));
});
});
});
});
}
@@ -17,5 +17,53 @@ void main() {
equals(organization.toJson()),
);
});
group('Equatable', () {
final date = DateTime.now();
group('when two instances are equal', () {
test('returns true', () {
final organization = Organization(
id: 1,
name: 'name',
createdAt: date,
updatedAt: date,
organizationType: OrganizationType.team,
stripeCustomerId: 'cus_123',
);
final otherOrganization = Organization(
id: 1,
name: 'name',
createdAt: date,
updatedAt: date,
organizationType: OrganizationType.team,
stripeCustomerId: 'cus_123',
);
expect(organization, equals(otherOrganization));
});
});
group('when two instances are not equal', () {
test('returns false', () {
final organization = Organization(
id: 1,
name: 'name',
createdAt: date,
updatedAt: date,
organizationType: OrganizationType.team,
stripeCustomerId: 'cus_123',
);
final otherOrganization = Organization(
id: 1,
name: 'name',
createdAt: date,
updatedAt: date,
organizationType: OrganizationType.team,
stripeCustomerId: 'cus_456',
);
expect(organization, isNot(equals(otherOrganization)));
});
});
});
});
}