mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-05-15 22:01:59 -04:00
test: minor fix to how cookies are passed around (#55259)
This commit is contained in:
committed by
GitHub
parent
dda9c929b0
commit
fd1bf0dd5a
@@ -1,4 +1,4 @@
|
||||
import { getCsrfToken } from './jest.utils';
|
||||
import { getCsrfToken, getCookies } from './jest.utils';
|
||||
|
||||
const fakeCookies = [
|
||||
'_csrf=123; Path=/; HttpOnly; SameSite=Strict',
|
||||
@@ -17,3 +17,18 @@ describe('getCsrfToken', () => {
|
||||
).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('setCookiesToCookies', () => {
|
||||
test('returns a string of cookies', () => {
|
||||
expect(getCookies(fakeCookies)).toEqual(
|
||||
'_csrf=123; csrf_token=abc-123; sessionId=CV-abc.123'
|
||||
);
|
||||
});
|
||||
test('handles bare cookies', () => {
|
||||
expect(getCookies(['_csrf=123'])).toEqual('_csrf=123');
|
||||
});
|
||||
|
||||
test('throws an error if the cookies are malformed', () => {
|
||||
expect(() => getCookies(['_csrf'])).toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -34,6 +34,13 @@ export const getCsrfToken = (setCookies: string[]): string | undefined => {
|
||||
|
||||
export const ORIGIN = 'https://www.freecodecamp.org';
|
||||
|
||||
export const getCookies = (setCookies: string[]): string => {
|
||||
for (const cookie of setCookies) {
|
||||
expect(cookie).toMatch(/.*=.*/);
|
||||
}
|
||||
return setCookies.map(cookie => cookie.split(';')[0]).join('; ');
|
||||
};
|
||||
|
||||
export function superRequest(
|
||||
resource: string,
|
||||
config: {
|
||||
@@ -48,7 +55,7 @@ export function superRequest(
|
||||
const req = requests[method](resource).set('Origin', ORIGIN);
|
||||
|
||||
if (setCookies) {
|
||||
void req.set('Cookie', setCookies);
|
||||
void req.set('Cookie', getCookies(setCookies));
|
||||
}
|
||||
|
||||
const csrfToken = (setCookies && getCsrfToken(setCookies)) ?? '';
|
||||
|
||||
Reference in New Issue
Block a user