Copilot commented on code in PR #6470: URL: https://github.com/apache/texera/pull/6470#discussion_r3598621129
########## frontend/src/app/common/service/user/auth.service.spec.ts: ########## @@ -0,0 +1,187 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { HttpClientTestingModule, HttpTestingController } from "@angular/common/http/testing"; +import { TestBed } from "@angular/core/testing"; +import { JwtHelperService } from "@auth0/angular-jwt"; +import { NzModalService } from "ng-zorro-antd/modal"; +import { AppSettings } from "../../app-setting"; +import { Role } from "../../type/user"; +import { AuthService, TOKEN_KEY } from "./auth.service"; +import { NotificationService } from "../notification/notification.service"; +import { GmailService } from "../gmail/gmail.service"; +import { GuiConfigService } from "../gui-config.service"; + +describe("AuthService", () => { + let service: AuthService; + let httpMock: HttpTestingController; + let jwt: { + isTokenExpired: ReturnType<typeof vi.fn>; + decodeToken: ReturnType<typeof vi.fn>; + getTokenExpirationDate: ReturnType<typeof vi.fn>; + }; + let notification: { error: ReturnType<typeof vi.fn> }; + let config: { env: { inviteOnly: boolean } }; + let modal: { info: ReturnType<typeof vi.fn>; create: ReturnType<typeof vi.fn> }; + + const api = AppSettings.getApiEndpoint(); + const claims = { + role: Role.REGULAR, + userId: 5, + email: "[email protected]", + sub: "Ursula", + googleId: "g", + googleAvatar: "a", + comment: "c", + joiningReason: "r", + }; + + beforeEach(() => { + localStorage.clear(); + jwt = { + isTokenExpired: vi.fn().mockReturnValue(false), + decodeToken: vi.fn().mockReturnValue(claims), + getTokenExpirationDate: vi.fn().mockReturnValue(new Date(Date.now() + 60_000)), + }; + notification = { error: vi.fn() }; + config = { env: { inviteOnly: false } }; + modal = { info: vi.fn(), create: vi.fn() }; + + TestBed.configureTestingModule({ + imports: [HttpClientTestingModule], + providers: [ + AuthService, + { provide: JwtHelperService, useValue: jwt }, + { provide: NotificationService, useValue: notification }, + { provide: GmailService, useValue: {} }, + { provide: GuiConfigService, useValue: config }, + { provide: NzModalService, useValue: modal }, + ], + }); + service = TestBed.inject(AuthService); + httpMock = TestBed.inject(HttpTestingController); + }); + + afterEach(() => { + httpMock.verify(); + localStorage.clear(); + }); Review Comment: `loginWithExistingToken()` schedules an RxJS `timer(...)` via `registerAutoLogout()` for the valid-token path. In this spec, the valid-token test sets `getTokenExpirationDate()` to ~60s in the future, but the suite never calls `logout()` / unsubscribes afterward, leaving a real pending timer handle that can keep Vitest alive or introduce late side effects in later tests. Clean up the subscription in `afterEach` (or use fake timers and reset to real timers). ########## frontend/src/app/common/service/user/auth.service.spec.ts: ########## @@ -0,0 +1,187 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { HttpClientTestingModule, HttpTestingController } from "@angular/common/http/testing"; +import { TestBed } from "@angular/core/testing"; +import { JwtHelperService } from "@auth0/angular-jwt"; +import { NzModalService } from "ng-zorro-antd/modal"; +import { AppSettings } from "../../app-setting"; +import { Role } from "../../type/user"; +import { AuthService, TOKEN_KEY } from "./auth.service"; +import { NotificationService } from "../notification/notification.service"; +import { GmailService } from "../gmail/gmail.service"; +import { GuiConfigService } from "../gui-config.service"; + +describe("AuthService", () => { + let service: AuthService; + let httpMock: HttpTestingController; + let jwt: { + isTokenExpired: ReturnType<typeof vi.fn>; + decodeToken: ReturnType<typeof vi.fn>; + getTokenExpirationDate: ReturnType<typeof vi.fn>; + }; + let notification: { error: ReturnType<typeof vi.fn> }; + let config: { env: { inviteOnly: boolean } }; + let modal: { info: ReturnType<typeof vi.fn>; create: ReturnType<typeof vi.fn> }; + + const api = AppSettings.getApiEndpoint(); + const claims = { + role: Role.REGULAR, + userId: 5, + email: "[email protected]", + sub: "Ursula", + googleId: "g", + googleAvatar: "a", + comment: "c", + joiningReason: "r", + }; + + beforeEach(() => { + localStorage.clear(); + jwt = { + isTokenExpired: vi.fn().mockReturnValue(false), + decodeToken: vi.fn().mockReturnValue(claims), + getTokenExpirationDate: vi.fn().mockReturnValue(new Date(Date.now() + 60_000)), + }; + notification = { error: vi.fn() }; + config = { env: { inviteOnly: false } }; + modal = { info: vi.fn(), create: vi.fn() }; + + TestBed.configureTestingModule({ + imports: [HttpClientTestingModule], + providers: [ + AuthService, + { provide: JwtHelperService, useValue: jwt }, + { provide: NotificationService, useValue: notification }, + { provide: GmailService, useValue: {} }, + { provide: GuiConfigService, useValue: config }, + { provide: NzModalService, useValue: modal }, + ], + }); + service = TestBed.inject(AuthService); + httpMock = TestBed.inject(HttpTestingController); + }); + + afterEach(() => { + httpMock.verify(); + localStorage.clear(); + }); + + it("should be created", () => { + expect(service).toBeTruthy(); + }); + + describe("static access-token helpers", () => { + it("set/get/remove roundtrip through localStorage", () => { + expect(AuthService.getAccessToken()).toBeNull(); + + AuthService.setAccessToken("tok"); + expect(localStorage.getItem(TOKEN_KEY)).toEqual("tok"); + expect(AuthService.getAccessToken()).toEqual("tok"); + + AuthService.removeAccessToken(); + expect(AuthService.getAccessToken()).toBeNull(); + }); + }); + + describe("HTTP auth endpoints", () => { + it("register() POSTs username/password to the register endpoint", () => { + service.register("alice", "pw").subscribe(); + const req = httpMock.expectOne(`${api}/${AuthService.REGISTER_ENDPOINT}`); + expect(req.request.method).toEqual("POST"); Review Comment: The PR description / issue acceptance criteria mention exercising both success and error paths for the HTTP auth endpoints, but the current specs only cover the happy-path `flush({ accessToken: ... })` cases for `register()`, `auth()`, and `googleAuth()`. Add at least one failing-response test per endpoint (e.g., `req.flush(..., { status: 400/500, statusText: ... })`) and assert the observable rejects with an `HttpErrorResponse` so the error path is actually covered. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
