mirror of
https://github.com/zen-browser/rices.git
synced 2025-07-07 17:05:40 +02:00
first commit
This commit is contained in:
commit
232d8b37d6
36 changed files with 11302 additions and 0 deletions
211
test/app.e2e-spec.ts
Normal file
211
test/app.e2e-spec.ts
Normal file
|
@ -0,0 +1,211 @@
|
|||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { INestApplication, ValidationPipe } from '@nestjs/common';
|
||||
import request from 'supertest';
|
||||
import { AppModule } from './../src/app.module';
|
||||
import * as path from 'path';
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
import { GitHubService } from '../src/github/github.service';
|
||||
|
||||
describe('Rices API E2E', () => {
|
||||
let app: INestApplication;
|
||||
let gitHubService: GitHubService;
|
||||
const moderationSecret = 'testSecret999';
|
||||
|
||||
beforeAll(async () => {
|
||||
require('dotenv').config({ path: '.env.test.local' });
|
||||
|
||||
const moduleFixture: TestingModule = await Test.createTestingModule({
|
||||
imports: [AppModule],
|
||||
}).compile();
|
||||
|
||||
gitHubService = moduleFixture.get<GitHubService>(GitHubService);
|
||||
|
||||
app = moduleFixture.createNestApplication();
|
||||
app.useGlobalPipes(new ValidationPipe({ transform: true }));
|
||||
|
||||
await app.init();
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await app.close();
|
||||
});
|
||||
|
||||
beforeEach(async () => {
|
||||
// await gitHubService.clearRepository();
|
||||
});
|
||||
|
||||
it('POST /rices - Create new zenrice', async () => {
|
||||
const response = await request(app.getHttpServer())
|
||||
.post('/rices')
|
||||
.field('name', 'My first zenrice')
|
||||
.attach('file', path.join(__dirname, 'files', 'example.zenrice'))
|
||||
.expect(201);
|
||||
|
||||
expect(response.body).toHaveProperty('identifier');
|
||||
expect(response.body).toHaveProperty('token');
|
||||
|
||||
const { identifier, token } = response.body;
|
||||
|
||||
const uploadedFileContent = await gitHubService.getFileContent(
|
||||
`rices/${identifier}/data.zenrice`,
|
||||
);
|
||||
expect(uploadedFileContent).not.toBeNull();
|
||||
expect(uploadedFileContent).toContain('This is an example zenrice file.');
|
||||
});
|
||||
|
||||
it('GET /rices/:identifier - Download zenrice', async () => {
|
||||
const createResponse = await request(app.getHttpServer())
|
||||
.post('/rices')
|
||||
.field('name', 'My first zenrice')
|
||||
.attach('file', path.join(__dirname, 'files', 'example.zenrice'))
|
||||
.expect(201);
|
||||
|
||||
const { identifier, token } = createResponse.body;
|
||||
|
||||
const response = await request(app.getHttpServer())
|
||||
.get(`/rices/${identifier}`)
|
||||
.expect(200);
|
||||
});
|
||||
|
||||
it('PUT /rices/:identifier - Update zenrice', async () => {
|
||||
const createResponse = await request(app.getHttpServer())
|
||||
.post('/rices')
|
||||
.field('name', 'My first zenrice')
|
||||
.attach('file', path.join(__dirname, 'files', 'example.zenrice'))
|
||||
.expect(201);
|
||||
|
||||
const { identifier, token } = createResponse.body;
|
||||
|
||||
const updateResponse = await request(app.getHttpServer())
|
||||
.put(`/rices/${identifier}`)
|
||||
.set('x-rices-token', token)
|
||||
.field('name', 'Mi rice renombrado')
|
||||
.attach('file', path.join(__dirname, 'files', 'example_update.zenrice'))
|
||||
.expect(200);
|
||||
|
||||
expect(updateResponse.body).toHaveProperty(
|
||||
'message',
|
||||
`Rice ${identifier} updated`,
|
||||
);
|
||||
|
||||
const uploadedFileContent = await gitHubService.getFileContent(
|
||||
`rices/${identifier}/data.zenrice`,
|
||||
);
|
||||
expect(uploadedFileContent).not.toBeNull();
|
||||
expect(uploadedFileContent).toContain(
|
||||
'This is an example zenrice file (modified).',
|
||||
);
|
||||
});
|
||||
|
||||
it('DELETE /rices/:identifier - Delete zenrice with previous token', async () => {
|
||||
const createResponse = await request(app.getHttpServer())
|
||||
.post('/rices')
|
||||
.field('name', 'My first zenrice')
|
||||
.attach('file', path.join(__dirname, 'files', 'example.zenrice'))
|
||||
.expect(201);
|
||||
|
||||
const { identifier, token } = createResponse.body;
|
||||
|
||||
await request(app.getHttpServer())
|
||||
.delete(`/rices/${identifier}`)
|
||||
.set('x-rices-token', token)
|
||||
.expect(204);
|
||||
|
||||
const riceJsonContent = await gitHubService.getFileContent(
|
||||
`rices/${identifier}/rice.json`,
|
||||
);
|
||||
expect(riceJsonContent).toBeNull();
|
||||
|
||||
const uploadedFileContent = await gitHubService.getFileContent(
|
||||
`rices/${identifier}/data.zenrice`,
|
||||
);
|
||||
expect(uploadedFileContent).toBeNull();
|
||||
});
|
||||
|
||||
it('GET /rices/:identifier - Trying to download deleted zenrice', async () => {
|
||||
const createResponse = await request(app.getHttpServer())
|
||||
.post('/rices')
|
||||
.field('name', 'My first zenrice')
|
||||
.attach('file', path.join(__dirname, 'files', 'example.zenrice'))
|
||||
.expect(201);
|
||||
|
||||
const { identifier, token } = createResponse.body;
|
||||
|
||||
await request(app.getHttpServer())
|
||||
.delete(`/rices/${identifier}`)
|
||||
.set('x-rices-token', token)
|
||||
.expect(204);
|
||||
|
||||
await request(app.getHttpServer()).get(`/rices/${identifier}`).expect(404);
|
||||
});
|
||||
|
||||
it('POST /rices - New zenrice for moderation test', async () => {
|
||||
const response = await request(app.getHttpServer())
|
||||
.post('/rices')
|
||||
.field('name', 'Rice for moderation')
|
||||
.attach('file', path.join(__dirname, 'files', 'example.zenrice'))
|
||||
.expect(201);
|
||||
|
||||
expect(response.body).toHaveProperty('identifier');
|
||||
expect(response.body).toHaveProperty('token');
|
||||
|
||||
const { identifier, token } = response.body;
|
||||
|
||||
const riceJsonContent = await gitHubService.getFileContent(
|
||||
`rices/${identifier}/rice.json`,
|
||||
);
|
||||
expect(riceJsonContent).not.toBeNull();
|
||||
|
||||
const riceData = JSON.parse(riceJsonContent!);
|
||||
expect(riceData).toMatchObject({
|
||||
id: identifier,
|
||||
token,
|
||||
name: 'Rice for moderation',
|
||||
});
|
||||
|
||||
const uploadedFileContent = await gitHubService.getFileContent(
|
||||
`rices/${identifier}/data.zenrice`,
|
||||
);
|
||||
expect(uploadedFileContent).not.toBeNull();
|
||||
expect(uploadedFileContent).toContain('This is an example zenrice file.');
|
||||
});
|
||||
|
||||
it('DELETE /rices/moderate/delete/:identifier - Delete zenrice for moderation using a correct secret', async () => {
|
||||
const createResponse = await request(app.getHttpServer())
|
||||
.post('/rices')
|
||||
.field('name', 'Rice for moderation')
|
||||
.attach('file', path.join(__dirname, 'files', 'example.zenrice'))
|
||||
.expect(201);
|
||||
|
||||
const { identifier, token } = createResponse.body;
|
||||
|
||||
await request(app.getHttpServer())
|
||||
.delete(`/rices/moderate/delete/${identifier}`)
|
||||
.set('x-moderation-secret', moderationSecret)
|
||||
.expect(204);
|
||||
|
||||
const riceJsonContent = await gitHubService.getFileContent(
|
||||
`rices/${identifier}/rice.json`,
|
||||
);
|
||||
expect(riceJsonContent).toBeNull();
|
||||
|
||||
const uploadedFileContent = await gitHubService.getFileContent(
|
||||
`rices/${identifier}/data.zenrice`,
|
||||
);
|
||||
expect(uploadedFileContent).toBeNull();
|
||||
});
|
||||
|
||||
it('DELETE /rices/moderate/delete/:identifier - Delete zenrice for moderation using an incorrect secret', async () => {
|
||||
await request(app.getHttpServer())
|
||||
.delete(`/rices/moderate/delete/${uuidv4()}`)
|
||||
.set('x-moderation-secret', 'claveIncorrecta')
|
||||
.expect(401);
|
||||
});
|
||||
|
||||
it('DELETE /rices/moderate/delete/:identifier - Delete non existent zenrice for moderation', async () => {
|
||||
await request(app.getHttpServer())
|
||||
.delete(`/rices/moderate/delete/${uuidv4()}`)
|
||||
.set('x-moderation-secret', moderationSecret)
|
||||
.expect(404);
|
||||
});
|
||||
});
|
1
test/files/example.zenrice
Normal file
1
test/files/example.zenrice
Normal file
|
@ -0,0 +1 @@
|
|||
This is an example zenrice file.
|
1
test/files/example_update.zenrice
Normal file
1
test/files/example_update.zenrice
Normal file
|
@ -0,0 +1 @@
|
|||
This is an example zenrice file (modified).
|
13
test/jest-e2e.json
Normal file
13
test/jest-e2e.json
Normal file
|
@ -0,0 +1,13 @@
|
|||
{
|
||||
"moduleFileExtensions": ["js", "json", "ts"],
|
||||
"rootDir": "../",
|
||||
"testRegex": ".e2e-spec.ts$",
|
||||
"transform": {
|
||||
"^.+\\.ts$": "ts-jest"
|
||||
},
|
||||
"setupFilesAfterEnv": ["<rootDir>/test/setup.ts"],
|
||||
"coverageDirectory": "./coverage",
|
||||
"testEnvironment": "node",
|
||||
"maxWorkers": 1,
|
||||
"testTimeout": 30000
|
||||
}
|
18
test/restclient/01_create_rice.http
Normal file
18
test/restclient/01_create_rice.http
Normal file
|
@ -0,0 +1,18 @@
|
|||
@baseUrl = http://localhost:3000
|
||||
@moderationSecret = superSecret123
|
||||
@random_identifier = 123e4567-e89b-12d3-a456-426614174000
|
||||
|
||||
|
||||
POST {{baseUrl}}/rices
|
||||
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
|
||||
|
||||
------WebKitFormBoundary7MA4YWxkTrZu0gW
|
||||
Content-Disposition: form-data; name="name"
|
||||
|
||||
Mi primer zenrice
|
||||
------WebKitFormBoundary7MA4YWxkTrZu0gW
|
||||
Content-Disposition: form-data; name="file"; filename="example.zenrice"
|
||||
Content-Type: text/plain
|
||||
|
||||
This is an example zenrice file.
|
||||
------WebKitFormBoundary7MA4YWxkTrZu0gW--
|
5
test/restclient/02_download_rice.http
Normal file
5
test/restclient/02_download_rice.http
Normal file
|
@ -0,0 +1,5 @@
|
|||
@baseUrl = http://localhost:3000
|
||||
@moderationSecret = superSecret123
|
||||
@previous_identifier = my-first-zenrice-feffbf61-b815-4357-ba0c-2cbadf94fcfe
|
||||
|
||||
GET {{baseUrl}}/rices/{{previous_identifier}}
|
19
test/restclient/03_update_rice.http
Normal file
19
test/restclient/03_update_rice.http
Normal file
|
@ -0,0 +1,19 @@
|
|||
@baseUrl = http://localhost:3000
|
||||
@moderationSecret = superSecret123
|
||||
@previous_identifier = my-first-zenrice-feffbf61-b815-4357-ba0c-2cbadf94fcfe
|
||||
@previous_token = 806cd360-6c14-44de-92db-f46f328dab5a
|
||||
|
||||
PUT {{baseUrl}}/rices/{{previous_identifier}}
|
||||
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
|
||||
x-rices-token: {{previous_token}}
|
||||
|
||||
------WebKitFormBoundary7MA4YWxkTrZu0gW
|
||||
Content-Disposition: form-data; name="name"
|
||||
|
||||
Mi rice renombrado
|
||||
------WebKitFormBoundary7MA4YWxkTrZu0gW
|
||||
Content-Disposition: form-data; name="file"; filename="newfile.txt"
|
||||
Content-Type: text/plain
|
||||
|
||||
This is an example zenrice file (modified).
|
||||
------WebKitFormBoundary7MA4YWxkTrZu0gW--
|
7
test/restclient/04_delete_rice.http
Normal file
7
test/restclient/04_delete_rice.http
Normal file
|
@ -0,0 +1,7 @@
|
|||
@baseUrl = http://localhost:3000
|
||||
@moderationSecret = superSecret123
|
||||
@previous_identifier = my-first-zenrice-b7b94d24-ecb6-4495-93de-ba85be2e3052
|
||||
@previous_token = 6181664b-00e8-4eef-8e23-1f7fa0c64021
|
||||
|
||||
DELETE {{baseUrl}}/rices/{{previous_identifier}}
|
||||
x-rices-token: {{previous_token}}
|
94
test/restclient/requests_full.http
Normal file
94
test/restclient/requests_full.http
Normal file
|
@ -0,0 +1,94 @@
|
|||
### Variables Iniciales
|
||||
@baseUrl = http://localhost:3000
|
||||
@moderationSecret = superSecret123
|
||||
@random_identifier = 123e4567-e89b-12d3-a456-426614174000 # Give a valid UUID
|
||||
|
||||
@identifier = YOUR_IDENTIFIER_HERE
|
||||
@token = YOUR_TOKEN_HERE
|
||||
|
||||
###
|
||||
|
||||
### 1. Crear un Nuevo Pack
|
||||
POST {{baseUrl}}/rices
|
||||
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
|
||||
|
||||
------WebKitFormBoundary7MA4YWxkTrZu0gW
|
||||
Content-Disposition: form-data; name="name"
|
||||
|
||||
Mi primer rice
|
||||
------WebKitFormBoundary7MA4YWxkTrZu0gW
|
||||
Content-Disposition: form-data; name="file"; filename="example.txt"
|
||||
Content-Type: text/plain
|
||||
|
||||
Contenido de mi archivo.
|
||||
------WebKitFormBoundary7MA4YWxkTrZu0gW--
|
||||
|
||||
### **Instrucciones Después de Ejecutar:**
|
||||
1. Ejecuta esta solicitud.
|
||||
2. En la respuesta, copia los valores de `identifier` y `token`.
|
||||
3. Reemplaza `YOUR_IDENTIFIER_HERE` y `YOUR_TOKEN_HERE` en las variables al inicio del archivo con los valores copiados.
|
||||
|
||||
###
|
||||
|
||||
GET {{baseUrl}}/rices/{{identifier}}
|
||||
|
||||
###
|
||||
|
||||
PUT {{baseUrl}}/rices/{{identifier}}
|
||||
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
|
||||
x-rices-token: {{token}}
|
||||
|
||||
------WebKitFormBoundary7MA4YWxkTrZu0gW
|
||||
Content-Disposition: form-data; name="name"
|
||||
|
||||
Mi rice renombrado
|
||||
------WebKitFormBoundary7MA4YWxkTrZu0gW
|
||||
Content-Disposition: form-data; name="file"; filename="newfile.txt"
|
||||
Content-Type: text/plain
|
||||
|
||||
Contenido de mi nuevo archivo.
|
||||
------WebKitFormBoundary7MA4YWxkTrZu0gW--
|
||||
|
||||
###
|
||||
|
||||
### 4. Eliminar el Pack Usando el Token
|
||||
DELETE {{baseUrl}}/rices/{{identifier}}
|
||||
x-rices-token: {{token}}
|
||||
|
||||
###
|
||||
|
||||
### 5. Intentar Obtener el Pack Eliminado (Debe Retornar 404)
|
||||
GET {{baseUrl}}/rices/{{identifier}}
|
||||
|
||||
###
|
||||
|
||||
### 6. Crear un Nuevo Pack para Moderación
|
||||
POST {{baseUrl}}/rices
|
||||
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
|
||||
|
||||
------WebKitFormBoundary7MA4YWxkTrZu0gW
|
||||
Content-Disposition: form-data; name="name"
|
||||
|
||||
Pack para moderación
|
||||
------WebKitFormBoundary7MA4YWxkTrZu0gW
|
||||
Content-Disposition: form-data; name="file"; filename="example.txt"
|
||||
Content-Type: text/plain
|
||||
|
||||
Contenido de mi archivo.
|
||||
------WebKitFormBoundary7MA4YWxkTrZu0gW--
|
||||
|
||||
###
|
||||
|
||||
### 7. Eliminar el Pack por Moderación con el Secreto Correcto
|
||||
DELETE {{baseUrl}}/rices/moderate/delete/{{identifier}}
|
||||
x-moderation-secret: {{moderationSecret}}
|
||||
|
||||
###
|
||||
|
||||
DELETE {{baseUrl}}/rices/moderate/delete/{{random_identifier}}
|
||||
x-moderation-secret: claveIncorrecta
|
||||
|
||||
###
|
||||
|
||||
DELETE {{baseUrl}}/rices/moderate/delete/{{random_identifier}}
|
||||
x-moderation-secret: {{moderationSecret}}
|
5
test/setup.ts
Normal file
5
test/setup.ts
Normal file
|
@ -0,0 +1,5 @@
|
|||
// test/setup.ts
|
||||
import * as dotenv from 'dotenv';
|
||||
import * as path from 'path';
|
||||
|
||||
dotenv.config({ path: path.resolve(__dirname, '.env.test.local') });
|
Loading…
Add table
Add a link
Reference in a new issue