Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Final backlog of code coverage #186

Merged
merged 8 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions backend/src/file_manager/file_manager.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,10 @@ export class FileManagerController {
if (
!markdownFileDTO.UserID ||
!markdownFileDTO.MarkdownID ||
!markdownFileDTO.ParentFolderID ||
!markdownFileDTO.Path
(!markdownFileDTO.ParentFolderID &&
markdownFileDTO.ParentFolderID !== '') ||
(!markdownFileDTO.Path &&
markdownFileDTO.Path !== '')
)
throw new HttpException(
'Invalid request data',
Expand Down Expand Up @@ -152,7 +154,8 @@ export class FileManagerController {
if (
!markdownFileDTO.UserID ||
!markdownFileDTO.MarkdownID ||
!markdownFileDTO.Content
(!markdownFileDTO.Content &&
markdownFileDTO.Content !== '')
)
throw new HttpException(
'Invalid request data',
Expand Down Expand Up @@ -235,7 +238,7 @@ export class FileManagerController {
if (
!folderDTO.UserID ||
!folderDTO.FolderName ||
!folderDTO.Path
(!folderDTO.Path && folderDTO.Path !== '')
)
throw new HttpException(
'Invalid request data',
Expand Down Expand Up @@ -317,8 +320,9 @@ export class FileManagerController {
if (
!folderDTO.UserID ||
!folderDTO.FolderID ||
!folderDTO.ParentFolderID ||
!folderDTO.Path
(!folderDTO.ParentFolderID &&
folderDTO.ParentFolderID !== '') ||
(!folderDTO.Path && folderDTO.Path !== '')
)
throw new HttpException(
'Invalid request data',
Expand Down Expand Up @@ -374,8 +378,10 @@ export class FileManagerController {
!importDTO.UserID ||
!importDTO.Type ||
!importDTO.Content ||
!importDTO.ParentFolderID ||
!importDTO.Path ||
(!importDTO.ParentFolderID &&
importDTO.ParentFolderID !== '') ||
(!importDTO.Path &&
importDTO.Path !== '') ||
!importDTO.Name
)
throw new HttpException(
Expand Down
3 changes: 1 addition & 2 deletions backend/src/file_manager/file_manager.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { ImportDTO } from './dto/import.dto';
import { ConversionService } from '../conversion/conversion.service';
import { ExportDTO } from './dto/export.dto';
import { UsersService } from '../users/users.service';
import { SHA256 } from 'crypto-js';
import * as CryptoJS from 'crypto-js';

@Injectable()
Expand Down Expand Up @@ -451,7 +450,7 @@ export class FileManagerService {
UserID,
);

const encryptionKey = SHA256(
const encryptionKey = CryptoJS.SHA256(
user.Password,
).toString();

Expand Down
188 changes: 184 additions & 4 deletions backend/src/folders/folders.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,20 @@ import {
} from '@nestjs/testing';
import { FoldersService } from './folders.service';
import { getRepositoryToken } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Any, Repository } from 'typeorm';
import { Folder } from './entities/folder.entity';
import { FolderDTO } from './dto/folder.dto';
import * as CryptoJS from 'crypto-js';

jest.mock('crypto-js', () => {
const mockedHash = jest.fn(() => 'mockedHash');

return {
SHA256: jest.fn().mockReturnValue({
toString: mockedHash,
}),
};
});

describe('FoldersService', () => {
let service: FoldersService;
Expand All @@ -27,9 +39,177 @@ describe('FoldersService', () => {
);
});

describe('root/config', () => {
it('folder service should be defined', () => {
expect(service).toBeDefined();
describe('findAllByUserID', () => {
it('should return an array of folders', async () => {
const folder1 = new Folder();
folder1.FolderID = '1';
folder1.FolderName = 'folder1';
folder1.UserID = 1;

const folder2 = new Folder();
folder2.FolderID = '2';
folder2.FolderName = 'folder2';
folder2.UserID = 1;

const result = [folder1, folder2];
jest
.spyOn(Repository.prototype, 'find')
.mockResolvedValue(result);

expect(
await service.findAllByUserID(1),
).toBe(result);
expect(
Repository.prototype.find,
).toBeCalled();
});
});

describe('create', () => {
it('should generate a folderID and save to the database', async () => {
const folderDTO = new FolderDTO();
folderDTO.UserID = 1;

const expectedDTO = new FolderDTO();
expectedDTO.UserID = 1;
expectedDTO.FolderID = 'mockedHash';

jest
.spyOn(Repository.prototype, 'save')
.mockResolvedValue(expectedDTO);

const result = service.create(folderDTO);

expect(CryptoJS.SHA256).toHaveBeenCalled();
expect(
Repository.prototype.save,
).toBeCalledWith(expectedDTO);
});

it('should return a new FolderDTO', async () => {
const folderDTO = new FolderDTO();
folderDTO.UserID = 1;

const expectedDTO = new FolderDTO();
expectedDTO.UserID = 1;
expectedDTO.FolderID = 'mockedHash';

jest
.spyOn(Repository.prototype, 'save')
.mockResolvedValue(expectedDTO);

const result = await service.create(
folderDTO,
);

expect(result).toBe(expectedDTO);
});
});

describe('updateName', () => {
it('should find the folder and update the name', async () => {
const folderDTO = new FolderDTO();
folderDTO.FolderID = '1';
folderDTO.FolderName = 'newName';

const folder = new FolderDTO();
folder.FolderID = '1';
folder.FolderName = 'oldName';

jest
.spyOn(Repository.prototype, 'findOne')
.mockResolvedValue(folderDTO);

jest
.spyOn(Repository.prototype, 'save')
.mockResolvedValue(folder);

const result = await service.updateName(
folderDTO,
);

expect(result).toBe(folder);
expect(
Repository.prototype.findOne,
).toBeCalledWith({
where: {
FolderID: folderDTO.FolderID,
},
});
expect(
Repository.prototype.save,
).toBeCalled();
});
});

describe('remove', () => {
it('should find the folder and remove it', async () => {
const folderDTO = new FolderDTO();
folderDTO.FolderID = '1';

const folder = new Folder();
folder.FolderID = '1';
folder.UserID = 1;

jest
.spyOn(Repository.prototype, 'findOne')
.mockResolvedValue(folder);

jest
.spyOn(Repository.prototype, 'remove')
.mockResolvedValue(folder);

const result = await service.remove(
folderDTO,
);

expect(result).toBe(folder);
expect(
Repository.prototype.findOne,
).toBeCalledWith({
where: {
FolderID: folderDTO.FolderID,
},
});
expect(
Repository.prototype.remove,
).toBeCalledWith(folder);
});
});

describe('updatePath', () => {
it('should find the folder and update the path', async () => {
const folderDTO = new FolderDTO();
folderDTO.FolderID = '1';
folderDTO.Path = 'newPath';

const folder = new FolderDTO();
folder.FolderID = '1';
folder.Path = 'oldPath';

jest
.spyOn(Repository.prototype, 'findOne')
.mockResolvedValue(folderDTO);

jest
.spyOn(Repository.prototype, 'save')
.mockResolvedValue(folder);

const result = await service.updatePath(
folderDTO,
);

expect(result).toBe(folder);
expect(
Repository.prototype.findOne,
).toBeCalledWith({
where: {
FolderID: folderDTO.FolderID,
},
});
expect(
Repository.prototype.save,
).toBeCalled();
});
});
});
4 changes: 2 additions & 2 deletions backend/src/folders/folders.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Folder } from './entities/folder.entity';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { FolderDTO } from './dto/folder.dto';
import { SHA256 } from 'crypto-js';
import * as CryptoJS from 'crypto-js';

@Injectable()
export class FoldersService {
Expand All @@ -19,7 +19,7 @@ export class FoldersService {
}

async create(createFolderDTO: FolderDTO) {
const folderID = SHA256(
const folderID = CryptoJS.SHA256(
createFolderDTO.UserID.toString() +
new Date().getTime().toString(),
).toString();
Expand Down
Loading