Skip to content

Commit

Permalink
Merge branch 'chore/unit-test-and-linter-fixes' into feat/create-priv…
Browse files Browse the repository at this point in the history
…ate-folder-permission
  • Loading branch information
c27gc committed Jul 6, 2023
2 parents 0a63e66 + 7f8bbae commit 09216fc
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 40 deletions.
20 changes: 13 additions & 7 deletions seeders/20230308180046-test-users.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const testUser = {
user_id: 'JohnDoe userId',
name: 'John',
lastname: 'Doe',
uuid: v4(),
uuid: '123e4567-e89b-12d3-a456-426614174000',
email: '[email protected]',
username: '[email protected]',
bridge_user: '[email protected]',
Expand All @@ -26,7 +26,7 @@ const referredTestUser = {
user_id: 'JohnDoe userId',
name: 'John',
lastname: 'Doe',
uuid: v4(),
uuid: '09b073a3-ffc0-42dd-aa6a-dea4702bfbd6',
email: '[email protected]',
username: '[email protected]',
bridge_user: '[email protected]',
Expand All @@ -50,7 +50,7 @@ module.exports = {
* name: 'John Doe',
* isBetaMember: false
* }], {});
*/
*/
const existingUsers = await queryInterface.sequelize.query(
'SELECT email FROM users WHERE email IN (:emails)',
{
Expand All @@ -75,8 +75,14 @@ module.exports = {
* Example:
* await queryInterface.bulkDelete('People', null, {});
*/
await queryInterface.bulkDelete('users', {
email: { [Op.in]: [testUser.email, referredTestUser.email] }
}, {});
}
await queryInterface.bulkDelete(
'users',
{
email: { [Op.in]: [testUser.email, referredTestUser.email] },
},
{},
);
},
};

module.exports.users = { testUser, referredTestUser };
10 changes: 8 additions & 2 deletions seeders/20230607000000-create-folders.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
const { v4 } = require('uuid');
const { Op, Sequelize } = require('sequelize');

let folderOneUUID, folderTwoUUID;

module.exports = {
async up(queryInterface) {
const users = await queryInterface.sequelize.query(
Expand Down Expand Up @@ -46,6 +48,8 @@ module.exports = {
updated_at: new Date(),
};

folderOneUUID = folderOne.uuid;

const folderTwo = {
parent_id: null,
name: 'FolderTwo',
Expand All @@ -60,16 +64,18 @@ module.exports = {
updated_at: new Date(),
};

folderTwoUUID = folderTwo.uuid;

await queryInterface.bulkInsert('folders', [folderOne, folderTwo]);
},

async down(queryInterface, Sequelize) {
await queryInterface.bulkDelete(
'folders',
{
uuid: { [Op.in]: [folderOne.uuid, folderTwo.uuid] },
uuid: { [Op.in]: [folderOneUUID, folderTwoUUID] },
},
{},
);
},
};
};
2 changes: 1 addition & 1 deletion seeders/20230614000000-create-share.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ module.exports = {
}
},

async down(queryInterface, Sequelize) {
async down(queryInterface) {
await queryInterface.bulkDelete('shares', null, {});
},
};
10 changes: 8 additions & 2 deletions seeders/20230618000000-create-private-folder.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
const { v4 } = require('uuid');
const { Op, Sequelize } = require('sequelize');

let sharingFolderOneId, sharingFolderTwoId;

module.exports = {
async up(queryInterface) {
const users = await queryInterface.sequelize.query(
Expand Down Expand Up @@ -38,6 +40,8 @@ module.exports = {
updated_at: new Date(),
};

sharingFolderOneId = sharingFolderOne.id;

const sharingFolderTwo = {
id: v4(),
folder_id: folderTwo.uuid,
Expand All @@ -48,17 +52,19 @@ module.exports = {
updated_at: new Date(),
};

sharingFolderTwoId = sharingFolderTwo.id;

await queryInterface.bulkInsert('private_sharing_folder', [
sharingFolderOne,
sharingFolderTwo,
]);
},

async down(queryInterface, Sequelize) {
async down(queryInterface) {
await queryInterface.bulkDelete(
'private_sharing_folder',
{
id: { [Op.in]: [sharingFolderOne.id, sharingFolderTwo.id] },
id: { [Op.in]: [sharingFolderOneId, sharingFolderTwoId] },
},
{},
);
Expand Down
2 changes: 1 addition & 1 deletion src/modules/folder/folder.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { User as UserDecorator } from '../auth/decorators/user.decorator';
import { User } from '../user/user.domain';
import { FileUseCases } from '../file/file.usecase';
import { Folder, SortableFolderAttributes } from './folder.domain';
import { File, FileStatus, SortableFileAttributes } from '../file/file.domain';
import { FileStatus, SortableFileAttributes } from '../file/file.domain';
import logger from '../../externals/logger';
import { validate } from 'uuid';

Expand Down
2 changes: 1 addition & 1 deletion src/modules/folder/folder.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { InjectModel } from '@nestjs/sequelize';
import { FindOptions, Op } from 'sequelize';
import { v4 } from 'uuid';

import { Folder, SortableFolderAttributes } from './folder.domain';
import { Folder } from './folder.domain';
import { FolderAttributes } from './folder.attributes';

import { UserModel } from '../user/user.model';
Expand Down
73 changes: 48 additions & 25 deletions src/modules/private-share-folder/private-sharing.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
Body,
Controller,
Get,
Logger,
Post,
Query,
} from '@nestjs/common';
Expand All @@ -19,7 +20,7 @@ import { Folder } from '../folder/folder.domain';
import { User } from '../user/user.domain';
import { OrderBy } from 'src/common/order.type';
import { Pagination } from 'src/lib/pagination';
import { GrantPrivilegesDto } from './dto/grant-privileges';
import { GrantPrivilegesDto } from './dto/grant-privileges.dto';

@ApiTags('Private Sharing')
@Controller('private-sharing')
Expand Down Expand Up @@ -82,20 +83,31 @@ export class PrivateSharingController {
@Query('perPage') perPage = 50,
@Query('orderBy') orderBy: OrderBy,
): Promise<Record<'folders', Folder[]>> {
const { offset, limit } = Pagination.calculatePagination(page, perPage);
try {
const { offset, limit } = Pagination.calculatePagination(page, perPage);

const order = orderBy
? [orderBy.split(':') as [string, string]]
: undefined;
const order = orderBy
? [orderBy.split(':') as [string, string]]
: undefined;

return {
folders: await this.privateSharingUseCase.getSharedFoldersBySharedWith(
user,
offset,
limit,
order,
),
};
return {
folders: await this.privateSharingUseCase.getSharedFoldersBySharedWith(
user,
offset,
limit,
order,
),
};
} catch (error) {
const err = error as Error;
Logger.error(
`[PRIVATESHARING/GETSHAREDWITH] Error while getting shared folders with user ${
user.uuid
}, ${err.stack || 'No stack trace'}`,
);

throw error;
}
}

@Get('sent/folders')
Expand Down Expand Up @@ -128,19 +140,30 @@ export class PrivateSharingController {
@Query('page') page = 0,
@Query('perPage') perPage = 50,
): Promise<Record<'folders', Folder[]>> {
const { offset, limit } = Pagination.calculatePagination(page, perPage);
try {
const { offset, limit } = Pagination.calculatePagination(page, perPage);

const order = orderBy
? [orderBy.split(':') as [string, string]]
: undefined;
const order = orderBy
? [orderBy.split(':') as [string, string]]
: undefined;

return {
folders: await this.privateSharingUseCase.getSharedFoldersByOwner(
user,
offset,
limit,
order,
),
};
return {
folders: await this.privateSharingUseCase.getSharedFoldersByOwner(
user,
offset,
limit,
order,
),
};
} catch (error) {
const err = error as Error;
Logger.error(
`[PRIVATESHARING/GETSHAREDBY] Error while getting shared folders by user ${
user.uuid
}, ${err.stack || 'No stack trace'}`,
);

throw error;
}
}
}
80 changes: 80 additions & 0 deletions src/modules/private-share-folder/private-sharing.e2e.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { Test, TestingModule } from '@nestjs/testing';
import { HttpStatus, INestApplication, ValidationPipe } from '@nestjs/common';
import request from 'supertest';

import { TransformInterceptor } from '../../lib/transform.interceptor';
import { AppModule } from '../../app.module';

import { users } from '../../../seeders/20230308180046-test-users.js';
import { ConfigService } from '@nestjs/config';
import { Sign } from '../../middlewares/passport';

const user = users.testUser;

describe('PrivateSharing module', () => {
let app: INestApplication;
let configService: ConfigService;

function getToken(): string {
return Sign(
{
payload: {
uuid: user.uuid,
email: user.email,
name: user.name,
lastname: user.lastname,
username: user.username,
sharedWorkspace: true,
networkCredentials: {
user: user.bridgeUser,
pass: user.userId,
},
},
},
configService.get('secrets.jwt'),
true,
);
}

beforeAll(async () => {
jest.resetModules();
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
}).compile();
app = moduleFixture.createNestApplication();
configService = moduleFixture.get<ConfigService>(ConfigService);
app.useGlobalPipes(new ValidationPipe());
app.useGlobalInterceptors(new TransformInterceptor());
await app.init();
});

afterAll(async () => {
await app.close();
});

it('/private-sharing/receive/folders (GET)', async () => {
console.log('getToken()', getToken());
return await request(app.getHttpServer())
.get('/private-sharing/receive/folders')
.set('Authorization', 'Bearer ' + getToken())
.expect(HttpStatus.OK);
});

it('/private-sharing/sent/folders (GET)', async () => {
return await request(app.getHttpServer())
.get('/private-sharing/sent/folders')
.set('Authorization', 'Bearer ' + getToken())
.expect(HttpStatus.OK);
});

it('/private-sharing/receive/folders (GET)', async () => {
console.log('getToken()', getToken());
return await request(app.getHttpServer())
.get('/private-sharing/receive/folders')
.set('Authorization', 'Bearer ' + getToken())
.then((response) => {
console.log('Error message:', response.body);
expect(response.status).toEqual(HttpStatus.OK);
});
});
});
1 change: 0 additions & 1 deletion src/modules/user/user.domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ export class User implements UserAttributes {
password,
mnemonic,
rootFolderId,
rootFolder,
hKey,
secret_2FA,
errorLoginCount,
Expand Down

0 comments on commit 09216fc

Please sign in to comment.