Skip to content

Commit

Permalink
fix: delete local key
Browse files Browse the repository at this point in the history
  • Loading branch information
sitaram-kalluri committed Nov 29, 2022
1 parent 27cb6fd commit 4b8f626
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 21 deletions.
2 changes: 2 additions & 0 deletions packages/at_commons/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
46 changes: 26 additions & 20 deletions packages/at_commons/lib/src/verb/delete_verb_builder.dart
Original file line number Diff line number Diff line change
@@ -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.
/// ```
Expand All @@ -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;

Expand All @@ -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;
}

Expand All @@ -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();
}
}
2 changes: 1 addition & 1 deletion packages/at_commons/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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

Expand Down
82 changes: 82 additions & 0 deletions packages/at_commons/test/delete_verb_test.dart
Original file line number Diff line number Diff line change
@@ -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')));
});
});
}

0 comments on commit 4b8f626

Please sign in to comment.