-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
67 lines (52 loc) · 2.54 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import jwtValid, { parseBase64ByPlattform, isValidBase64, parseJwtToken } from './index'
const jwtToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqd3QiOiJpcyBhd2Vzb21lISJ9.j8Z3twgi5nCrZJXH1JoxfQ8q1u5btwr3vS3wyqfytOo'
describe('JWT Valid', () => {
describe('isValidBase64', () => {
it('returns false if a not valid base64 string is passed', () => {
expect(isValidBase64('No Valid Base64 String')).to.be.false
})
it('returns true if a valid base64 string is passed', () => {
expect(isValidBase64('SSBhbSB2YWxpZA==')).to.be.true
})
})
describe('parseBase64ByPlattform', () => {
it('returns error if no valid token is passed', () => {
expect(() => parseBase64ByPlattform()).to.throw('No valid base64 passed')
})
it('returns encoded base64 string', () => {
expect(parseBase64ByPlattform('SGVsbG8gV29ybGQ=')).to.equal('Hello World')
})
})
describe('parseJwtToken', () => {
it('returns error if nothing is passed', () => {
expect(() => parseJwtToken()).to.throw('No valid JWT token passed')
})
it('returns error if no valid token is passed', () => {
expect(() => parseJwtToken('iam-no-jwt-token')).to.throw('No valid JWT token passed')
})
it('returns error if no valid token is passed', () => {
expect(() => parseJwtToken('a.b.c')).to.throw('No valid JWT token passed')
})
it('returns error if no valid token is passed', () => {
expect(() => parseJwtToken('SGVsbG8gV29ybGQ=.SGVsbG8gV29ybGQ=.foobar')).to.throw('No valid JWT token passed')
})
it('returns error if no valid token is passed', () => {
expect(() => parseJwtToken('eyJhbGciOiJpbnZhbGlkIiwidHlwIjoiaW52YWxpZCJ9.eyBub3RoaW5nOiAndHJ1ZScgfQ==.some_signature')).to.throw('No valid JWT token passed')
})
it('returns a object', () => {
expect(parseJwtToken(jwtToken)).to.be.a('object')
})
it('returns a object with the props header, payload and signature', () => {
expect(parseJwtToken(jwtToken)).to.include.keys('header', 'payload', 'signature')
})
it('returns a object with the header containing the decoded informations', () => {
expect(parseJwtToken(jwtToken).header).to.deep.equal({ alg: 'HS256', typ: 'JWT' })
})
it('returns a object with the payload containing the decoded informations', () => {
expect(parseJwtToken(jwtToken).payload).to.deep.equal({ jwt: 'is awesome!' })
})
it('returns a object with the signature string', () => {
expect(parseJwtToken(jwtToken).signature).to.equal(jwtToken.split('.')[2])
})
})
})