-
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 #47 from jembi/feat/get-shlink-accesses
Feat/get shlink accesses
- Loading branch information
Showing
12 changed files
with
343 additions
and
42 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/** | ||
* @jest-environment node | ||
*/ | ||
|
||
import { NOT_FOUND } from "@/app/constants/http-constants"; | ||
import { handleApiValidationError } from "@/app/utils/error-handler"; | ||
import { mapModelToDto } from "@/mappers/shlink-access-mapper"; | ||
import { getSHLinkAccessesUseCase } from "@/usecases/shlink-access/get-shlink-accesses"; | ||
import { getSingleSHLinkUseCase } from "@/usecases/shlinks/get-single-shlink"; | ||
|
||
import { POST } from "./route"; | ||
|
||
jest.mock("@/container", () => ({ | ||
container: { | ||
get: jest.fn(), | ||
}, | ||
SHLinkAccessRepositoryToken: Symbol('SHLinkAccessRepositoryToken'), | ||
SHLinkRepositoryToken: Symbol('SHLinkRepositoryToken'), | ||
})); | ||
|
||
jest.mock("@/usecases/shlink-access/get-shlink-accesses", () => ({ | ||
getSHLinkAccessesUseCase: jest.fn(), | ||
})); | ||
|
||
jest.mock("@/usecases/shlinks/get-single-shlink", () => ({ | ||
getSingleSHLinkUseCase: jest.fn(), | ||
})); | ||
|
||
jest.mock("@/mappers/shlink-access-mapper", () => ({ | ||
mapModelToDto: jest.fn(), | ||
})); | ||
|
||
jest.mock("@/app/utils/error-handler", () => ({ | ||
handleApiValidationError: jest.fn(), | ||
})); | ||
|
||
describe("POST function", () => { | ||
let mockGetSingleSHLinkUseCase: jest.Mock; | ||
let mockGetSHLinkAccessesUseCase: jest.Mock; | ||
let mockMapModelToDto: jest.Mock; | ||
let mockHandleApiValidationError: jest.Mock; | ||
|
||
beforeEach(() => { | ||
mockGetSingleSHLinkUseCase = getSingleSHLinkUseCase as jest.Mock; | ||
mockGetSHLinkAccessesUseCase = getSHLinkAccessesUseCase as jest.Mock; | ||
mockMapModelToDto = mapModelToDto as jest.Mock; | ||
mockHandleApiValidationError = handleApiValidationError as jest.Mock; | ||
}); | ||
|
||
it("should return 404 if shlink is not found", async () => { | ||
// Arrange | ||
const request = new Request("http://localhost", { | ||
method: "POST", | ||
body: JSON.stringify({ managementToken: "token" }), | ||
}); | ||
const params = { id: "non-existent-id" }; | ||
|
||
mockGetSingleSHLinkUseCase.mockResolvedValue(null); | ||
|
||
// Act | ||
const response = await POST(request, params); | ||
|
||
// Assert | ||
expect(response.status).toBe(404); | ||
expect(await response.json()).toEqual({ message: NOT_FOUND }); | ||
}); | ||
|
||
it("should return 200 with access data if shlink is found", async () => { | ||
// Arrange | ||
const request = new Request("http://localhost", { | ||
method: "POST", | ||
body: JSON.stringify({ managementToken: "token" }), | ||
}); | ||
const params = { id: "existent-id" }; | ||
const shlink = { getId: jest.fn().mockReturnValue("shlink-id") }; | ||
const accesses = [{ id: "access-id-1" }, { id: "access-id-2" }]; | ||
const dto = { id: "access-id-1" }; | ||
|
||
mockGetSingleSHLinkUseCase.mockResolvedValue(shlink); | ||
mockGetSHLinkAccessesUseCase.mockResolvedValue(accesses); | ||
mockMapModelToDto.mockImplementation((model) => ({ id: model.id })); | ||
|
||
// Act | ||
const response = await POST(request, params); | ||
|
||
// Assert | ||
expect(response.status).toBe(200); | ||
expect(await response.json()).toEqual(accesses.map(x => ({ id: x.id }))); | ||
}); | ||
|
||
it("should handle errors and call handleApiValidationError", async () => { | ||
// Arrange | ||
const request = new Request("http://localhost", { | ||
method: "POST", | ||
body: JSON.stringify({ managementToken: "token" }), | ||
}); | ||
const params = { id: "some-id" }; | ||
|
||
mockGetSingleSHLinkUseCase.mockRejectedValue(new Error("Test error")); | ||
|
||
// Act | ||
await POST(request, params); | ||
|
||
// Assert | ||
expect(mockHandleApiValidationError).toHaveBeenCalled(); | ||
}); | ||
}); |
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,30 @@ | ||
import { NextResponse } from "next/server"; | ||
|
||
import { NOT_FOUND } from "@/app/constants/http-constants"; | ||
import { handleApiValidationError } from "@/app/utils/error-handler"; | ||
import { container, SHLinkAccessRepositoryToken, SHLinkRepositoryToken } from "@/container"; | ||
import { SHLinkAccessRequestDto } from "@/domain/dtos/shlink-access"; | ||
import { ISHLinkAccessRepository } from "@/infrastructure/repositories/interfaces/shlink-access-repository"; | ||
import { ISHLinkRepository } from "@/infrastructure/repositories/interfaces/shlink-repository"; | ||
import { mapModelToDto } from "@/mappers/shlink-access-mapper"; | ||
import { getSHLinkAccessesUseCase } from "@/usecases/shlink-access/get-shlink-accesses"; | ||
import { getSingleSHLinkUseCase } from "@/usecases/shlinks/get-single-shlink"; | ||
|
||
const repo = container.get<ISHLinkAccessRepository>(SHLinkAccessRepositoryToken); | ||
const shlinkRepo = container.get<ISHLinkRepository>(SHLinkRepositoryToken); | ||
|
||
export async function POST(request: Request, params: {id: string }) { | ||
try{ | ||
const dto: SHLinkAccessRequestDto = await request.json(); | ||
const shlink = await getSingleSHLinkUseCase({ repo: shlinkRepo }, { id: params.id, managementToken: dto.managementToken }); | ||
if(!shlink){ | ||
return NextResponse.json({ message: NOT_FOUND }, { status: 404 }); | ||
} | ||
|
||
const accesses = await getSHLinkAccessesUseCase({ repo }, { shlinkId: shlink.getId() }); | ||
return NextResponse.json(accesses.map(x => mapModelToDto(x)), { status: 200 }); | ||
} | ||
catch(error){ | ||
return handleApiValidationError(error); | ||
} | ||
} |
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
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,10 @@ | ||
export class SHLinkAccessRequestDto { | ||
managementToken: string | ||
} | ||
|
||
export class SHLinkAccessDto { | ||
shlinkId: string; | ||
accessTime: Date; | ||
recipient: string; | ||
id?: string; | ||
} |
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 |
---|---|---|
@@ -1,7 +1,8 @@ | ||
import { SHLinkAccessDto } from '@/domain/dtos/shlink-access'; | ||
import { SHLinkAccessModel } from '@/domain/models/shlink-access'; | ||
import { SHLinkAccessEntity } from '@/entities/shlink-access'; | ||
|
||
import { mapModelToEntity } from './shlink-access-mapper'; | ||
import { mapModelToEntity, mapEntityToModel, mapModelToDto } from './shlink-access-mapper'; | ||
|
||
// Mock the SHLinkAccessModel class | ||
jest.mock('@/domain/models/shlink-access', () => { | ||
|
@@ -54,3 +55,74 @@ describe('mapModelToEntity', () => { | |
expect(result).toBeUndefined(); | ||
}); | ||
}); | ||
|
||
describe('mapEntityToModel', () => { | ||
it('should map a SHLinkAccessEntity to SHLinkAccessModel', () => { | ||
const accessTime = new Date('2024-09-01T12:00:00Z'); | ||
const recipient = '[email protected]'; | ||
const shlinkId = 'abc123'; | ||
const id = 'def456'; | ||
|
||
const entity: SHLinkAccessEntity = { | ||
access_time: accessTime, | ||
recipient: recipient, | ||
shlink_id: shlinkId, | ||
id: id, | ||
}; | ||
|
||
const expectedModel = new SHLinkAccessModel(shlinkId, accessTime, recipient, id); | ||
|
||
const result = mapEntityToModel(entity); | ||
|
||
expect(result.getId()).toEqual(expectedModel.getId()); | ||
expect(result.getAccessTime()).toEqual(expectedModel.getAccessTime()); | ||
expect(result.getRecipient()).toEqual(expectedModel.getRecipient()); | ||
expect(result.getSHLinkId()).toEqual(expectedModel.getSHLinkId()); | ||
}); | ||
|
||
it('should return undefined if SHLinkAccessEntity is undefined', () => { | ||
const result = mapEntityToModel(undefined); | ||
|
||
expect(result).toBeUndefined(); | ||
}); | ||
|
||
it('should return undefined if SHLinkAccessEntity is null', () => { | ||
const result = mapEntityToModel(null as any); | ||
|
||
expect(result).toBeUndefined(); | ||
}); | ||
}); | ||
|
||
describe('mapModelToDto', () => { | ||
it('should map a SHLinkAccessModel to SHLinkAccessDto', () => { | ||
const accessTime = new Date('2024-09-01T12:00:00Z'); | ||
const recipient = '[email protected]'; | ||
const shlinkId = 'abc123'; | ||
const id = 'def456'; | ||
|
||
const model = new SHLinkAccessModel(shlinkId, accessTime, recipient, id); | ||
|
||
const expectedDto: SHLinkAccessDto = { | ||
accessTime: accessTime, | ||
recipient: recipient, | ||
shlinkId: shlinkId, | ||
id: id, | ||
}; | ||
|
||
const result = mapModelToDto(model); | ||
|
||
expect(result).toEqual(expectedDto); | ||
}); | ||
|
||
it('should return undefined if SHLinkAccessModel is undefined', () => { | ||
const result = mapModelToDto(undefined); | ||
|
||
expect(result).toBeUndefined(); | ||
}); | ||
|
||
it('should return undefined if SHLinkAccessModel is null', () => { | ||
const result = mapModelToDto(null as any); | ||
|
||
expect(result).toBeUndefined(); | ||
}); | ||
}); |
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
Oops, something went wrong.