forked from GitbookIO/gitbook
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix visitor auth logic by testing multiple basePaths (GitbookIO#239)
* WIP * add search params to target in multi-path * Simplify * Format * Ensure trailing slash * Add unit test * fix lint * use search from target in redirect * remove unused import --------- Co-authored-by: Samy Pessé <samypesse@gmail.com>
- Loading branch information
1 parent
1eb4bff
commit 0da4e9a
Showing
6 changed files
with
275 additions
and
10 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { it, describe, expect } from 'bun:test'; | ||
import { NextRequest } from 'next/server'; | ||
|
||
import { | ||
getVisitorAuthCookieName, | ||
getVisitorAuthCookieValue, | ||
getVisitorAuthToken, | ||
} from './visitor-auth'; | ||
|
||
describe('getVisitorAuthToken', () => { | ||
it('should return the token from the query parameters', () => { | ||
const request = nextRequest('https://example.com?jwt_token=123'); | ||
expect(getVisitorAuthToken(request, request.nextUrl)).toEqual('123'); | ||
}); | ||
|
||
it('should return the token from the cookie root basepath', () => { | ||
const request = nextRequest('https://example.com', { | ||
[getVisitorAuthCookieName('/')]: { value: getVisitorAuthCookieValue('/', '123') }, | ||
}); | ||
expect(getVisitorAuthToken(request, request.nextUrl)).toEqual('123'); | ||
}); | ||
|
||
it('should return the token from the cookie root basepath for a sub-path', () => { | ||
const request = nextRequest('https://example.com/hello/world', { | ||
[getVisitorAuthCookieName('/')]: { value: getVisitorAuthCookieValue('/', '123') }, | ||
}); | ||
expect(getVisitorAuthToken(request, request.nextUrl)).toEqual('123'); | ||
}); | ||
|
||
it('should return the closest token from the path', () => { | ||
const request = nextRequest('https://example.com/hello/world', { | ||
[getVisitorAuthCookieName('/')]: { value: getVisitorAuthCookieValue('/', 'no') }, | ||
[getVisitorAuthCookieName('/hello/')]: { | ||
value: getVisitorAuthCookieValue('/hello/', '123'), | ||
}, | ||
}); | ||
expect(getVisitorAuthToken(request, request.nextUrl)).toEqual('123'); | ||
}); | ||
|
||
it('should return the token from the cookie in a collection type url', () => { | ||
const request = nextRequest('https://example.com/hello/v/space1/cool', { | ||
[getVisitorAuthCookieName('/hello/v/space1/')]: { | ||
value: getVisitorAuthCookieValue('/hello/v/space1/', '123'), | ||
}, | ||
}); | ||
expect(getVisitorAuthToken(request, request.nextUrl)).toEqual('123'); | ||
}); | ||
|
||
it('should return undefined if no cookie and no query param', () => { | ||
const request = nextRequest('https://example.com'); | ||
expect(getVisitorAuthToken(request, request.nextUrl)).toBeUndefined(); | ||
}); | ||
}); | ||
|
||
function nextRequest(url: string, cookies: Record<string, { value: string }> = {}) { | ||
const nextUrl = new URL(url); | ||
// @ts-ignore | ||
return { | ||
url: nextUrl.toString(), | ||
nextUrl, | ||
headers: new Headers(), | ||
cookies: Object.entries(cookies), | ||
} as NextRequest; | ||
} |
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