chore: format
This commit is contained in:
@@ -1,16 +1,16 @@
|
||||
import { describe, it, expect } from "vitest"
|
||||
import { request } from "@helpers/request"
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { request } from "@helpers/request";
|
||||
|
||||
describe("Auth", () => {
|
||||
it("Should return 401 without API Key", async () => {
|
||||
const response = await request.get("/api/subscribers")
|
||||
expect(response.status).toBe(401)
|
||||
})
|
||||
it("Should return 401 without API Key", async () => {
|
||||
const response = await request.get("/api/subscribers");
|
||||
expect(response.status).toBe(401);
|
||||
});
|
||||
|
||||
it("Should return 401 with invalid API Key", async () => {
|
||||
const response = await request
|
||||
.get("/api/subscribers")
|
||||
.set("x-api-key", "invalid")
|
||||
expect(response.status).toBe(401)
|
||||
})
|
||||
})
|
||||
it("Should return 401 with invalid API Key", async () => {
|
||||
const response = await request
|
||||
.get("/api/subscribers")
|
||||
.set("x-api-key", "invalid");
|
||||
expect(response.status).toBe(401);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,19 +1,19 @@
|
||||
import { describe, expect, it } from "vitest"
|
||||
import { createUser } from "@helpers/user/user"
|
||||
import { request } from "@helpers/request"
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { createUser } from "@helpers/user/user";
|
||||
import { request } from "@helpers/request";
|
||||
|
||||
describe("Create List", () => {
|
||||
it("should create a list", async () => {
|
||||
const { apiKey } = await createUser()
|
||||
it("should create a list", async () => {
|
||||
const { apiKey } = await createUser();
|
||||
|
||||
const response = await request
|
||||
.post("/api/lists")
|
||||
.set("x-api-key", apiKey.key)
|
||||
.send({ name: "Test List" })
|
||||
const response = await request
|
||||
.post("/api/lists")
|
||||
.set("x-api-key", apiKey.key)
|
||||
.send({ name: "Test List" });
|
||||
|
||||
// TODO: Add tests
|
||||
// expect(response.status).toBe(201)
|
||||
// expect(response.body).toBeDefined()
|
||||
// expect(response.body.name).toBe("Test List")
|
||||
})
|
||||
})
|
||||
// TODO: Add tests
|
||||
// expect(response.status).toBe(201)
|
||||
// expect(response.body).toBeDefined()
|
||||
// expect(response.body.name).toBe("Test List")
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,321 +1,321 @@
|
||||
import { describe, it, expect } from "vitest"
|
||||
import { createUser } from "@helpers/user/user"
|
||||
import { request } from "@helpers/request"
|
||||
import { createList } from "@tests/integration/helpers/list/list"
|
||||
import { prisma } from "@src/utils/prisma"
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { createUser } from "@helpers/user/user";
|
||||
import { request } from "@helpers/request";
|
||||
import { createList } from "@tests/integration/helpers/list/list";
|
||||
import { prisma } from "@src/utils/prisma";
|
||||
|
||||
describe("[POST] /api/subscribers", () => {
|
||||
it("should create a subscriber", async () => {
|
||||
const {
|
||||
apiKey: { key: apiKey },
|
||||
orgId,
|
||||
} = await createUser()
|
||||
const list = await createList({
|
||||
name: "Test List",
|
||||
organizationId: orgId,
|
||||
description: "This is a new list for testing",
|
||||
})
|
||||
it("should create a subscriber", async () => {
|
||||
const {
|
||||
apiKey: { key: apiKey },
|
||||
orgId,
|
||||
} = await createUser();
|
||||
const list = await createList({
|
||||
name: "Test List",
|
||||
organizationId: orgId,
|
||||
description: "This is a new list for testing",
|
||||
});
|
||||
|
||||
const response = await request
|
||||
.post("/api/subscribers")
|
||||
.set("x-api-key", apiKey)
|
||||
.send({
|
||||
email: "test@test.com",
|
||||
lists: [list.id],
|
||||
})
|
||||
const response = await request
|
||||
.post("/api/subscribers")
|
||||
.set("x-api-key", apiKey)
|
||||
.send({
|
||||
email: "test@test.com",
|
||||
lists: [list.id],
|
||||
});
|
||||
|
||||
expect(response.status).toBe(201)
|
||||
expect(response.body).toBeDefined()
|
||||
expect(response.status).toBe(201);
|
||||
expect(response.body).toBeDefined();
|
||||
|
||||
const subscriber = response.body
|
||||
expect(subscriber.email).toBe("test@test.com")
|
||||
expect(subscriber.lists).toBeDefined()
|
||||
expect(subscriber.lists.length).toBe(1)
|
||||
expect(subscriber.lists[0].id).toBe(list.id)
|
||||
subscriber.lists.forEach(
|
||||
(list: { name: string; id: string; description: string }) => {
|
||||
expect(list.name).toBe("Test List")
|
||||
expect(list.description).toBe("This is a new list for testing")
|
||||
}
|
||||
)
|
||||
})
|
||||
const subscriber = response.body;
|
||||
expect(subscriber.email).toBe("test@test.com");
|
||||
expect(subscriber.lists).toBeDefined();
|
||||
expect(subscriber.lists.length).toBe(1);
|
||||
expect(subscriber.lists[0].id).toBe(list.id);
|
||||
subscriber.lists.forEach(
|
||||
(list: { name: string; id: string; description: string }) => {
|
||||
expect(list.name).toBe("Test List");
|
||||
expect(list.description).toBe("This is a new list for testing");
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
it("should create a subscriber with existing lists", async () => {
|
||||
const {
|
||||
apiKey: { key: apiKey },
|
||||
orgId,
|
||||
} = await createUser()
|
||||
it("should create a subscriber with existing lists", async () => {
|
||||
const {
|
||||
apiKey: { key: apiKey },
|
||||
orgId,
|
||||
} = await createUser();
|
||||
|
||||
const list1 = await createList({
|
||||
name: "Test List 1",
|
||||
organizationId: orgId,
|
||||
description: "This is a new list for testing",
|
||||
})
|
||||
const list1 = await createList({
|
||||
name: "Test List 1",
|
||||
organizationId: orgId,
|
||||
description: "This is a new list for testing",
|
||||
});
|
||||
|
||||
const list2 = await createList({
|
||||
name: "Test List 2",
|
||||
organizationId: orgId,
|
||||
description: "This is a new list for testing",
|
||||
})
|
||||
const list2 = await createList({
|
||||
name: "Test List 2",
|
||||
organizationId: orgId,
|
||||
description: "This is a new list for testing",
|
||||
});
|
||||
|
||||
const response = await request
|
||||
.post("/api/subscribers")
|
||||
.set("x-api-key", apiKey)
|
||||
.send({
|
||||
email: "test@test.com",
|
||||
lists: [list1.id, list2.id],
|
||||
})
|
||||
const response = await request
|
||||
.post("/api/subscribers")
|
||||
.set("x-api-key", apiKey)
|
||||
.send({
|
||||
email: "test@test.com",
|
||||
lists: [list1.id, list2.id],
|
||||
});
|
||||
|
||||
expect(response.status).toBe(201)
|
||||
expect(response.body).toBeDefined()
|
||||
expect(response.status).toBe(201);
|
||||
expect(response.body).toBeDefined();
|
||||
|
||||
const subscriber = response.body
|
||||
expect(subscriber.email).toBe("test@test.com")
|
||||
expect(subscriber.lists).toBeDefined()
|
||||
expect(subscriber.lists.length).toBe(2)
|
||||
const subscriber = response.body;
|
||||
expect(subscriber.email).toBe("test@test.com");
|
||||
expect(subscriber.lists).toBeDefined();
|
||||
expect(subscriber.lists.length).toBe(2);
|
||||
|
||||
subscriber.lists.forEach((list: { id: string }) => {
|
||||
expect([list1.id, list2.id]).toContain(list.id)
|
||||
})
|
||||
})
|
||||
subscriber.lists.forEach((list: { id: string }) => {
|
||||
expect([list1.id, list2.id]).toContain(list.id);
|
||||
});
|
||||
});
|
||||
|
||||
it("should create a subscriber that already exists, merge lists and remove duplicates", async () => {
|
||||
const {
|
||||
apiKey: { key: apiKey },
|
||||
orgId,
|
||||
} = await createUser()
|
||||
it("should create a subscriber that already exists, merge lists and remove duplicates", async () => {
|
||||
const {
|
||||
apiKey: { key: apiKey },
|
||||
orgId,
|
||||
} = await createUser();
|
||||
|
||||
const list1 = await createList({
|
||||
name: "Test List 1",
|
||||
organizationId: orgId,
|
||||
description: "This is a new list for testing",
|
||||
})
|
||||
const list1 = await createList({
|
||||
name: "Test List 1",
|
||||
organizationId: orgId,
|
||||
description: "This is a new list for testing",
|
||||
});
|
||||
|
||||
const list2 = await createList({
|
||||
name: "Test List 2",
|
||||
organizationId: orgId,
|
||||
description: "This is a new list for testing",
|
||||
})
|
||||
const list2 = await createList({
|
||||
name: "Test List 2",
|
||||
organizationId: orgId,
|
||||
description: "This is a new list for testing",
|
||||
});
|
||||
|
||||
const response = await request
|
||||
.post("/api/subscribers")
|
||||
.set("x-api-key", apiKey)
|
||||
.send({
|
||||
email: "test@test.com",
|
||||
lists: [list1.id],
|
||||
})
|
||||
const response = await request
|
||||
.post("/api/subscribers")
|
||||
.set("x-api-key", apiKey)
|
||||
.send({
|
||||
email: "test@test.com",
|
||||
lists: [list1.id],
|
||||
});
|
||||
|
||||
expect(response.status).toBe(201)
|
||||
expect(response.body).toBeDefined()
|
||||
expect(response.status).toBe(201);
|
||||
expect(response.body).toBeDefined();
|
||||
|
||||
const subscriber = response.body
|
||||
expect(subscriber.email).toBe("test@test.com")
|
||||
expect(subscriber.lists).toBeDefined()
|
||||
expect(subscriber.lists.length).toBe(1)
|
||||
expect(subscriber.lists[0].id).toBe(list1.id)
|
||||
const subscriber = response.body;
|
||||
expect(subscriber.email).toBe("test@test.com");
|
||||
expect(subscriber.lists).toBeDefined();
|
||||
expect(subscriber.lists.length).toBe(1);
|
||||
expect(subscriber.lists[0].id).toBe(list1.id);
|
||||
|
||||
const response2 = await request
|
||||
.post("/api/subscribers")
|
||||
.set("x-api-key", apiKey)
|
||||
.send({
|
||||
email: "test@test.com",
|
||||
lists: [list1.id, list2.id],
|
||||
})
|
||||
const response2 = await request
|
||||
.post("/api/subscribers")
|
||||
.set("x-api-key", apiKey)
|
||||
.send({
|
||||
email: "test@test.com",
|
||||
lists: [list1.id, list2.id],
|
||||
});
|
||||
|
||||
expect(response2.status).toBe(201)
|
||||
expect(response2.body).toBeDefined()
|
||||
expect(response2.status).toBe(201);
|
||||
expect(response2.body).toBeDefined();
|
||||
|
||||
const subscriber2 = response2.body
|
||||
expect(subscriber2.email).toBe("test@test.com")
|
||||
expect(subscriber2.lists).toBeDefined()
|
||||
expect(subscriber2.lists.length).toBe(2)
|
||||
expect(subscriber2.lists[0].id).toBe(list2.id)
|
||||
expect(subscriber2.lists[1].id).toBe(list1.id)
|
||||
})
|
||||
const subscriber2 = response2.body;
|
||||
expect(subscriber2.email).toBe("test@test.com");
|
||||
expect(subscriber2.lists).toBeDefined();
|
||||
expect(subscriber2.lists.length).toBe(2);
|
||||
expect(subscriber2.lists[0].id).toBe(list2.id);
|
||||
expect(subscriber2.lists[1].id).toBe(list1.id);
|
||||
});
|
||||
|
||||
it("should reject invalid email format", async () => {
|
||||
const {
|
||||
apiKey: { key: apiKey },
|
||||
orgId,
|
||||
} = await createUser()
|
||||
it("should reject invalid email format", async () => {
|
||||
const {
|
||||
apiKey: { key: apiKey },
|
||||
orgId,
|
||||
} = await createUser();
|
||||
|
||||
const list = await createList({
|
||||
name: "Test List",
|
||||
organizationId: orgId,
|
||||
})
|
||||
const list = await createList({
|
||||
name: "Test List",
|
||||
organizationId: orgId,
|
||||
});
|
||||
|
||||
const response = await request
|
||||
.post("/api/subscribers")
|
||||
.set("x-api-key", apiKey)
|
||||
.send({
|
||||
email: "invalid-email",
|
||||
lists: [list.id],
|
||||
})
|
||||
const response = await request
|
||||
.post("/api/subscribers")
|
||||
.set("x-api-key", apiKey)
|
||||
.send({
|
||||
email: "invalid-email",
|
||||
lists: [list.id],
|
||||
});
|
||||
|
||||
expect(response.status).toBe(400)
|
||||
expect(response.body.error).toBe("Invalid email format")
|
||||
})
|
||||
expect(response.status).toBe(400);
|
||||
expect(response.body.error).toBe("Invalid email format");
|
||||
});
|
||||
|
||||
it("should reject empty lists array", async () => {
|
||||
const {
|
||||
apiKey: { key: apiKey },
|
||||
} = await createUser()
|
||||
it("should reject empty lists array", async () => {
|
||||
const {
|
||||
apiKey: { key: apiKey },
|
||||
} = await createUser();
|
||||
|
||||
const response = await request
|
||||
.post("/api/subscribers")
|
||||
.set("x-api-key", apiKey)
|
||||
.send({
|
||||
email: "test@test.com",
|
||||
lists: [],
|
||||
})
|
||||
const response = await request
|
||||
.post("/api/subscribers")
|
||||
.set("x-api-key", apiKey)
|
||||
.send({
|
||||
email: "test@test.com",
|
||||
lists: [],
|
||||
});
|
||||
|
||||
expect(response.status).toBe(400)
|
||||
expect(response.body.error).toBe("At least one listId is required")
|
||||
})
|
||||
expect(response.status).toBe(400);
|
||||
expect(response.body.error).toBe("At least one listId is required");
|
||||
});
|
||||
|
||||
it("should create a subscriber with optional name field", async () => {
|
||||
const {
|
||||
apiKey: { key: apiKey },
|
||||
orgId,
|
||||
} = await createUser()
|
||||
it("should create a subscriber with optional name field", async () => {
|
||||
const {
|
||||
apiKey: { key: apiKey },
|
||||
orgId,
|
||||
} = await createUser();
|
||||
|
||||
const list = await createList({
|
||||
name: "Test List",
|
||||
organizationId: orgId,
|
||||
})
|
||||
const list = await createList({
|
||||
name: "Test List",
|
||||
organizationId: orgId,
|
||||
});
|
||||
|
||||
const response = await request
|
||||
.post("/api/subscribers")
|
||||
.set("x-api-key", apiKey)
|
||||
.send({
|
||||
email: "test@test.com",
|
||||
name: "John Doe",
|
||||
lists: [list.id],
|
||||
})
|
||||
const response = await request
|
||||
.post("/api/subscribers")
|
||||
.set("x-api-key", apiKey)
|
||||
.send({
|
||||
email: "test@test.com",
|
||||
name: "John Doe",
|
||||
lists: [list.id],
|
||||
});
|
||||
|
||||
expect(response.status).toBe(201)
|
||||
expect(response.body.email).toBe("test@test.com")
|
||||
expect(response.body.name).toBe("John Doe")
|
||||
})
|
||||
expect(response.status).toBe(201);
|
||||
expect(response.body.email).toBe("test@test.com");
|
||||
expect(response.body.name).toBe("John Doe");
|
||||
});
|
||||
|
||||
it("should reject missing required fields", async () => {
|
||||
const {
|
||||
apiKey: { key: apiKey },
|
||||
} = await createUser()
|
||||
it("should reject missing required fields", async () => {
|
||||
const {
|
||||
apiKey: { key: apiKey },
|
||||
} = await createUser();
|
||||
|
||||
const response = await request
|
||||
.post("/api/subscribers")
|
||||
.set("x-api-key", apiKey)
|
||||
.send({
|
||||
name: "John Doe",
|
||||
})
|
||||
const response = await request
|
||||
.post("/api/subscribers")
|
||||
.set("x-api-key", apiKey)
|
||||
.send({
|
||||
name: "John Doe",
|
||||
});
|
||||
|
||||
expect(response.status).toBe(400)
|
||||
expect(response.body.error).toBeDefined()
|
||||
})
|
||||
expect(response.status).toBe(400);
|
||||
expect(response.body.error).toBeDefined();
|
||||
});
|
||||
|
||||
it("should reject non-existent list IDs", async () => {
|
||||
const {
|
||||
apiKey: { key: apiKey },
|
||||
} = await createUser()
|
||||
it("should reject non-existent list IDs", async () => {
|
||||
const {
|
||||
apiKey: { key: apiKey },
|
||||
} = await createUser();
|
||||
|
||||
const response = await request
|
||||
.post("/api/subscribers")
|
||||
.set("x-api-key", apiKey)
|
||||
.send({
|
||||
email: "test@test.com",
|
||||
lists: ["non-existent-id"],
|
||||
})
|
||||
const response = await request
|
||||
.post("/api/subscribers")
|
||||
.set("x-api-key", apiKey)
|
||||
.send({
|
||||
email: "test@test.com",
|
||||
lists: ["non-existent-id"],
|
||||
});
|
||||
|
||||
expect(response.status).toBe(400)
|
||||
expect(response.body.error).toBe("List with id non-existent-id not found")
|
||||
})
|
||||
expect(response.status).toBe(400);
|
||||
expect(response.body.error).toBe("List with id non-existent-id not found");
|
||||
});
|
||||
|
||||
it("should create a subscriber with doubleOptIn true, send verification email, and set emailVerified to false", async () => {
|
||||
const {
|
||||
apiKey: { key: apiKey },
|
||||
orgId,
|
||||
user,
|
||||
} = await createUser()
|
||||
it("should create a subscriber with doubleOptIn true, send verification email, and set emailVerified to false", async () => {
|
||||
const {
|
||||
apiKey: { key: apiKey },
|
||||
orgId,
|
||||
user,
|
||||
} = await createUser();
|
||||
|
||||
if (!process.env.RESEND_API_KEY) {
|
||||
throw new Error("RESEND_API_KEY is not set")
|
||||
}
|
||||
if (!process.env.RESEND_TEST_EMAIL) {
|
||||
throw new Error("RESEND_TEST_EMAIL is not set")
|
||||
}
|
||||
if (!process.env.RESEND_API_KEY) {
|
||||
throw new Error("RESEND_API_KEY is not set");
|
||||
}
|
||||
if (!process.env.RESEND_TEST_EMAIL) {
|
||||
throw new Error("RESEND_TEST_EMAIL is not set");
|
||||
}
|
||||
|
||||
// Delete existing SMTP settings for the org to ensure a clean slate for this test
|
||||
await prisma.smtpSettings.deleteMany({
|
||||
where: { organizationId: orgId },
|
||||
})
|
||||
// Delete existing SMTP settings for the org to ensure a clean slate for this test
|
||||
await prisma.smtpSettings.deleteMany({
|
||||
where: { organizationId: orgId },
|
||||
});
|
||||
|
||||
// Create specific SMTP settings for this test with SSL_TLS encryption
|
||||
await prisma.smtpSettings.create({
|
||||
data: {
|
||||
organizationId: orgId,
|
||||
host: "smtp.resend.com",
|
||||
port: 465, // Standard SSL/TLS port
|
||||
username: "resend",
|
||||
password: process.env.RESEND_API_KEY,
|
||||
fromEmail: process.env.RESEND_TEST_EMAIL,
|
||||
encryption: "SSL_TLS", // Explicitly set to SSL_TLS
|
||||
secure: true, // Ensure secure is true, though Mailer handles it for SSL_TLS
|
||||
},
|
||||
})
|
||||
// Create specific SMTP settings for this test with SSL_TLS encryption
|
||||
await prisma.smtpSettings.create({
|
||||
data: {
|
||||
organizationId: orgId,
|
||||
host: "smtp.resend.com",
|
||||
port: 465, // Standard SSL/TLS port
|
||||
username: "resend",
|
||||
password: process.env.RESEND_API_KEY,
|
||||
fromEmail: process.env.RESEND_TEST_EMAIL,
|
||||
encryption: "SSL_TLS", // Explicitly set to SSL_TLS
|
||||
secure: true, // Ensure secure is true, though Mailer handles it for SSL_TLS
|
||||
},
|
||||
});
|
||||
|
||||
// Upsert General settings (organizationId is unique here, so upsert is fine)
|
||||
await prisma.generalSettings.upsert({
|
||||
where: { organizationId: orgId },
|
||||
update: {
|
||||
baseURL: "http://localhost:3000",
|
||||
defaultFromEmail: process.env.RESEND_TEST_EMAIL,
|
||||
},
|
||||
create: {
|
||||
organizationId: orgId,
|
||||
baseURL: "http://localhost:3000",
|
||||
defaultFromEmail: process.env.RESEND_TEST_EMAIL,
|
||||
},
|
||||
})
|
||||
// Upsert General settings (organizationId is unique here, so upsert is fine)
|
||||
await prisma.generalSettings.upsert({
|
||||
where: { organizationId: orgId },
|
||||
update: {
|
||||
baseURL: "http://localhost:3000",
|
||||
defaultFromEmail: process.env.RESEND_TEST_EMAIL,
|
||||
},
|
||||
create: {
|
||||
organizationId: orgId,
|
||||
baseURL: "http://localhost:3000",
|
||||
defaultFromEmail: process.env.RESEND_TEST_EMAIL,
|
||||
},
|
||||
});
|
||||
|
||||
const list = await createList({
|
||||
name: "Double Opt-In List",
|
||||
organizationId: orgId,
|
||||
})
|
||||
const list = await createList({
|
||||
name: "Double Opt-In List",
|
||||
organizationId: orgId,
|
||||
});
|
||||
|
||||
const receivingEmail = process.env.RESEND_TEST_EMAIL_RECEIVER
|
||||
if (!receivingEmail) {
|
||||
throw new Error("RESEND_TEST_EMAIL_RECEIVER is not set")
|
||||
}
|
||||
const receivingEmail = process.env.RESEND_TEST_EMAIL_RECEIVER;
|
||||
if (!receivingEmail) {
|
||||
throw new Error("RESEND_TEST_EMAIL_RECEIVER is not set");
|
||||
}
|
||||
|
||||
const response = await request
|
||||
.post("/api/subscribers")
|
||||
.set("x-api-key", apiKey)
|
||||
.send({
|
||||
email: receivingEmail,
|
||||
lists: [list.id],
|
||||
doubleOptIn: true,
|
||||
})
|
||||
const response = await request
|
||||
.post("/api/subscribers")
|
||||
.set("x-api-key", apiKey)
|
||||
.send({
|
||||
email: receivingEmail,
|
||||
lists: [list.id],
|
||||
doubleOptIn: true,
|
||||
});
|
||||
|
||||
expect(response.status).toBe(201)
|
||||
expect(response.body).toBeDefined()
|
||||
expect(response.status).toBe(201);
|
||||
expect(response.body).toBeDefined();
|
||||
|
||||
const subscriber = response.body
|
||||
expect(subscriber.email).toBe(receivingEmail.toLowerCase())
|
||||
expect(subscriber.lists).toBeDefined()
|
||||
expect(subscriber.lists.length).toBe(1)
|
||||
expect(subscriber.lists[0].id).toBe(list.id)
|
||||
expect(subscriber.emailVerified).toBe(false)
|
||||
const subscriber = response.body;
|
||||
expect(subscriber.email).toBe(receivingEmail.toLowerCase());
|
||||
expect(subscriber.lists).toBeDefined();
|
||||
expect(subscriber.lists.length).toBe(1);
|
||||
expect(subscriber.lists[0].id).toBe(list.id);
|
||||
expect(subscriber.emailVerified).toBe(false);
|
||||
|
||||
const dbSubscriber = await prisma.subscriber.findUnique({
|
||||
where: { id: subscriber.id },
|
||||
})
|
||||
expect(dbSubscriber).toBeDefined()
|
||||
expect(dbSubscriber?.emailVerified).toBe(false)
|
||||
const dbSubscriber = await prisma.subscriber.findUnique({
|
||||
where: { id: subscriber.id },
|
||||
});
|
||||
expect(dbSubscriber).toBeDefined();
|
||||
expect(dbSubscriber?.emailVerified).toBe(false);
|
||||
|
||||
await prisma.generalSettings.deleteMany({
|
||||
where: { organizationId: orgId },
|
||||
})
|
||||
await prisma.smtpSettings.deleteMany({ where: { organizationId: orgId } })
|
||||
})
|
||||
})
|
||||
await prisma.generalSettings.deleteMany({
|
||||
where: { organizationId: orgId },
|
||||
});
|
||||
await prisma.smtpSettings.deleteMany({ where: { organizationId: orgId } });
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,101 +1,101 @@
|
||||
import { describe, it, expect } from "vitest"
|
||||
import { createUser } from "@helpers/user/user"
|
||||
import { request } from "@helpers/request"
|
||||
import { createList } from "@tests/integration/helpers/list/list"
|
||||
import { prisma } from "@src/utils/prisma"
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { createUser } from "@helpers/user/user";
|
||||
import { request } from "@helpers/request";
|
||||
import { createList } from "@tests/integration/helpers/list/list";
|
||||
import { prisma } from "@src/utils/prisma";
|
||||
|
||||
describe("[DELETE] /api/subscribers/:id", () => {
|
||||
it("should delete a subscriber", async () => {
|
||||
const {
|
||||
apiKey: { key: apiKey },
|
||||
orgId,
|
||||
} = await createUser()
|
||||
it("should delete a subscriber", async () => {
|
||||
const {
|
||||
apiKey: { key: apiKey },
|
||||
orgId,
|
||||
} = await createUser();
|
||||
|
||||
const subscriber = await prisma.subscriber.create({
|
||||
data: {
|
||||
email: "test@test.com",
|
||||
organizationId: orgId,
|
||||
},
|
||||
})
|
||||
const subscriber = await prisma.subscriber.create({
|
||||
data: {
|
||||
email: "test@test.com",
|
||||
organizationId: orgId,
|
||||
},
|
||||
});
|
||||
|
||||
const response = await request
|
||||
.delete(`/api/subscribers/${subscriber.id}`)
|
||||
.set("x-api-key", apiKey)
|
||||
const response = await request
|
||||
.delete(`/api/subscribers/${subscriber.id}`)
|
||||
.set("x-api-key", apiKey);
|
||||
|
||||
expect(response.status).toBe(200)
|
||||
expect(response.body.success).toBe(true)
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.body.success).toBe(true);
|
||||
|
||||
const deletedSubscriber = await prisma.subscriber.findUnique({
|
||||
where: { id: subscriber.id },
|
||||
})
|
||||
expect(deletedSubscriber).toBeNull()
|
||||
})
|
||||
const deletedSubscriber = await prisma.subscriber.findUnique({
|
||||
where: { id: subscriber.id },
|
||||
});
|
||||
expect(deletedSubscriber).toBeNull();
|
||||
});
|
||||
|
||||
it("should delete subscriber and associated list subscriptions", async () => {
|
||||
const {
|
||||
apiKey: { key: apiKey },
|
||||
orgId,
|
||||
} = await createUser()
|
||||
it("should delete subscriber and associated list subscriptions", async () => {
|
||||
const {
|
||||
apiKey: { key: apiKey },
|
||||
orgId,
|
||||
} = await createUser();
|
||||
|
||||
const list = await createList({
|
||||
name: "Test List",
|
||||
organizationId: orgId,
|
||||
})
|
||||
const list = await createList({
|
||||
name: "Test List",
|
||||
organizationId: orgId,
|
||||
});
|
||||
|
||||
const subscriber = await prisma.subscriber.create({
|
||||
data: {
|
||||
email: "test@test.com",
|
||||
organizationId: orgId,
|
||||
ListSubscribers: {
|
||||
create: {
|
||||
List: { connect: { id: list.id } },
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
const subscriber = await prisma.subscriber.create({
|
||||
data: {
|
||||
email: "test@test.com",
|
||||
organizationId: orgId,
|
||||
ListSubscribers: {
|
||||
create: {
|
||||
List: { connect: { id: list.id } },
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const response = await request
|
||||
.delete(`/api/subscribers/${subscriber.id}`)
|
||||
.set("x-api-key", apiKey)
|
||||
const response = await request
|
||||
.delete(`/api/subscribers/${subscriber.id}`)
|
||||
.set("x-api-key", apiKey);
|
||||
|
||||
expect(response.status).toBe(200)
|
||||
expect(response.status).toBe(200);
|
||||
|
||||
const listSubscriptions = await prisma.listSubscriber.findMany({
|
||||
where: { subscriberId: subscriber.id },
|
||||
})
|
||||
expect(listSubscriptions).toHaveLength(0)
|
||||
})
|
||||
const listSubscriptions = await prisma.listSubscriber.findMany({
|
||||
where: { subscriberId: subscriber.id },
|
||||
});
|
||||
expect(listSubscriptions).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("should return 404 for non-existent subscriber", async () => {
|
||||
const {
|
||||
apiKey: { key: apiKey },
|
||||
} = await createUser()
|
||||
it("should return 404 for non-existent subscriber", async () => {
|
||||
const {
|
||||
apiKey: { key: apiKey },
|
||||
} = await createUser();
|
||||
|
||||
const response = await request
|
||||
.delete(`/api/subscribers/non-existent-id`)
|
||||
.set("x-api-key", apiKey)
|
||||
const response = await request
|
||||
.delete(`/api/subscribers/non-existent-id`)
|
||||
.set("x-api-key", apiKey);
|
||||
|
||||
expect(response.status).toBe(404)
|
||||
expect(response.body.error).toBe("Subscriber not found")
|
||||
})
|
||||
expect(response.status).toBe(404);
|
||||
expect(response.body.error).toBe("Subscriber not found");
|
||||
});
|
||||
|
||||
it("should return 404 when trying to delete subscriber from another organization", async () => {
|
||||
const {
|
||||
apiKey: { key: apiKey },
|
||||
} = await createUser()
|
||||
const { orgId: otherOrgId } = await createUser()
|
||||
it("should return 404 when trying to delete subscriber from another organization", async () => {
|
||||
const {
|
||||
apiKey: { key: apiKey },
|
||||
} = await createUser();
|
||||
const { orgId: otherOrgId } = await createUser();
|
||||
|
||||
const subscriber = await prisma.subscriber.create({
|
||||
data: {
|
||||
email: "test@test.com",
|
||||
organizationId: otherOrgId,
|
||||
},
|
||||
})
|
||||
const subscriber = await prisma.subscriber.create({
|
||||
data: {
|
||||
email: "test@test.com",
|
||||
organizationId: otherOrgId,
|
||||
},
|
||||
});
|
||||
|
||||
const response = await request
|
||||
.delete(`/api/subscribers/${subscriber.id}`)
|
||||
.set("x-api-key", apiKey)
|
||||
const response = await request
|
||||
.delete(`/api/subscribers/${subscriber.id}`)
|
||||
.set("x-api-key", apiKey);
|
||||
|
||||
expect(response.status).toBe(404)
|
||||
})
|
||||
})
|
||||
expect(response.status).toBe(404);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,101 +1,101 @@
|
||||
import { describe, it, expect } from "vitest"
|
||||
import { createUser } from "@helpers/user/user"
|
||||
import { request } from "@helpers/request"
|
||||
import { createList } from "@tests/integration/helpers/list/list"
|
||||
import { prisma } from "@src/utils/prisma"
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { createUser } from "@helpers/user/user";
|
||||
import { request } from "@helpers/request";
|
||||
import { createList } from "@tests/integration/helpers/list/list";
|
||||
import { prisma } from "@src/utils/prisma";
|
||||
|
||||
describe("[GET] /api/subscribers/:id", () => {
|
||||
it("should get a subscriber by id", async () => {
|
||||
const {
|
||||
apiKey: { key: apiKey },
|
||||
orgId,
|
||||
} = await createUser()
|
||||
it("should get a subscriber by id", async () => {
|
||||
const {
|
||||
apiKey: { key: apiKey },
|
||||
orgId,
|
||||
} = await createUser();
|
||||
|
||||
const subscriber = await prisma.subscriber.create({
|
||||
data: {
|
||||
email: "test@test.com",
|
||||
name: "Test User",
|
||||
organizationId: orgId,
|
||||
},
|
||||
})
|
||||
const subscriber = await prisma.subscriber.create({
|
||||
data: {
|
||||
email: "test@test.com",
|
||||
name: "Test User",
|
||||
organizationId: orgId,
|
||||
},
|
||||
});
|
||||
|
||||
const response = await request
|
||||
.get(`/api/subscribers/${subscriber.id}`)
|
||||
.set("x-api-key", apiKey)
|
||||
const response = await request
|
||||
.get(`/api/subscribers/${subscriber.id}`)
|
||||
.set("x-api-key", apiKey);
|
||||
|
||||
expect(response.status).toBe(200)
|
||||
expect(response.body.id).toBe(subscriber.id)
|
||||
expect(response.body.email).toBe("test@test.com")
|
||||
expect(response.body.name).toBe("Test User")
|
||||
})
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.body.id).toBe(subscriber.id);
|
||||
expect(response.body.email).toBe("test@test.com");
|
||||
expect(response.body.name).toBe("Test User");
|
||||
});
|
||||
|
||||
it("should get subscriber with lists", async () => {
|
||||
const {
|
||||
apiKey: { key: apiKey },
|
||||
orgId,
|
||||
} = await createUser()
|
||||
it("should get subscriber with lists", async () => {
|
||||
const {
|
||||
apiKey: { key: apiKey },
|
||||
orgId,
|
||||
} = await createUser();
|
||||
|
||||
const list = await createList({
|
||||
name: "Test List",
|
||||
description: "Test Description",
|
||||
organizationId: orgId,
|
||||
})
|
||||
const list = await createList({
|
||||
name: "Test List",
|
||||
description: "Test Description",
|
||||
organizationId: orgId,
|
||||
});
|
||||
|
||||
const subscriber = await prisma.subscriber.create({
|
||||
data: {
|
||||
email: "test@test.com",
|
||||
organizationId: orgId,
|
||||
ListSubscribers: {
|
||||
create: {
|
||||
List: { connect: { id: list.id } },
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
const subscriber = await prisma.subscriber.create({
|
||||
data: {
|
||||
email: "test@test.com",
|
||||
organizationId: orgId,
|
||||
ListSubscribers: {
|
||||
create: {
|
||||
List: { connect: { id: list.id } },
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const response = await request
|
||||
.get(`/api/subscribers/${subscriber.id}`)
|
||||
.set("x-api-key", apiKey)
|
||||
const response = await request
|
||||
.get(`/api/subscribers/${subscriber.id}`)
|
||||
.set("x-api-key", apiKey);
|
||||
|
||||
expect(response.status).toBe(200)
|
||||
expect(response.body.lists).toHaveLength(1)
|
||||
expect(response.body.lists[0]).toEqual({
|
||||
id: list.id,
|
||||
name: "Test List",
|
||||
description: "Test Description",
|
||||
})
|
||||
})
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.body.lists).toHaveLength(1);
|
||||
expect(response.body.lists[0]).toEqual({
|
||||
id: list.id,
|
||||
name: "Test List",
|
||||
description: "Test Description",
|
||||
});
|
||||
});
|
||||
|
||||
it("should return 404 for non-existent subscriber", async () => {
|
||||
const {
|
||||
apiKey: { key: apiKey },
|
||||
} = await createUser()
|
||||
it("should return 404 for non-existent subscriber", async () => {
|
||||
const {
|
||||
apiKey: { key: apiKey },
|
||||
} = await createUser();
|
||||
|
||||
const response = await request
|
||||
.get(`/api/subscribers/non-existent-id`)
|
||||
.set("x-api-key", apiKey)
|
||||
const response = await request
|
||||
.get(`/api/subscribers/non-existent-id`)
|
||||
.set("x-api-key", apiKey);
|
||||
|
||||
expect(response.status).toBe(404)
|
||||
expect(response.body.error).toBe("Subscriber not found")
|
||||
})
|
||||
expect(response.status).toBe(404);
|
||||
expect(response.body.error).toBe("Subscriber not found");
|
||||
});
|
||||
|
||||
it("should return 404 when trying to get subscriber from another organization", async () => {
|
||||
const {
|
||||
apiKey: { key: apiKey },
|
||||
} = await createUser()
|
||||
const { orgId: otherOrgId } = await createUser()
|
||||
it("should return 404 when trying to get subscriber from another organization", async () => {
|
||||
const {
|
||||
apiKey: { key: apiKey },
|
||||
} = await createUser();
|
||||
const { orgId: otherOrgId } = await createUser();
|
||||
|
||||
const subscriber = await prisma.subscriber.create({
|
||||
data: {
|
||||
email: "test@test.com",
|
||||
organizationId: otherOrgId,
|
||||
},
|
||||
})
|
||||
const subscriber = await prisma.subscriber.create({
|
||||
data: {
|
||||
email: "test@test.com",
|
||||
organizationId: otherOrgId,
|
||||
},
|
||||
});
|
||||
|
||||
const response = await request
|
||||
.get(`/api/subscribers/${subscriber.id}`)
|
||||
.set("x-api-key", apiKey)
|
||||
const response = await request
|
||||
.get(`/api/subscribers/${subscriber.id}`)
|
||||
.set("x-api-key", apiKey);
|
||||
|
||||
expect(response.status).toBe(404)
|
||||
})
|
||||
})
|
||||
expect(response.status).toBe(404);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,181 +1,181 @@
|
||||
import { describe, it, expect } from "vitest"
|
||||
import { createUser } from "@helpers/user/user"
|
||||
import { request } from "@helpers/request"
|
||||
import { createList } from "@tests/integration/helpers/list/list"
|
||||
import { prisma } from "@src/utils/prisma"
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { createUser } from "@helpers/user/user";
|
||||
import { request } from "@helpers/request";
|
||||
import { createList } from "@tests/integration/helpers/list/list";
|
||||
import { prisma } from "@src/utils/prisma";
|
||||
|
||||
describe("[GET] /api/subscribers", () => {
|
||||
it("should get subscribers with pagination", async () => {
|
||||
const {
|
||||
apiKey: { key: apiKey },
|
||||
orgId,
|
||||
} = await createUser()
|
||||
it("should get subscribers with pagination", async () => {
|
||||
const {
|
||||
apiKey: { key: apiKey },
|
||||
orgId,
|
||||
} = await createUser();
|
||||
|
||||
await Promise.all(
|
||||
Array.from({ length: 15 }, (_, i) =>
|
||||
prisma.subscriber.create({
|
||||
data: {
|
||||
email: `test${i}@test.com`,
|
||||
name: `Test User ${i}`,
|
||||
organizationId: orgId,
|
||||
},
|
||||
})
|
||||
)
|
||||
)
|
||||
await Promise.all(
|
||||
Array.from({ length: 15 }, (_, i) =>
|
||||
prisma.subscriber.create({
|
||||
data: {
|
||||
email: `test${i}@test.com`,
|
||||
name: `Test User ${i}`,
|
||||
organizationId: orgId,
|
||||
},
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
const response = await request
|
||||
.get("/api/subscribers?page=1&perPage=10")
|
||||
.set("x-api-key", apiKey)
|
||||
const response = await request
|
||||
.get("/api/subscribers?page=1&perPage=10")
|
||||
.set("x-api-key", apiKey);
|
||||
|
||||
expect(response.status).toBe(200)
|
||||
expect(response.body.data).toHaveLength(10)
|
||||
expect(response.body.pagination).toEqual({
|
||||
total: 15,
|
||||
page: 1,
|
||||
perPage: 10,
|
||||
totalPages: 2,
|
||||
hasMore: true,
|
||||
})
|
||||
})
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.body.data).toHaveLength(10);
|
||||
expect(response.body.pagination).toEqual({
|
||||
total: 15,
|
||||
page: 1,
|
||||
perPage: 10,
|
||||
totalPages: 2,
|
||||
hasMore: true,
|
||||
});
|
||||
});
|
||||
|
||||
it("should get subscribers with lists", async () => {
|
||||
const {
|
||||
apiKey: { key: apiKey },
|
||||
orgId,
|
||||
} = await createUser()
|
||||
it("should get subscribers with lists", async () => {
|
||||
const {
|
||||
apiKey: { key: apiKey },
|
||||
orgId,
|
||||
} = await createUser();
|
||||
|
||||
const list = await createList({
|
||||
name: "Test List",
|
||||
description: "Test Description",
|
||||
organizationId: orgId,
|
||||
})
|
||||
const list = await createList({
|
||||
name: "Test List",
|
||||
description: "Test Description",
|
||||
organizationId: orgId,
|
||||
});
|
||||
|
||||
await prisma.subscriber.create({
|
||||
data: {
|
||||
email: "test@test.com",
|
||||
name: "Test User",
|
||||
organizationId: orgId,
|
||||
ListSubscribers: {
|
||||
create: {
|
||||
List: { connect: { id: list.id } },
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
await prisma.subscriber.create({
|
||||
data: {
|
||||
email: "test@test.com",
|
||||
name: "Test User",
|
||||
organizationId: orgId,
|
||||
ListSubscribers: {
|
||||
create: {
|
||||
List: { connect: { id: list.id } },
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const response = await request
|
||||
.get("/api/subscribers")
|
||||
.set("x-api-key", apiKey)
|
||||
const response = await request
|
||||
.get("/api/subscribers")
|
||||
.set("x-api-key", apiKey);
|
||||
|
||||
expect(response.status).toBe(200)
|
||||
expect(response.body.data[0].lists).toHaveLength(1)
|
||||
expect(response.body.data[0].lists[0]).toEqual({
|
||||
id: list.id,
|
||||
name: "Test List",
|
||||
description: "Test Description",
|
||||
})
|
||||
})
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.body.data[0].lists).toHaveLength(1);
|
||||
expect(response.body.data[0].lists[0]).toEqual({
|
||||
id: list.id,
|
||||
name: "Test List",
|
||||
description: "Test Description",
|
||||
});
|
||||
});
|
||||
|
||||
it("should filter subscribers by exact email", async () => {
|
||||
const {
|
||||
apiKey: { key: apiKey },
|
||||
orgId,
|
||||
} = await createUser()
|
||||
it("should filter subscribers by exact email", async () => {
|
||||
const {
|
||||
apiKey: { key: apiKey },
|
||||
orgId,
|
||||
} = await createUser();
|
||||
|
||||
await prisma.subscriber.createMany({
|
||||
data: [
|
||||
{
|
||||
email: "test1@test.com",
|
||||
name: "Test User 1",
|
||||
organizationId: orgId,
|
||||
},
|
||||
{
|
||||
email: "test2@test.com",
|
||||
name: "Test User 2",
|
||||
organizationId: orgId,
|
||||
},
|
||||
],
|
||||
})
|
||||
await prisma.subscriber.createMany({
|
||||
data: [
|
||||
{
|
||||
email: "test1@test.com",
|
||||
name: "Test User 1",
|
||||
organizationId: orgId,
|
||||
},
|
||||
{
|
||||
email: "test2@test.com",
|
||||
name: "Test User 2",
|
||||
organizationId: orgId,
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const response = await request
|
||||
.get("/api/subscribers?emailEquals=test1@test.com")
|
||||
.set("x-api-key", apiKey)
|
||||
const response = await request
|
||||
.get("/api/subscribers?emailEquals=test1@test.com")
|
||||
.set("x-api-key", apiKey);
|
||||
|
||||
expect(response.status).toBe(200)
|
||||
expect(response.body.data).toHaveLength(1)
|
||||
expect(response.body.data[0].email).toBe("test1@test.com")
|
||||
})
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.body.data).toHaveLength(1);
|
||||
expect(response.body.data[0].email).toBe("test1@test.com");
|
||||
});
|
||||
|
||||
it("should filter subscribers by exact name", async () => {
|
||||
const {
|
||||
apiKey: { key: apiKey },
|
||||
orgId,
|
||||
} = await createUser()
|
||||
it("should filter subscribers by exact name", async () => {
|
||||
const {
|
||||
apiKey: { key: apiKey },
|
||||
orgId,
|
||||
} = await createUser();
|
||||
|
||||
await prisma.subscriber.createMany({
|
||||
data: [
|
||||
{
|
||||
email: "test1@test.com",
|
||||
name: "Test User 1",
|
||||
organizationId: orgId,
|
||||
},
|
||||
{
|
||||
email: "test2@test.com",
|
||||
name: "Test User 2",
|
||||
organizationId: orgId,
|
||||
},
|
||||
],
|
||||
})
|
||||
await prisma.subscriber.createMany({
|
||||
data: [
|
||||
{
|
||||
email: "test1@test.com",
|
||||
name: "Test User 1",
|
||||
organizationId: orgId,
|
||||
},
|
||||
{
|
||||
email: "test2@test.com",
|
||||
name: "Test User 2",
|
||||
organizationId: orgId,
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const response = await request
|
||||
.get("/api/subscribers?nameEquals=Test User 1")
|
||||
.set("x-api-key", apiKey)
|
||||
const response = await request
|
||||
.get("/api/subscribers?nameEquals=Test User 1")
|
||||
.set("x-api-key", apiKey);
|
||||
|
||||
expect(response.status).toBe(200)
|
||||
expect(response.body.data).toHaveLength(1)
|
||||
expect(response.body.data[0].name).toBe("Test User 1")
|
||||
})
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.body.data).toHaveLength(1);
|
||||
expect(response.body.data[0].name).toBe("Test User 1");
|
||||
});
|
||||
|
||||
it("should only return subscribers from the authenticated organization", async () => {
|
||||
const {
|
||||
apiKey: { key: apiKey },
|
||||
orgId,
|
||||
} = await createUser()
|
||||
const { orgId: otherOrgId } = await createUser()
|
||||
it("should only return subscribers from the authenticated organization", async () => {
|
||||
const {
|
||||
apiKey: { key: apiKey },
|
||||
orgId,
|
||||
} = await createUser();
|
||||
const { orgId: otherOrgId } = await createUser();
|
||||
|
||||
await prisma.subscriber.create({
|
||||
data: {
|
||||
email: "test1@test.com",
|
||||
organizationId: orgId,
|
||||
},
|
||||
})
|
||||
await prisma.subscriber.create({
|
||||
data: {
|
||||
email: "test1@test.com",
|
||||
organizationId: orgId,
|
||||
},
|
||||
});
|
||||
|
||||
await prisma.subscriber.create({
|
||||
data: {
|
||||
email: "test2@test.com",
|
||||
organizationId: otherOrgId,
|
||||
},
|
||||
})
|
||||
await prisma.subscriber.create({
|
||||
data: {
|
||||
email: "test2@test.com",
|
||||
organizationId: otherOrgId,
|
||||
},
|
||||
});
|
||||
|
||||
const response = await request
|
||||
.get("/api/subscribers")
|
||||
.set("x-api-key", apiKey)
|
||||
const response = await request
|
||||
.get("/api/subscribers")
|
||||
.set("x-api-key", apiKey);
|
||||
|
||||
expect(response.status).toBe(200)
|
||||
expect(response.body.data).toHaveLength(1)
|
||||
expect(response.body.data[0].email).toBe("test1@test.com")
|
||||
})
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.body.data).toHaveLength(1);
|
||||
expect(response.body.data[0].email).toBe("test1@test.com");
|
||||
});
|
||||
|
||||
it("should return 400 for invalid query parameters", async () => {
|
||||
const {
|
||||
apiKey: { key: apiKey },
|
||||
} = await createUser()
|
||||
it("should return 400 for invalid query parameters", async () => {
|
||||
const {
|
||||
apiKey: { key: apiKey },
|
||||
} = await createUser();
|
||||
|
||||
const response = await request
|
||||
.get("/api/subscribers?page=invalid")
|
||||
.set("x-api-key", apiKey)
|
||||
const response = await request
|
||||
.get("/api/subscribers?page=invalid")
|
||||
.set("x-api-key", apiKey);
|
||||
|
||||
expect(response.status).toBe(400)
|
||||
expect(response.body.error).toBeDefined()
|
||||
})
|
||||
})
|
||||
expect(response.status).toBe(400);
|
||||
expect(response.body.error).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,198 +1,198 @@
|
||||
import { describe, it, expect } from "vitest"
|
||||
import { createUser } from "@helpers/user/user"
|
||||
import { request } from "@helpers/request"
|
||||
import { createList } from "@tests/integration/helpers/list/list"
|
||||
import { prisma } from "@src/utils/prisma"
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { createUser } from "@helpers/user/user";
|
||||
import { request } from "@helpers/request";
|
||||
import { createList } from "@tests/integration/helpers/list/list";
|
||||
import { prisma } from "@src/utils/prisma";
|
||||
|
||||
describe("[PUT] /api/subscribers/:id", () => {
|
||||
it("should update subscriber details", async () => {
|
||||
const {
|
||||
apiKey: { key: apiKey },
|
||||
orgId,
|
||||
} = await createUser()
|
||||
it("should update subscriber details", async () => {
|
||||
const {
|
||||
apiKey: { key: apiKey },
|
||||
orgId,
|
||||
} = await createUser();
|
||||
|
||||
const list = await createList({
|
||||
name: "Test List",
|
||||
organizationId: orgId,
|
||||
})
|
||||
const list = await createList({
|
||||
name: "Test List",
|
||||
organizationId: orgId,
|
||||
});
|
||||
|
||||
const subscriber = await prisma.subscriber.create({
|
||||
data: {
|
||||
email: "test@test.com",
|
||||
organizationId: orgId,
|
||||
ListSubscribers: {
|
||||
create: {
|
||||
List: {
|
||||
connect: {
|
||||
id: list.id,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
const subscriber = await prisma.subscriber.create({
|
||||
data: {
|
||||
email: "test@test.com",
|
||||
organizationId: orgId,
|
||||
ListSubscribers: {
|
||||
create: {
|
||||
List: {
|
||||
connect: {
|
||||
id: list.id,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const response = await request
|
||||
.put(`/api/subscribers/${subscriber.id}`)
|
||||
.set("x-api-key", apiKey)
|
||||
.send({
|
||||
email: "updated@test.com",
|
||||
name: "Updated Name",
|
||||
})
|
||||
const response = await request
|
||||
.put(`/api/subscribers/${subscriber.id}`)
|
||||
.set("x-api-key", apiKey)
|
||||
.send({
|
||||
email: "updated@test.com",
|
||||
name: "Updated Name",
|
||||
});
|
||||
|
||||
expect(response.status).toBe(200)
|
||||
expect(response.body.email).toBe("updated@test.com")
|
||||
expect(response.body.name).toBe("Updated Name")
|
||||
})
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.body.email).toBe("updated@test.com");
|
||||
expect(response.body.name).toBe("Updated Name");
|
||||
});
|
||||
|
||||
it("should update subscriber lists", async () => {
|
||||
const {
|
||||
apiKey: { key: apiKey },
|
||||
orgId,
|
||||
} = await createUser()
|
||||
it("should update subscriber lists", async () => {
|
||||
const {
|
||||
apiKey: { key: apiKey },
|
||||
orgId,
|
||||
} = await createUser();
|
||||
|
||||
const list1 = await createList({
|
||||
name: "List 1",
|
||||
organizationId: orgId,
|
||||
})
|
||||
const list1 = await createList({
|
||||
name: "List 1",
|
||||
organizationId: orgId,
|
||||
});
|
||||
|
||||
const list2 = await createList({
|
||||
name: "List 2",
|
||||
organizationId: orgId,
|
||||
})
|
||||
const list2 = await createList({
|
||||
name: "List 2",
|
||||
organizationId: orgId,
|
||||
});
|
||||
|
||||
const subscriber = await prisma.subscriber.create({
|
||||
data: {
|
||||
email: "test@test.com",
|
||||
organizationId: orgId,
|
||||
ListSubscribers: {
|
||||
create: {
|
||||
List: {
|
||||
connect: {
|
||||
id: list1.id,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
const subscriber = await prisma.subscriber.create({
|
||||
data: {
|
||||
email: "test@test.com",
|
||||
organizationId: orgId,
|
||||
ListSubscribers: {
|
||||
create: {
|
||||
List: {
|
||||
connect: {
|
||||
id: list1.id,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const response = await request
|
||||
.put(`/api/subscribers/${subscriber.id}`)
|
||||
.set("x-api-key", apiKey)
|
||||
.send({
|
||||
email: "test@test.com",
|
||||
lists: [list2.id],
|
||||
})
|
||||
const response = await request
|
||||
.put(`/api/subscribers/${subscriber.id}`)
|
||||
.set("x-api-key", apiKey)
|
||||
.send({
|
||||
email: "test@test.com",
|
||||
lists: [list2.id],
|
||||
});
|
||||
|
||||
expect(response.status).toBe(200)
|
||||
expect(response.body.lists).toHaveLength(1)
|
||||
expect(response.body.lists[0].id).toBe(list2.id)
|
||||
})
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.body.lists).toHaveLength(1);
|
||||
expect(response.body.lists[0].id).toBe(list2.id);
|
||||
});
|
||||
|
||||
it("should return 404 for non-existent subscriber", async () => {
|
||||
const {
|
||||
apiKey: { key: apiKey },
|
||||
} = await createUser()
|
||||
it("should return 404 for non-existent subscriber", async () => {
|
||||
const {
|
||||
apiKey: { key: apiKey },
|
||||
} = await createUser();
|
||||
|
||||
const response = await request
|
||||
.put(`/api/subscribers/non-existent-id`)
|
||||
.set("x-api-key", apiKey)
|
||||
.send({
|
||||
email: "test@test.com",
|
||||
})
|
||||
const response = await request
|
||||
.put(`/api/subscribers/non-existent-id`)
|
||||
.set("x-api-key", apiKey)
|
||||
.send({
|
||||
email: "test@test.com",
|
||||
});
|
||||
|
||||
expect(response.status).toBe(404)
|
||||
})
|
||||
expect(response.status).toBe(404);
|
||||
});
|
||||
|
||||
it("should keep existing lists when lists not provided in update", async () => {
|
||||
const {
|
||||
apiKey: { key: apiKey },
|
||||
orgId,
|
||||
} = await createUser()
|
||||
it("should keep existing lists when lists not provided in update", async () => {
|
||||
const {
|
||||
apiKey: { key: apiKey },
|
||||
orgId,
|
||||
} = await createUser();
|
||||
|
||||
const list = await createList({
|
||||
name: "Test List",
|
||||
organizationId: orgId,
|
||||
})
|
||||
const list = await createList({
|
||||
name: "Test List",
|
||||
organizationId: orgId,
|
||||
});
|
||||
|
||||
const subscriber = await prisma.subscriber.create({
|
||||
data: {
|
||||
email: "test@test.com",
|
||||
organizationId: orgId,
|
||||
ListSubscribers: {
|
||||
create: {
|
||||
List: { connect: { id: list.id } },
|
||||
},
|
||||
},
|
||||
},
|
||||
include: {
|
||||
ListSubscribers: { include: { List: true } },
|
||||
},
|
||||
})
|
||||
const subscriber = await prisma.subscriber.create({
|
||||
data: {
|
||||
email: "test@test.com",
|
||||
organizationId: orgId,
|
||||
ListSubscribers: {
|
||||
create: {
|
||||
List: { connect: { id: list.id } },
|
||||
},
|
||||
},
|
||||
},
|
||||
include: {
|
||||
ListSubscribers: { include: { List: true } },
|
||||
},
|
||||
});
|
||||
|
||||
const response = await request
|
||||
.put(`/api/subscribers/${subscriber.id}`)
|
||||
.set("x-api-key", apiKey)
|
||||
.send({
|
||||
name: "Updated Name",
|
||||
})
|
||||
const response = await request
|
||||
.put(`/api/subscribers/${subscriber.id}`)
|
||||
.set("x-api-key", apiKey)
|
||||
.send({
|
||||
name: "Updated Name",
|
||||
});
|
||||
|
||||
expect(response.status).toBe(200)
|
||||
expect(response.body.email).toBe("test@test.com")
|
||||
expect(response.body.name).toBe("Updated Name")
|
||||
expect(response.body.lists).toHaveLength(1)
|
||||
expect(response.body.lists[0].id).toBe(list.id)
|
||||
})
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.body.email).toBe("test@test.com");
|
||||
expect(response.body.name).toBe("Updated Name");
|
||||
expect(response.body.lists).toHaveLength(1);
|
||||
expect(response.body.lists[0].id).toBe(list.id);
|
||||
});
|
||||
|
||||
it("should keep existing name when name not provided in update", async () => {
|
||||
const {
|
||||
apiKey: { key: apiKey },
|
||||
orgId,
|
||||
} = await createUser()
|
||||
it("should keep existing name when name not provided in update", async () => {
|
||||
const {
|
||||
apiKey: { key: apiKey },
|
||||
orgId,
|
||||
} = await createUser();
|
||||
|
||||
const subscriber = await prisma.subscriber.create({
|
||||
data: {
|
||||
email: "test@test.com",
|
||||
name: "Original Name",
|
||||
organizationId: orgId,
|
||||
},
|
||||
})
|
||||
const subscriber = await prisma.subscriber.create({
|
||||
data: {
|
||||
email: "test@test.com",
|
||||
name: "Original Name",
|
||||
organizationId: orgId,
|
||||
},
|
||||
});
|
||||
|
||||
const response = await request
|
||||
.put(`/api/subscribers/${subscriber.id}`)
|
||||
.set("x-api-key", apiKey)
|
||||
.send({
|
||||
email: "updated@test.com",
|
||||
})
|
||||
const response = await request
|
||||
.put(`/api/subscribers/${subscriber.id}`)
|
||||
.set("x-api-key", apiKey)
|
||||
.send({
|
||||
email: "updated@test.com",
|
||||
});
|
||||
|
||||
expect(response.status).toBe(200)
|
||||
expect(response.body.email).toBe("updated@test.com")
|
||||
expect(response.body.name).toBe("Original Name")
|
||||
})
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.body.email).toBe("updated@test.com");
|
||||
expect(response.body.name).toBe("Original Name");
|
||||
});
|
||||
|
||||
it("should keep existing email when email not provided in update", async () => {
|
||||
const {
|
||||
apiKey: { key: apiKey },
|
||||
orgId,
|
||||
} = await createUser()
|
||||
it("should keep existing email when email not provided in update", async () => {
|
||||
const {
|
||||
apiKey: { key: apiKey },
|
||||
orgId,
|
||||
} = await createUser();
|
||||
|
||||
const subscriber = await prisma.subscriber.create({
|
||||
data: {
|
||||
email: "test@test.com",
|
||||
organizationId: orgId,
|
||||
},
|
||||
})
|
||||
const subscriber = await prisma.subscriber.create({
|
||||
data: {
|
||||
email: "test@test.com",
|
||||
organizationId: orgId,
|
||||
},
|
||||
});
|
||||
|
||||
const response = await request
|
||||
.put(`/api/subscribers/${subscriber.id}`)
|
||||
.set("x-api-key", apiKey)
|
||||
.send({
|
||||
name: "New Name",
|
||||
})
|
||||
const response = await request
|
||||
.put(`/api/subscribers/${subscriber.id}`)
|
||||
.set("x-api-key", apiKey)
|
||||
.send({
|
||||
name: "New Name",
|
||||
});
|
||||
|
||||
expect(response.status).toBe(200)
|
||||
expect(response.body.email).toBe("test@test.com")
|
||||
expect(response.body.name).toBe("New Name")
|
||||
})
|
||||
})
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.body.email).toBe("test@test.com");
|
||||
expect(response.body.name).toBe("New Name");
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user