-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathUmaTokenExtractor.test.ts
More file actions
131 lines (113 loc) · 4.39 KB
/
UmaTokenExtractor.test.ts
File metadata and controls
131 lines (113 loc) · 4.39 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { AccessMode, BadRequestHttpError, HttpRequest, NotImplementedHttpError } from '@solid/community-server';
import { UmaToken } from '../uma/UmaClient';
import { CredentialGroup } from './Credentials';
import { UmaTokenExtractor } from './UmaTokenExtractor';
const MockUmaClient = {
getAsUrl: jest.fn(),
verifyToken: jest.fn(),
fetchUMAConfig: jest.fn(),
fetchPermissionTicket: jest.fn(),
};
const WEB_ID = 'https://example.org/alice';
const CLIENT = 'https://app.example.org';
const RESOURCE = 'https://pod.example.org/123';
const MODES = ['http://www.w3.org/ns/auth/acl#Read'];
const mockUmaToken: UmaToken = {webid: WEB_ID, azp: CLIENT,
resource: RESOURCE, modes: MODES};
describe('A UMATicketExtractor', () => {
const ticketExtractor = new UmaTokenExtractor({umaClient: MockUmaClient});
afterEach((): void => {
jest.clearAllMocks();
});
describe('on a request without Authorization header', () => {
const request = {
method: 'GET',
headers: { },
} as any as HttpRequest;
it('throws an error', async () =>{
const result = ticketExtractor.handleSafe(request);
await expect(result).rejects.toThrow(NotImplementedHttpError);
await expect(result).rejects.toThrow('No Bearer Authorization header specified.');
});
});
describe('on a request without Bearer Authorization header', () => {
const request = {
method: 'GET',
headers: {'authorization': 'Token 123'},
} as any as HttpRequest;
it('throws an error', async () =>{
const result = ticketExtractor.handleSafe(request);
await expect(result).rejects.toThrow(NotImplementedHttpError);
await expect(result).rejects.toThrow('No Bearer Authorization header specified.');
});
});
describe('on a request with Bearer Authorization header', () => {
const request = {
method: 'GET',
headers: {'authorization': 'Bearer 123'},
} as any as HttpRequest;
beforeEach(() => {
MockUmaClient.verifyToken.mockResolvedValueOnce(mockUmaToken);
});
it('calls the verifier with correct parameters', async () =>{
await ticketExtractor.handleSafe(request);
expect(MockUmaClient.verifyToken).toHaveBeenCalledTimes(1);
expect(MockUmaClient.verifyToken).toHaveBeenCalledWith('123');
});
it('returns a CredentialSet with the ticket', async () =>{
const result = ticketExtractor.handleSafe(request);
await expect(result).resolves.toEqual({[CredentialGroup.ticket]: {webId: WEB_ID,
resource: {path: RESOURCE},
modes: new Set(['read'])}});
});
});
describe('on a request with Bearer Authorization header and unsupported mode', () => {
const request = {
method: 'GET',
headers: {'authorization': 'Bearer 123'},
} as any as HttpRequest;
beforeEach(() => {
MockUmaClient.verifyToken.mockResolvedValueOnce({...mockUmaToken, modes: ['abc']});
});
it('throws an error.', async () =>{
const result = ticketExtractor.handleSafe(request);
await expect(result).rejects.toThrow(BadRequestHttpError);
await expect(result).rejects.toThrow('Error verifying WebID via Bearer access token: ' +
'Unknown ACL Mode \'abc\' in token.');
});
});
describe('on a request with Authorization and a lowercase Bearer token', (): void => {
const request = {
method: 'GET',
headers: {
authorization: 'bearer 123',
},
} as any as HttpRequest;
beforeEach(() => {
MockUmaClient.verifyToken.mockResolvedValueOnce(mockUmaToken);
});
it('calls the verifier with correct parameters', async () =>{
await ticketExtractor.handleSafe(request);
expect(MockUmaClient.verifyToken).toHaveBeenCalledTimes(1);
expect(MockUmaClient.verifyToken).toHaveBeenCalledWith('123');
});
});
describe('when verification throws an error', (): void => {
const request = {
method: 'GET',
headers: {
authorization: 'Bearer token-1234',
},
} as any as HttpRequest;
beforeEach((): void => {
MockUmaClient.verifyToken.mockImplementationOnce((): void => {
throw new Error('invalid');
});
});
it('throws an error.', async (): Promise<void> => {
const result = ticketExtractor.handleSafe(request);
await expect(result).rejects.toThrow(BadRequestHttpError);
await expect(result).rejects.toThrow('Error verifying WebID via Bearer access token: invalid');
});
});
});