fet: Add changes lol

Signed-off-by: Florian BOUILLON <f.bouillon@aptatio.com>
This commit is contained in:
2023-06-28 17:30:18 +02:00
parent 9530be5c43
commit ff07f8f4a5
24 changed files with 656 additions and 511 deletions

View File

@ -0,0 +1,22 @@
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

@ -0,0 +1,50 @@
import { afterEach, assert, beforeEach, describe, expect, it, test, vi } 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

@ -0,0 +1,122 @@
import { afterEach, assert, beforeEach, describe, expect, it, test, 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()
})
})