|
| 1 | +import { Test, TestingModule } from "@nestjs/testing"; |
| 2 | +import { Response } from "express"; |
| 3 | +import { AuthController } from "./auth.controller"; |
| 4 | +import { AuthService } from "./auth.service"; |
| 5 | +import { |
| 6 | + AuthResultQueryDto, |
| 7 | + LogoutQueryDto, |
| 8 | + OAuthCallbackQueryDto, |
| 9 | + RefreshTokenDto, |
| 10 | +} from "./dto"; |
| 11 | +import { Public } from "./public.decorator"; |
| 12 | + |
| 13 | +describe("AuthController", () => { |
| 14 | + let controller: AuthController; |
| 15 | + let authService: jest.Mocked<AuthService>; |
| 16 | + let res: jest.Mocked<Response>; |
| 17 | + |
| 18 | + beforeEach(async () => { |
| 19 | + authService = { |
| 20 | + refreshAccessToken: jest.fn(), |
| 21 | + getLoginUrl: jest.fn(), |
| 22 | + getLogoutUrl: jest.fn(), |
| 23 | + handleCallback: jest.fn(), |
| 24 | + buildAuthResultRedirect: jest.fn(), |
| 25 | + buildErrorRedirect: jest.fn(), |
| 26 | + consumeAuthResult: jest.fn(), |
| 27 | + } as any; |
| 28 | + res = { |
| 29 | + redirect: jest.fn(), |
| 30 | + status: jest.fn().mockReturnThis(), |
| 31 | + json: jest.fn(), |
| 32 | + } as any; |
| 33 | + const module: TestingModule = await Test.createTestingModule({ |
| 34 | + controllers: [AuthController], |
| 35 | + providers: [{ provide: AuthService, useValue: authService }], |
| 36 | + }).compile(); |
| 37 | + controller = module.get<AuthController>(AuthController); |
| 38 | + }); |
| 39 | + |
| 40 | + describe("refreshToken", () => { |
| 41 | + it("should return tokens from service", async () => { |
| 42 | + const dto: RefreshTokenDto = { refresh_token: "refresh" }; |
| 43 | + authService.refreshAccessToken.mockResolvedValue({ |
| 44 | + access_token: "a", |
| 45 | + refresh_token: "r", |
| 46 | + id_token: "i", |
| 47 | + expires_in: 123, |
| 48 | + token_type: "Bearer", |
| 49 | + }); |
| 50 | + const result = await controller.refreshToken(dto); |
| 51 | + expect(authService.refreshAccessToken).toHaveBeenCalledWith("refresh"); |
| 52 | + expect(result).toEqual({ |
| 53 | + access_token: "a", |
| 54 | + refresh_token: "r", |
| 55 | + id_token: "i", |
| 56 | + expires_in: 123, |
| 57 | + }); |
| 58 | + }); |
| 59 | + }); |
| 60 | + |
| 61 | + describe("getLoginUrl", () => { |
| 62 | + it("should redirect to login url", async () => { |
| 63 | + authService.getLoginUrl.mockReturnValue("http://login"); |
| 64 | + await controller.getLoginUrl(res); |
| 65 | + expect(authService.getLoginUrl).toHaveBeenCalled(); |
| 66 | + expect(res.redirect).toHaveBeenCalledWith("http://login"); |
| 67 | + }); |
| 68 | + |
| 69 | + it("should return 500 if error", async () => { |
| 70 | + authService.getLoginUrl.mockImplementation(() => { |
| 71 | + throw new Error(); |
| 72 | + }); |
| 73 | + await controller.getLoginUrl(res); |
| 74 | + expect(res.status).toHaveBeenCalledWith(500); |
| 75 | + expect(res.json).toHaveBeenCalledWith({ error: expect.any(String) }); |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + describe("logout", () => { |
| 80 | + it("should redirect to logout url", async () => { |
| 81 | + authService.getLogoutUrl.mockReturnValue("http://logout"); |
| 82 | + const query: LogoutQueryDto = { id_token_hint: "idtoken" }; |
| 83 | + await controller.logout(query, res); |
| 84 | + expect(authService.getLogoutUrl).toHaveBeenCalledWith("idtoken"); |
| 85 | + expect(res.redirect).toHaveBeenCalledWith("http://logout"); |
| 86 | + }); |
| 87 | + |
| 88 | + it("should return 500 if error", async () => { |
| 89 | + authService.getLogoutUrl.mockImplementation(() => { |
| 90 | + throw new Error(); |
| 91 | + }); |
| 92 | + await controller.logout({ id_token_hint: "idtoken" }, res); |
| 93 | + expect(res.status).toHaveBeenCalledWith(500); |
| 94 | + expect(res.json).toHaveBeenCalledWith({ error: expect.any(String) }); |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + describe("oauthCallback", () => { |
| 99 | + it("should handle callback and redirect", async () => { |
| 100 | + authService.handleCallback.mockResolvedValue("resultId"); |
| 101 | + authService.buildAuthResultRedirect.mockReturnValue("/redirect"); |
| 102 | + const query: OAuthCallbackQueryDto = { code: "c", state: "s" } as any; |
| 103 | + await controller.oauthCallback(query, res); |
| 104 | + expect(authService.handleCallback).toHaveBeenCalledWith("c", "s"); |
| 105 | + expect(authService.buildAuthResultRedirect).toHaveBeenCalledWith( |
| 106 | + "resultId", |
| 107 | + ); |
| 108 | + expect(res.redirect).toHaveBeenCalledWith("/redirect"); |
| 109 | + }); |
| 110 | + |
| 111 | + it("should redirect to error if exception", async () => { |
| 112 | + authService.handleCallback.mockRejectedValue(new Error("fail")); |
| 113 | + authService.buildErrorRedirect.mockReturnValue("/error"); |
| 114 | + const query: OAuthCallbackQueryDto = { code: "c", state: "s" } as any; |
| 115 | + await controller.oauthCallback(query, res); |
| 116 | + expect(authService.buildErrorRedirect).toHaveBeenCalledWith( |
| 117 | + "callback_failed", |
| 118 | + ); |
| 119 | + expect(res.redirect).toHaveBeenCalledWith("/error"); |
| 120 | + }); |
| 121 | + }); |
| 122 | + |
| 123 | + describe("consumeResult", () => { |
| 124 | + it("should return result from service", async () => { |
| 125 | + authService.consumeAuthResult.mockReturnValue({ |
| 126 | + access_token: "token", |
| 127 | + expires_in: 3600, |
| 128 | + token_type: "Bearer", |
| 129 | + }); |
| 130 | + const query: AuthResultQueryDto = { result: "uuid" }; |
| 131 | + const result = await controller.consumeResult(query); |
| 132 | + expect(authService.consumeAuthResult).toHaveBeenCalledWith("uuid"); |
| 133 | + expect(result).toEqual({ |
| 134 | + access_token: "token", |
| 135 | + expires_in: 3600, |
| 136 | + token_type: "Bearer", |
| 137 | + }); |
| 138 | + }); |
| 139 | + }); |
| 140 | +}); |
0 commit comments