Start working on coverage

This commit is contained in:
sayhiben
2025-01-20 00:21:07 -08:00
parent c58a4c3bd3
commit 3c48c2c6ca
3 changed files with 52 additions and 2 deletions

View File

@@ -1,14 +1,14 @@
{
"name": "streamwall",
"version": "1.0.0",
"description": "View streams in a grid",
"description": "View streams in a grid",
"main": "./.webpack/main",
"scripts": {
"start": "electron-forge start -- --trace-warnings --control.address=http://localhost:4444 --control.username=admin --control.password=password",
"make": "electron-forge make",
"package": "electron-forge package",
"publish": "electron-forge publish",
"test": "jest",
"test": "jest --verbose --detectOpenHandles --no-stack-trace-limit",
"test:ci": "jest --ci --reporters=default --reporters=jest-junit --coverage"
},
"author": "Max Goodhart <c@chromakode.com>",

39
src/node/auth.test.js Normal file
View File

@@ -0,0 +1,39 @@
import { Auth } from './auth'
describe('Auth', () => {
it('uses provided salt from persistData', () => {
const auth = new Auth({
adminUsername: 'admin',
adminPassword: 'pass',
persistData: { salt: 'MY_FIXED_SALT' }
})
expect(auth.salt).toBe('MY_FIXED_SALT')
})
it('throws on invalid role', async () => {
const auth = new Auth({ adminUsername: 'admin', adminPassword: 'pass' })
await expect(
auth.createToken({ kind: 'session', role: 'notreal', name: 'Test' })
).rejects.toThrow('invalid role')
})
it('logs when logEnabled is true', async () => {
const consoleSpy = jest.spyOn(console, 'log').mockImplementation(() => {})
const auth = new Auth({
adminUsername: 'admin',
adminPassword: 'pass',
logEnabled: true
})
await auth.createToken({ kind: 'session', role: 'operator', name: 'Test' })
expect(consoleSpy).toHaveBeenCalledWith(
expect.stringContaining('Created session token:'),
expect.objectContaining({ role: 'operator', name: 'Test' })
)
consoleSpy.mockRestore()
})
it('silently ignores deleting a non-existent token', () => {
const auth = new Auth({ adminUsername: 'admin', adminPassword: 'pass' })
expect(() => auth.deleteToken('nope')).not.toThrow()
})
})

View File

@@ -212,6 +212,17 @@ describe('streamwall server', () => {
})
})
it('rejects websocket upgrade if origin is invalid', async () => {
// Make a raw WebSocket connection with a bogus origin
const badWs = new WebSocket(`ws://localhost:${port}/ws`, [], {
origin: 'http://evilsite.com', // not the expectedOrigin
})
const closePromise = once(badWs, 'close')
const [code] = await closePromise
expect(code).toBe(401)
// or just expect the server forcibly closed the connection
})
describe('admin role', () => {
it('can view tokens', async () => {
await auth.createToken({