How do I test authentication written in Nextauth and prisma queries using jest?
Unanswered
Polish posted this in #help-forum
PolishOP
I'm having a little trouble finding tutorials to properly test authentication (user log in or create account) as well as Prisma database querying testing. Every tutorial out there seems to be on testing user interface only, which is not really what I need. The Jest docs is a little too simple in my use case too. If there is a place where I can find tutorials on testing, please point me in the right direction.
3 Replies
PolishOP
side note: I wrote my codes in "3 layer architecture", so most of the test cases would be in the domain logic.
Here are some example codes:
Here are some example codes:
Here are some example codes:
"use server";
import {createUser, getNumberOfUsers, getUserByEmail, updateUser} from "@/data/local/userRepo";
import {newUserSchema, updateProfileSchema} from "@/schema/custom";
import {z} from "zod";
import bcrypt from "bcryptjs";
export async function updateProfile(values: z.infer<typeof updateProfileSchema>) {
const validation = updateProfileSchema.safeParse(values)
if (!validation.success) {
return {error: "Invalid input"}
}
const {email, originalPassword, password} = validation.data
const existingUser = await getUserByEmail(email)
if (!existingUser) {
return {error: "User not found"}
}
const passwordMatch = await bcrypt.compare(originalPassword, existingUser.password)
if (!passwordMatch) {
return {error: "Incorrect password"}
}
const hashedPassword = await bcrypt.hash(password, 10)
await updateUser(existingUser.id, {email, password: hashedPassword})
}
export async function checkNumberOfUsers() {
return await getNumberOfUsers()
}
export const createNewUser = async (values: z.infer<typeof newUserSchema>) => {
const validatedFields = newUserSchema.safeParse(values);
if (!validatedFields.success) {
return {error: "Invalid fields!"};
}
const {email, password, confirmPassword} = validatedFields.data;
if (password !== confirmPassword) {
return {error: "Passwords do not match!"};
}
const hashedPassword = await bcrypt.hash(password, 10);
const existingUser = await getUserByEmail(email);
if (existingUser) {
return {error: "Email already in use!"};
}
await createUser({
email: email,
password: hashedPassword,
});
// Add any other business logic here (e.g., checking credentials)
return {success: "User created."};
};
here are my repo codes
import {db} from "@/lib/db";
import {Antenna, Function} from "@prisma/client";
/**
* Adds an antenna to the database.
*
* @param data - The data for the antenna to be added.
* @returns A promise that resolves to the newly created antenna.
*/
export async function addAntenna(data: { antennaPort: number, function: Function }): Promise<Antenna> {
return await db.antenna.create({
data: {
antennaPort: data.antennaPort,
function: data.function
}
})
}
/**
* Updates an antenna in the database.
* @param data - The data object containing the properties of the antenna to be updated.
* @returns A Promise that resolves to the updated Antenna object.
*/
export async function updateAntenna(data: { id: string, antennaPort: number, function: Function }): Promise<Antenna> {
return await db.antenna.update({
where: {
id: data.id
},
data: {
antennaPort: data.antennaPort,
function: data.function
}
})
}
/**
* Retrieves all antennas with the GEOFENCE function.
* @returns A promise that resolves to an array of antennas.
*/
export async function getAllGeofenceAntennas(): Promise<Antenna[]> {
return await db.antenna.findMany({
where: {
function: Function.GEOFENCE
}
});
}