11import { beforeEach , describe , expect , it , vi } from "vitest" ;
22import { NextRequest } from "next/server" ;
33
4- function request ( fields : Record < string , string > ) {
4+ function request ( fields : Record < string , string > , url ?: string ) {
55 const body = new URLSearchParams ( fields ) ;
6- return new NextRequest ( "http://localhost:3000/api/team/switch" , {
6+ return new NextRequest ( url ?? "http://localhost:3000/api/team/switch" , {
77 method : "POST" ,
88 headers : { "content-type" : "application/x-www-form-urlencoded" } ,
99 body,
@@ -13,6 +13,7 @@ function request(fields: Record<string, string>) {
1313describe ( "team switch" , ( ) => {
1414 beforeEach ( ( ) => {
1515 vi . unstubAllEnvs ( ) ;
16+ vi . resetModules ( ) ;
1617 } ) ;
1718
1819 it ( "sets the selected team and redirects within the application" , async ( ) => {
@@ -21,6 +22,7 @@ describe("team switch", () => {
2122 request ( { teamId : "team_123" , redirectTo : "/contacts?from=team" } ) ,
2223 ) ;
2324
25+ expect ( response . status ) . toBe ( 303 ) ;
2426 expect ( response . headers . get ( "location" ) ) . toBe (
2527 "http://localhost:3000/contacts?from=team" ,
2628 ) ;
@@ -31,6 +33,22 @@ describe("team switch", () => {
3133 expect ( response . headers . get ( "set-cookie" ) ) . not . toContain ( "HttpOnly" ) ;
3234 } ) ;
3335
36+ it ( "redirects using WEB_CLIENT even when req.url is the container bind address" , async ( ) => {
37+ vi . stubEnv ( "WEB_CLIENT" , "https://app.sendlit.clqa.site" ) ;
38+ vi . resetModules ( ) ;
39+ const { POST } = await import ( "./route" ) ;
40+ const response = await POST (
41+ request (
42+ { teamId : "team_123" , redirectTo : "/teams" } ,
43+ "http://0.0.0.0:3000/api/team/switch" ,
44+ ) ,
45+ ) ;
46+
47+ expect ( response . headers . get ( "location" ) ) . toBe (
48+ "https://app.sendlit.clqa.site/teams" ,
49+ ) ;
50+ } ) ;
51+
3452 it . each ( [ "https://attacker.example/steal" , "//attacker.example/steal" ] ) (
3553 "rejects an external redirect target: %s" ,
3654 async ( redirectTo ) => {
0 commit comments