Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
74 commits
Select commit Hold shift + click to select a range
5377b8d
test: add unit tests for various services and controllers
tomast1337 Nov 11, 2024
7f5fad0
chore: remove jest configuration from package.json
tomast1337 Nov 11, 2024
7f32cff
fix: update coverage directory path in jest configuration
tomast1337 Nov 12, 2024
be1dcd9
fix: add module name mapping for server imports in jest configuration
tomast1337 Nov 12, 2024
e829c7f
refactor: update file module to use dynamic configuration and remove …
tomast1337 Nov 12, 2024
bd7fb02
test: enhance unit tests by adding service providers for dependency i…
tomast1337 Nov 12, 2024
fddc98f
refactor: update AuthModule and FileModule to use asynchronous initia…
tomast1337 Nov 12, 2024
ae12a74
test: enhance FileService unit tests with S3 client mocks and error h…
tomast1337 Nov 14, 2024
9cf840d
test: enhance AuthService unit tests with axios mocks and token verif…
tomast1337 Nov 14, 2024
06fb75d
test: enhance AuthController unit tests with mock AuthService and exc…
tomast1337 Nov 14, 2024
2de8b3b
test: enhance MySongsController unit tests with mock SongService and …
tomast1337 Nov 14, 2024
1dd4c87
test: enhance SongUploadService unit tests with mock FileService and …
tomast1337 Nov 14, 2024
0daee3d
test: add TODO for additional tests in SongUploadService unit tests
tomast1337 Nov 14, 2024
94a7539
test: enhance SongController unit tests with mock services and error …
tomast1337 Nov 14, 2024
d380f0b
test: enhance PlaylistController unit tests with mock PlaylistService…
tomast1337 Nov 14, 2024
42205cd
test: rename controller variable for consistency in SongController un…
tomast1337 Nov 15, 2024
84731bb
test: enhance SongBrowserController unit tests with mock service meth…
tomast1337 Nov 15, 2024
37a78c0
test: enhance UserController unit tests with mock UserService and add…
tomast1337 Nov 15, 2024
7c759f1
Merge branch 'main' of https://github.com/OpenNBS/NoteBlockWorld into…
tomast1337 Nov 15, 2024
b9f2ea8
refactor: replace console.error with Logger in FileService for improv…
tomast1337 Nov 15, 2024
9b185f9
test: mock thumbnail and song pack features in SongUploadService tests
tomast1337 Nov 15, 2024
df8bda5
refactor: update method signatures in SongUploadService for improved …
tomast1337 Nov 15, 2024
b67f438
Merge https://github.com/OpenNBS/NoteBlockWorld into unit-testing
tomast1337 Nov 17, 2024
489fe16
test: add exception handling for null userDocument in MySongsController
tomast1337 Nov 17, 2024
5d7f3c3
fix: remove unnecessary null check for user in getSelfUserData method
tomast1337 Nov 17, 2024
1240741
test: update user controller test to handle null user with HttpException
tomast1337 Nov 17, 2024
3c61013
feat: song service tests
tomast1337 Nov 17, 2024
afa9e21
test: enhance SongService tests with improved mock implementations an…
tomast1337 Nov 17, 2024
fd5910d
fix: simplify deleteOne call in SongService by removing unnecessary p…
tomast1337 Nov 17, 2024
89666c3
test: refactor delete song test to use a more detailed song entity mock
tomast1337 Nov 17, 2024
108b84a
test: enhance SongService tests with improved mock implementations an…
tomast1337 Nov 18, 2024
0f8197c
test: improve SongService tests with additional edge case scenarios a…
tomast1337 Nov 19, 2024
cbcc884
refactor: streamline SongService methods by removing unnecessary exec…
tomast1337 Nov 19, 2024
59b8d71
test: fix getSong test by removing TODO and updating mock implementation
tomast1337 Nov 19, 2024
ce63c38
test: fix uploadSong test
tomast1337 Nov 19, 2024
5672f03
test: enhance UserService tests with comprehensive coverage for user …
tomast1337 Nov 19, 2024
a60bb1f
feat: update GetUser DTO to make email, username, and id fields optional
tomast1337 Nov 19, 2024
a3adb4a
test: simplify username generation tests by refining mock behavior an…
tomast1337 Nov 19, 2024
5c24865
feat: update UserService to create users directly and enhance paginat…
tomast1337 Nov 19, 2024
0a88818
refactor: remove redundant S3 configuration validation in FileService
tomast1337 Nov 19, 2024
2ced633
test: add unit test for uploadPackedSong method in FileService
tomast1337 Nov 19, 2024
1f1feff
style: format jest.config.js for improved readability
tomast1337 Nov 19, 2024
5f91dd2
refactor: remove user existence check in SongService for streamlined …
tomast1337 Nov 19, 2024
fab4cb0
test: add error handling tests for SongService methods
tomast1337 Nov 19, 2024
53240fa
test: add unit tests for getFeaturedSongs and getRecentSongs methods …
tomast1337 Nov 19, 2024
a9729d6
test: update getFeaturedSongs test to remove unnecessary result assig…
tomast1337 Nov 19, 2024
72968de
test: add unit tests for processUploadedSong and processSongPatch met…
tomast1337 Nov 19, 2024
874ea6e
test: enhance SongUploadService tests with instrument layers and note…
tomast1337 Nov 19, 2024
36a909e
test: update jest configuration to ignore specific paths for test and…
tomast1337 Nov 19, 2024
c8005c1
test: rename userService variable to _userService for consistency in …
tomast1337 Nov 19, 2024
6fa169e
test: add unit tests for JWT, GitHub, and Google strategies
tomast1337 Nov 19, 2024
c4716bb
test: update tests for DiscordStrategy to replace GithubStrategy refe…
tomast1337 Nov 19, 2024
e97fdfb
test: add unit tests for GetRequestUser, initializeSwagger, and Parse…
tomast1337 Nov 19, 2024
385e1d5
test: remove unused DocumentBuilder import in initializeSwagger tests
tomast1337 Nov 19, 2024
a255315
refactor: simplify configuration retrieval in auth strategies
tomast1337 Nov 20, 2024
7fb1e68
refactor: update FileModule initialization to use forRootAsync
tomast1337 Nov 20, 2024
2fe046b
refactor: simplify MongoDB connection configuration by using getOrThrow
tomast1337 Nov 20, 2024
4167c5a
refactor: update JwtStrategy tests to use getOrThrow for configuratio…
tomast1337 Nov 28, 2024
de7ac73
refactor: update authentication strategy tests to use getOrThrow for …
tomast1337 Nov 28, 2024
c10e46b
chore: add `vscode-jest` to workspace recommended extensions
Bentroen Nov 28, 2024
30cf453
feat: add Jest configuration and initial test suite for song statistics
tomast1337 Nov 29, 2024
eb6fb17
chore: add workspace configuration for project structure and Jest set…
tomast1337 Nov 29, 2024
1398e8f
refactor: improve test descriptions for song statistics calculations
tomast1337 Nov 29, 2024
37533cf
Merge branch 'develop' of https://github.com/OpenNBS/NoteBlockWorld i…
tomast1337 Nov 29, 2024
3af6a7d
test: update expected username in UserService tests
tomast1337 Nov 29, 2024
807be74
test: implement tests for googleLogin, githubLogin, and discordLogin …
tomast1337 Dec 1, 2024
ca4a44d
test: add unit tests for JWT token creation and user verification in …
tomast1337 Dec 1, 2024
f05438e
chore: remove commented-out configuration code in AuthService constru…
tomast1337 Dec 1, 2024
7286ab0
test: add error handling tests for thumbnail and file uploads in Song…
tomast1337 Dec 1, 2024
7b379cf
Merge branch 'develop' into unit-testing
Bentroen Dec 1, 2024
114266d
chore: remove unused dependencies from pnpm-lock.yaml
tomast1337 Dec 1, 2024
c64a7ac
feat: add SongWebhookService mock and formatDuration utility function
tomast1337 Dec 1, 2024
da3713b
refactor: mark formatDuration for future relocation to shared utilities
tomast1337 Dec 1, 2024
9fcf4ae
refactor: add TODO to move formatDuration to shared/util
tomast1337 Dec 1, 2024
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
3 changes: 2 additions & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"recommendations": [
"dbaeumer.vscode-eslint",
"esbenp.prettier-vscode",
"unifiedjs.vscode-mdx"
"unifiedjs.vscode-mdx",
"orta.vscode-jest"
]
}
24 changes: 24 additions & 0 deletions NoteBlockWorld.code-workspace
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"folders": [
{
"path": ".",
"name": "Root"
},
{
"path": "./server",
"name": "Backend"
},
{
"path": "./shared",
"name": "Shared"
},
{
"path": "./web",
"name": "Frontend"
}
],
"settings": {
"mdx.server.enable": true,
"jest.disabledWorkspaceFolders": ["Root", "Frontend"]
}
}
35 changes: 35 additions & 0 deletions server/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module.exports = {
moduleFileExtensions: ['js', 'json', 'ts'],
rootDir: '.',
testRegex: '.*\\.spec\\.ts$',
transform: {
'^.+\\.(t|j)s$': [
'ts-jest',
{
tsconfig: '<rootDir>/tsconfig.json',
ignoreCodes: ['TS151001'],
},
],
},
collectCoverageFrom: ['**/*.(t|j)s'],
coverageDirectory: './coverage',
testEnvironment: 'node',
moduleNameMapper: {
'^@shared/(.*)$': '<rootDir>/../shared/$1',
'^@server/(.*)$': '<rootDir>/src/$1',
},
testPathIgnorePatterns: [
'<rootDir>/node_modules/',
'<rootDir>/dist/',
'<rootDir>/coverage/',
],
coveragePathIgnorePatterns: [
'<rootDir>/node_modules/',
'<rootDir>/coverage/',
'<rootDir>/dist/',
'<rootDir>/src/.*\\.module\\.ts$',
'<rootDir>/src/main.ts',
'.eslintrc.js',
'jest.config.js',
],
};
17 changes: 0 additions & 17 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,22 +76,5 @@
"ts-node": "^10.9.1",
"tsconfig-paths": "^4.2.0",
"typescript": "^5.1.3"
},
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "src",
"testRegex": ".*\\.spec\\.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"collectCoverageFrom": [
"**/*.(t|j)s"
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
}
42 changes: 42 additions & 0 deletions server/src/GetRequestUser.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { ExecutionContext, HttpException, HttpStatus } from '@nestjs/common';

import { GetRequestToken, validateUser } from './GetRequestUser';
import { UserDocument } from './user/entity/user.entity';

describe('GetRequestToken', () => {
it('should be a defined decorator', () => {
const mockExecutionContext = {
switchToHttp: jest.fn().mockReturnThis(),
} as unknown as ExecutionContext;

const result = GetRequestToken(null, mockExecutionContext);

expect(typeof result).toBe('function');
});
});

describe('validateUser', () => {
it('should return the user if the user exists', () => {
const mockUser = {
_id: 'test-id',
username: 'testuser',
} as unknown as UserDocument;

const result = validateUser(mockUser);

expect(result).toEqual(mockUser);
});

it('should throw an error if the user does not exist', () => {
expect(() => validateUser(null)).toThrowError(
new HttpException(
{
error: {
user: 'User not found',
},
},
HttpStatus.UNAUTHORIZED,
),
);
});
});
9 changes: 3 additions & 6 deletions server/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ import { UserModule } from './user/user.module';
useFactory: (
configService: ConfigService,
): MongooseModuleFactoryOptions => {
const url = configService.get<string>('MONGO_URL');
if (!url) {
throw new Error('Missing DB config');
}
const url = configService.getOrThrow<string>('MONGO_URL');
Logger.debug(`Connecting to ${url}`);

return {
Expand All @@ -39,8 +36,8 @@ import { UserModule } from './user/user.module';
}),
SongModule,
UserModule,
AuthModule,
FileModule,
AuthModule.forRootAsync(),
FileModule.forRootAsync(),
SongBrowserModule,
],
controllers: [],
Expand Down
113 changes: 113 additions & 0 deletions server/src/auth/auth.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import { Test, TestingModule } from '@nestjs/testing';
import { Request, Response } from 'express';

import { AuthController } from './auth.controller';
import { AuthService } from './auth.service';

const mockAuthService = {
githubLogin: jest.fn(),
googleLogin: jest.fn(),
discordLogin: jest.fn(),
verifyToken: jest.fn(),
};

describe('AuthController', () => {
let controller: AuthController;
let authService: AuthService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [AuthController],
providers: [
{
provide: AuthService,
useValue: mockAuthService,
},
],
}).compile();

controller = module.get<AuthController>(AuthController);
authService = module.get<AuthService>(AuthService);
});

it('should be defined', () => {
expect(controller).toBeDefined();
});

describe('githubRedirect', () => {
it('should call AuthService.githubLogin', async () => {
const req = {} as Request;
const res = {} as Response;

await controller.githubRedirect(req, res);

expect(authService.githubLogin).toHaveBeenCalledWith(req, res);
});

it('should handle exceptions', async () => {
const req = {} as Request;
const res = {} as Response;
const error = new Error('Test error');
(authService.githubLogin as jest.Mock).mockRejectedValueOnce(error);

await expect(controller.githubRedirect(req, res)).rejects.toThrow(
'Test error',
);
});
});

describe('googleRedirect', () => {
it('should call AuthService.googleLogin', async () => {
const req = {} as Request;
const res = {} as Response;

await controller.googleRedirect(req, res);

expect(authService.googleLogin).toHaveBeenCalledWith(req, res);
});

it('should handle exceptions', async () => {
const req = {} as Request;
const res = {} as Response;
const error = new Error('Test error');
(authService.googleLogin as jest.Mock).mockRejectedValueOnce(error);

await expect(controller.googleRedirect(req, res)).rejects.toThrow(
'Test error',
);
});
});

describe('discordRedirect', () => {
it('should call AuthService.discordLogin', async () => {
const req = {} as Request;
const res = {} as Response;

await controller.discordRedirect(req, res);

expect(authService.discordLogin).toHaveBeenCalledWith(req, res);
});

it('should handle exceptions', async () => {
const req = {} as Request;
const res = {} as Response;
const error = new Error('Test error');
(authService.discordLogin as jest.Mock).mockRejectedValueOnce(error);

await expect(controller.discordRedirect(req, res)).rejects.toThrow(
'Test error',
);
});
});

describe('verify', () => {
it('should call AuthService.verifyToken', async () => {
const req = {} as Request;
const res = {} as Response;

await controller.verify(req, res);

expect(authService.verifyToken).toHaveBeenCalledWith(req, res);
});
});
});
Loading