This repository has been archived by the owner on Feb 10, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
AuthorizationUtils.test.ts
87 lines (62 loc) · 3.4 KB
/
AuthorizationUtils.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
// Copyright (c) 2023. Heusala Group Oy <info@hg.fi>. All rights reserved.
import { AuthorizationUtils } from "./AuthorizationUtils";
describe('AuthorizationUtils', () => {
describe('createBasicHeader', () => {
it('should create a Basic header from the given token', () => {
const token = 'test_token';
const expectedHeader = 'Basic test_token';
const header = AuthorizationUtils.createBasicHeader(token);
expect(header).toEqual(expectedHeader);
});
});
describe('createBasicHeaderWithUserAndPassword', () => {
it('should create a Basic header with Base64-encoded username and password', () => {
const username = 'test_user';
const password = 'test_password';
const expectedHeader = `Basic dGVzdF91c2VyOnRlc3RfcGFzc3dvcmQ=`;
const header = AuthorizationUtils.createBasicHeaderWithUserAndPassword(username, password);
expect(header).toEqual(expectedHeader);
});
it('should create a Basic header with Base64-encoded username and password with special characters', () => {
const username = 'test user %123$!^ääå,.-/|\&%€#"\'!`¨*;:_,.-<>§°@';
const password = 'test password %123$!^ääå,.-/|\&%€#"\'!`¨*;:_,.-<>§°@';
const expectedHeader = `Basic dGVzdCB1c2VyICUxMjMkIV7DpMOkw6UsLi0vfCYl4oKsIyInIWDCqCo7Ol8sLi08PsKnwrBAOnRlc3QgcGFzc3dvcmQgJTEyMyQhXsOkw6TDpSwuLS98JiXigqwjIichYMKoKjs6XywuLTw+wqfCsEA=`;
const header = AuthorizationUtils.createBasicHeaderWithUserAndPassword(username, password);
expect(header).toEqual(expectedHeader);
});
});
describe('parseBasicToken', () => {
it('should return the token from the Basic header', () => {
const token = 'dGVzdCB1c2VyICUxMjMkIV7k5OU6dGVzdCB1c2VyICUxMjMkIV7k5A==';
const header = `Basic ${token}`;
const parsedToken = AuthorizationUtils.parseBasicToken(header);
expect(parsedToken).toEqual(token);
});
it('should return undefined if the header does not start with "Basic "', () => {
const header = 'Bearer dGVzdCB1c2VyICUxMjMkIV7k5OU6dGVzdCB1c2VyICUxMjMkIV7k5A==';
const parsedToken = AuthorizationUtils.parseBasicToken(header);
expect(parsedToken).toBeUndefined();
});
});
describe('createBearerHeader', () => {
it('should create a Bearer header from the given token', () => {
const token = 'dGVzdCB1c2VyICUxMjMkIV7k5OU6dGVzdCB1c2VyICUxMjMkIV7k5A==';
const expectedHeader = 'Bearer dGVzdCB1c2VyICUxMjMkIV7k5OU6dGVzdCB1c2VyICUxMjMkIV7k5A==';
const header = AuthorizationUtils.createBearerHeader(token);
expect(header).toEqual(expectedHeader);
});
});
describe('parseBearerToken', () => {
it('should return the token from the Bearer header', () => {
const token = 'test_token';
const header = `Bearer ${token}`;
const parsedToken = AuthorizationUtils.parseBearerToken(header);
expect(parsedToken).toEqual(token);
});
it('should return undefined if the header does not start with "Bearer "', () => {
const header = 'Basic test_token';
const parsedToken = AuthorizationUtils.parseBearerToken(header);
expect(parsedToken).toBeUndefined();
});
});
});