Skip to content

Commit d6d2742

Browse files
committed
Fixed e2e test errors
1 parent d520126 commit d6d2742

3 files changed

Lines changed: 47 additions & 47 deletions

File tree

‎packages/api-gateway/test/file_delete.e2e-spec.ts‎

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { CreateBucketCommand, DeleteBucketCommand, S3Client } from '@aws-sdk/client-s3';
12
import { Test, TestingModule } from '@nestjs/testing';
23
import {
34
ClassSerializerInterceptor,
@@ -18,6 +19,8 @@ import * as GRPC from '@grpc/grpc-js';
1819
import * as ProtoLoader from '@grpc/proto-loader';
1920
import { RpcExceptionFilter } from 'src/rpc_exception_filter';
2021

22+
jest.setTimeout(15000);
23+
2124
let app: INestApplication;
2225
const ADMIN_EMAIL = 'test-superadmin@test.com';
2326
const ADMIN_PASSWORD = 'test-password';
@@ -38,14 +41,17 @@ async function APIKeyForProjectName(projectName: string): Promise<string> {
3841
return key.body['apiKey'];
3942
}
4043

41-
const bucketName = 'test-delete-gw-juno';
44+
const bucketName = 'test-delete-bog-juno';
4245
const configId = 0;
4346
const providerName = 'backblazeb2-delete-gw';
4447

4548
const accessKeyId = process.env.accessKeyId;
4649
const secretAccessKey = process.env.secretAccessKey;
4750
const baseURL = process.env.baseURL;
4851

52+
const s3BucketName = `${bucketName}-${configId}-prod`;
53+
let s3Client: S3Client;
54+
4955
beforeAll(async () => {
5056
const proto = ProtoLoader.loadSync([ResetProtoFile]) as any;
5157
const protoGRPC = GRPC.loadPackageDefinition(proto) as any;
@@ -98,11 +104,32 @@ beforeAll(async () => {
98104
() => resolve(0),
99105
);
100106
});
107+
108+
s3Client = new S3Client({
109+
endpoint: baseURL as string,
110+
region: 'us-east-005',
111+
credentials: {
112+
accessKeyId: accessKeyId as string,
113+
secretAccessKey: secretAccessKey as string,
114+
},
115+
forcePathStyle: true,
116+
});
117+
try {
118+
await s3Client.send(new CreateBucketCommand({ Bucket: s3BucketName }));
119+
} catch {
120+
// Bucket may already exist from a previous run
121+
}
101122
});
102123

103-
afterAll((done) => {
104-
app.close();
105-
done();
124+
afterAll(async () => {
125+
if (app) await app.close();
126+
if (s3Client) {
127+
try {
128+
await s3Client.send(new DeleteBucketCommand({ Bucket: s3BucketName }));
129+
} catch {
130+
// Ignore cleanup errors
131+
}
132+
}
106133
});
107134

108135
beforeEach(async () => {
@@ -198,14 +225,15 @@ describe('File Delete Routes', () => {
198225
);
199226
});
200227

201-
return request(app.getHttpServer())
228+
const res = await request(app.getHttpServer())
202229
.delete('/file/delete')
203230
.set('Authorization', 'Bearer ' + apiKey)
204231
.send({
205232
bucketName,
206233
configId,
207234
fileNames: ['FileToDeleteGW'],
208-
})
209-
.expect(200);
235+
});
236+
237+
expect(res.status).toBe(200);
210238
});
211239
});

‎packages/file-service/src/modules/file_transfer/azure_handler.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ export class AzureFileHandler {
5555
const containerClient = blobServiceClient.getContainerClient(
5656
`${request.bucketName}-${request.configId}-${request.configEnv}`,
5757
);
58+
const containerExists = await containerClient.exists();
59+
if (!containerExists) return;
5860
await Promise.all(
5961
request.fileNames.map((name) =>
6062
containerClient.getBlobClient(name).deleteIfExists(),

‎packages/file-service/src/modules/file_transfer/s3_handler.ts‎

Lines changed: 10 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import {
33
GetObjectCommand,
44
PutObjectCommand,
55
DeleteObjectsCommand,
6-
ListObjectVersionsCommand,
76
} from '@aws-sdk/client-s3';
87
import { FileProto, FileProviderProto } from 'juno-proto';
98
import { RpcException } from '@nestjs/microservices';
@@ -81,46 +80,17 @@ export class S3FileHandler {
8180
async deleteFiles(request: FileProto.DeleteFilesRequest): Promise<void> {
8281
try {
8382
const s3Client = await this.getS3Client('us-east-005');
84-
const bucket = `${request.bucketName}-${request.configId}-${request.configEnv}`;
85-
86-
const objectsToDelete: { Key: string; VersionId?: string }[] = [];
87-
88-
for (const name of request.fileNames) {
89-
const listCommand = new ListObjectVersionsCommand({
90-
Bucket: bucket,
91-
Prefix: name,
92-
});
93-
const versions = await s3Client.send(listCommand);
94-
95-
for (const version of versions.Versions ?? []) {
96-
objectsToDelete.push({
97-
Key: version.Key!,
98-
VersionId: version.VersionId,
99-
});
100-
}
101-
for (const marker of versions.DeleteMarkers ?? []) {
102-
objectsToDelete.push({
103-
Key: marker.Key!,
104-
VersionId: marker.VersionId,
105-
});
106-
}
107-
}
108-
109-
if (objectsToDelete.length > 0) {
110-
const deleteCommand = new DeleteObjectsCommand({
111-
Bucket: bucket,
112-
Delete: { Objects: objectsToDelete },
113-
});
114-
const result = await s3Client.send(deleteCommand);
115-
if (result.Errors && result.Errors.length > 0) {
116-
const failedKeys = result.Errors.map((e) => e.Key).join(', ');
117-
throw new RpcException({
118-
code: status.FAILED_PRECONDITION,
119-
message: `Failed to delete some objects from S3: ${failedKeys}`,
120-
});
121-
}
122-
}
83+
const deleteCommand = new DeleteObjectsCommand({
84+
Bucket: `${request.bucketName}-${request.configId}-${request.configEnv}`,
85+
Delete: {
86+
Objects: request.fileNames.map((name) => ({ Key: name })),
87+
},
88+
});
89+
await s3Client.send(deleteCommand);
12390
} catch (err) {
91+
if ((err as any).name === 'NoSuchBucket') {
92+
return;
93+
}
12494
throw new RpcException({
12595
code: status.FAILED_PRECONDITION,
12696
message: `Failed to delete files from S3: ${err}`,

0 commit comments

Comments
 (0)