Repository navigation
Expand file tree
/
Copy pathfile_bucket.e2e-spec.ts
More file actions
251 lines (218 loc) · 6.64 KB
/
Copy pathfile_bucket.e2e-spec.ts
File metadata and controls
251 lines (218 loc) · 6.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import { DeleteBucketCommand, S3Client } from '@aws-sdk/client-s3';
import * as GRPC from '@grpc/grpc-js';
import * as ProtoLoader from '@grpc/proto-loader';
import {
ClassSerializerInterceptor,
INestApplication,
ValidationPipe,
} from '@nestjs/common';
import { Reflector } from '@nestjs/core';
import { Test, TestingModule } from '@nestjs/testing';
import {
FileProviderProto,
FileProviderProtoFile,
ResetProtoFile,
} from 'juno-proto';
import { RpcExceptionFilter } from 'src/rpc_exception_filter';
import * as request from 'supertest';
import { AppModule } from './../src/app.module';
let app: INestApplication;
const ADMIN_EMAIL = 'test-superadmin@test.com';
const ADMIN_PASSWORD = 'test-password';
let apiKey: string | undefined = undefined;
const region = 'us-east-005';
const accessKeyId = process.env.accessKeyId;
const secretAccessKey = process.env.secretAccessKey;
const baseURL = process.env.baseURL;
const providerName = 'backblazeb2-upload';
jest.setTimeout(10000);
async function APIKeyForProjectName(projectName: string): Promise<string> {
const key = await request(app.getHttpServer())
.post('/auth/key')
.set('X-User-Email', ADMIN_EMAIL)
.set('X-User-Password', ADMIN_PASSWORD)
.send({
environment: 'prod',
project: {
name: projectName,
},
});
return key.body['apiKey'];
}
beforeAll(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
}).compile();
app = moduleFixture.createNestApplication();
app.useGlobalPipes(
new ValidationPipe({
transform: true,
}),
);
app.useGlobalInterceptors(new ClassSerializerInterceptor(app.get(Reflector)));
app.useGlobalFilters(new RpcExceptionFilter());
await app.init();
const proto = ProtoLoader.loadSync([ResetProtoFile]) as any;
const protoGRPC = GRPC.loadPackageDefinition(proto) as any;
const resetClient = new protoGRPC.juno.reset_db.DatabaseReset(
process.env.DB_SERVICE_ADDR,
GRPC.credentials.createInsecure(),
);
await new Promise((resolve, reject) => {
resetClient.resetDb({}, (err: any) => {
if (err) return reject(err);
resolve(0);
});
});
const providerProto = ProtoLoader.loadSync([FileProviderProtoFile]) as any;
const providerProtoGRPC = GRPC.loadPackageDefinition(providerProto) as any;
const providerClient =
new providerProtoGRPC.juno.file_service.provider.FileProviderDbService(
process.env.DB_SERVICE_ADDR,
GRPC.credentials.createInsecure(),
);
await new Promise((resolve, reject) => {
providerClient.createProvider(
{
providerName: providerName,
accessKey: JSON.stringify({
accessKeyId: accessKeyId,
secretAccessKey: secretAccessKey,
}),
metadata: JSON.stringify({ endpoint: baseURL }),
bucket: [],
type: FileProviderProto.ProviderType.S3,
},
(err: any) => {
if (err) return reject(err);
resolve(0);
},
);
});
});
afterAll((done) => {
app.close();
done();
});
beforeEach(async () => {
if (!apiKey) {
apiKey = await APIKeyForProjectName('test-seed-project');
}
});
describe('File Bucket Routes', () => {
let uniqueBucketName: string;
beforeEach(() => {
uniqueBucketName = `Bucket-${Date.now()}`;
});
it('Creating a bucket successfully', async () => {
const fileBucketBody = {
name: uniqueBucketName,
configId: 0,
fileProviderName: providerName,
FileServiceFile: [],
};
await request(app.getHttpServer())
.post('/file/bucket')
.set('Authorization', 'Bearer ' + apiKey)
.send(fileBucketBody)
.expect(201);
const metadata = {
endpoint: baseURL,
region: region,
credentials: {
accessKeyId: accessKeyId as string,
secretAccessKey: secretAccessKey as string,
},
};
const client = new S3Client(metadata);
const command = new DeleteBucketCommand({
Bucket: `${uniqueBucketName}-0-prod`,
});
await client.send(command);
});
it('Unsuccessful creation due to missing bucket name', async () => {
const fileBucketBody = {
name: '',
configId: 1,
fileProviderName: 'Test Provider',
FileServiceFile: [],
};
return request(app.getHttpServer())
.post('/file/bucket')
.set('Authorization', 'Bearer ' + apiKey)
.send(fileBucketBody)
.expect(400);
});
it('Unsuccessful creation due to missing config ID', async () => {
const fileBucketBody = {
name: uniqueBucketName,
configId: undefined,
fileProviderName: 'Test Provider',
FileServiceFile: [],
};
return request(app.getHttpServer())
.post('/file/bucket')
.set('Authorization', 'Bearer ' + apiKey)
.send(fileBucketBody)
.expect(400);
});
it('Unsuccessful creation due to missing file provider name', async () => {
const fileBucketBody = {
name: uniqueBucketName,
configId: 1,
fileProviderName: '',
FileServiceFile: [],
};
return request(app.getHttpServer())
.post('/file/bucket')
.set('Authorization', 'Bearer ' + apiKey)
.send(fileBucketBody)
.expect(400);
});
it('Creating an existing bucket (should fail)', async () => {
const fileBucketBody = {
name: uniqueBucketName + '-duplicate',
configId: 0,
fileProviderName: providerName,
FileServiceFile: [],
};
await request(app.getHttpServer())
.post('/file/bucket')
.set('Authorization', 'Bearer ' + apiKey)
.send(fileBucketBody)
.expect(201);
await request(app.getHttpServer())
.post('/file/bucket')
.set('Authorization', 'Bearer ' + apiKey)
.send(fileBucketBody)
.expect(400);
const metadata = {
endpoint: baseURL,
region: region,
credentials: {
accessKeyId: accessKeyId as string,
secretAccessKey: secretAccessKey as string,
},
};
const client = new S3Client(metadata);
const command = new DeleteBucketCommand({
Bucket: uniqueBucketName + '-duplicate-0-prod',
});
await client.send(command);
});
it('Gets all files successfully', async () => {
await request(app.getHttpServer())
.get('/file/all/0')
.set('Authorization', 'Bearer ' + apiKey)
.expect(200);
});
it('Unsuccessfully gets all files due to invalid configId', async () => {
await request(app.getHttpServer())
.get('/file/all/invalid')
.set('Authorization', 'Bearer ' + apiKey)
.expect(400);
});
it('Unsuccessfully gets all files due to missing authorization', async () => {
await request(app.getHttpServer()).get('/file/all/0').expect(401);
});
});