-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1067589
commit 512fe7b
Showing
7 changed files
with
210 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,39 @@ | ||
import { AppController } from './app.controller'; | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { firstValueFrom, of, throwError } from 'rxjs'; | ||
import { | ||
firstValueFrom, | ||
lastValueFrom, | ||
of, | ||
Subject, | ||
Subscription, | ||
throwError, | ||
} from 'rxjs'; | ||
import { HttpService } from '@nestjs/axios'; | ||
import { BadRequestException, UnauthorizedException } from '@nestjs/common'; | ||
import { DeploymentInfo } from './deployment-info.dto'; | ||
import { ConfigModule } from '@nestjs/config'; | ||
import * as fs from 'fs'; | ||
|
||
let onSubscription: Subscription; | ||
let lines: Subject<string>; | ||
jest.mock('tail', () => { | ||
// Mock Tail constructor | ||
return { | ||
Tail: jest.fn().mockReturnValue({ | ||
on: (event, callback) => (onSubscription = lines.subscribe(callback)), | ||
unwatch: () => { | ||
console.log('subscription', onSubscription); | ||
onSubscription.unsubscribe(); | ||
}, | ||
}), | ||
}; | ||
}); | ||
|
||
describe('AppController', () => { | ||
let controller: AppController; | ||
let mockHttp: { post: jest.Mock }; | ||
let mockWs: { write: jest.Mock; close: jest.Mock }; | ||
|
||
const deploymentData: DeploymentInfo = { | ||
name: 'test-name', | ||
locale: 'de', | ||
|
@@ -23,6 +47,9 @@ describe('AppController', () => { | |
}; | ||
|
||
beforeEach(async () => { | ||
lines = new Subject(); | ||
mockWs = { write: jest.fn(), close: jest.fn() }; | ||
jest.spyOn(fs, 'createWriteStream').mockReturnValue(mockWs as any); | ||
mockHttp = { | ||
post: jest.fn().mockReturnValue(of({ data: undefined })), | ||
}; | ||
|
@@ -51,45 +78,131 @@ describe('AppController', () => { | |
}); | ||
}); | ||
|
||
it('should throw bad request exception if data has wrong format', (done) => { | ||
const invalidData = { ...deploymentData, name: 'with space' }; | ||
// TODO: add an extensive list of invalid formats including attempts someone could pass to try and inject code? | ||
it('should throw bad request exception if name has wrong format', async () => { | ||
passDeployment(); | ||
function testName(name: string) { | ||
return lastValueFrom(controller.deployApp({ ...deploymentData, name })); | ||
} | ||
await expect(testName('with space')).rejects.toBeInstanceOf( | ||
BadRequestException, | ||
); | ||
await expect(testName('withCapital')).resolves.toBeTruthy(); | ||
await expect(testName('withSymbol?')).rejects.toBeInstanceOf( | ||
BadRequestException, | ||
); | ||
await expect(testName('withNumber123')).resolves.toBeTruthy(); | ||
await expect(testName('with-dash')).resolves.toBeTruthy(); | ||
await expect(testName('with_underscore')).rejects.toBeInstanceOf( | ||
BadRequestException, | ||
); | ||
}); | ||
|
||
controller.deployApp(invalidData).subscribe({ | ||
error: (err) => { | ||
expect(err).toBeInstanceOf(BadRequestException); | ||
done(); | ||
}, | ||
function passDeployment() { | ||
// automatically finish deployment | ||
jest.spyOn(lines, 'subscribe').mockImplementation((fn) => { | ||
setTimeout(() => fn('DONE')); | ||
return { unsubscribe: () => undefined } as any; | ||
}); | ||
} | ||
|
||
it('should throw bad request exception if username has wrong format', async () => { | ||
passDeployment(); | ||
function testName(username: string) { | ||
return lastValueFrom( | ||
controller.deployApp({ ...deploymentData, username }), | ||
); | ||
} | ||
|
||
await expect(testName('with space')).rejects.toBeInstanceOf( | ||
BadRequestException, | ||
); | ||
await expect(testName('withCapital')).resolves.toBeTruthy(); | ||
await expect(testName('withSymbol?')).rejects.toBeInstanceOf( | ||
BadRequestException, | ||
); | ||
await expect(testName('withNumber123')).resolves.toBeTruthy(); | ||
await expect(testName('with-dash')).resolves.toBeTruthy(); | ||
await expect(testName('with_underscore')).rejects.toBeInstanceOf( | ||
BadRequestException, | ||
); | ||
}); | ||
|
||
it('should write arguments to file', async () => { | ||
const mockWs = { write: jest.fn(), close: jest.fn() }; | ||
jest.spyOn(fs, 'createWriteStream').mockReturnValue(mockWs as any); | ||
it('should throw bad request exception if email has wrong format', async () => { | ||
passDeployment(); | ||
function testMail(email: string) { | ||
return lastValueFrom(controller.deployApp({ ...deploymentData, email })); | ||
} | ||
|
||
await firstValueFrom(controller.deployApp(deploymentData)); | ||
await expect(testMail('testmail')).rejects.toBeInstanceOf( | ||
BadRequestException, | ||
); | ||
await expect(testMail('test@mail')).rejects.toBeInstanceOf( | ||
BadRequestException, | ||
); | ||
await expect(testMail('Test@[email protected]')).rejects.toBeInstanceOf( | ||
BadRequestException, | ||
); | ||
await expect(testMail('[email protected]')).resolves.toBeTruthy(); | ||
await expect(testMail('[email protected]')).resolves.toBeTruthy(); | ||
await expect(testMail('[email protected]')).resolves.toBeTruthy(); | ||
await expect(testMail('[email protected]')).resolves.toBeTruthy(); | ||
await expect(testMail('[email protected]')).resolves.toBeTruthy(); | ||
}); | ||
|
||
it('should throw error if ERROR is written to log', async () => { | ||
const res = firstValueFrom(controller.deployApp(deploymentData)); | ||
lines.next('some logs'); | ||
lines.next('ERROR my custom error'); | ||
|
||
try { | ||
await res; | ||
} catch (err) { | ||
expect(err).toBeInstanceOf(BadRequestException); | ||
expect(err.message).toBe('my custom error'); | ||
// Ensure tail is properly "unwatched" | ||
expect(onSubscription.closed).toBeTruthy(); | ||
return; | ||
} | ||
|
||
throw new Error('No error thrown'); | ||
}); | ||
|
||
it('should write arguments to file', () => { | ||
const res = firstValueFrom(controller.deployApp(deploymentData)); | ||
|
||
lines.next('DONE'); | ||
|
||
expect(res).resolves.toBeTruthy(); | ||
expect(mockWs.write).toHaveBeenCalledWith( | ||
'test-name de [email protected] test-username test-base y n', | ||
); | ||
expect(mockWs.close).toHaveBeenCalled(); | ||
// Ensure tail is properly "unwatched" | ||
expect(onSubscription.closed).toBeTruthy(); | ||
}); | ||
|
||
it('should use the default locale if empty or omitted', async () => { | ||
const mockWs = { write: jest.fn(), close: jest.fn() }; | ||
jest.spyOn(fs, 'createWriteStream').mockReturnValue(mockWs as any); | ||
it('should use the default locale if empty', (done) => { | ||
const emptyLocale = { ...deploymentData, locale: '' }; | ||
controller.deployApp(emptyLocale).subscribe(() => { | ||
expect(mockWs.write).toHaveBeenCalledWith( | ||
'test-name en [email protected] test-username test-base y n', | ||
); | ||
done(); | ||
}); | ||
|
||
const withoutLocale = { ...deploymentData, locale: '' }; | ||
await firstValueFrom(controller.deployApp(withoutLocale)); | ||
expect(mockWs.write).toHaveBeenCalledWith( | ||
'test-name en [email protected] test-username test-base y n', | ||
); | ||
lines.next('DONE'); | ||
}); | ||
|
||
mockWs.write.mockReset(); | ||
delete withoutLocale.locale; | ||
await firstValueFrom(controller.deployApp(withoutLocale)); | ||
expect(mockWs.write).toHaveBeenCalledWith( | ||
'test-name en [email protected] test-username test-base y n', | ||
); | ||
it('should use the default locale if omitted', (done) => { | ||
const noLocale = { ...deploymentData }; | ||
delete noLocale.locale; | ||
controller.deployApp(noLocale).subscribe(() => { | ||
expect(mockWs.write).toHaveBeenCalledWith( | ||
'test-name en [email protected] test-username test-base y n', | ||
); | ||
done(); | ||
}); | ||
|
||
lines.next('DONE'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.