Skip to content

Commit

Permalink
Merge branch 'chore/unit-test-and-linter-fixes' of github.com:internx…
Browse files Browse the repository at this point in the history
…t/drive-server-wip into chore/unit-test-and-linter-fixes
  • Loading branch information
c27gc committed Jul 4, 2023
2 parents 0b87e67 + 92cd485 commit 190bc0c
Show file tree
Hide file tree
Showing 10 changed files with 184 additions and 59 deletions.
14 changes: 9 additions & 5 deletions seeders/20230308180046-test-users.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,12 @@ 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] },
},
{},
);
},
};
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, {});
},
};
2 changes: 1 addition & 1 deletion seeders/20230618000000-create-private-folder.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ module.exports = {
]);
},

async down(queryInterface, Sequelize) {
async down(queryInterface) {
await queryInterface.bulkDelete(
'private_sharing_folder',
{
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
47 changes: 47 additions & 0 deletions src/modules/private-share-folder/private-sharing.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,54 @@ import { Pagination } from '../../lib/pagination';
@Controller('private-sharing')
export class PrivateSharingController {
constructor(private readonly privateSharingUseCase: PrivateSharingUseCase) {}

@Get('receive/folders')
@ApiOperation({
summary: 'Get all folders shared with a user',
})
@ApiQuery({
description: 'Number of page to take by ( default 0 )',
name: 'page',
required: false,
type: Number,
})
@ApiQuery({
description: 'Number of items per page ( default 50 )',
name: 'perPage',
required: false,
type: Number,
})
@ApiQuery({
description: 'Order by',
name: 'orderBy',
required: false,
type: String,
})
@ApiOkResponse({ description: 'Get all folders shared with a user' })
@ApiBearerAuth()
async getSharedFoldersWithAUser(
@UserDecorator() user: User,
@Query('page') page = 0,
@Query('perPage') perPage = 50,
@Query('orderBy') orderBy: OrderBy,
): Promise<Record<'folders', Folder[]>> {
const { offset, limit } = Pagination.calculatePagination(page, perPage);

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

return {
folders: await this.privateSharingUseCase.getSharedFoldersBySharedWith(
user,
offset,
limit,
order,
),
};
}

@Get('sent/folders')
@ApiOperation({
summary: 'Get all folders shared by a user',
})
Expand Down
36 changes: 35 additions & 1 deletion src/modules/private-share-folder/private-sharing.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ export interface PrivateSharingRepository {
limit: number,
orderBy?: [string, string][],
): Promise<Folder[]>;
findBySharedWith(
userUuid: string,
offset: number,
limit: number,
orderBy?: [string, string][],
): Promise<Folder[]>;
}

@Injectable()
Expand All @@ -38,7 +44,35 @@ export class SequelizePrivateSharingRepository
{
model: this.folderModel,
required: true,
foreignKey: 'folderUuid',
foreignKey: 'folderId',
on: {
uuid: { [Op.eq]: col('PrivateSharingFolderModel.folder_id') },
},
},
],
order: orderBy,
limit,
offset,
});

return sharedFolders.map((folder) => folder.get({ plain: true }));
}

async findBySharedWith(
userUuid: string,
offset: number,
limit: number,
orderBy?: [string, string][],
): Promise<Folder[]> {
const sharedFolders = await this.privateSharingFolderModel.findAll({
where: {
sharedWith: userUuid,
},
include: [
{
model: this.folderModel,
required: true,
foreignKey: 'folderId',
on: {
uuid: { [Op.eq]: col('PrivateSharingFolderModel.folder_id') },
},
Expand Down
15 changes: 15 additions & 0 deletions src/modules/private-share-folder/private-sharing.usecase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,19 @@ export class PrivateSharingUseCase {
);
return folders;
}

async getSharedFoldersBySharedWith(
user: User,
offset: number,
limit: number,
order: [string, string][],
): Promise<Folder[]> {
const folders = await this.privateSharingRespository.findBySharedWith(
user.uuid,
offset,
limit,
order,
);
return folders;
}
}
122 changes: 74 additions & 48 deletions src/modules/private-share-folder/private-sharring.usecase.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,57 @@ describe('PrivateSharingUseCase', () => {

const mockRepository = {
findByOwner: jest.fn(),
findBySharedWith: jest.fn(),
};

const user = User.build({
userId: 'JohnDoe userId',
name: 'John',
lastname: 'Doe',
uuid: v4(),
email: '[email protected]',
username: '[email protected]',
bridgeUser: '[email protected]',
password: '',
mnemonic: 'john doe mnemonic',
referrer: v4(),
referralCode: v4(),
credit: 0,
hKey: new Buffer('john doe hKey'),
rootFolderId: 1,
errorLoginCount: 0,
isEmailActivitySended: 1,
lastResend: new Date(),
syncDate: new Date(),
welcomePack: true,
registerCompleted: true,
id: 0,
secret_2FA: '',
backupsBucket: '',
sharedWorkspace: false,
tempKey: '',
avatar: '',
});

const folders: Folder[] = [
Folder.build({
id: 0,
parentId: null,
name: 'FolderTwo',
bucket: 'bucketTwo',
userId: user.id,
uuid: v4(),
plainName: 'FolderTwo',
encryptVersion: '03-aes',
deleted: false,
removed: false,
createdAt: new Date(),
updatedAt: new Date(),
removedAt: null,
deletedAt: null,
}),
];

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
Expand All @@ -39,54 +88,6 @@ describe('PrivateSharingUseCase', () => {

describe('getSharedFoldersByOwner', () => {
it('should return the folders shared by a specific user', async () => {
const user = User.build({
userId: 'JohnDoe userId',
name: 'John',
lastname: 'Doe',
uuid: v4(),
email: '[email protected]',
username: '[email protected]',
bridgeUser: '[email protected]',
password: '',
mnemonic: 'john doe mnemonic',
referrer: v4(),
referralCode: v4(),
credit: 0,
hKey: new Buffer('john doe hKey'),
rootFolderId: 1,
errorLoginCount: 0,
isEmailActivitySended: 1,
lastResend: new Date(),
syncDate: new Date(),
welcomePack: true,
registerCompleted: true,
id: 0,
secret_2FA: '',
backupsBucket: '',
sharedWorkspace: false,
tempKey: '',
avatar: '',
});

const folders: Folder[] = [
Folder.build({
id: 0,
parentId: null,
name: 'FolderTwo',
bucket: 'bucketTwo',
userId: user.id,
uuid: v4(),
plainName: 'FolderTwo',
encryptVersion: '03-aes',
deleted: false,
removed: false,
createdAt: new Date(),
updatedAt: new Date(),
removedAt: null,
deletedAt: null,
}),
];

jest
.spyOn(privateSharingRespository, 'findByOwner')
.mockResolvedValue(folders);
Expand All @@ -110,5 +111,30 @@ describe('PrivateSharingUseCase', () => {
order,
);
});

it('should return the folders shared with a specific user', async () => {
jest
.spyOn(privateSharingRespository, 'findBySharedWith')
.mockResolvedValue(folders);

const offset = 0;
const limit = 10;
const order: [string, string][] = [['createdAt', 'DESC']];

const result = await privateSharingUseCase.getSharedFoldersBySharedWith(
user,
offset,
limit,
order,
);

expect(result).toEqual(folders);
expect(privateSharingRespository.findBySharedWith).toHaveBeenCalledWith(
user.uuid,
offset,
limit,
order,
);
});
});
});
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 190bc0c

Please sign in to comment.