Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions services/splitwise-auth.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import * as Sentry from "@sentry/nextjs";
import { AxiosError } from "axios";
import type { SplitwiseGroup, SplitwiseUser } from "../types/splitwise";
import { createSplitwiseAxios, SplitwiseError } from "./splitwise-axios";
import {
createSplitwiseAxios,
SplitwiseError,
SplitwiseForbiddenError,
SplitwiseNotFoundError,
} from "./splitwise-axios";

export async function validateSplitwiseApiKey(apiKey: string) {
try {
Expand Down Expand Up @@ -88,7 +93,7 @@ export async function getSplitwiseGroup(accessToken: string, groupId: string) {
};
} catch (error) {
// 403 means user doesn't have access to this group
if (error instanceof AxiosError && error.response?.status === 403) {
if (error instanceof SplitwiseForbiddenError) {
return {
success: false,
error: "No access to group",
Expand All @@ -97,7 +102,7 @@ export async function getSplitwiseGroup(accessToken: string, groupId: string) {
}

// 404 means group doesn't exist
if (error instanceof AxiosError && error.response?.status === 404) {
if (error instanceof SplitwiseNotFoundError) {
return {
success: false,
error: "Group not found",
Expand Down
83 changes: 83 additions & 0 deletions tests/services/splitwise-auth.test.ts

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests do not have 100% coverage. Please increase coverage to 100% before approval.

Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { describe, it, expect, beforeEach, vi } from "vitest";
import { http, HttpResponse } from "msw";
import * as Sentry from "@sentry/nextjs";
import { getSplitwiseGroup } from "@/services/splitwise-auth";
import { server } from "../setup";

const SPLITWISE_BASE_URL = "https://secure.splitwise.com/api/v3.0";

describe("getSplitwiseGroup", () => {
beforeEach(() => {
vi.mocked(Sentry.captureException).mockClear();
});

it("returns { success: false, error: 'Group not found' } on 404 without reporting to Sentry", async () => {
server.use(
http.get(`${SPLITWISE_BASE_URL}/get_group/:id`, () =>
HttpResponse.json(
{ errors: { base: ["Invalid API Request: record not found"] } },
{ status: 404 },
),
),
);

const result = await getSplitwiseGroup("token", "999999");

expect(result).toEqual({
success: false,
error: "Group not found",
group: null,
});
expect(Sentry.captureException).not.toHaveBeenCalled();
});

it("returns { success: false, error: 'No access to group' } on 403 without reporting to Sentry", async () => {
server.use(
http.get(`${SPLITWISE_BASE_URL}/get_group/:id`, () =>
HttpResponse.json(
{ errors: { base: ["You do not have permission"] } },
{ status: 403 },
),
),
);

const result = await getSplitwiseGroup("token", "123");

expect(result).toEqual({
success: false,
error: "No access to group",
group: null,
});
expect(Sentry.captureException).not.toHaveBeenCalled();
});

it("returns { success: true, group } on 200", async () => {
server.use(
http.get(`${SPLITWISE_BASE_URL}/get_group/:id`, () =>
HttpResponse.json({
group: { id: 123, name: "Test", members: [] },
}),
),
);

const result = await getSplitwiseGroup("token", "123");

expect(result.success).toBe(true);
expect(result.group).toEqual({ id: 123, name: "Test", members: [] });
expect(Sentry.captureException).not.toHaveBeenCalled();
});

it("reports unexpected errors (5xx) to Sentry and returns failure shape", async () => {
server.use(
http.get(`${SPLITWISE_BASE_URL}/get_group/:id`, () =>
HttpResponse.json({ error: "boom" }, { status: 500 }),
),
);

const result = await getSplitwiseGroup("token", "123");

expect(result.success).toBe(false);
expect(result.group).toBeNull();
expect(Sentry.captureException).toHaveBeenCalledTimes(1);
});
});
Loading