#Jest integration test for user verification (email)

3 messages · Page 1 of 1 (latest)

solemn zealot
#

Hi all,

For my project I have created a self-register endpoint for users. This way, in the frontend of my app, users can create an account with email and password. They will then receive the verification email from the Users collection. In that email there is a link to verify their email.

I want to write integration tests to test the whole flow. However, I do not know how to check the email step.

I have used the test setup example from https://github.com/payloadcms/payload/pull/3333 that has been set up in this thread #1146098441810870463 message.

There are two issues I am running into:

  • How to access whether an email has been sent, and how to access the content (body and subject) of that email.
  • The payload I am using to compare the verification token is not the same as the one I initialized in my globalSetup.ts. This one does not have any collections, whereas the payload in globalSetup.ts does have the collections.

Can anyone please help me with this?
Thanks in advance 🙂

GitHub

Description
This adds a new testing folder inside the examples directory. The purpose is to show users how to get up and running with testing within their own payload projects.

I have read and un...

Discord

Discord is the easiest way to communicate over voice, video, and text. Chat, hang out, and stay close with your friends and communities.

shut vesselBOT
solemn zealot
#

This is my Users collection config Users.ts:

import { CollectionConfig } from 'payload/types';
import { Endpoint } from 'payload/config';

const appUrl = process.env.PAYLOAD_PUBLIC_APP_URL;

const WHITELISTED_EMAIL_DOMAINS = [
  'gmail.com',
];

const selfRegister: Endpoint = {
  path: '/self-register',
  method: 'post',
  handler: async (req, res) => {
    const { payload, body } = req;
    const { email, password, locale } = body;

    if (!email || !password) {
      return res.status(400).json({
        errors: [
          {
            message: 'Email and password are required.',
          },
        ],
      });
    }

    const emailDomain = email.split('@')[1] || '';

    if (!WHITELISTED_EMAIL_DOMAINS.includes(emailDomain)) {
      return res.status(403).json({
        errors: [
          {
            message: 'Your email address is unauthorized.',
          },
        ],
      });
    }

    try {
      const response = await payload.create({
        collection: 'users',
        locale: locale || 'nl',
        data: {
          email,
          password,
          // Self registered users are always viewers
          roles: ['viewer'],
        },
        req,
      });
      return res.status(200).json(response);
    } catch (error) {
      return res.status(500).json(error);
    }
  },
};

const Users: CollectionConfig = {
  slug: 'users',
  auth: {
    verify: {
      generateEmailSubject: () => 'Verify your email',
      generateEmailHTML: ({ req, token, user }) => {
        const url = `${appUrl}${
          req.locale === 'en' ? '/en' : ''
        }/auth/verify?token=${token}`;

        const message =
          req.locale === 'en'
            ? `Hey ${user.email}, verify your email by clicking here: ${url}`
            : `Hallo ${user.email}, verifieer je email door hier te klikken: ${url}`;

        return message;
      },
    },
  },
  fields: [rolesField(), isSuperAdminField()],
  endpoints: [selfRegister],
};

export default Users;