-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #110 from arnaugomez/feat/tests-services
Feat/tests-services
- Loading branch information
Showing
12 changed files
with
205 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/ai-generator/data/services/ai-notes-generator-service-fake-impl.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { describe, expect, it } from "vitest"; | ||
import { AiNotesGeneratorSourceType } from "../../domain/models/ai-notes-generator-source-type"; | ||
import { AiNotesGeneratorServiceFakeImpl } from "./ai-notes-generator-service-fake-impl"; | ||
|
||
describe("AiNotesGeneratorServiceFakeImpl", () => { | ||
it("generate returns a non-empty list of notes regardless of the input", async () => { | ||
const aiNotesGeneratorServiceFakeImpl = | ||
new AiNotesGeneratorServiceFakeImpl(); | ||
|
||
await expect( | ||
aiNotesGeneratorServiceFakeImpl.generate({ | ||
notesCount: 0, | ||
noteTypes: [], | ||
sourceType: AiNotesGeneratorSourceType.file, | ||
text: "", | ||
}), | ||
).resolves.not.toHaveLength(0); | ||
await expect( | ||
aiNotesGeneratorServiceFakeImpl.generate({ | ||
notesCount: 10000, | ||
noteTypes: [], | ||
sourceType: AiNotesGeneratorSourceType.text, | ||
text: "Example Test", | ||
}), | ||
).resolves.not.toHaveLength(0); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { cookies } from "next/headers"; | ||
import { afterEach, describe, expect, it, vi } from "vitest"; | ||
import { CookieServiceNextImpl } from "@/src/common/data/services/cookie-service-next-impl"; | ||
|
||
vi.mock("next/headers", () => { | ||
const mockGet = vi.fn(); | ||
const mockSet = vi.fn(); | ||
return { | ||
cookies: () => ({ | ||
get: mockGet, | ||
set: mockSet, | ||
}), | ||
}; | ||
}); | ||
|
||
describe("CookieServiceNextImpl", () => { | ||
afterEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
describe("get method", () => { | ||
it("returns the cookie value", () => { | ||
const cookieService = new CookieServiceNextImpl(); | ||
const cookieName = "test-cookie-name"; | ||
const cookieValue = "Test cookie value"; | ||
vi.mocked(cookies().get).mockReturnValue({ | ||
name: cookieName, | ||
value: cookieValue, | ||
}); | ||
|
||
expect(cookieService.get(cookieName)).toBe(cookieValue); | ||
expect(cookies().get).toHaveBeenCalledWith(cookieName); | ||
}); | ||
|
||
it("returns undefined when the cookie does not exist", () => { | ||
const cookieService = new CookieServiceNextImpl(); | ||
const cookieName = "test-cookie-name-2"; | ||
vi.mocked(cookies().get).mockReturnValue(undefined); | ||
|
||
expect(cookieService.get(cookieName)).toBeUndefined(); | ||
expect(cookies().get).toHaveBeenCalledWith(cookieName); | ||
}); | ||
}); | ||
|
||
describe("set method", () => { | ||
it("should set the cookie with correct attributes", () => { | ||
const cookieService = new CookieServiceNextImpl(); | ||
const cookieName = "test-cookie-name-3"; | ||
const cookieValue = "Test Cookie Value 3"; | ||
const attributes = { path: "/", maxAge: 3600 }; | ||
|
||
cookieService.set({ name: cookieName, value: cookieValue, attributes }); | ||
|
||
expect(cookies().set).toHaveBeenCalledWith( | ||
cookieName, | ||
cookieValue, | ||
attributes, | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/common/data/services/email-service-resend-impl.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { afterEach } from "node:test"; | ||
import { Resend } from "resend"; | ||
import { describe, expect, it, vi } from "vitest"; | ||
import { locator } from "../../di/locator"; | ||
import { EmailServiceResendImpl } from "./email-service-resend-impl"; | ||
|
||
vi.mock("resend", () => { | ||
const sendMock = vi.fn(); | ||
class ResendMock { | ||
emails = { | ||
send: sendMock, | ||
}; | ||
} | ||
return { Resend: ResendMock }; | ||
}); | ||
|
||
describe("EmailServiceResendImpl", () => { | ||
afterEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
it("sendVerificationCode sends an email with a verfication code", async () => { | ||
const email = "test@example.com"; | ||
const verificationCode = "123456"; | ||
|
||
const envService = locator.EnvService(); | ||
|
||
const emailService = new EmailServiceResendImpl(envService); | ||
await emailService.sendVerificationCode(email, verificationCode); | ||
|
||
expect(new Resend().emails.send).toHaveBeenCalledWith({ | ||
from: "El equipo de clubmemo <noreply@app.clubmemo.com>", | ||
to: email, | ||
subject: "¡Bienvenido a clubmemo! Verifica tu email.", | ||
html: expect.stringContaining(verificationCode), | ||
}); | ||
}); | ||
|
||
it("sendForgotPasswordLink a forgot password email", async () => { | ||
const email = "test@example.com"; | ||
const token = "reset-token"; | ||
|
||
const envService = locator.EnvService(); | ||
const emailService = new EmailServiceResendImpl(envService); | ||
await emailService.sendForgotPasswordLink(email, token); | ||
|
||
const expectedUrl = | ||
"https://www.clubmemo.com/auth/reset-password?email=test%40example.com&token=reset-token"; | ||
|
||
expect(new Resend().emails.send).toHaveBeenCalledWith({ | ||
from: "El equipo de clubmemo <noreply@app.clubmemo.com>", | ||
to: email, | ||
subject: "Recupera tu cuenta de clubmemo", | ||
html: expect.stringContaining(`href="${expectedUrl}"`), | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import type { ReadonlyHeaders } from "next/dist/server/web/spec-extension/adapters/headers"; | ||
import { headers } from "next/headers"; | ||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; | ||
import { IpServiceVercelImpl } from "./ip-service-vercel-impl"; | ||
|
||
vi.mock("next/headers", () => { | ||
const mockGet = vi.fn(); | ||
return { | ||
headers() { | ||
return { | ||
get: mockGet, | ||
}; | ||
}, | ||
}; | ||
}); | ||
|
||
describe("IpServiceVercelImpl", () => { | ||
let mockHeaders: ReadonlyHeaders; | ||
|
||
beforeEach(() => { | ||
mockHeaders = headers(); | ||
}); | ||
|
||
afterEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
it("returns the first IP from x-forwarded-for, with trimmed spaces", async () => { | ||
const ipService = new IpServiceVercelImpl(); | ||
vi.mocked(mockHeaders.get).mockReturnValue(" 192.168.1.1, 192.168.1.2"); | ||
await expect(ipService.getIp()).resolves.toBe("192.168.1.1"); | ||
expect(mockHeaders.get).toHaveBeenCalledWith("x-forwarded-for"); | ||
}); | ||
|
||
it("returns the IP from x-real-ip (with trimmed spaces) if x-forwarded-for is null", async () => { | ||
const ipService = new IpServiceVercelImpl(); | ||
vi.mocked(mockHeaders.get).mockImplementation((header) => | ||
header === "x-real-ip" ? " 192.168.1.3 " : null, | ||
); | ||
await expect(ipService.getIp()).resolves.toBe("192.168.1.3"); | ||
expect(mockHeaders.get).toHaveBeenCalledWith("x-forwarded-for"); | ||
expect(mockHeaders.get).toHaveBeenCalledWith("x-real-ip"); | ||
}); | ||
|
||
it("returns '0.0.0.0' x-forwarded-for and x-real-ip are both null", async () => { | ||
const ipService = new IpServiceVercelImpl(); | ||
vi.mocked(mockHeaders.get).mockReturnValue(null); | ||
await expect(ipService.getIp()).resolves.toBe("0.0.0.0"); | ||
expect(mockHeaders.get).toHaveBeenCalledWith("x-forwarded-for"); | ||
expect(mockHeaders.get).toHaveBeenCalledWith("x-real-ip"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters