Disable deployment SSO protection for tests (#10799)

Our tests rely on not being behind deployment protection which is now enabled by default so this adds a step to tests to disable this setting to allow tests to continue working as expected.
This commit is contained in:
JJ Kasper
2023-11-06 21:04:29 -08:00
committed by GitHub
parent 644b8a52cb
commit 7b553c7032
8 changed files with 162 additions and 16 deletions

View File

@@ -100,9 +100,73 @@ async function nowDeploy(projectName, bodies, randomness, uploadNowJson, opts) {
await new Promise(r => setTimeout(r, 1000));
}
await disableSSO(deploymentId);
return { deploymentId, deploymentUrl };
}
async function disableSSO(deploymentId, useTeam = true) {
if (deploymentId.startsWith('https://')) {
deploymentId = new URL(deploymentId).hostname;
}
const deployRes = await fetchWithAuth(
`https://vercel.com/api/v13/deployments/${encodeURIComponent(
deploymentId
)}`,
{
method: 'GET',
}
);
if (!deployRes.ok) {
throw new Error(
`Failed to get deployment info (status: ${
deployRes.status
}, body: ${await deployRes.text()})`
);
}
const deploymentInfo = await deployRes.json();
const { projectId, url: deploymentUrl } = deploymentInfo;
const settingRes = await fetchWithAuth(
`https://vercel.com/api/v5/projects/${encodeURIComponent(projectId)}`,
{
method: 'PATCH',
headers: {
'content-type': 'application/json',
},
body: JSON.stringify({
ssoProtection: null,
}),
...(useTeam
? {}
: {
skipTeam: true,
}),
}
);
if (settingRes.ok) {
for (let i = 0; i < 5; i++) {
const res = await fetch(`https://${deploymentUrl}`);
if (res.status !== 401) {
break;
}
await new Promise(resolve => setTimeout(resolve, 5 * 1000));
}
console.log(
`Disabled deployment protection for deploymentId: ${deploymentId} projectId: ${projectId}`
);
} else {
console.error(settingRes.status, await settingRes.text(), deploymentInfo);
throw new Error(
`Failed to disable deployment protection projectId: ${projectId} deploymentId ${deploymentId}`
);
}
}
function digestOfFile(body) {
return createHash('sha1').update(body).digest('hex');
}
@@ -183,18 +247,24 @@ async function fetchWithAuth(url, opts = {}) {
opts.headers.Authorization = `Bearer ${await fetchCachedToken()}`;
}
const { VERCEL_TEAM_ID } = process.env;
if (opts.skipTeam) {
delete opts.skipTeam;
} else {
const { VERCEL_TEAM_ID } = process.env;
if (VERCEL_TEAM_ID) {
url += `${url.includes('?') ? '&' : '?'}teamId=${VERCEL_TEAM_ID}`;
if (VERCEL_TEAM_ID) {
url += `${url.includes('?') ? '&' : '?'}teamId=${VERCEL_TEAM_ID}`;
}
}
return await fetchApi(url, opts);
}
/**
* @returns { Promise<String> }
*/
async function fetchCachedToken() {
if (!token || tokenCreated < Date.now() - MAX_TOKEN_AGE) {
tokenCreated = Date.now();
token = await fetchTokenWithRetry();
return fetchTokenWithRetry();
}
return token;
}
@@ -246,6 +316,11 @@ async function fetchTokenWithRetry(retries = 5) {
const text = JSON.stringify(data);
throw new Error(`Unexpected response from registration: ${text}`);
}
// Cache the token to be returned via `fetchCachedToken`
token = data.token;
tokenCreated = Date.now();
return data.token;
} catch (error) {
logWithinTest(
@@ -262,9 +337,11 @@ async function fetchTokenWithRetry(retries = 5) {
}
async function fetchApi(url, opts = {}) {
const apiHost = process.env.API_HOST || 'api.vercel.com';
const urlWithHost = `https://${apiHost}${url}`;
const { method = 'GET', body } = opts;
const apiHost = process.env.API_HOST || 'api.vercel.com';
const urlWithHost = url.startsWith('https://')
? url
: `https://${apiHost}${url}`;
if (process.env.VERBOSE) {
logWithinTest('fetch', method, url);
@@ -290,7 +367,8 @@ module.exports = {
fetchApi,
fetchWithAuth,
nowDeploy,
fetchTokenWithRetry,
fetchCachedToken,
fetchTokenWithRetry,
fileModeSymbol,
disableSSO,
};