Skip to content

Commit ce15b4e

Browse files
committed
test: authorization in request panel
1 parent 130e190 commit ce15b4e

File tree

4 files changed

+247
-1
lines changed

4 files changed

+247
-1
lines changed

demo/models/test-api/test-api.raml

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,64 @@ version: v2
44
description: Our test API
55
baseUri: https://example/
66

7+
securitySchemes:
8+
customScheme:
9+
description: |
10+
A custom security scheme for authenticating requests.
11+
type: x-custom
12+
describedBy:
13+
headers:
14+
SpecialToken:
15+
description: |
16+
Used to send a custom token.
17+
type: string
18+
example: special-token
19+
responses:
20+
401:
21+
description: |
22+
Bad token.
23+
403:
24+
oauth_1_0:
25+
description: |
26+
OAuth 1.0 continues to be supported for all API requests, but OAuth 2.0 is now preferred.
27+
type: OAuth 1.0
28+
settings:
29+
requestTokenUri: https://api.mysampleapi.com/1/oauth/request_token
30+
authorizationUri: https://api.mysampleapi.com/1/oauth/authorize
31+
tokenCredentialsUri: https://api.mysampleapi.com/1/oauth/access_token
32+
signatures: [ 'HMAC-SHA1', 'PLAINTEXT' ]
33+
oauth_2_0:
34+
description: |
35+
Dropbox supports OAuth 2.0 for authenticating all API requests.
36+
type: OAuth 2.0
37+
describedBy:
38+
headers:
39+
Authorization:
40+
description: |
41+
Used to send a valid OAuth 2 access token. Do not use
42+
with the "access_token" query string parameter.
43+
type: string
44+
queryParameters:
45+
access_token:
46+
description: |
47+
Used to send a valid OAuth 2 access token. Do not use with
48+
the "Authorization" header.
49+
type: string
50+
responses:
51+
401:
52+
description: |
53+
Bad or expired token. This can happen if the user or Dropbox
54+
revoked or expired an access token. To fix, re-authenticate
55+
the user.
56+
403:
57+
description: |
58+
Bad OAuth request (wrong consumer key, bad nonce, expired
59+
timestamp...). Unfortunately, re-authenticating the user won't help here.
60+
settings:
61+
authorizationUri: https://www.dropbox.com/1/oauth2/authorize
62+
accessTokenUri: https://api.dropbox.com/1/oauth2/token
63+
authorizationGrants: [ authorization_code, implicit, 'urn:ietf:params:oauth:grant-type:saml2-bearer' ]
64+
765
/test-headers:
866
displayName: Headers
967
post:
@@ -13,3 +71,16 @@ baseUri: https://example/
1371
example: only-if-cached
1472
body:
1573
application/json:
74+
/test-custom-scheme:
75+
displayName: Custom security scheme
76+
get:
77+
securedBy: [customScheme]
78+
/test-oauth10-scheme:
79+
displayName: Oauth 1.0 security scheme
80+
get:
81+
securedBy: [oauth_1_0]
82+
/test-oauth20-scheme:
83+
displayName: Oauth 2.0 security scheme
84+
get:
85+
securedBy: [oauth_2_0]
86+

test/api-console-request.test.js

Lines changed: 170 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import '../api-console.js';
55
import {
66
documentationTryItButton,
77
navigationSelectEndpointMethod,
8-
requestBodySection,
8+
requestBodySection, requestCredentialsSection,
99
requestHeadersSection,
1010
requestQueryParamSection, requestSendButton,
1111
requestUrlSection
@@ -133,6 +133,175 @@ describe('API Console request', () => {
133133
assert.exists(body.shadowRoot.querySelector('raw-payload-editor'));
134134
});
135135
});
136+
137+
describe('Authorization', () => {
138+
const assertDropdownMenu = (form, name, menuLabel, value) => {
139+
const menu = form.querySelector(`anypoint-dropdown-menu[name="${name}"]`);
140+
assert.exists(menu);
141+
assert.exists(menu.shadowRoot.querySelector('.label').innerText, menuLabel);
142+
assert.exists(menu.shadowRoot.querySelector('.input-wrapper').innerText, value);
143+
}
144+
145+
const assertMaskedInput = (form, name, inputLabel) => {
146+
const input = form.querySelector(`anypoint-masked-input[name="${name}"]`);
147+
assert.exists(input);
148+
assert.exists(input.querySelector('label').innerText, inputLabel);
149+
}
150+
151+
const assertInput = (form, name, inputLabel) => {
152+
const input = form.querySelector(`anypoint-input[name="${name}"]`);
153+
assert.exists(input);
154+
assert.exists(input.querySelector('label').innerText, inputLabel);
155+
}
156+
157+
describe('x-other', () => {
158+
let credentialsSection
159+
160+
beforeEach(async () => {
161+
await navigationSelectEndpointMethod(element, '/test-custom-scheme', 'get');
162+
await aTimeout(50)
163+
documentationTryItButton(element).click()
164+
await aTimeout(50)
165+
credentialsSection = requestCredentialsSection(element);
166+
});
167+
168+
it(`should render credentials section`, async () => {
169+
assert.exists(credentialsSection);
170+
});
171+
172+
it(`should render auth label`, async () => {
173+
assert.equal(credentialsSection.shadowRoot.querySelector('.auth-selector-label').innerText, 'x-custom');
174+
});
175+
176+
it(`should render authorization method`, async () => {
177+
const authorizationMethod = credentialsSection.shadowRoot.querySelector('api-authorization-method');
178+
assert.equal(authorizationMethod.getAttribute('type'), 'custom');
179+
180+
const authorizationMethodTitle = authorizationMethod.shadowRoot.querySelector('.subtitle');
181+
assert.equal(authorizationMethodTitle.querySelector('span').innerText, 'Scheme: customScheme');
182+
assert.exists(authorizationMethodTitle.querySelector('.hint-icon'));
183+
});
184+
185+
it(`should render scheme fields`, async () => {
186+
const authorizationMethod = credentialsSection.shadowRoot.querySelector('api-authorization-method');
187+
const authorizationMethodForm = authorizationMethod.shadowRoot.querySelector('form');
188+
assert.equal(authorizationMethodForm.querySelector('.section-title').innerText, 'Headers');
189+
190+
const fields = authorizationMethodForm.querySelectorAll('.field-value');
191+
assert.lengthOf(fields, 1);
192+
const formItem = fields[0].querySelector('api-form-item');
193+
const input = formItem.shadowRoot.querySelector('anypoint-input');
194+
assert.equal(input.querySelector('label').innerText, 'SpecialToken*');
195+
assert.exists(fields[0].querySelector('.hint-icon'));
196+
});
197+
198+
describe('Request with credentials', () => {
199+
beforeEach(async () => {
200+
spy = sinon.spy();
201+
document.body.addEventListener('api-request', spy);
202+
});
203+
204+
it(`should add all credential headers to request`, async () => {
205+
requestSendButton(element).click();
206+
await nextFrame();
207+
208+
assert.isTrue(spy.called);
209+
assert.equal(spy.getCall(0).args[0].detail.headers, 'SpecialToken: special-token');
210+
});
211+
});
212+
})
213+
214+
describe('Oauth 1.0', () => {
215+
let credentialsSection
216+
217+
beforeEach(async () => {
218+
await navigationSelectEndpointMethod(element, '/test-oauth10-scheme', 'get');
219+
await aTimeout(50)
220+
documentationTryItButton(element).click()
221+
await aTimeout(50)
222+
credentialsSection = requestCredentialsSection(element);
223+
});
224+
225+
it(`should render credentials section`, async () => {
226+
assert.exists(credentialsSection);
227+
});
228+
229+
it(`should render auth label`, async () => {
230+
assert.equal(credentialsSection.shadowRoot.querySelector('.auth-selector-label').innerText, 'OAuth 1.0');
231+
});
232+
233+
it(`should render authorization method`, async () => {
234+
const authorizationMethod = credentialsSection.shadowRoot.querySelector('api-authorization-method');
235+
assert.equal(authorizationMethod.getAttribute('type'), 'oauth 1');
236+
});
237+
238+
it(`should render scheme fields`, async () => {
239+
const authorizationMethod = credentialsSection.shadowRoot.querySelector('api-authorization-method');
240+
const authorizationMethodForm = authorizationMethod.shadowRoot.querySelector('form');
241+
242+
assertDropdownMenu(authorizationMethodForm, 'authTokenMethod', 'Authorization token method', 'POST')
243+
assertDropdownMenu(authorizationMethodForm, 'authParamsLocation', 'Oauth parameters location', 'Authorization header')
244+
assertDropdownMenu(authorizationMethodForm, 'signatureMethod', 'Signature method', 'HMAC-SHA1')
245+
246+
assertMaskedInput(authorizationMethodForm, 'consumerKey', 'Consumer key')
247+
assertMaskedInput(authorizationMethodForm, 'consumerSecret', 'Consumer secret')
248+
assertMaskedInput(authorizationMethodForm, 'token', 'Token')
249+
assertMaskedInput(authorizationMethodForm, 'tokenSecret', 'Token secret')
250+
assertMaskedInput(authorizationMethodForm, 'realm', 'Realm')
251+
252+
assertInput(authorizationMethodForm, 'requestTokenUri', 'Request token URI')
253+
assertInput(authorizationMethodForm, 'accessTokenUri', 'Token Authorization URI')
254+
assertInput(authorizationMethodForm, 'authorizationUri', 'User authorization dialog URI')
255+
assertInput(authorizationMethodForm, 'redirectUri', 'Redirect URI')
256+
assertInput(authorizationMethodForm, 'timestamp', 'Timestamp')
257+
assertInput(authorizationMethodForm, 'nonce', 'Nonce')
258+
259+
assert.exists(authorizationMethod.shadowRoot.querySelector('.auth-button'));
260+
});
261+
})
262+
263+
describe('Oauth 2.0', () => {
264+
let credentialsSection
265+
266+
beforeEach(async () => {
267+
await navigationSelectEndpointMethod(element, '/test-oauth20-scheme', 'get');
268+
await aTimeout(50)
269+
documentationTryItButton(element).click()
270+
await aTimeout(50)
271+
credentialsSection = requestCredentialsSection(element);
272+
});
273+
274+
it(`should render credentials section`, async () => {
275+
assert.exists(credentialsSection);
276+
});
277+
278+
it(`should render auth label`, async () => {
279+
assert.equal(credentialsSection.shadowRoot.querySelector('.auth-selector-label').innerText, 'OAuth 2.0');
280+
});
281+
282+
it(`should render authorization method`, async () => {
283+
const authorizationMethod = credentialsSection.shadowRoot.querySelector('api-authorization-method');
284+
assert.equal(authorizationMethod.getAttribute('type'), 'oauth 2');
285+
});
286+
287+
it(`should render scheme fields`, async () => {
288+
const authorizationMethod = credentialsSection.shadowRoot.querySelector('api-authorization-method');
289+
const authorizationMethodForm = authorizationMethod.shadowRoot.querySelector('form');
290+
291+
assertDropdownMenu(authorizationMethodForm, 'grantType', 'Response type', 'Access token')
292+
assertMaskedInput(authorizationMethodForm, 'clientId', 'Client id')
293+
assertInput(authorizationMethodForm, 'authorizationUri', 'Authorization URI')
294+
295+
const scopes = authorizationMethod.shadowRoot.querySelector('oauth2-scope-selector');
296+
assert.exists(scopes);
297+
assert.equal(scopes.shadowRoot.querySelector('.form-label').innerText, 'Scopes');
298+
assert.exists(scopes.shadowRoot.querySelector('.scope-input'));
299+
300+
assert.exists(authorizationMethod.shadowRoot.querySelector('.redirect-section span').innerText, 'https://auth.advancedrestclient.com/oauth-popup.html');
301+
assert.exists(authorizationMethod.shadowRoot.querySelector('.auth-button'));
302+
});
303+
})
304+
});
136305
});
137306
});
138307
});

test/testHelper.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,5 @@ export declare function requestUrlSection(element: ApiConsole): Element|null;
3636
export declare function requestQueryParamSection(element: ApiConsole): Element|null;
3737
export declare function requestHeadersSection(element: ApiConsole): Element|null;
3838
export declare function requestBodySection(element: ApiConsole): Element|null;
39+
export declare function requestCredentialsSection(element: ApiConsole): Element|null;
3940
export declare function requestSendButton(element: ApiConsole): Element|null;

test/testHelper.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@ export const requestBodySection = (element) => {
133133
return editor.shadowRoot.querySelector('api-body-editor');
134134
}
135135

136+
export const requestCredentialsSection = (element) => {
137+
const editor = requestEditor(element);
138+
return editor.shadowRoot.querySelector('api-authorization');
139+
}
140+
136141
export const requestSendButton = (element) => {
137142
const editor = requestEditor(element);
138143
return editor.shadowRoot.querySelector('.send-button');

0 commit comments

Comments
 (0)