Skip to content

A full-featured CRM built with React, shadcn/ui, and Supabase.

add basic unit tests

Jean-Baptiste Kaiser64b7a9f99b39
diff --git a/makefile b/makefile
index 325e0576..97bc1390 100644
--- a/makefile
+++ b/makefile
@@ -5,9 +5,6 @@ help:
install: package.json ## install dependencies
npm install;
-
-start-supabase-functions: ## start the supabase Functions watcher
- npx supabase functions serve
start-supabase: ## start supabase locally
npx supabase start
diff --git a/src/tests/supabase/functions/postmark.spec.ts b/src/tests/supabase/functions/postmark.spec.ts
new file mode 100644
index 00000000..a1dffdd5
--- /dev/null
+++ b/src/tests/supabase/functions/postmark.spec.ts
@@ -0,0 +1,75 @@
+import { getExpectedAuthorization } from '../../../../supabase/functions/postmark/getExpectedAuthorization.js';
+import { extractMailContactData } from '../../../../supabase/functions/postmark/extractMailContactData.js';
+
+describe('getExpectedAuthorization', () => {
+ it('should return the expected Authorization header from provided user and password', () => {
+ const result = getExpectedAuthorization('testuser', 'testpwd');
+ expect(result).toBe('Basic dGVzdHVzZXI6dGVzdHB3ZA==');
+ });
+});
+
+describe('extractMailContactData', () => {
+ it('should extract data from a single mail contact with a full name', () => {
+ const result = extractMailContactData([
+ {
+ Email: '[email protected]',
+ Name: 'Firstname Lastname',
+ },
+ ]);
+ expect(result).toEqual({
+ firstName: 'Firstname',
+ lastName: 'Lastname',
+ email: '[email protected]',
+ domain: 'marmelab.com',
+ });
+ });
+
+ it('should ignore extra recipients', () => {
+ const result = extractMailContactData([
+ {
+ Email: '[email protected]',
+ Name: 'Firstname Lastname',
+ },
+ {
+ Email: '[email protected]',
+ Name: 'John Doe',
+ },
+ ]);
+ expect(result).toEqual({
+ firstName: 'Firstname',
+ lastName: 'Lastname',
+ email: '[email protected]',
+ domain: 'marmelab.com',
+ });
+ });
+
+ it('should use a single word name as last name', () => {
+ const result = extractMailContactData([
+ {
+ Email: '[email protected]',
+ Name: 'Name',
+ },
+ ]);
+ expect(result).toEqual({
+ firstName: '',
+ lastName: 'Name',
+ email: '[email protected]',
+ domain: 'marmelab.com',
+ });
+ });
+
+ it('should support multi word last name', () => {
+ const result = extractMailContactData([
+ {
+ Email: '[email protected]',
+ Name: 'Multi Word Name',
+ },
+ ]);
+ expect(result).toEqual({
+ firstName: 'Multi',
+ lastName: 'Word Name',
+ email: '[email protected]',
+ domain: 'marmelab.com',
+ });
+ });
+});
diff --git a/supabase/functions/postmark/extractMailContactData.ts b/supabase/functions/postmark/extractMailContactData.ts
new file mode 100644
index 00000000..54b69d83
--- /dev/null
+++ b/supabase/functions/postmark/extractMailContactData.ts
@@ -0,0 +1,40 @@
+/**
+ * Extracts the first name, last name, email, and domain from a mail contact.
+ *
+ * Example:
+ * "ToFull": [
+ * {
+ * "Email": "[email protected]",
+ * "Name": "Firstname Lastname"
+ * }
+ * ]
+ *
+ * Return Value:
+ * {
+ * firstName: "Firstname",
+ * lastName: "Lastname",
+ * email: "[email protected]",
+ * domain: "marmelab.com"
+ * }
+ *
+ */
+export const extractMailContactData = (
+ ToFull: {
+ Email: string;
+ Name: string;
+ }[]
+) => {
+ // We only support one recipient for now
+ const contact = ToFull[0];
+
+ const domain = contact.Email.split('@')[1];
+ const fullName = contact.Name;
+ let firstName = '';
+ let lastName = fullName;
+ if (fullName && fullName.includes(' ')) {
+ const parts = fullName.split(' ');
+ firstName = parts[0];
+ lastName = parts.slice(1).join(' ');
+ }
+ return { firstName, lastName, email: contact.Email, domain };
+};
diff --git a/supabase/functions/postmark/getExpectedAuthorization.ts b/supabase/functions/postmark/getExpectedAuthorization.ts
new file mode 100644
index 00000000..605f03bb
--- /dev/null
+++ b/supabase/functions/postmark/getExpectedAuthorization.ts
@@ -0,0 +1,4 @@
+export const getExpectedAuthorization = (
+ webhookUser: string,
+ webhookPassword: string
+) => `Basic ${btoa(`${webhookUser}:${webhookPassword}`)}`;
diff --git a/supabase/functions/postmark/index.ts b/supabase/functions/postmark/index.ts
index f990071a..75206cd6 100644
--- a/supabase/functions/postmark/index.ts
+++ b/supabase/functions/postmark/index.ts
@@ -5,6 +5,8 @@
// Setup type definitions for built-in Supabase Runtime APIs
import 'jsr:@supabase/functions-js/edge-runtime.d.ts';
import { supabaseAdmin } from '../_shared/supabaseAdmin.ts';
+import { getExpectedAuthorization } from './getExpectedAuthorization.ts';
+import { extractMailContactData } from './extractMailContactData.ts';
const webhookUser = Deno.env.get('POSTMARK_WEBHOOK_USER');
const webhookPassword = Deno.env.get('POSTMARK_WEBHOOK_PASSWORD');
@@ -57,11 +59,6 @@ Deno.serve(async req => {
});
});
-export const getExpectedAuthorization = (
- webhookUser: string,
- webhookPassword: string
-) => `Basic ${btoa(`${webhookUser}:${webhookPassword}`)}`;
-
const checkRequestTypeAndHeaders = (req: Request) => {
// Only allow known IP addresses
// We can use the x-forwarded-for header as it is populated by Supabase
@@ -108,51 +105,7 @@ const checkBody = (json: any) => {
});
};
-/**
- * Extracts the first name, last name, email, and domain from a mail contact.
- *
- * Example:
- * "ToFull": [
- * {
- * "Email": "[email protected]",
- * "Name": "Firstname Lastname"
- * }
- * ]
- *
- * Return Value:
- * {
- * firstName: "Firstname",
- * lastName: "Lastname",
- * email: "[email protected]",
- * domain: "marmelab.com"
- * }
- *
- */
-export const extractMailContactData = (
- ToFull: {
- Email: string;
- Name: string;
- }[]
-) => {
- // We only support one recipient for now
- const contact = ToFull[0];
-
- const domain = contact.Email.split('@')[1];
- const fullName = contact.Name;
- let firstName = '';
- let lastName = fullName;
- if (fullName && fullName.includes(' ')) {
- const parts = fullName.split(' ');
- firstName = parts[0];
- lastName = parts.slice(1).join(' ');
- }
- return { firstName, lastName, email: contact.Email, domain };
-};
-
-export const getNoteContent = (
- subject: string,
- strippedText: string
-) => `${subject}
+const getNoteContent = (subject: string, strippedText: string) => `${subject}
${strippedText}`;