test: preserve active organization fixture session
This commit is contained in:
@@ -53,7 +53,7 @@ markVerifiedAndAdmin({
|
||||
adminId: databaseAdmin.id,
|
||||
});
|
||||
|
||||
const userSession = await signIn({ email: userEmail, password });
|
||||
let userSession = await signIn({ email: userEmail, password });
|
||||
state.API_TEST_SESSION_COOKIE = userSession.cookie;
|
||||
state.API_TOKEN = userSession.token ?? "";
|
||||
state.API_TEST_USER_ID = userSession.user.id;
|
||||
@@ -69,6 +69,14 @@ const organization = await ensureOrganization({
|
||||
slug: organizationSlug,
|
||||
});
|
||||
if (organization?.id) {
|
||||
userSession = {
|
||||
...userSession,
|
||||
cookie: await setActiveOrganization({
|
||||
cookie: userSession.cookie,
|
||||
organizationId: organization.id,
|
||||
}),
|
||||
};
|
||||
state.API_TEST_SESSION_COOKIE = userSession.cookie;
|
||||
state.API_TEST_ORGANIZATION_ID = organization.id;
|
||||
}
|
||||
|
||||
@@ -212,6 +220,22 @@ async function ensureOrganization({ cookie, name, slug }) {
|
||||
return null;
|
||||
}
|
||||
|
||||
async function setActiveOrganization({ cookie, organizationId }) {
|
||||
const response = await request("/organization/set-active", {
|
||||
method: "POST",
|
||||
cookie,
|
||||
body: { organizationId },
|
||||
});
|
||||
|
||||
if (response.status !== 200) {
|
||||
throw new Error(
|
||||
`Failed to set active organization ${organizationId}: ${response.status} ${JSON.stringify(response.body)}`,
|
||||
);
|
||||
}
|
||||
|
||||
return sessionCookieFromSetCookie(response.setCookie) || cookie;
|
||||
}
|
||||
|
||||
async function createApiKey({ cookie, runId }) {
|
||||
if (existingEnv.API_TEST_API_KEY && existingEnv.API_TEST_API_KEY_ID) {
|
||||
return { id: existingEnv.API_TEST_API_KEY_ID, key: existingEnv.API_TEST_API_KEY };
|
||||
@@ -261,10 +285,14 @@ function splitSetCookie(value) {
|
||||
}
|
||||
|
||||
function sessionCookieFromSetCookie(setCookieHeaders) {
|
||||
return setCookieHeaders
|
||||
const cookies = new Map();
|
||||
for (const entry of setCookieHeaders
|
||||
.map((entry) => entry.split(";")[0])
|
||||
.filter((entry) => /better-auth\.session|session_token|session/.test(entry))
|
||||
.join("; ");
|
||||
.filter((entry) => /better-auth\.session|session_token|session/.test(entry))) {
|
||||
const index = entry.indexOf("=");
|
||||
if (index !== -1) cookies.set(entry.slice(0, index), entry);
|
||||
}
|
||||
return [...cookies.values()].join("; ");
|
||||
}
|
||||
|
||||
function markVerifiedAndAdmin({ userId, adminId }) {
|
||||
|
||||
Reference in New Issue
Block a user