2020-05-25 21:30:55 -05:00
|
|
|
import { ResourceType } from "@azure/cosmos/dist-esm/common/constants";
|
2020-08-06 14:03:46 -05:00
|
|
|
import { configContext, Platform, updateConfigContext, resetConfigContext } from "../ConfigContext";
|
|
|
|
import { updateUserContext } from "../UserContext";
|
|
|
|
import { endpoint, getTokenFromAuthService, requestPlugin, tokenProvider } from "./CosmosClient";
|
2020-05-25 21:30:55 -05:00
|
|
|
|
|
|
|
describe("tokenProvider", () => {
|
|
|
|
const options = {
|
|
|
|
verb: "GET" as any,
|
|
|
|
path: "/",
|
|
|
|
resourceId: "",
|
|
|
|
resourceType: "dbs" as ResourceType,
|
|
|
|
headers: {},
|
|
|
|
getAuthorizationTokenUsingMasterKey: () => ""
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
window.dataExplorer = { extensionEndpoint: () => "https://main.documentdb.ext.azure.com" } as any;
|
|
|
|
window.fetch = jest.fn().mockImplementation(() => {
|
|
|
|
return {
|
|
|
|
json: () => "{}",
|
|
|
|
headers: new Map()
|
|
|
|
};
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
jest.restoreAllMocks();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("calls the auth token service if no master key is set", async () => {
|
|
|
|
await tokenProvider(options);
|
|
|
|
expect((window.fetch as any).mock.calls.length).toBe(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("does not call the auth service if a master key is set", async () => {
|
2020-08-06 14:03:46 -05:00
|
|
|
updateUserContext({
|
|
|
|
masterKey: "foo"
|
|
|
|
});
|
2020-05-25 21:30:55 -05:00
|
|
|
await tokenProvider(options);
|
|
|
|
expect((window.fetch as any).mock.calls.length).toBe(0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("getTokenFromAuthService", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
delete window.dataExplorer;
|
2020-08-06 14:03:46 -05:00
|
|
|
resetConfigContext();
|
2020-05-25 21:30:55 -05:00
|
|
|
window.fetch = jest.fn().mockImplementation(() => {
|
|
|
|
return {
|
|
|
|
json: () => "{}",
|
|
|
|
headers: new Map()
|
|
|
|
};
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
jest.restoreAllMocks();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("builds the correct URL in production", () => {
|
|
|
|
window.dataExplorer = { extensionEndpoint: () => "https://main.documentdb.ext.azure.com" } as any;
|
|
|
|
getTokenFromAuthService("GET", "dbs", "foo");
|
|
|
|
expect(window.fetch).toHaveBeenCalledWith(
|
|
|
|
"https://main.documentdb.ext.azure.com/api/guest/runtimeproxy/authorizationTokens",
|
|
|
|
expect.any(Object)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("builds the correct URL in dev", () => {
|
2020-08-06 14:03:46 -05:00
|
|
|
updateConfigContext({
|
|
|
|
BACKEND_ENDPOINT: "https://localhost:1234"
|
|
|
|
});
|
2020-05-25 21:30:55 -05:00
|
|
|
getTokenFromAuthService("GET", "dbs", "foo");
|
|
|
|
expect(window.fetch).toHaveBeenCalledWith(
|
|
|
|
"https://localhost:1234/api/guest/runtimeproxy/authorizationTokens",
|
|
|
|
expect.any(Object)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("endpoint", () => {
|
|
|
|
it("falls back to _databaseAccount", () => {
|
2020-08-06 14:03:46 -05:00
|
|
|
updateUserContext({
|
|
|
|
databaseAccount: {
|
|
|
|
id: "foo",
|
|
|
|
name: "foo",
|
|
|
|
location: "foo",
|
|
|
|
type: "foo",
|
|
|
|
kind: "foo",
|
|
|
|
tags: [],
|
|
|
|
properties: {
|
|
|
|
documentEndpoint: "bar",
|
|
|
|
gremlinEndpoint: "foo",
|
|
|
|
tableEndpoint: "foo",
|
|
|
|
cassandraEndpoint: "foo"
|
|
|
|
}
|
2020-05-25 21:30:55 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
expect(endpoint()).toEqual("bar");
|
|
|
|
});
|
|
|
|
it("uses _endpoint if set", () => {
|
2020-08-06 14:03:46 -05:00
|
|
|
updateUserContext({
|
|
|
|
endpoint: "baz"
|
|
|
|
});
|
2020-05-25 21:30:55 -05:00
|
|
|
expect(endpoint()).toEqual("baz");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("requestPlugin", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
delete window.dataExplorerPlatform;
|
2020-08-06 14:03:46 -05:00
|
|
|
resetConfigContext();
|
2020-05-25 21:30:55 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("Hosted", () => {
|
|
|
|
it("builds a proxy URL in development", () => {
|
|
|
|
const next = jest.fn();
|
2020-08-06 14:03:46 -05:00
|
|
|
updateConfigContext({
|
|
|
|
platform: Platform.Hosted,
|
|
|
|
BACKEND_ENDPOINT: "https://localhost:1234",
|
|
|
|
PROXY_PATH: "/proxy"
|
|
|
|
});
|
2020-05-25 21:30:55 -05:00
|
|
|
const headers = {};
|
|
|
|
const endpoint = "https://docs.azure.com";
|
|
|
|
const path = "/dbs/foo";
|
|
|
|
requestPlugin({ endpoint, headers, path } as any, next as any);
|
|
|
|
expect(next.mock.calls[0][0]).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("Emulator", () => {
|
|
|
|
it("builds a url for emulator proxy via webpack", () => {
|
|
|
|
const next = jest.fn();
|
2020-08-06 14:03:46 -05:00
|
|
|
updateConfigContext({ platform: Platform.Emulator, PROXY_PATH: "/proxy" });
|
2020-05-25 21:30:55 -05:00
|
|
|
const headers = {};
|
|
|
|
const endpoint = "";
|
|
|
|
const path = "/dbs/foo";
|
|
|
|
requestPlugin({ endpoint, headers, path } as any, next as any);
|
|
|
|
expect(next.mock.calls[0][0]).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|