2023-12-14 12:50:51 +03:00
|
|
|
import type { INestApplication } from '@nestjs/common';
|
2024-03-12 13:00:09 +03:00
|
|
|
import { PrismaClient } from '@prisma/client';
|
2024-03-26 05:24:17 +03:00
|
|
|
import request, { type Response } from 'supertest';
|
2023-12-14 12:50:51 +03:00
|
|
|
|
2024-03-26 05:24:17 +03:00
|
|
|
import type { ClientTokenType, CurrentUser } from '../../src/core/auth';
|
2024-03-12 13:00:09 +03:00
|
|
|
import type { UserType } from '../../src/core/user';
|
2023-12-14 12:50:51 +03:00
|
|
|
import { gql } from './common';
|
|
|
|
|
2024-03-26 05:24:17 +03:00
|
|
|
export function sessionCookie(headers: any) {
|
|
|
|
const cookie = headers['set-cookie']?.find((c: string) =>
|
|
|
|
c.startsWith('sid=')
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!cookie) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return cookie.split(';')[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getSession(
|
|
|
|
app: INestApplication,
|
|
|
|
signInRes: Response
|
|
|
|
): Promise<{ user?: CurrentUser }> {
|
|
|
|
const cookie = sessionCookie(signInRes.headers);
|
|
|
|
const res = await request(app.getHttpServer())
|
|
|
|
.get('/api/auth/session')
|
|
|
|
.set('cookie', cookie)
|
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
return res.body;
|
|
|
|
}
|
|
|
|
|
2023-12-14 12:50:51 +03:00
|
|
|
export async function signUp(
|
|
|
|
app: INestApplication,
|
|
|
|
name: string,
|
|
|
|
email: string,
|
2024-03-12 13:00:09 +03:00
|
|
|
password: string,
|
|
|
|
autoVerifyEmail = true
|
|
|
|
): Promise<UserType & { token: ClientTokenType }> {
|
2023-12-14 12:50:51 +03:00
|
|
|
const res = await request(app.getHttpServer())
|
|
|
|
.post(gql)
|
|
|
|
.set({ 'x-request-id': 'test', 'x-operation-name': 'test' })
|
|
|
|
.send({
|
|
|
|
query: `
|
|
|
|
mutation {
|
|
|
|
signUp(name: "${name}", email: "${email}", password: "${password}") {
|
|
|
|
id, name, email, token { token }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
})
|
|
|
|
.expect(200);
|
2024-03-12 13:00:09 +03:00
|
|
|
|
|
|
|
if (autoVerifyEmail) {
|
|
|
|
await setEmailVerified(app, email);
|
|
|
|
}
|
|
|
|
|
2023-12-14 12:50:51 +03:00
|
|
|
return res.body.data.signUp;
|
|
|
|
}
|
|
|
|
|
2024-03-12 13:00:09 +03:00
|
|
|
async function setEmailVerified(app: INestApplication, email: string) {
|
|
|
|
await app.get(PrismaClient).user.update({
|
|
|
|
where: { email },
|
|
|
|
data: {
|
|
|
|
emailVerifiedAt: new Date(),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-12-14 12:50:51 +03:00
|
|
|
export async function currentUser(app: INestApplication, token: string) {
|
|
|
|
const res = await request(app.getHttpServer())
|
|
|
|
.post(gql)
|
|
|
|
.auth(token, { type: 'bearer' })
|
|
|
|
.set({ 'x-request-id': 'test', 'x-operation-name': 'test' })
|
|
|
|
.send({
|
|
|
|
query: `
|
|
|
|
query {
|
|
|
|
currentUser {
|
2024-03-12 13:00:09 +03:00
|
|
|
id, name, email, emailVerified, avatarUrl, hasPassword,
|
2023-12-14 12:50:51 +03:00
|
|
|
token { token }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
})
|
|
|
|
.expect(200);
|
|
|
|
return res.body.data.currentUser;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function sendChangeEmail(
|
|
|
|
app: INestApplication,
|
|
|
|
userToken: string,
|
|
|
|
email: string,
|
|
|
|
callbackUrl: string
|
|
|
|
): Promise<boolean> {
|
|
|
|
const res = await request(app.getHttpServer())
|
|
|
|
.post(gql)
|
|
|
|
.auth(userToken, { type: 'bearer' })
|
|
|
|
.set({ 'x-request-id': 'test', 'x-operation-name': 'test' })
|
|
|
|
.send({
|
|
|
|
query: `
|
|
|
|
mutation {
|
|
|
|
sendChangeEmail(email: "${email}", callbackUrl: "${callbackUrl}")
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
})
|
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
return res.body.data.sendChangeEmail;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function sendVerifyChangeEmail(
|
|
|
|
app: INestApplication,
|
|
|
|
userToken: string,
|
|
|
|
token: string,
|
|
|
|
email: string,
|
|
|
|
callbackUrl: string
|
|
|
|
): Promise<boolean> {
|
|
|
|
const res = await request(app.getHttpServer())
|
|
|
|
.post(gql)
|
|
|
|
.auth(userToken, { type: 'bearer' })
|
|
|
|
.set({ 'x-request-id': 'test', 'x-operation-name': 'test' })
|
|
|
|
.send({
|
|
|
|
query: `
|
|
|
|
mutation {
|
|
|
|
sendVerifyChangeEmail(token:"${token}", email: "${email}", callbackUrl: "${callbackUrl}")
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
})
|
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
return res.body.data.sendVerifyChangeEmail;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function changeEmail(
|
|
|
|
app: INestApplication,
|
|
|
|
userToken: string,
|
2024-03-12 13:00:09 +03:00
|
|
|
token: string,
|
|
|
|
email: string
|
|
|
|
): Promise<UserType & { token: ClientTokenType }> {
|
2023-12-14 12:50:51 +03:00
|
|
|
const res = await request(app.getHttpServer())
|
|
|
|
.post(gql)
|
|
|
|
.auth(userToken, { type: 'bearer' })
|
|
|
|
.set({ 'x-request-id': 'test', 'x-operation-name': 'test' })
|
|
|
|
.send({
|
|
|
|
query: `
|
|
|
|
mutation {
|
2024-03-12 13:00:09 +03:00
|
|
|
changeEmail(token: "${token}", email: "${email}") {
|
2023-12-14 12:50:51 +03:00
|
|
|
id
|
|
|
|
name
|
|
|
|
avatarUrl
|
|
|
|
email
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
})
|
|
|
|
.expect(200);
|
|
|
|
return res.body.data.changeEmail;
|
|
|
|
}
|