Skip to content

Commit 8cff34e

Browse files
committed
added project deletion tests
1 parent ed44d75 commit 8cff34e

File tree

2 files changed

+129
-2
lines changed

2 files changed

+129
-2
lines changed

apps/backend/src/route-handlers/smart-request.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ async function parseAuth(req: NextRequest): Promise<SmartRequestAuth | null> {
231231

232232
const project = await getProject(projectId);
233233
if (!project) {
234-
throw new StackAssertionError("Project not found; this should never happen because having a project ID should guarantee a project");
234+
throw new StackAssertionError("Project not found; this should only happen if the project was deleted and the access token is still valid", { projectId });
235235
}
236236

237237
let user = null;

apps/e2e/tests/backend/endpoints/api/v1/projects.test.ts

Lines changed: 128 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { it } from "../../../../helpers";
22
import { Auth, InternalProjectKeys, Project, backendContext, niceBackendFetch } from "../../../backend-helpers";
33

44

5-
it("should not have have access to the project", async ({ expect }) => {
5+
it("should not have have access to the project without project keys", async ({ expect }) => {
66
backendContext.set({
77
projectKeys: 'no-project'
88
});
@@ -801,3 +801,130 @@ it("updates the project oauth configuration", async ({ expect }) => {
801801
}
802802
`);
803803
});
804+
805+
it("deletes a project with admin access", async ({ expect }) => {
806+
await Auth.Otp.signIn();
807+
const { adminAccessToken } = await Project.createAndGetAdminToken();
808+
809+
// Delete the project
810+
const deleteResponse = await niceBackendFetch(`/api/v1/projects/current`, {
811+
accessType: "admin",
812+
method: "DELETE",
813+
headers: {
814+
'x-stack-admin-access-token': adminAccessToken,
815+
}
816+
});
817+
818+
expect(deleteResponse).toMatchInlineSnapshot(`
819+
NiceResponse {
820+
"status": 200,
821+
"body": { "success": true },
822+
"headers": Headers { <some fields may have been hidden> },
823+
}
824+
`);
825+
});
826+
827+
it("deletes a project with server access", async ({ expect }) => {
828+
await Auth.Otp.signIn();
829+
const { adminAccessToken } = await Project.createAndGetAdminToken();
830+
831+
// Delete the project
832+
const deleteResponse = await niceBackendFetch(`/api/v1/projects/current`, {
833+
accessType: "server",
834+
method: "DELETE",
835+
headers: {
836+
'x-stack-admin-access-token': adminAccessToken,
837+
}
838+
});
839+
840+
expect(deleteResponse).toMatchInlineSnapshot(`
841+
NiceResponse {
842+
"status": 401,
843+
"body": {
844+
"code": "INSUFFICIENT_ACCESS_TYPE",
845+
"details": {
846+
"actual_access_type": "server",
847+
"allowed_access_types": ["admin"],
848+
},
849+
"error": "The x-stack-access-type header must be 'admin', but was 'server'.",
850+
},
851+
"headers": Headers {
852+
"x-stack-known-error": "INSUFFICIENT_ACCESS_TYPE",
853+
<some fields may have been hidden>,
854+
},
855+
}
856+
`);
857+
});
858+
859+
it("deletes a project with users, teams, and permissions", async ({ expect }) => {
860+
await Auth.Otp.signIn();
861+
const { adminAccessToken } = await Project.createAndGetAdminToken();
862+
863+
// Create a user
864+
const userResponse = await niceBackendFetch(`/api/v1/users`, {
865+
accessType: "server",
866+
method: "POST",
867+
headers: {
868+
'x-stack-admin-access-token': adminAccessToken,
869+
},
870+
body: {
871+
primary_email: "test@test.com",
872+
password: "testing",
873+
primary_email_auth_enabled: true,
874+
}
875+
});
876+
expect(userResponse.status).toBe(201);
877+
878+
// Create a team
879+
const teamResponse = await niceBackendFetch(`/api/v1/teams`, {
880+
accessType: "server",
881+
method: "POST",
882+
headers: {
883+
'x-stack-admin-access-token': adminAccessToken,
884+
},
885+
body: {
886+
display_name: "Test Team",
887+
}
888+
});
889+
expect(teamResponse.status).toBe(201);
890+
891+
// create a team permission
892+
const teamPermissionResponse = await niceBackendFetch(`/api/v1/team-permission-definitions`, {
893+
accessType: "admin",
894+
method: "POST",
895+
body: {
896+
id: 'p1'
897+
},
898+
headers: {
899+
'x-stack-admin-access-token': adminAccessToken
900+
},
901+
});
902+
expect(teamPermissionResponse.status).toBe(201);
903+
904+
// Delete the project
905+
const deleteResponse = await niceBackendFetch(`/api/v1/projects/current`, {
906+
accessType: "server",
907+
method: "DELETE",
908+
headers: {
909+
'x-stack-admin-access-token': adminAccessToken,
910+
}
911+
});
912+
913+
expect(deleteResponse).toMatchInlineSnapshot(`
914+
NiceResponse {
915+
"status": 401,
916+
"body": {
917+
"code": "INSUFFICIENT_ACCESS_TYPE",
918+
"details": {
919+
"actual_access_type": "server",
920+
"allowed_access_types": ["admin"],
921+
},
922+
"error": "The x-stack-access-type header must be 'admin', but was 'server'.",
923+
},
924+
"headers": Headers {
925+
"x-stack-known-error": "INSUFFICIENT_ACCESS_TYPE",
926+
<some fields may have been hidden>,
927+
},
928+
}
929+
`);
930+
});

0 commit comments

Comments
 (0)