mirror of
https://github.com/stack-auth/stack.git
synced 2026-06-13 21:01:21 +08:00
* added basic api testing framework * added credential signup test * added current user test * added github action * fixed bugs in action file * updated action * added pnpm setup * added dependency install * updated pnpm lock * only run server tests * added new package for e2e test * removed unused tests * updated action * updated test command * added env var reading * fixed typo * fixed typo * fixed unit tests with staging * added delay e2e test * added start server to action * fixed typo * fix aciton * updated github action * fixed bugs * fixed eslint error
74 lines
2.2 KiB
TypeScript
74 lines
2.2 KiB
TypeScript
import { describe, expect, test } from "vitest";
|
|
import request from "supertest";
|
|
import { BASE_URL, PROJECT_CLIENT_KEY, PROJECT_ID } from "./helpers";
|
|
|
|
const AUTH_HEADER = {
|
|
"x-stack-project-id": PROJECT_ID,
|
|
"x-stack-publishable-client-key": PROJECT_CLIENT_KEY,
|
|
};
|
|
|
|
const JSON_HEADER = {
|
|
"content-type": "application/json"
|
|
}
|
|
|
|
function randomString() {
|
|
return Math.random().toString(36);
|
|
}
|
|
|
|
async function signUpWithEmailPassword() {
|
|
const email = randomString() + "@example.com";
|
|
const password = randomString();
|
|
const response = await request(BASE_URL).post("/api/v1/auth/signup").set(AUTH_HEADER).set(JSON_HEADER).send({
|
|
email,
|
|
password,
|
|
emailVerificationRedirectUrl: 'https://localhost:3000/verify-email',
|
|
});
|
|
|
|
return { email, password, response };
|
|
}
|
|
|
|
async function signInWithEmailPassword(email: string, password: string) {
|
|
const response = await request(BASE_URL).post("/api/v1/auth/signin").set(AUTH_HEADER).set(JSON_HEADER).send({
|
|
email,
|
|
password,
|
|
});
|
|
|
|
return { email, password, response };
|
|
}
|
|
|
|
describe("Basic", () => {
|
|
test("Main Page", async () => {
|
|
const response = await request(BASE_URL).get("/");
|
|
expect(response.status).toBe(307);
|
|
});
|
|
|
|
test("Test API", async () => {
|
|
const response = await request(BASE_URL).get("/api/v1");
|
|
expect(response.status).toBe(200);
|
|
expect(response.text).contains("Stack API")
|
|
});
|
|
|
|
test("Credential Sign Up", async () => {
|
|
const { response } = await signUpWithEmailPassword();
|
|
expect(response.status).toBe(200);
|
|
});
|
|
|
|
test("Credential Sign In", async () => {
|
|
const { email, password } = await signUpWithEmailPassword();
|
|
const { response } = await signInWithEmailPassword(email, password);
|
|
|
|
expect(response.status).toBe(200);
|
|
});
|
|
|
|
test("Get Current User", async () => {
|
|
const { email, password, response } = await signUpWithEmailPassword();
|
|
await signInWithEmailPassword(email, password);
|
|
|
|
const response2 = await request(BASE_URL).get("/api/v1/current-user").set({
|
|
...AUTH_HEADER,
|
|
'authorization': 'StackSession ' + response.body.accessToken,
|
|
});
|
|
expect(response2.status).toBe(200);
|
|
expect(response2.body.primaryEmail).toBe(email)
|
|
});
|
|
}); |