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();