Skip to content

Commit

Permalink
fix: origin URL matching (#32)
Browse files Browse the repository at this point in the history
* fix: origin url matching

* fix: origin url matching tests
  • Loading branch information
priyanshuverma-dev authored Nov 20, 2024
1 parent f329151 commit b1ff38d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
5 changes: 3 additions & 2 deletions src/api/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ export const trackAction = async (
});
return;
}
console.log(req.headers["x-signature"]);
const originUrl = req.headers.origin;

const locationData = await fetchLocationData(req);
logger.debug("Fetched location data", { locationData });

const site = sitesConfig.find((site) => site.url === data.url);
const site = sitesConfig.find((site) => site.url === originUrl);
if (!site) {
logger.warn(`Site not found for URL: ${data.url}`);
res.status(400).send("Site not found");
Expand Down Expand Up @@ -191,7 +193,6 @@ export const trackingScript = async (req: Request, res: Response) => {
res.type("application/javascript").send(minified.code);
} catch (error) {
logger.error("Minification error:", error);
console.error("Minification error:", error);
res.status(500).send("Error generating the script");
}
};
25 changes: 20 additions & 5 deletions tests/api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ describe("API Handlers Tests", () => {
});

it("should track event successfully", async () => {
const response = await request(server).post("/track").send(mockValidData);
const response = await request(server)
.post("/track")
.set("Origin", sitesConfig[0].url)
.send(mockValidData);

expect(response.status).toBe(200);
expect(response.text).toBe("Event tracked successfully");
Expand All @@ -81,7 +84,10 @@ describe("API Handlers Tests", () => {
eventType: undefined,
};

const response = await request(server).post("/track").send(invalidData);
const response = await request(server)
.post("/track")
.set("Origin", sitesConfig[0].url)
.send(invalidData);

expect(response.status).toBe(400);
expect(response.body.message).toBe("Validation failed");
Expand All @@ -96,6 +102,7 @@ describe("API Handlers Tests", () => {

const response = await request(server)
.post("/track")
.set("Origin", unknownSiteData.url)
.send(unknownSiteData);

expect(response.status).toBe(400);
Expand All @@ -106,15 +113,21 @@ describe("API Handlers Tests", () => {
const mockErrorChannel = new Error("Channel not found");
(client.channels.fetch as jest.Mock).mockRejectedValue(mockErrorChannel);

const response = await request(server).post("/track").send(mockValidData);
const response = await request(server)
.post("/track")
.set("Origin", sitesConfig[0].url)
.send(mockValidData);

expect(response.status).toBe(500);
expect(response.text).toBe("Error tracking event");
});
it("should return 500 and 'Channel not found' when the channel does not exist", async () => {
(client.channels.fetch as jest.Mock).mockResolvedValue(null);

const response = await request(server).post("/track").send(mockValidData);
const response = await request(server)
.post("/track")
.set("Origin", sitesConfig[0].url)
.send(mockValidData);

expect(response.status).toBe(500);
expect(response.text).toBe("Channel not found");
Expand Down Expand Up @@ -238,7 +251,9 @@ describe("API Handlers Tests", () => {
new Error("Minification error")
);

const res = await request(server).get("/scripts/tracker");
const res = await request(server)
.get("/scripts/tracker")
.set("Origin", sitesConfig[0].url);

// Ensure 500 error is returned
expect(res.status).toBe(500);
Expand Down

0 comments on commit b1ff38d

Please sign in to comment.