feat(code_push_client): add createAppCollaborator (#493)

This commit is contained in:
Felix Angelov
2023-05-15 13:41:06 -05:00
committed by GitHub
parent b912598d5b
commit f114079c30
2 changed files with 100 additions and 0 deletions
@@ -40,6 +40,22 @@ class CodePushClient {
/// The hosted uri for the Shorebird CodePush API.
final Uri hostedUri;
/// Add a new collaborator to the app.
/// Collaborators can manage the app including its releases and patches.
Future<void> createAppCollaborator({
required String appId,
required int userId,
}) async {
final response = await _httpClient.post(
Uri.parse('$hostedUri/api/v1/apps/$appId/collaborators'),
body: json.encode(CreateAppCollaboratorRequest(userId: userId).toJson()),
);
if (response.statusCode != HttpStatus.created) {
throw _parseErrorResponse(response.body);
}
}
/// Fetches the currently logged-in user.
Future<User?> getCurrentUser() async {
final uri = Uri.parse('$hostedUri/api/v1/users/me');
@@ -53,6 +53,90 @@ void main() {
});
});
group('createAppCollaborator', () {
const appId = 'test-app-id';
const userId = 42;
test('throws an exception if the http request fails (unknown)', () async {
when(
() => httpClient.post(
any(),
headers: any(named: 'headers'),
body: any(named: 'body'),
),
).thenAnswer(
(_) async => http.Response('', HttpStatus.failedDependency),
);
expect(
codePushClient.createAppCollaborator(appId: appId, userId: userId),
throwsA(
isA<CodePushException>().having(
(e) => e.message,
'message',
CodePushClient.unknownErrorMessage,
),
),
);
});
test('throws an exception if the http request fails', () async {
when(
() => httpClient.post(
any(),
headers: any(named: 'headers'),
body: any(named: 'body'),
),
).thenAnswer(
(_) async => http.Response(
json.encode(errorResponse.toJson()),
HttpStatus.failedDependency,
),
);
expect(
codePushClient.createAppCollaborator(appId: appId, userId: userId),
throwsA(
isA<CodePushException>().having(
(e) => e.message,
'message',
errorResponse.message,
),
),
);
});
test('completes when request succeeds', () async {
when(
() => httpClient.post(
any(),
headers: any(named: 'headers'),
body: any(named: 'body'),
),
).thenAnswer((_) async => http.Response('', HttpStatus.created));
await codePushClient.createAppCollaborator(
appId: appId,
userId: userId,
);
final uri = verify(
() => httpClient.post(
captureAny(),
headers: any(named: 'headers'),
body: any(named: 'body'),
),
).captured.single as Uri;
expect(
uri,
codePushClient.hostedUri.replace(
path: '/api/v1/apps/$appId/collaborators',
),
);
});
});
group('getCurrentUser', () {
const user = User(id: 123, email: 'tester@shorebird.dev');