2023-12-14 12:50:51 +03:00
|
|
|
import type { INestApplication } from '@nestjs/common';
|
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 12:56:38 +03:00
|
|
|
import {
|
|
|
|
AuthService,
|
|
|
|
type ClientTokenType,
|
|
|
|
type CurrentUser,
|
|
|
|
} from '../../src/core/auth';
|
2024-05-06 14:11:56 +03:00
|
|
|
import { sessionUser } from '../../src/core/auth/service';
|
|
|
|
import { UserService, type UserType } from '../../src/core/user';
|
2023-12-14 12:50:51 +03:00
|
|
|
import { gql } from './common';
|
|
|
|
|
2024-04-30 06:46:59 +03:00
|
|
|
export async function internalSignIn(app: INestApplication, userId: string) {
|
|
|
|
const auth = app.get(AuthService);
|
|
|
|
|
|
|
|
const session = await auth.createUserSession({ id: userId });
|
|
|
|
|
|
|
|
return `${AuthService.sessionCookieName}=${session.sessionId}`;
|
|
|
|
}
|
|
|
|
|
2024-04-17 11:32:26 +03:00
|
|
|
export function sessionCookie(headers: any): string {
|
2024-03-26 05:24:17 +03:00
|
|
|
const cookie = headers['set-cookie']?.find((c: string) =>
|
2024-03-26 12:56:38 +03:00
|
|
|
c.startsWith(`${AuthService.sessionCookieName}=`)
|
2024-03-26 05:24:17 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
if (!cookie) {
|
2024-04-17 11:32:26 +03:00
|
|
|
return '';
|
2024-03-26 05:24:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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')
|
2024-04-17 11:32:26 +03:00
|
|
|
.set('cookie', cookie!)
|
2024-03-26 05:24:17 +03:00
|
|
|
.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 }> {
|
2024-05-06 14:11:56 +03:00
|
|
|
const user = await app.get(UserService).createUser({
|
|
|
|
name,
|
|
|
|
email,
|
2024-07-23 13:39:33 +03:00
|
|
|
password,
|
2024-05-06 14:11:56 +03:00
|
|
|
emailVerifiedAt: autoVerifyEmail ? new Date() : null,
|
2024-03-12 13:00:09 +03:00
|
|
|
});
|
2024-05-06 14:11:56 +03:00
|
|
|
const { sessionId } = await app.get(AuthService).createUserSession(user);
|
|
|
|
|
|
|
|
return {
|
|
|
|
...sessionUser(user),
|
|
|
|
token: { token: sessionId, refresh: '' },
|
|
|
|
};
|
2024-03-12 13:00:09 +03:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-05-20 09:38:48 +03:00
|
|
|
export async function sendSetPasswordEmail(
|
|
|
|
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 {
|
|
|
|
sendSetPasswordEmail(email: "${email}", callbackUrl: "${callbackUrl}")
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
})
|
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
return res.body.data.sendChangeEmail;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function changePassword(
|
|
|
|
app: INestApplication,
|
2024-08-14 06:34:35 +03:00
|
|
|
userId: string,
|
2024-05-20 09:38:48 +03:00
|
|
|
token: string,
|
|
|
|
password: string
|
|
|
|
): Promise<string> {
|
|
|
|
const res = await request(app.getHttpServer())
|
|
|
|
.post(gql)
|
|
|
|
.set({ 'x-request-id': 'test', 'x-operation-name': 'test' })
|
|
|
|
.send({
|
|
|
|
query: `
|
2024-08-14 06:34:35 +03:00
|
|
|
mutation changePassword($token: String!, $userId: String!, $password: String!) {
|
|
|
|
changePassword(token: $token, userId: $userId, newPassword: $password)
|
2024-05-20 09:38:48 +03:00
|
|
|
}
|
|
|
|
`,
|
2024-08-14 06:34:35 +03:00
|
|
|
variables: { token, password, userId },
|
2024-05-20 09:38:48 +03:00
|
|
|
})
|
|
|
|
.expect(200);
|
2024-08-14 06:34:35 +03:00
|
|
|
return res.body.data.changePassword;
|
2024-05-20 09:38:48 +03:00
|
|
|
}
|
|
|
|
|
2023-12-14 12:50:51 +03:00
|
|
|
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;
|
|
|
|
}
|