Skip to content

Commit

Permalink
basic blossom tests
Browse files Browse the repository at this point in the history
  • Loading branch information
leo-lox committed Jan 11, 2025
1 parent d6b43ec commit cec6252
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 97 deletions.
10 changes: 8 additions & 2 deletions packages/ndk/lib/data_layer/data_sources/http_request.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,14 @@ class HttpRequestDS {
return response;
}

Future<http.Response> delete(Uri url) async {
http.Response response = await _client.delete(url);
Future<http.Response> delete({
required Uri url,
required headers,
}) async {
http.Response response = await _client.delete(
url,
headers: headers,
);

if (response.statusCode != 200) {
throw Exception(
Expand Down
19 changes: 14 additions & 5 deletions packages/ndk/lib/data_layer/repositories/blossom/blossom_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -228,16 +228,25 @@ class BlossomRepositoryImpl implements BlossomRepository {
required List<String> serverUrls,
required Nip01Event authorization,
}) async {
final results = await Future.wait(
serverUrls.map((url) => _deleteFromServer(url, sha256)));
final results = await Future.wait(serverUrls.map((url) => _deleteFromServer(
serverUrl: url,
sha256: sha256,
authorization: authorization,
)));
return results;
}

Future<BlobDeleteResult> _deleteFromServer(
String serverUrl, String sha256) async {
Future<BlobDeleteResult> _deleteFromServer({
required String serverUrl,
required String sha256,
required Nip01Event authorization,
}) async {
try {
final response = await client.delete(
Uri.parse('$serverUrl/$sha256'),
url: Uri.parse('$serverUrl/$sha256'),
headers: {
'Authorization': "Nostr ${authorization.toBase64()}",
},
);

return BlobDeleteResult(
Expand Down
4 changes: 2 additions & 2 deletions packages/ndk/lib/domain_layer/usecases/files/blossom.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class Blossom {
List<String>? serverUrls,
String? pubkeyToFetchUserServerList,
}) async {
late final Nip01Event myAuthorization;
Nip01Event? myAuthorization;

if (useAuth) {
final now = DateTime.now().millisecondsSinceEpoch ~/ 1000;
Expand Down Expand Up @@ -114,7 +114,7 @@ class Blossom {
DateTime? since,
DateTime? until,
}) async {
late final Nip01Event myAuthorization;
Nip01Event? myAuthorization;

if (useAuth) {
final now = DateTime.now().millisecondsSinceEpoch ~/ 1000;
Expand Down
5 changes: 0 additions & 5 deletions packages/ndk/lib/domain_layer/usecases/files/files.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,4 @@ class Files {
final Blossom blossom;

Files(this.blossom);



getFile({})

}
4 changes: 3 additions & 1 deletion packages/ndk/test/mocks/mock_blossom_server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,14 @@ class MockBlossomServer {
router.put('/upload', (Request request) async {
// Check for authorization header
final authHeader = request.headers['authorization'];

if (authHeader == null) {
return Response.forbidden('Missing authorization');
}

try {
final authEvent = json.decode(authHeader);
final authEvent =
json.decode(utf8.decode(base64Decode(authHeader.split(' ')[1])));
if (!_verifyAuthEvent(authEvent, 'upload')) {
return Response.forbidden('Invalid authorization event');
}
Expand Down
109 changes: 109 additions & 0 deletions packages/ndk/test/usecases/files/blossom_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import 'dart:convert';
import 'dart:typed_data';
import 'package:http/http.dart' as http;

import 'package:ndk/data_layer/data_sources/http_request.dart';
import 'package:ndk/data_layer/repositories/blossom/blossom_impl.dart';
import 'package:ndk/data_layer/repositories/signers/bip340_event_signer.dart';
import 'package:ndk/domain_layer/usecases/files/blossom.dart';

import 'package:ndk/shared/nips/nip01/bip340.dart';
import 'package:ndk/shared/nips/nip01/key_pair.dart';
import 'package:test/test.dart';

import '../../mocks/mock_blossom_server.dart';

void main() {
late MockBlossomServer server;
late Blossom client;

setUp(() async {
server = MockBlossomServer(port: 3000);
await server.start();

final blossomRepo = BlossomRepositoryImpl(
client: HttpRequestDS(http.Client()),
);

KeyPair key1 = Bip340.generatePrivateKey();
final signer = Bip340EventSigner(
privateKey: key1.privateKey, publicKey: key1.publicKey);
client = Blossom(blossomRepo, signer);
});

tearDown(() async {
await server.stop();
});

group('Blossom Client Integration Tests', () {
test('Upload and retrieve blob', () async {
final testData = Uint8List.fromList(utf8.encode('Hello, Blossom!'));

// Upload blob
final uploadResponse = await client.uploadBlob(
data: testData,
serverUrls: ['http://localhost:3000'],
);
expect(uploadResponse.first.success, true);

final sha256 = uploadResponse.first.descriptor!.sha256;

// Retrieve blob
final getResponse = await client.getBlob(
sha256: sha256,
serverUrls: ['http://localhost:3000'],
);
expect(utf8.decode(getResponse), equals('Hello, Blossom!'));
});

test('List blobs for user', () async {
// Upload some test blobs first
final testData1 = Uint8List.fromList(utf8.encode('Test 1'));
final testData2 = Uint8List.fromList(utf8.encode('Test 2'));

await client.uploadBlob(
data: testData1,
serverUrls: ['http://localhost:3000'],
);
await client.uploadBlob(
data: testData2,
serverUrls: ['http://localhost:3000'],
);

final listResponse = await client.listBlobs(
pubkey: 'test_pubkey',
serverUrls: ['http://localhost:3000'],
);

expect(listResponse.length, equals(2));
});

test('Delete blob', () async {
final testData = Uint8List.fromList(utf8.encode('Hello, Blossom!'));

// Upload blob
final uploadResponse = await client.uploadBlob(
data: testData,
serverUrls: ['http://localhost:3000'],
);
expect(uploadResponse.first.success, true);

final sha256 = uploadResponse.first.descriptor!.sha256;

// Delete blob
final deleteResponse = await client.delteBlob(
sha256: sha256,
serverUrls: ['http://localhost:3000'],
);
expect(deleteResponse.first.success, true);

// Retrieve blob
final getResponse = client.getBlob(
sha256: sha256,
serverUrls: ['http://localhost:3000'],
);
//check that something throws an error
expect(getResponse, throwsException);
});
});
}
82 changes: 0 additions & 82 deletions packages/ndk/test/usecases/files/files_test.dart

This file was deleted.

0 comments on commit cec6252

Please sign in to comment.