mirror of
https://github.com/Azure/cosmos-explorer.git
synced 2025-12-20 17:30:46 +00:00
Initial Move from Azure DevOps to GitHub
This commit is contained in:
162
src/GitHub/GitHubOAuthService.test.ts
Normal file
162
src/GitHub/GitHubOAuthService.test.ts
Normal file
@@ -0,0 +1,162 @@
|
||||
import ko from "knockout";
|
||||
import { HttpStatusCodes } from "../Common/Constants";
|
||||
import * as ViewModels from "../Contracts/ViewModels";
|
||||
import { JunoClient } from "../Juno/JunoClient";
|
||||
import { GitHubConnector, IGitHubConnectorParams } from "./GitHubConnector";
|
||||
import { GitHubOAuthService } from "./GitHubOAuthService";
|
||||
import { ConsoleDataType } from "../Explorer/Menus/NotificationConsole/NotificationConsoleComponent";
|
||||
|
||||
const sampleDatabaseAccount: ViewModels.DatabaseAccount = {
|
||||
id: "id",
|
||||
name: "name",
|
||||
location: "location",
|
||||
type: "type",
|
||||
kind: "kind",
|
||||
tags: [],
|
||||
properties: {
|
||||
documentEndpoint: "documentEndpoint",
|
||||
gremlinEndpoint: "gremlinEndpoint",
|
||||
tableEndpoint: "tableEndpoint",
|
||||
cassandraEndpoint: "cassandraEndpoint"
|
||||
}
|
||||
};
|
||||
|
||||
describe("GitHubOAuthService", () => {
|
||||
let junoClient: JunoClient;
|
||||
let gitHubOAuthService: GitHubOAuthService;
|
||||
let originalDataExplorer: ViewModels.Explorer;
|
||||
|
||||
beforeEach(() => {
|
||||
junoClient = new JunoClient(ko.observable<ViewModels.DatabaseAccount>(sampleDatabaseAccount));
|
||||
gitHubOAuthService = new GitHubOAuthService(junoClient);
|
||||
originalDataExplorer = window.dataExplorer;
|
||||
window.dataExplorer = {
|
||||
...originalDataExplorer,
|
||||
gitHubOAuthService,
|
||||
logConsoleData: (data): void =>
|
||||
data.type === ConsoleDataType.Error ? console.error(data.message) : console.log(data.message)
|
||||
} as ViewModels.Explorer;
|
||||
});
|
||||
|
||||
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).toBeCalled();
|
||||
expect(gitHubOAuthService.getTokenObservable()()).toBeUndefined();
|
||||
});
|
||||
|
||||
it("resetToken resets token", () => {
|
||||
gitHubOAuthService.resetToken();
|
||||
expect(gitHubOAuthService.getTokenObservable()()).toBeUndefined();
|
||||
});
|
||||
|
||||
it("startOAuth resets OAuth state", () => {
|
||||
let url: string;
|
||||
const windowOpenCallback = jest.fn().mockImplementation((value: string) => {
|
||||
url = value;
|
||||
});
|
||||
window.open = windowOpenCallback;
|
||||
|
||||
gitHubOAuthService.startOAuth("scope");
|
||||
expect(windowOpenCallback).toBeCalled();
|
||||
|
||||
const initialParams = new URLSearchParams(new URL(url).search);
|
||||
expect(initialParams.get("state")).toBeDefined();
|
||||
|
||||
gitHubOAuthService.startOAuth("another scope");
|
||||
expect(windowOpenCallback).toBeCalled();
|
||||
|
||||
const newParams = new URLSearchParams(new URL(url).search);
|
||||
expect(newParams.get("state")).toBeDefined();
|
||||
expect(newParams.get("state")).not.toEqual(initialParams.get("state"));
|
||||
});
|
||||
|
||||
it("finishOAuth is called whenever GitHubConnector is started", async () => {
|
||||
const finishOAuthCallback = jest.fn().mockImplementation();
|
||||
gitHubOAuthService.finishOAuth = finishOAuthCallback;
|
||||
|
||||
const params: IGitHubConnectorParams = {
|
||||
state: "state",
|
||||
code: "code"
|
||||
};
|
||||
const searchParams = new URLSearchParams({ ...params });
|
||||
|
||||
const gitHubConnector = new GitHubConnector();
|
||||
gitHubConnector.start(searchParams, window);
|
||||
|
||||
// GitHubConnector uses Window.postMessage and there's no good way to know when the message has received
|
||||
await new Promise(resolve => setTimeout(resolve, 100));
|
||||
|
||||
expect(finishOAuthCallback).toBeCalledWith(params);
|
||||
});
|
||||
|
||||
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 = gitHubOAuthService.startOAuth("scope");
|
||||
|
||||
const params: IGitHubConnectorParams = {
|
||||
state,
|
||||
code: "code"
|
||||
};
|
||||
await gitHubOAuthService.finishOAuth(params);
|
||||
const updatedToken = gitHubOAuthService.getTokenObservable()();
|
||||
|
||||
expect(getGitHubTokenCallback).toBeCalledWith("code");
|
||||
expect(initialToken).not.toEqual(updatedToken);
|
||||
});
|
||||
|
||||
it("finishOAuth updates token to error if state doesn't match", async () => {
|
||||
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 = gitHubOAuthService.startOAuth("scope");
|
||||
|
||||
const params: IGitHubConnectorParams = {
|
||||
state,
|
||||
code: "code"
|
||||
};
|
||||
await gitHubOAuthService.finishOAuth(params);
|
||||
|
||||
expect(getGitHubTokenCallback).toBeCalledWith("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();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user