Add user and auth tests
This commit is contained in:
parent
72d7b22891
commit
4bc3be87b4
15 changed files with 288 additions and 26 deletions
|
|
@ -1,4 +1,3 @@
|
|||
{
|
||||
"port": 3012,
|
||||
"db": "db.json"
|
||||
"port": 3012
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ import type {
|
|||
ILoginResponseSuccess,
|
||||
} from "./types.js";
|
||||
import { loginUser, registerUser } from "../../services/auth/auth.js";
|
||||
import { API_ERROR } from "../errors.js";
|
||||
|
||||
const postRegister = async (request: FastifyRequest, _reply: FastifyReply) => {
|
||||
const { username, password, email } = request.body as IRegisterRequest;
|
||||
|
|
@ -20,7 +21,7 @@ const postRegister = async (request: FastifyRequest, _reply: FastifyReply) => {
|
|||
|
||||
if (!newUser) {
|
||||
return {
|
||||
error: "user already exists",
|
||||
error: API_ERROR.USER_ALREADY_EXISTS,
|
||||
} as IRegisterResponseError;
|
||||
}
|
||||
|
||||
|
|
@ -42,7 +43,7 @@ const postLogin = async (request: FastifyRequest, _reply: FastifyReply) => {
|
|||
if (!session) {
|
||||
return {
|
||||
username: username,
|
||||
error: "incorrect credentials",
|
||||
error: API_ERROR.ACCESS_DENIED,
|
||||
} as ILoginResponseError;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
import type { API_ERROR } from "../errors.js";
|
||||
|
||||
interface IRegisterRequest {
|
||||
username: string;
|
||||
password: string;
|
||||
|
|
@ -11,7 +13,7 @@ interface IRegisterResponseSuccess {
|
|||
}
|
||||
|
||||
interface IRegisterResponseError {
|
||||
error: string;
|
||||
error: API_ERROR;
|
||||
}
|
||||
|
||||
interface ILoginRequest {
|
||||
|
|
@ -27,7 +29,7 @@ interface ILoginResponseSuccess {
|
|||
|
||||
interface ILoginResponseError {
|
||||
username: string;
|
||||
error: string;
|
||||
error: API_ERROR;
|
||||
}
|
||||
|
||||
export {
|
||||
|
|
|
|||
|
|
@ -1,10 +1,12 @@
|
|||
import type { API_ERROR } from "../errors.js";
|
||||
|
||||
interface IGetChannelParams {
|
||||
id: string;
|
||||
}
|
||||
|
||||
interface IGetChannelResponseError {
|
||||
id: string;
|
||||
error: string;
|
||||
error: API_ERROR;
|
||||
}
|
||||
|
||||
interface IGetChannelResponseSuccess {
|
||||
|
|
|
|||
|
|
@ -1,10 +1,12 @@
|
|||
import type { API_ERROR } from "../errors.js";
|
||||
|
||||
interface IGetCommunityParams {
|
||||
id: string;
|
||||
}
|
||||
|
||||
interface IGetCommunityResponseError {
|
||||
id: string;
|
||||
error: string;
|
||||
error: API_ERROR;
|
||||
}
|
||||
|
||||
interface IGetCommunityResponseSuccess {
|
||||
|
|
@ -25,7 +27,7 @@ interface IPatchCommunityRequest {
|
|||
|
||||
interface IPatchCommunityResponseError {
|
||||
id: string;
|
||||
error: string;
|
||||
error: API_ERROR;
|
||||
}
|
||||
|
||||
interface IPatchCommunityResponseSuccess {
|
||||
|
|
@ -40,7 +42,7 @@ interface IGetMembersParams {
|
|||
|
||||
interface IGetMembersResponseError {
|
||||
id: string;
|
||||
error: string;
|
||||
error: API_ERROR;
|
||||
}
|
||||
|
||||
interface IGetMembersResponseSuccess {
|
||||
|
|
@ -60,7 +62,7 @@ interface IGetChannelsParams {
|
|||
|
||||
interface IGetChannelsResponseError {
|
||||
id: string;
|
||||
error: string;
|
||||
error: API_ERROR;
|
||||
}
|
||||
|
||||
interface IGetChannelsResponseSuccess {
|
||||
|
|
@ -80,7 +82,7 @@ interface IGetRolesParams {
|
|||
|
||||
interface IGetRolesResponseError {
|
||||
id: string;
|
||||
error: string;
|
||||
error: API_ERROR;
|
||||
}
|
||||
|
||||
interface IGetRolesResponseSuccess {
|
||||
|
|
@ -106,7 +108,7 @@ interface IPostCreateInviteRequest {
|
|||
|
||||
interface IPostCreateInviteResponseError {
|
||||
id: string;
|
||||
error: string;
|
||||
error: API_ERROR;
|
||||
}
|
||||
|
||||
interface IPostCreateInviteResponseSuccess {
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
enum API_ERROR {
|
||||
USER_ALREADY_EXISTS = "USER_ALREADY_EXISTS",
|
||||
NOT_FOUND = "NOT_FOUND",
|
||||
ACCESS_DENIED = "ACCESS_DENIED",
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,10 +1,12 @@
|
|||
import type { API_ERROR } from "../errors.js";
|
||||
|
||||
interface IGetRoleParams {
|
||||
id: string;
|
||||
}
|
||||
|
||||
interface IGetRoleResponseError {
|
||||
id: string;
|
||||
error: string;
|
||||
error: API_ERROR;
|
||||
}
|
||||
|
||||
interface IGetRoleResponseSuccess {
|
||||
|
|
|
|||
|
|
@ -1,10 +1,12 @@
|
|||
import type { API_ERROR } from "../errors.js";
|
||||
|
||||
interface IGetSessionParams {
|
||||
id: string;
|
||||
}
|
||||
|
||||
interface IGetSessionResponseError {
|
||||
id: string;
|
||||
error: string;
|
||||
error: API_ERROR;
|
||||
}
|
||||
|
||||
interface IGetSessionResponseSuccess {
|
||||
|
|
@ -19,7 +21,7 @@ interface IDeleteSessionParams {
|
|||
|
||||
interface IDeleteSessionResponseError {
|
||||
id: string;
|
||||
error: string;
|
||||
error: API_ERROR;
|
||||
}
|
||||
|
||||
interface IDeleteSessionResponseSuccess {
|
||||
|
|
|
|||
|
|
@ -1,10 +1,12 @@
|
|||
import type { API_ERROR } from "../errors.js";
|
||||
|
||||
interface IGetUserParams {
|
||||
id: string;
|
||||
}
|
||||
|
||||
interface IGetUserResponseError {
|
||||
id: string;
|
||||
error: string;
|
||||
error: API_ERROR;
|
||||
}
|
||||
|
||||
interface IGetUserResponseSuccess {
|
||||
|
|
@ -28,7 +30,7 @@ interface IPatchUserRequest {
|
|||
|
||||
interface IPatchUserResponseError {
|
||||
id: string;
|
||||
error: string;
|
||||
error: API_ERROR;
|
||||
}
|
||||
|
||||
interface IPatchUserResponseSuccess {
|
||||
|
|
@ -43,7 +45,7 @@ interface IGetSessionsParams {
|
|||
|
||||
interface IGetSessionsResponseError {
|
||||
id: string;
|
||||
error: string;
|
||||
error: API_ERROR;
|
||||
}
|
||||
|
||||
interface IGetSessionsResponseSuccess {
|
||||
|
|
|
|||
|
|
@ -9,12 +9,20 @@ import { getJwtSecret } from "./helpers.js";
|
|||
const registerUser = async (
|
||||
registration: IUserRegistration,
|
||||
): Promise<User | null> => {
|
||||
const existingUser = await getDB().user.findUnique({
|
||||
const existingUserUsername = await getDB().user.findUnique({
|
||||
where: { username: registration.username },
|
||||
});
|
||||
if (existingUser) {
|
||||
if (existingUserUsername) {
|
||||
return null;
|
||||
}
|
||||
if (registration.email) {
|
||||
const existingUserEmail = await getDB().user.findUnique({
|
||||
where: { email: registration.email },
|
||||
});
|
||||
if (existingUserEmail) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
const passwordHash = await hashPassword(registration.password);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue