-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(aws): Merge pull request #438 from boostercloud/enable-cors-in-th…
…e-auth-endpoints Enable CORS in the auth endpoints
- Loading branch information
Showing
4 changed files
with
217 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
190 changes: 190 additions & 0 deletions
190
...s/framework-integration-tests/integration/providers/aws/functionality/cors.integration.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
import { authClientID, createPassword, signInURL, signOutURL, signUpURL } from '../utils' | ||
import { internet } from 'faker' | ||
import fetch from 'cross-fetch' | ||
import { expect } from '@boostercloud/framework-provider-aws/test/expect' | ||
|
||
describe('Given the Authentication API', () => { | ||
let clientId: string | ||
const username = internet.email() | ||
const password = createPassword() | ||
const role = 'SuperUserNoConfirmation' | ||
before(async () => { | ||
clientId = await authClientID() | ||
}) | ||
|
||
context('When /auth/sign-up', () => { | ||
let signUpUrl: string | ||
let validAuthBody: string | ||
const invalidAuthBody = JSON.stringify({}) | ||
const methodsToCheck = ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS', 'PATCH'] | ||
const preflightOptions = generatePreflightOptionsList(methodsToCheck) | ||
|
||
before(async () => { | ||
signUpUrl = await signUpURL() | ||
validAuthBody = JSON.stringify({ | ||
clientId, | ||
username, | ||
password, | ||
userAttributes: { | ||
role, | ||
}, | ||
}) | ||
}) | ||
|
||
context('OPTIONS', () => { | ||
it('should allow all the headers and methods regardless the requests values', async () => { | ||
const responses = await Promise.all(preflightOptions.map(performPreflightRequest(signUpUrl))) | ||
|
||
responses.forEach(assertResponseContainsPreflightHeaders) | ||
}) | ||
}) | ||
|
||
context('POST', () => { | ||
it('should return the Access-Control-Allow-Origin header for 200 responses', async () => { | ||
const response = await fetch(signUpUrl, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'Application/json', | ||
}, | ||
body: validAuthBody, | ||
}) | ||
|
||
await verifyResponseAndAllowedOriginHeader(response, 200, '*', await response.json()) | ||
}) | ||
|
||
it('should return the Access-Control-Allow-Origin header for 400 responses', async () => { | ||
const response = await fetch(signUpUrl, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'Application/json', | ||
}, | ||
body: invalidAuthBody, | ||
}) | ||
|
||
await verifyResponseAndAllowedOriginHeader(response, 400, '*', await response.json()) | ||
}) | ||
|
||
it('should return the Access-Control-Allow-Origin header for 500 responses') | ||
}) | ||
|
||
context('And then /auth/sign-in', () => { | ||
let signInUrl: string | ||
let accessToken: string | ||
|
||
before(async () => { | ||
signInUrl = await signInURL() | ||
}) | ||
|
||
context('OPTIONS', () => { | ||
it('should allow all the headers and methods regardless the requests values', async () => { | ||
const responses = await Promise.all(preflightOptions.map(performPreflightRequest(signInUrl))) | ||
|
||
responses.forEach(assertResponseContainsPreflightHeaders) | ||
}) | ||
}) | ||
|
||
context('POST', () => { | ||
|
||
it('should return the Access-Control-Allow-Origin header for 200 responses', async () => { | ||
const response = await fetch(signInUrl, { | ||
method: 'POST', | ||
headers: { 'Content-Type': 'Application/json' }, | ||
body: validAuthBody, | ||
}) | ||
const jsonBody = await response.json() | ||
accessToken = jsonBody['accessToken'] | ||
|
||
await verifyResponseAndAllowedOriginHeader(response, 200, '*', jsonBody) | ||
}) | ||
it('should return the Access-Control-Allow-Origin header for 400 responses', async () => { | ||
const response = await fetch(signInUrl, { | ||
method: 'POST', | ||
headers: { 'Content-Type': 'Application/json' }, | ||
body: invalidAuthBody, | ||
}) | ||
|
||
await verifyResponseAndAllowedOriginHeader(response, 400, '*', await response.json()) | ||
}) | ||
it('should return the Access-Control-Allow-Origin header for 500 responses') | ||
}) | ||
|
||
context('And then /auth/sign-out', () => { | ||
let signOutUrl: string | ||
|
||
before(async () => { | ||
signOutUrl = await signOutURL() | ||
}) | ||
|
||
context('OPTIONS', () => { | ||
it('should allow all the headers and methods regardless the requests values', async () => { | ||
const responses = await Promise.all(preflightOptions.map(performPreflightRequest(signOutUrl))) | ||
|
||
responses.forEach(assertResponseContainsPreflightHeaders) | ||
}) | ||
}) | ||
|
||
context('POST', () => { | ||
it('should return the Access-Control-Allow-Origin header for 200 responses', async () => { | ||
const response = await fetch(signOutUrl, { | ||
method: 'POST', | ||
headers: { 'Content-Type': 'Application/json' }, | ||
body: JSON.stringify({ | ||
accessToken: accessToken, | ||
}), | ||
}) | ||
|
||
await verifyResponseAndAllowedOriginHeader(response, 200, '*', await response.json()) | ||
}) | ||
it('should return the Access-Control-Allow-Origin header for 400 responses', async () => { | ||
const response = await fetch(signOutUrl, { | ||
method: 'POST', | ||
headers: { 'Content-Type': 'Application/json' }, | ||
body: invalidAuthBody, | ||
}) | ||
|
||
await verifyResponseAndAllowedOriginHeader(response, 400, '*', await response.json()) | ||
}) | ||
it('should return the Access-Control-Allow-Origin header for 500 responses') | ||
}) | ||
}) | ||
}) | ||
|
||
function generatePreflightOptionsList(desiredHttpMethods: string[]): RequestInit[] { | ||
// For more info about preflight requests see: https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request | ||
return desiredHttpMethods.map( | ||
(method: string): RequestInit => ({ | ||
method: 'OPTIONS', | ||
headers: { | ||
'Access-Control-Request-Method': method, | ||
'Access-Control-Request-Headers': 'X-any-header', | ||
Origin: internet.url(), | ||
}, | ||
}) | ||
) | ||
} | ||
|
||
function performPreflightRequest(url: string) { | ||
return (options: RequestInit) => fetch(url, options) | ||
} | ||
|
||
function assertResponseContainsPreflightHeaders(response: Response): void { | ||
expect(response.status).to.be.eq(204) | ||
expect(response.headers.get('Access-Control-Allow-Origin')).to.be.eq('*') | ||
expect(response.headers.get('Access-Control-Allow-Headers')).to.be.eq('*') | ||
expect(response.headers.get('Access-Control-Allow-Methods')) | ||
.to.include('OPTIONS') | ||
.and.to.include('POST') | ||
} | ||
|
||
async function verifyResponseAndAllowedOriginHeader( | ||
response: Response, | ||
expectedHttpStatus: number, | ||
expectedAllowedOrigin: string, | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
jsonBody: any | ||
): Promise<void> { | ||
expect(response.status).to.be.eq(expectedHttpStatus, `Response body was: ${JSON.stringify(jsonBody)}`) | ||
expect(response.headers.get('Access-Control-Allow-Origin')).to.be.eq(expectedAllowedOrigin) | ||
} | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters