feat: first version
Some checks failed
Build, check & Test / run (push) Failing after 39s

Signed-off-by: Florian BOUILLON <f.bouillon@aptatio.com>
This commit is contained in:
2023-07-20 17:41:16 +02:00
parent 2bd59f902f
commit 09ed4c487d
80 changed files with 1171 additions and 2755 deletions

5
tests/README.md Normal file
View File

@ -0,0 +1,5 @@
# Tests
Old Unit tests for each elements
the paths should correspond to the base folder from `src`

View File

@ -1,5 +1,4 @@
import { assert, expect, test } from 'vitest'
import { comparePassword, hashPassword } from '../src/libs/AuthUtils'
import { expect, test } from 'vitest'
// Edit an assertion and save to see HMR in action
@ -8,26 +7,3 @@ test('Math.sqrt()', () => {
expect(Math.sqrt(144)).toBe(12);
expect(Math.sqrt(2)).toBe(Math.SQRT2);
});
test('JSON', () => {
const input = {
foo: 'hello',
bar: 'world',
};
const output = JSON.stringify(input);
expect(output).eq('{"foo":"hello","bar":"world"}');
assert.deepEqual(JSON.parse(output), input, 'matches original');
});
test('auth util', async () => {
const password = 'pouet'
const out1 = await hashPassword(password)
expect(out1).not.toBe(password)
const out2 = await hashPassword(password)
expect(out2).not.toBe(out1)
expect(await comparePassword(password, out1)).toBe(true)
expect(await comparePassword(password, out2)).toBe(true)
})

View File

@ -1,22 +0,0 @@
import { describe, expect, it } from 'vitest'
import { comparePassword, hashPassword } from '../../src/libs/AuthUtils'
describe('AuthUtils', () => {
it('should hash the password', async () => {
expect(await hashPassword('test')).toBeDefined()
})
it('should compared succeffully password', async () => {
const pass = 'test'
const hash = await hashPassword(pass)
expect(await comparePassword(pass, hash)).toBe(true)
})
it('should not generate twice the same hash', async () => {
const pass = 'test'
const hash1 = await hashPassword(pass)
const hash2 = await hashPassword(pass)
expect(hash1).not.toBe(hash2)
})
})

View File

@ -1,50 +0,0 @@
import { describe, expect, it } from 'vitest'
import { getParams } from '../../src/libs/gcodeUtils'
describe('gcodeUtils', () => {
it('should get parameters', () => {
const gcode = `
balfs
dgfdf
sd
httphq
estimated_printing_time_normal_modewef
; test=a
fgd
;test =b
;test= c
;test = d
;number=1.12
;number2=-1
;invalid=
;invalid
;estimated_printing_time_normal_mode=1d 1h 1m 1s
sdffgaf
fgsdf
g
sfd
hh
ehf
`
expect(getParams(gcode)).toEqual({
test: 'a',
test_1: 'b',
test_2: 'c',
test_3: 'd',
number: 1.12,
number2: -1,
estimated_printing_time_normal_mode: '1d 1h 1m 1s',
estimated_printing_time_seconds: 90061
})
})
it('should get parameters', () => {
const gcode = `
;estimated_printing_time_normal_mode=1w 1d 1h 1m 1s
`
expect(() => getParams(gcode)).toThrow(/1w/g)
})
})

View File

@ -1,122 +0,0 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import RateLimiter from '../../src/libs/RateLimiter'
// Mock Response
vi.stubGlobal('Response', class {
public constructor(
public body?: any,
public init?: any
) {}
})
describe('Rate Limit', () => {
beforeEach(() => {
vi.useFakeTimers()
})
it('should work with global variant', () => {
const now1 = new Date(1000)
vi.setSystemTime(now1)
const limit = RateLimiter.getInstance()
const result1 = limit.consume('key')
expect(result1).toEqual({
"X-RateLimit-Limit": '10',
"X-RateLimit-Remaining": '9',
"X-RateLimit-Reset": '61'
})
const limit2 = RateLimiter.getInstance()
const result2 = limit2.consume('key')
expect(result2).toEqual({
"X-RateLimit-Limit": '10',
"X-RateLimit-Remaining": '8',
"X-RateLimit-Reset": '61'
})
})
it('should consume points', () => {
const now1 = new Date(1000)
vi.setSystemTime(now1)
const limit = new RateLimiter(2, 10)
const result1 = limit.consume('key')
expect(result1).toEqual({
"X-RateLimit-Limit": '2',
"X-RateLimit-Remaining": '1',
"X-RateLimit-Reset": '11'
})
const result2 = limit.consume('key')
expect(result2).toEqual({
"X-RateLimit-Limit": '2',
"X-RateLimit-Remaining": '0',
"X-RateLimit-Reset": '11'
})
})
it('should rate limit', () => {
const now1 = new Date(1000)
vi.setSystemTime(now1)
const limit = new RateLimiter(1, 10)
const result1 = limit.consume('key')
expect(result1).toEqual({
"X-RateLimit-Limit": '1',
"X-RateLimit-Remaining": '0',
"X-RateLimit-Reset": '11'
})
const result2 = limit.consume('key')
expect(result2).instanceOf(Response)
const result3 = limit.consume('key')
expect(result3).instanceOf(Response)
})
it('should reset after some time', () => {
const now1 = new Date(1000)
vi.setSystemTime(now1)
const limit = new RateLimiter(2, 10)
const result1 = limit.consume('key')
expect(result1).toEqual({
"X-RateLimit-Limit": '2',
"X-RateLimit-Remaining": '1',
"X-RateLimit-Reset": '11'
})
const result2 = limit.consume('key')
expect(result2).toEqual({
"X-RateLimit-Limit": '2',
"X-RateLimit-Remaining": '0',
"X-RateLimit-Reset": '11'
})
const result3 = limit.consume('key')
expect(result3).instanceOf(Response)
const now2 = new Date(11000)
vi.setSystemTime(now2)
const result4 = limit.consume('key')
expect(result4).toEqual({
"X-RateLimit-Limit": '2',
"X-RateLimit-Remaining": '1',
"X-RateLimit-Reset": '21'
})
const result5 = limit.consume('key')
expect(result5).toEqual({
"X-RateLimit-Limit": '2',
"X-RateLimit-Remaining": '0',
"X-RateLimit-Reset": '21'
})
const result6 = limit.consume('key')
expect(result6).instanceOf(Response)
})
afterEach(() => {
vi.useRealTimers()
})
})