-
Notifications
You must be signed in to change notification settings - Fork 138
/
Copy pathJWT.test.ts
108 lines (100 loc) · 3.2 KB
/
JWT.test.ts
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/* global describe, it, expect */
import { Kind } from 'graphql/language';
import { GraphQLJWT } from '../src/scalars/JWT.js';
// HEADER: {
// "alg": "HS256",
// "typ": "JWT"
// }
//
// PAYLOAD: {
// "sub": "1234567890",
// "iat": 1516239022,
// "project": "graphql-scalars"
// }
//
// SIGNATURE: {
// HMACSHA256(
// base64UrlEncode(header) + "." +
// base64UrlEncode(payload),
// password
// )
// }
const JWT = `eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwiaWF0IjoxNTE2MjM5MDIyLCJwcm9qZWN0IjoiZ3JhcGhxbC1zY2FsYXJzIn0.nYdrSfE2nNRAgpiEU1uKgn2AYYKLo28Z0nhPXvsuIww`;
describe(`JWT`, () => {
describe(`valid`, () => {
it(`serialize`, () => {
expect(GraphQLJWT.serialize(JWT)).toEqual(JWT);
});
it(`parseValue`, () => {
expect(GraphQLJWT.parseValue(JWT)).toEqual(JWT);
});
it(`parseLiteral`, () => {
expect(
GraphQLJWT.parseLiteral(
{
value: JWT,
kind: Kind.STRING,
},
{},
),
).toEqual(JWT);
});
});
describe(`invalid`, () => {
describe(`not a valid JWT`, () => {
it(`serialize`, () => {
expect(() => GraphQLJWT.serialize(123)).toThrow(/Value is not string/);
expect(() => GraphQLJWT.serialize(`this is not a jwt`)).toThrow(/Value is not a valid JWT/);
expect(() => GraphQLJWT.serialize(`missing.signature`)).toThrow(/Value is not a valid JWT/);
expect(() => GraphQLJWT.serialize(`missing.`)).toThrow(/Value is not a valid JWT/);
expect(() => GraphQLJWT.serialize(`missing`)).toThrow(/Value is not a valid JWT/);
});
it(`parseValue`, () => {
expect(() => GraphQLJWT.parseValue(123)).toThrow(/Value is not string/);
expect(() => GraphQLJWT.parseValue(`this is not a JWT`)).toThrow(
/Value is not a valid JWT/,
);
expect(() => GraphQLJWT.parseValue(`missing.signature`)).toThrow(
/Value is not a valid JWT/,
);
expect(() => GraphQLJWT.parseValue(`missing.`)).toThrow(/Value is not a valid JWT/);
expect(() => GraphQLJWT.parseValue(`missing`)).toThrow(/Value is not a valid JWT/);
});
it(`parseLiteral`, () => {
expect(() => GraphQLJWT.parseLiteral({ value: 123, kind: Kind.INT } as any, {})).toThrow(
/Can only validate strings as JWT but got a/,
);
expect(() =>
GraphQLJWT.parseLiteral({ value: `this is not a JWT`, kind: Kind.STRING }, {}),
).toThrow(/Value is not a valid JWT/);
expect(() =>
GraphQLJWT.parseLiteral(
{
value: `missing.signature`,
kind: Kind.STRING,
},
{},
),
).toThrow(/Value is not a valid JWT/);
expect(() =>
GraphQLJWT.parseLiteral(
{
value: `missing.`,
kind: Kind.STRING,
},
{},
),
).toThrow(/Value is not a valid JWT/);
expect(() =>
GraphQLJWT.parseLiteral(
{
value: `missing`,
kind: Kind.STRING,
},
{},
),
).toThrow(/Value is not a valid JWT/);
});
});
});
});