2020-08-03 17:11:07 -05:00
|
|
|
import { armRequest } from "./request";
|
2020-10-01 14:00:46 +02:00
|
|
|
import fetch from "node-fetch";
|
2021-01-08 21:56:29 -06:00
|
|
|
import { updateUserContext } from "../../UserContext";
|
|
|
|
import { AuthType } from "../../AuthType";
|
2020-10-01 14:00:46 +02:00
|
|
|
|
|
|
|
interface Global {
|
|
|
|
Headers: unknown;
|
|
|
|
}
|
|
|
|
|
|
|
|
((global as unknown) as Global).Headers = ((fetch as unknown) as Global).Headers;
|
2020-08-03 17:11:07 -05:00
|
|
|
|
|
|
|
describe("ARM request", () => {
|
2021-01-08 21:56:29 -06:00
|
|
|
updateUserContext({
|
2021-02-22 14:43:58 -06:00
|
|
|
authType: AuthType.AAD,
|
2021-01-20 09:15:01 -06:00
|
|
|
authorizationToken: "some-token",
|
2021-01-08 21:56:29 -06:00
|
|
|
});
|
|
|
|
|
2020-08-03 17:11:07 -05:00
|
|
|
it("should call window.fetch", async () => {
|
|
|
|
window.fetch = jest.fn().mockResolvedValue({
|
|
|
|
ok: true,
|
|
|
|
json: async () => {
|
|
|
|
return {};
|
2021-01-20 09:15:01 -06:00
|
|
|
},
|
2020-08-03 17:11:07 -05:00
|
|
|
});
|
|
|
|
await armRequest({ apiVersion: "2001-01-01", host: "https://foo.com", path: "foo", method: "GET" });
|
|
|
|
expect(window.fetch).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should poll for async operations", async () => {
|
|
|
|
const headers = new Headers();
|
2020-11-02 16:59:08 -08:00
|
|
|
headers.set("location", "https://foo.com/operationStatus");
|
2020-08-03 17:11:07 -05:00
|
|
|
window.fetch = jest.fn().mockResolvedValue({
|
|
|
|
ok: true,
|
|
|
|
headers,
|
2020-11-02 16:59:08 -08:00
|
|
|
status: 200,
|
2021-01-20 09:15:01 -06:00
|
|
|
json: async () => ({}),
|
2020-08-03 17:11:07 -05:00
|
|
|
});
|
|
|
|
await armRequest({ apiVersion: "2001-01-01", host: "https://foo.com", path: "foo", method: "GET" });
|
|
|
|
expect(window.fetch).toHaveBeenCalledTimes(2);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should throw for failed async operations", async () => {
|
|
|
|
const headers = new Headers();
|
2020-11-02 16:59:08 -08:00
|
|
|
headers.set("location", "https://foo.com/operationStatus");
|
2020-08-03 17:11:07 -05:00
|
|
|
window.fetch = jest.fn().mockResolvedValue({
|
|
|
|
ok: true,
|
|
|
|
headers,
|
2020-11-02 16:59:08 -08:00
|
|
|
status: 200,
|
2020-08-03 17:11:07 -05:00
|
|
|
json: async () => {
|
|
|
|
return { status: "Failed" };
|
2021-01-20 09:15:01 -06:00
|
|
|
},
|
2020-08-03 17:11:07 -05:00
|
|
|
});
|
|
|
|
await expect(() =>
|
|
|
|
armRequest({ apiVersion: "2001-01-01", host: "https://foo.com", path: "foo", method: "GET" })
|
|
|
|
).rejects.toThrow();
|
|
|
|
expect(window.fetch).toHaveBeenCalledTimes(2);
|
|
|
|
});
|
2021-01-08 21:56:29 -06:00
|
|
|
|
|
|
|
it("should throw token error", async () => {
|
|
|
|
updateUserContext({
|
2021-02-22 14:43:58 -06:00
|
|
|
authType: AuthType.AAD,
|
2021-01-20 09:15:01 -06:00
|
|
|
authorizationToken: undefined,
|
2021-01-08 21:56:29 -06:00
|
|
|
});
|
|
|
|
const headers = new Headers();
|
|
|
|
headers.set("location", "https://foo.com/operationStatus");
|
|
|
|
window.fetch = jest.fn().mockResolvedValue({
|
|
|
|
ok: true,
|
|
|
|
headers,
|
|
|
|
status: 200,
|
|
|
|
json: async () => {
|
|
|
|
return { status: "Failed" };
|
2021-01-20 09:15:01 -06:00
|
|
|
},
|
2021-01-08 21:56:29 -06:00
|
|
|
});
|
|
|
|
await expect(() =>
|
|
|
|
armRequest({ apiVersion: "2001-01-01", host: "https://foo.com", path: "foo", method: "GET" })
|
|
|
|
).rejects.toThrow("No authority token provided");
|
|
|
|
});
|
2020-08-03 17:11:07 -05:00
|
|
|
});
|