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
9 changes: 8 additions & 1 deletion .github/workflows/backend-qa.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,16 @@ jobs:
node-version: '24'
- name: Install dependencies
run: npm ci
# Prisma-generated types needed
# ENV is just so this will run
- name: Generate Prisma files
working-directory: apps/backend-services
run: npm run db:generate
env:
DATABASE_URL: "file:dev.db"
- name: Run tests
working-directory: apps/backend-services
run: npm test
run: npm run test:cov
- name: Run Linter
working-directory: apps/backend-services
run: npm run lint
31 changes: 31 additions & 0 deletions apps/backend-services/biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,37 @@
}
}
}
},
{
"includes": ["**/*.spec.ts"],
"linter": {
"rules": {
"correctness": {
"noUndeclaredVariables": "off"
},
"suspicious": {
"noExplicitAny": "off",
"noEmptyBlockStatements": "off"
},
"security": {
"noSecrets": "off"
}
}
}
},
{
"includes": ["**/testUtils/*.*"],
"linter": {
"rules": {
"correctness": {
"noUndeclaredVariables": "off"
},
"suspicious": {
"noExplicitAny": "off",
"noEmptyBlockStatements": "off"
}
}
}
}
],
"assist": {
Expand Down
27 changes: 24 additions & 3 deletions apps/backend-services/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"devDependencies": {
"@nestjs/cli": "^11.0.13",
"@nestjs/schematics": "^11.0.9",
"@nestjs/testing": "^11.1.9",
"@nestjs/testing": "11.1.9",
"@swc/cli": "^0.7.9",
"@swc/core": "^1.15.3",
"@types/jest": "30.0.0",
Expand Down Expand Up @@ -80,9 +80,30 @@
"^.+\\.(t|j)s$": "ts-jest"
},
"collectCoverageFrom": [
"**/*.(t|j)s"
"**/*.(t|j)s",
"!**/*.spec.ts",
"!**/node_modules/**",
"!**/generated/**",
"!main.ts"
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
"testEnvironment": "node",
"transformIgnorePatterns": [
"node_modules/(?!uuid)"
],
"moduleNameMapper": {
"^@/(.*)$": "<rootDir>/$1"
},
"setupFiles": [
"./testUtils/testSetup.ts"
],
"coverageThreshold": {
"global": {
"branches": 70,
"functions": 70,
"lines": 70,
"statements": 70
}
}
}
}
140 changes: 140 additions & 0 deletions apps/backend-services/src/auth/auth.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import { Test, TestingModule } from "@nestjs/testing";
import { Response } from "express";
import { AuthController } from "./auth.controller";
import { AuthService } from "./auth.service";
import {
AuthResultQueryDto,
LogoutQueryDto,
OAuthCallbackQueryDto,
RefreshTokenDto,
} from "./dto";
import { Public } from "./public.decorator";

describe("AuthController", () => {
let controller: AuthController;
let authService: jest.Mocked<AuthService>;
let res: jest.Mocked<Response>;

beforeEach(async () => {
authService = {
refreshAccessToken: jest.fn(),
getLoginUrl: jest.fn(),
getLogoutUrl: jest.fn(),
handleCallback: jest.fn(),
buildAuthResultRedirect: jest.fn(),
buildErrorRedirect: jest.fn(),
consumeAuthResult: jest.fn(),
} as any;
res = {
redirect: jest.fn(),
status: jest.fn().mockReturnThis(),
json: jest.fn(),
} as any;
const module: TestingModule = await Test.createTestingModule({
controllers: [AuthController],
providers: [{ provide: AuthService, useValue: authService }],
}).compile();
controller = module.get<AuthController>(AuthController);
});

describe("refreshToken", () => {
it("should return tokens from service", async () => {
const dto: RefreshTokenDto = { refresh_token: "refresh" };
authService.refreshAccessToken.mockResolvedValue({
access_token: "a",
refresh_token: "r",
id_token: "i",
expires_in: 123,
token_type: "Bearer",
});
const result = await controller.refreshToken(dto);
expect(authService.refreshAccessToken).toHaveBeenCalledWith("refresh");
expect(result).toEqual({
access_token: "a",
refresh_token: "r",
id_token: "i",
expires_in: 123,
});
});
});

describe("getLoginUrl", () => {
it("should redirect to login url", async () => {
authService.getLoginUrl.mockReturnValue("http://login");
await controller.getLoginUrl(res);
expect(authService.getLoginUrl).toHaveBeenCalled();
expect(res.redirect).toHaveBeenCalledWith("http://login");
});

it("should return 500 if error", async () => {
authService.getLoginUrl.mockImplementation(() => {
throw new Error();
});
await controller.getLoginUrl(res);
expect(res.status).toHaveBeenCalledWith(500);
expect(res.json).toHaveBeenCalledWith({ error: expect.any(String) });
});
});

describe("logout", () => {
it("should redirect to logout url", async () => {
authService.getLogoutUrl.mockReturnValue("http://logout");
const query: LogoutQueryDto = { id_token_hint: "idtoken" };
await controller.logout(query, res);
expect(authService.getLogoutUrl).toHaveBeenCalledWith("idtoken");
expect(res.redirect).toHaveBeenCalledWith("http://logout");
});

it("should return 500 if error", async () => {
authService.getLogoutUrl.mockImplementation(() => {
throw new Error();
});
await controller.logout({ id_token_hint: "idtoken" }, res);
expect(res.status).toHaveBeenCalledWith(500);
expect(res.json).toHaveBeenCalledWith({ error: expect.any(String) });
});
});

describe("oauthCallback", () => {
it("should handle callback and redirect", async () => {
authService.handleCallback.mockResolvedValue("resultId");
authService.buildAuthResultRedirect.mockReturnValue("/redirect");
const query: OAuthCallbackQueryDto = { code: "c", state: "s" } as any;
await controller.oauthCallback(query, res);
expect(authService.handleCallback).toHaveBeenCalledWith("c", "s");
expect(authService.buildAuthResultRedirect).toHaveBeenCalledWith(
"resultId",
);
expect(res.redirect).toHaveBeenCalledWith("/redirect");
});

it("should redirect to error if exception", async () => {
authService.handleCallback.mockRejectedValue(new Error("fail"));
authService.buildErrorRedirect.mockReturnValue("/error");
const query: OAuthCallbackQueryDto = { code: "c", state: "s" } as any;
await controller.oauthCallback(query, res);
expect(authService.buildErrorRedirect).toHaveBeenCalledWith(
"callback_failed",
);
expect(res.redirect).toHaveBeenCalledWith("/error");
});
});

describe("consumeResult", () => {
it("should return result from service", async () => {
authService.consumeAuthResult.mockReturnValue({
access_token: "token",
expires_in: 3600,
token_type: "Bearer",
});
const query: AuthResultQueryDto = { result: "uuid" };
const result = await controller.consumeResult(query);
expect(authService.consumeAuthResult).toHaveBeenCalledWith("uuid");
expect(result).toEqual({
access_token: "token",
expires_in: 3600,
token_type: "Bearer",
});
});
});
});
Loading