From 9ae64e9fff61a3a6a7c2502a6aea03e742604f19 Mon Sep 17 00:00:00 2001 From: Bryan Oltman Date: Wed, 3 May 2023 14:28:53 -0400 Subject: [PATCH] feat(shorebird_code_push_client): add deleteRelease (#430) --- .../lib/src/code_push_client.dart | 11 +++ .../test/src/code_push_client_test.dart | 70 +++++++++++++++++++ 2 files changed, 81 insertions(+) diff --git a/packages/shorebird_code_push_client/lib/src/code_push_client.dart b/packages/shorebird_code_push_client/lib/src/code_push_client.dart index 56216d67d..83386118d 100644 --- a/packages/shorebird_code_push_client/lib/src/code_push_client.dart +++ b/packages/shorebird_code_push_client/lib/src/code_push_client.dart @@ -195,6 +195,17 @@ class CodePushClient { return Release.fromJson(body); } + /// Delete the release with the provided [releaseId]. + Future deleteRelease({required int releaseId}) async { + final response = await _httpClient.delete( + Uri.parse('$hostedUri/api/v1/releases/$releaseId'), + ); + + if (response.statusCode != HttpStatus.noContent) { + throw _parseErrorResponse(response.body); + } + } + /// 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. diff --git a/packages/shorebird_code_push_client/test/src/code_push_client_test.dart b/packages/shorebird_code_push_client/test/src/code_push_client_test.dart index 7c8796d4e..3b1ffaf24 100644 --- a/packages/shorebird_code_push_client/test/src/code_push_client_test.dart +++ b/packages/shorebird_code_push_client/test/src/code_push_client_test.dart @@ -777,6 +777,76 @@ void main() { }); }); + group('deleteRelease', () { + const releaseId = 42; + + test('throws an exception if the http request fails (unknown)', () async { + when( + () => httpClient.delete(any(), headers: any(named: 'headers')), + ).thenAnswer( + (_) async => http.Response('', HttpStatus.failedDependency), + ); + + expect( + codePushClient.deleteRelease(releaseId: releaseId), + throwsA( + isA().having( + (e) => e.message, + 'message', + CodePushClient.unknownErrorMessage, + ), + ), + ); + }); + + test('throws an exception if the http request fails', () async { + when( + () => httpClient.delete(any(), headers: any(named: 'headers')), + ).thenAnswer( + (_) async => http.Response( + json.encode(errorResponse.toJson()), + HttpStatus.failedDependency, + ), + ); + + expect( + codePushClient.deleteRelease(releaseId: releaseId), + throwsA( + isA().having( + (e) => e.message, + 'message', + errorResponse.message, + ), + ), + ); + }); + + test('completes when request succeeds', () async { + when( + () => httpClient.delete( + any(), + headers: any(named: 'headers'), + ), + ).thenAnswer((_) async => http.Response('', HttpStatus.noContent)); + + await codePushClient.deleteRelease(releaseId: releaseId); + + final uri = verify( + () => httpClient.delete( + captureAny(), + headers: any(named: 'headers'), + ), + ).captured.single as Uri; + + expect( + uri, + codePushClient.hostedUri.replace( + path: '/api/v1/releases/$releaseId', + ), + ); + }); + }); + group('createUser', () { const userName = 'Jane Doe'; final user = User(