diff --git a/packages/at_commons/CHANGELOG.md b/packages/at_commons/CHANGELOG.md index 10a61e10..fec998aa 100644 --- a/packages/at_commons/CHANGELOG.md +++ b/packages/at_commons/CHANGELOG.md @@ -1,3 +1,5 @@ +## 3.0.32 +- fix: Enable deletion of a local key ## 3.0.31 - feat: Added AtTelemetryService. Marked @experimental while the feature is in early stages. ## 3.0.30 diff --git a/packages/at_commons/lib/src/verb/delete_verb_builder.dart b/packages/at_commons/lib/src/verb/delete_verb_builder.dart index cfc837ae..af856fdc 100644 --- a/packages/at_commons/lib/src/verb/delete_verb_builder.dart +++ b/packages/at_commons/lib/src/verb/delete_verb_builder.dart @@ -1,5 +1,5 @@ import 'package:at_commons/at_commons.dart'; -import 'package:at_commons/src/verb/verb_builder.dart'; +import 'package:at_commons/src/verb/abstract_verb_builder.dart'; /// Delete verb builder generates a command to delete a [atKey] from the secondary server. /// ``` @@ -8,7 +8,7 @@ import 'package:at_commons/src/verb/verb_builder.dart'; /// // @bob deleting the key “phone” shared with @alice /// var deleteBuilder = DeleteVerbBuilder()..key = '@alice:phone@bob'; /// ``` -class DeleteVerbBuilder implements VerbBuilder { +class DeleteVerbBuilder extends AbstractVerbBuilder { /// The key to delete String? atKey; @@ -22,27 +22,14 @@ class DeleteVerbBuilder implements VerbBuilder { bool isCached = false; + /// Indicates if the key is local + /// If the key is local, the key does not sync between cloud and local secondary + bool isLocal = false; + @override String buildCommand() { var command = 'delete:'; - - if (isCached) { - command += 'cached:'; - } - - if (isPublic) { - command += 'public:'; - } - - if (sharedWith != null && sharedWith!.isNotEmpty) { - command += '${VerbUtil.formatAtSign(sharedWith)}:'; - } - if (sharedBy != null && sharedBy!.isNotEmpty) { - command += '$atKey${VerbUtil.formatAtSign(sharedBy)}'; - } else { - command += atKey!; - } - command += '\n'; + command += '${buildKey()}\n'; return command; } @@ -58,4 +45,23 @@ class DeleteVerbBuilder implements VerbBuilder { bool checkParams() { return atKey != null; } + + String buildKey() { + if (atKeyObj.key != null) { + return atKeyObj.toString(); + } + super.atKeyObj + ..key = atKey + ..sharedBy = sharedBy + ..sharedWith = sharedWith + ..isLocal = isLocal + ..metadata = (Metadata() + ..isPublic = isPublic + ..isCached = isCached); + // validates the data in the verb builder. + // If validation is successful, build and return the key; + // else throws exception. + validateKey(); + return super.atKeyObj.toString(); + } } diff --git a/packages/at_commons/pubspec.yaml b/packages/at_commons/pubspec.yaml index 06d49e77..95c9b61a 100644 --- a/packages/at_commons/pubspec.yaml +++ b/packages/at_commons/pubspec.yaml @@ -1,6 +1,6 @@ name: at_commons description: A library of Dart and Flutter utility classes that are used across other components of the atPlatform. -version: 3.0.31 +version: 3.0.32 repository: https://github.com/atsign-foundation/at_tools homepage: https://atsign.dev diff --git a/packages/at_commons/test/delete_verb_test.dart b/packages/at_commons/test/delete_verb_test.dart new file mode 100644 index 00000000..fb719a55 --- /dev/null +++ b/packages/at_commons/test/delete_verb_test.dart @@ -0,0 +1,82 @@ +import 'package:at_commons/at_builders.dart'; +import 'package:at_commons/at_commons.dart'; +import 'package:test/test.dart'; + +void main() { + group('A group tests to validate delete verb builder', () { + test('test to verify deletion command for a local key', () { + var deleteVerbBuilder = DeleteVerbBuilder() + ..isLocal = true + ..atKey = 'phone' + ..sharedBy = '@bob'; + + expect(deleteVerbBuilder.buildCommand(), 'delete:local:phone@bob\n'); + }); + + test('test to deletion of shared key', () { + var deleteVerbBuilder = DeleteVerbBuilder() + ..atKey = 'phone' + ..sharedBy = '@bob' + ..sharedWith = '@alice'; + + expect(deleteVerbBuilder.buildCommand(), 'delete:@alice:phone@bob\n'); + }); + + test('test to deletion of public key', () { + var deleteVerbBuilder = DeleteVerbBuilder() + ..isPublic = true + ..atKey = 'phone' + ..sharedBy = '@bob'; + + expect(deleteVerbBuilder.buildCommand(), 'delete:public:phone@bob\n'); + }); + }); + + group('A group of tests to validate the exceptions',(){ + test('test to verify cached local key throws invalid atkey exception', () { + var deleteVerbBuilder = DeleteVerbBuilder() + ..isCached = true + ..isLocal = true + ..sharedWith = '@alice' + ..atKey = 'phone' + ..sharedBy = '@bob'; + + expect( + () => deleteVerbBuilder.buildCommand(), + throwsA(predicate((dynamic e) => + e is InvalidAtKeyException && + e.message == + 'sharedWith must be null when isLocal is set to true'))); + }); + + test( + 'test to verify isPublic set to true with sharedWith populated throws invalid atkey exception', + () { + var deleteVerbBuilder = DeleteVerbBuilder() + ..isPublic = true + ..sharedWith = '@alice' + ..atKey = 'phone' + ..sharedBy = '@bob'; + + expect( + () => deleteVerbBuilder.buildCommand(), + throwsA(predicate((dynamic e) => + e is InvalidAtKeyException && + e.message == + 'When isPublic is set to true, sharedWith cannot be populated'))); + }); + + test('test to verify Key cannot be null or empty', () { + var deleteVerbBuilder = DeleteVerbBuilder() + ..sharedWith = '@alice' + ..atKey = '' + ..sharedBy = '@bob'; + + expect( + () => deleteVerbBuilder.buildCommand(), + throwsA(predicate((dynamic e) => + e is InvalidAtKeyException && + e.message == 'Key cannot be null or empty'))); + }); + }); +}