Skip to content

armgit / papra

Public Imported from GitHub
0

The minimalistic document archiving platform.

3.8 KB
Raw Blame
import { describe, expect, test } from 'vitest';
import {
  buildEmailAddress,
  getEmailUsername,
  getIsFromAllowedOrigin,
  getRecipientAddresses,
  parseEmailAddress,
} from './intake-emails.models';

describe('intake-emails models', () => {
  describe('getEmailUsername', () => {
    test('the username is the whole part of the email address before the @', () => {
      expect(getEmailUsername({ email: '[email protected]' })).to.eql({ username: 'foo' });
      expect(getEmailUsername({ email: '[email protected]' })).to.eql({ username: 'foo+bar' });
      expect(getEmailUsername({ email: undefined })).to.eql({ username: undefined });
    });
  });

  describe('getIsFromAllowedOrigin', () => {
    test('an origin is allowed if it is in the list of the intake email allowed origins email address', () => {
      expect(
        getIsFromAllowedOrigin({
          origin: '[email protected]',
          allowedOrigins: ['[email protected]', '[email protected]'],
        }),
      ).to.eql(true);

      expect(
        getIsFromAllowedOrigin({
          origin: '[email protected]',
          allowedOrigins: ['[email protected]'],
        }),
      ).to.eql(false);
    });

    test('email addresses are case insensitive', () => {
      expect(
        getIsFromAllowedOrigin({
          origin: '[email protected]',
          allowedOrigins: ['[email protected]'],
        }),
      ).to.eql(true);
    });

    test('if no allowed origins are provided, the origin is not allowed', () => {
      expect(
        getIsFromAllowedOrigin({
          origin: '[email protected]',
          allowedOrigins: [],
        }),
      ).to.eql(false);
    });
  });

  describe('buildEmailAddress', () => {
    test('it builds an email address from a username, a domain and an optional plus part', () => {
      expect(buildEmailAddress({ username: 'foo', domain: 'example.fr' })).to.eql('[email protected]');
      expect(buildEmailAddress({ username: 'foo', domain: 'example.fr', plusPart: 'bar' })).to.eql(
        '[email protected]',
      );
    });
  });

  describe('parseEmailAddress', () => {
    test('it parses an email address into a username, a domain and an optional plus part', () => {
      expect(parseEmailAddress({ email: '[email protected]' })).to.eql({
        username: 'foo',
        domain: 'example.fr',
        plusPart: undefined,
      });
      expect(parseEmailAddress({ email: '[email protected]' })).to.eql({
        username: 'foo',
        domain: 'example.fr',
        plusPart: 'bar',
      });
      expect(parseEmailAddress({ email: '[email protected]' })).to.eql({
        username: 'foo',
        domain: 'example.fr',
        plusPart: 'bar+baz',
      });
    });
  });

  describe('getRecipientAddresses', () => {
    test(`the recipient addresses are the addresses the email was sent to, they are either in the to or originalTo fields, deduplicated
          there is a difference between to and originalTo as originalTo contains the actual email address used whereas to can be
          modified by mailing lists or email forwarding (like proton or cowmail)`, () => {
      const email = {
        to: [{ address: '[email protected]' }, { address: '[email protected]' }],
        originalTo: [{ address: '[email protected]' }, { address: '[email protected]' }],
      };

      expect(getRecipientAddresses({ email })).to.eql([
        '[email protected]',
        '[email protected]',
        '[email protected]',
      ]);
    });

    test('email addresses are deduplicated in a case insensitive way', () => {
      const email = {
        to: [{ address: '[email protected]' }, { address: '[email protected]' }],
        originalTo: [{ address: '[email protected]' }, { address: '[email protected]' }],
      };

      expect(getRecipientAddresses({ email })).to.eql([
        '[email protected]',
        '[email protected]',
        '[email protected]',
      ]);
    });
  });
});