cosmos-explorer/src/GitHub/GitHubOAuthService.test.ts
Laurent Nguyen 1bf4683894
Make Data Explorer work on node v18 (#1654)
* Upgrade packages to enable npm i with node 18

* Fix crypto and querystring issue

* Fix webpack errors during npm start

* Upgrade monaco editor. Fix alias in webconfig

* Remove deprecated file-loader. Upgrade webpack to latest.

* Fix format

* Upgrade webpack, eslint and typescript

* Update p-retry and fluentui packages

* Revert monaco package upgrade

* Fix notebook compile errors

* Fix lint errors

* Update jest snapshots

* Fix unit tests

* Update node version to 18

* Fix compile error

* Fix compile error

* Fix format

* Turn off warning overlay for webpack devServer

* Fix format

* Re-add monaco webpack plugin and upgrade monaco-editor

* Update package-lock.json

* Fix build issue

* Move MonacoWebpackPlugin to previous place in webpack.config.js

* update package-lock.json

* Fix package-lock.json

* Update package-lock.json

* Fix export ChoiceItem not found warning for self serve. Remove warning turn off in webpack config.

* Update checkout and setup actions in for ci tests

* Disable Gallery callout

* Fix disable gallery header

* Totally disable New gallery callout

* Upgrade all github actions to latest
2023-12-13 10:24:40 -08:00

128 lines
4.6 KiB
TypeScript

import { HttpStatusCodes } from "../Common/Constants";
import Explorer from "../Explorer/Explorer";
import NotebookManager from "../Explorer/Notebook/NotebookManager";
import { JunoClient } from "../Juno/JunoClient";
import { IGitHubConnectorParams } from "./GitHubConnector";
import { GitHubOAuthService } from "./GitHubOAuthService";
describe("GitHubOAuthService", () => {
let junoClient: JunoClient;
let gitHubOAuthService: GitHubOAuthService;
let originalDataExplorer: Explorer;
beforeEach(() => {
junoClient = new JunoClient();
gitHubOAuthService = new GitHubOAuthService(junoClient);
originalDataExplorer = window.dataExplorer;
window.dataExplorer = {
...originalDataExplorer,
} as Explorer;
window.dataExplorer.notebookManager = new NotebookManager();
window.dataExplorer.notebookManager.junoClient = junoClient;
window.dataExplorer.notebookManager.gitHubOAuthService = gitHubOAuthService;
});
afterEach(() => {
jest.resetAllMocks();
window.dataExplorer = originalDataExplorer;
originalDataExplorer = undefined;
gitHubOAuthService = undefined;
junoClient = undefined;
});
it("logout deletes app authorization and resets token", async () => {
const deleteAppAuthorizationCallback = jest.fn().mockReturnValue({ status: HttpStatusCodes.NoContent });
junoClient.deleteAppAuthorization = deleteAppAuthorizationCallback;
await gitHubOAuthService.logout();
expect(deleteAppAuthorizationCallback).toHaveBeenCalled();
expect(gitHubOAuthService.getTokenObservable()()).toBeUndefined();
});
it("resetToken resets token", () => {
gitHubOAuthService.resetToken();
expect(gitHubOAuthService.getTokenObservable()()).toBeUndefined();
});
it("startOAuth resets OAuth state", async () => {
let url: string;
const windowOpenCallback = jest.fn().mockImplementation((value: string) => {
url = value;
});
window.open = windowOpenCallback;
await gitHubOAuthService.startOAuth("scope");
expect(windowOpenCallback).toHaveBeenCalled();
const initialParams = new URLSearchParams(new URL(url).search);
expect(initialParams.get("state")).toBeDefined();
await gitHubOAuthService.startOAuth("another scope");
expect(windowOpenCallback).toHaveBeenCalled();
const newParams = new URLSearchParams(new URL(url).search);
expect(newParams.get("state")).toBeDefined();
expect(newParams.get("state")).not.toEqual(initialParams.get("state"));
});
it("finishOAuth updates token", async () => {
const data = { key: "value" };
const getGitHubTokenCallback = jest.fn().mockReturnValue({ status: HttpStatusCodes.OK, data });
junoClient.getGitHubToken = getGitHubTokenCallback;
const initialToken = gitHubOAuthService.getTokenObservable()();
const state = await gitHubOAuthService.startOAuth("scope");
const params: IGitHubConnectorParams = {
state,
code: "code",
};
await gitHubOAuthService.finishOAuth(params);
const updatedToken = gitHubOAuthService.getTokenObservable()();
expect(getGitHubTokenCallback).toHaveBeenCalledWith("code");
expect(initialToken).not.toEqual(updatedToken);
});
it("finishOAuth updates token to error if state doesn't match", async () => {
await gitHubOAuthService.startOAuth("scope");
const params: IGitHubConnectorParams = {
state: "state",
code: "code",
};
await gitHubOAuthService.finishOAuth(params);
expect(gitHubOAuthService.getTokenObservable()().error).toBeDefined();
});
it("finishOAuth updates token to error if unable to fetch token", async () => {
const getGitHubTokenCallback = jest.fn().mockReturnValue({ status: HttpStatusCodes.NotFound });
junoClient.getGitHubToken = getGitHubTokenCallback;
const state = await gitHubOAuthService.startOAuth("scope");
const params: IGitHubConnectorParams = {
state,
code: "code",
};
await gitHubOAuthService.finishOAuth(params);
expect(getGitHubTokenCallback).toHaveBeenCalledWith("code");
expect(gitHubOAuthService.getTokenObservable()().error).toBeDefined();
});
it("isLoggedIn returns false if resetToken is called", () => {
gitHubOAuthService.resetToken();
expect(gitHubOAuthService.isLoggedIn()).toBeFalsy();
});
it("isLoggedIn returns false if logout is called", async () => {
const deleteAppAuthorizationCallback = jest.fn().mockReturnValue({ status: HttpStatusCodes.NoContent });
junoClient.deleteAppAuthorization = deleteAppAuthorizationCallback;
await gitHubOAuthService.logout();
expect(gitHubOAuthService.isLoggedIn()).toBeFalsy();
});
});