Skip to content

Commit

Permalink
Merge pull request #1394 from authts/test-1
Browse files Browse the repository at this point in the history
improve unit-tests
  • Loading branch information
pamapa authored Feb 8, 2024
2 parents 93ca866 + 49d2551 commit 009f770
Show file tree
Hide file tree
Showing 11 changed files with 355 additions and 111 deletions.
34 changes: 17 additions & 17 deletions src/JsonService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe("JsonService", () => {
await expect(subject.getJson("http://test")).rejects.toThrow();

// assert
expect(fetch).toBeCalledWith(
expect(fetch).toHaveBeenCalledWith(
"http://test",
expect.objectContaining({
headers: { Accept: "application/json" },
Expand All @@ -54,10 +54,10 @@ describe("JsonService", () => {
await expect(customStaticHeaderSubject.getJson("http://test")).rejects.toThrow();

// assert
expect(fetch).toBeCalledWith(
expect(fetch).toHaveBeenCalledWith(
"http://test",
expect.objectContaining({
headers: {
headers: {
Accept: "application/json",
"Custom-Header-1": "this-is-header-1",
"Custom-Header-2": "this-is-header-2",
Expand All @@ -72,10 +72,10 @@ describe("JsonService", () => {
await expect(customDynamicHeaderSubject.getJson("http://test")).rejects.toThrow();

// assert
expect(fetch).toBeCalledWith(
expect(fetch).toHaveBeenCalledWith(
"http://test",
expect.objectContaining({
headers: {
headers: {
Accept: "application/json",
"Custom-Header-1": "my-name-is-header-1",
"Custom-Header-2": "my-name-is-header-2",
Expand All @@ -90,7 +90,7 @@ describe("JsonService", () => {
await expect(subject.getJson("http://test", { token: "token" })).rejects.toThrow();

// assert
expect(fetch).toBeCalledWith(
expect(fetch).toHaveBeenCalledWith(
"http://test",
expect.objectContaining({
headers: { Accept: "application/json", Authorization: "Bearer token" },
Expand All @@ -104,10 +104,10 @@ describe("JsonService", () => {
await expect(customStaticHeaderSubject.getJson("http://test", { token: "token" })).rejects.toThrow();

// assert
expect(fetch).toBeCalledWith(
expect(fetch).toHaveBeenCalledWith(
"http://test",
expect.objectContaining({
headers: {
headers: {
Accept: "application/json",
Authorization: "Bearer token",
"Custom-Header-1": "this-is-header-1",
Expand All @@ -123,11 +123,11 @@ describe("JsonService", () => {
await expect(customDynamicHeaderSubject.getJson("http://test", { token: "token" })).rejects.toThrow();

// assert
expect(fetch).toBeCalledWith(
expect(fetch).toHaveBeenCalledWith(
"http://test",
expect.objectContaining({
headers: {
Accept: "application/json",
headers: {
Accept: "application/json",
Authorization: "Bearer token",
"Custom-Header-1": "my-name-is-header-1",
"Custom-Header-2": "my-name-is-header-2",
Expand Down Expand Up @@ -257,7 +257,7 @@ describe("JsonService", () => {
await subject.getJson("http://test");

// assert
expect(jwtHandler).toBeCalledWith(text);
expect(jwtHandler).toHaveBeenCalledWith(text);
});
});

Expand All @@ -267,7 +267,7 @@ describe("JsonService", () => {
await expect(subject.postForm("http://test", { body: new URLSearchParams("a=b") })).rejects.toThrow();

// assert
expect(fetch).toBeCalledWith(
expect(fetch).toHaveBeenCalledWith(
"http://test",
expect.objectContaining({
headers: {
Expand All @@ -285,7 +285,7 @@ describe("JsonService", () => {
await expect(customStaticHeaderSubject.postForm("http://test", { body: new URLSearchParams("a=b") })).rejects.toThrow();

// assert
expect(fetch).toBeCalledWith(
expect(fetch).toHaveBeenCalledWith(
"http://test",
expect.objectContaining({
headers: {
Expand All @@ -305,7 +305,7 @@ describe("JsonService", () => {
await expect(customDynamicHeaderSubject.postForm("http://test", { body: new URLSearchParams("a=b") })).rejects.toThrow();

// assert
expect(fetch).toBeCalledWith(
expect(fetch).toHaveBeenCalledWith(
"http://test",
expect.objectContaining({
headers: {
Expand All @@ -325,7 +325,7 @@ describe("JsonService", () => {
await expect(subject.postForm("http://test", { body: new URLSearchParams("payload=dummy"), basicAuth: "basicAuth" })).rejects.toThrow();

// assert
expect(fetch).toBeCalledWith(
expect(fetch).toHaveBeenCalledWith(
"http://test",
expect.objectContaining({
headers: {
Expand All @@ -347,7 +347,7 @@ describe("JsonService", () => {
const body = new URLSearchParams();
body.set("payload", "dummy");

expect(fetch).toBeCalledWith(
expect(fetch).toHaveBeenCalledWith(
"http://test",
expect.objectContaining({
headers: {
Expand Down
8 changes: 4 additions & 4 deletions src/MetadataService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ describe("MetadataService", () => {
await subject.getMetadata();

// assert
expect(getJsonMock).toBeCalledWith("authority/.well-known/openid-configuration", { credentials: "same-origin" });
expect(getJsonMock).toHaveBeenCalledWith("authority/.well-known/openid-configuration", { credentials: "same-origin" });
});

it("should fail when no authority or metadataUrl configured", async () => {
Expand Down Expand Up @@ -93,7 +93,7 @@ describe("MetadataService", () => {
await subject.getMetadata();

// assert
expect(getJsonMock).toBeCalledWith("http://sts/metadata", { credentials: "same-origin" });
expect(getJsonMock).toHaveBeenCalledWith("http://sts/metadata", { credentials: "same-origin" });
});

it("should return metadata from json call", async () => {
Expand Down Expand Up @@ -196,7 +196,7 @@ describe("MetadataService", () => {
await subject.getMetadata();

// assert
expect(getJsonMock).toBeCalledWith("http://sts/metadata", { credentials: "include" });
expect(getJsonMock).toHaveBeenCalledWith("http://sts/metadata", { credentials: "include" });
});
});

Expand Down Expand Up @@ -535,7 +535,7 @@ describe("MetadataService", () => {
await subject.getSigningKeys();

// assert
expect(getJsonMock).toBeCalledWith("http://sts/metadata/keys");
expect(getJsonMock).toHaveBeenCalledWith("http://sts/metadata/keys");
});

it("should return keys from jwks_uri", async () => {
Expand Down
16 changes: 8 additions & 8 deletions src/OidcClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ describe("OidcClient", () => {
await subject.createSigninRequest(args);

// assert
expect(setMock).toBeCalled();
expect(setMock).toHaveBeenCalled();
});
});

Expand Down Expand Up @@ -335,7 +335,7 @@ describe("OidcClient", () => {
const response = await subject.processSigninResponse("http://app/cb?state=1");

// assert
expect(validateSigninResponseMock).toBeCalledWith(response, item);
expect(validateSigninResponseMock).toHaveBeenCalledWith(response, item);
});
});

Expand Down Expand Up @@ -648,7 +648,7 @@ describe("OidcClient", () => {
});

// assert
expect(setMock).toBeCalled();
expect(setMock).toHaveBeenCalled();
});

it("should not generate state if no data", async () => {
Expand All @@ -660,7 +660,7 @@ describe("OidcClient", () => {
await subject.createSignoutRequest();

// assert
expect(setMock).not.toBeCalled();
expect(setMock).not.toHaveBeenCalled();
});
});

Expand Down Expand Up @@ -741,7 +741,7 @@ describe("OidcClient", () => {
const response = await subject.processSignoutResponse("http://app/cb?state=1&error=foo");

// assert
expect(validateSignoutResponse).toBeCalledWith(response, item);
expect(validateSignoutResponse).toHaveBeenCalledWith(response, item);
});
});

Expand Down Expand Up @@ -806,7 +806,7 @@ describe("OidcClient", () => {
const response = await subject.processSignoutResponse("http://app/cb?state=1");

// assert
expect(validateSignoutResponse).toBeCalledWith(response, item);
expect(validateSignoutResponse).toHaveBeenCalledWith(response, item);
});

it("should call validator with state even if error in response", async () => {
Expand All @@ -825,7 +825,7 @@ describe("OidcClient", () => {
const response = await subject.processSignoutResponse("http://app/cb?state=1&error=foo");

// assert
expect(validateSignoutResponse).toBeCalledWith(response, item);
expect(validateSignoutResponse).toHaveBeenCalledWith(response, item);
});
});

Expand All @@ -839,7 +839,7 @@ describe("OidcClient", () => {
await subject.clearStaleState();

// assert
expect(clearStaleState).toBeCalled();
expect(clearStaleState).toHaveBeenCalled();
});
});

Expand Down
40 changes: 20 additions & 20 deletions src/TokenClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ describe("TokenClient", () => {

// assert
expect(generateBasicAuthSpy).toHaveBeenCalledWith("client_id", "client_secret");
expect(getTokenEndpointMock).toBeCalledWith(false);
expect(postFormMock).toBeCalledWith(
expect(getTokenEndpointMock).toHaveBeenCalledWith(false);
expect(postFormMock).toHaveBeenCalledWith(
"http://sts/token_endpoint",
expect.objectContaining({
body: expect.any(URLSearchParams),
Expand All @@ -109,8 +109,8 @@ describe("TokenClient", () => {
await subject.exchangeCode({ code: "code", code_verifier: "code_verifier" });

// assert
expect(getTokenEndpointMock).toBeCalledWith(false);
expect(postFormMock).toBeCalledWith(
expect(getTokenEndpointMock).toHaveBeenCalledWith(false);
expect(postFormMock).toHaveBeenCalledWith(
"http://sts/token_endpoint",
expect.objectContaining({
body: expect.any(URLSearchParams),
Expand All @@ -133,8 +133,8 @@ describe("TokenClient", () => {
await subject.exchangeCode({ code: "code", code_verifier: "code_verifier" });

// assert
expect(getTokenEndpointMock).toBeCalledWith(false);
expect(postFormMock).toBeCalledWith(
expect(getTokenEndpointMock).toHaveBeenCalledWith(false);
expect(postFormMock).toHaveBeenCalledWith(
"http://sts/token_endpoint",
expect.objectContaining({
body: expect.any(URLSearchParams),
Expand Down Expand Up @@ -185,8 +185,8 @@ describe("TokenClient", () => {

// assert
expect(generateBasicAuthSpy).toHaveBeenCalledWith("client_id", "client_secret");
expect(getTokenEndpointMock).toBeCalledWith(false);
expect(postFormMock).toBeCalledWith(
expect(getTokenEndpointMock).toHaveBeenCalledWith(false);
expect(postFormMock).toHaveBeenCalledWith(
"http://sts/token_endpoint",
expect.objectContaining({
body: expect.any(URLSearchParams),
Expand All @@ -209,8 +209,8 @@ describe("TokenClient", () => {
await subject.exchangeCredentials({ username: "u", password: "p" });

// assert
expect(getTokenEndpointMock).toBeCalledWith(false);
expect(postFormMock).toBeCalledWith(
expect(getTokenEndpointMock).toHaveBeenCalledWith(false);
expect(postFormMock).toHaveBeenCalledWith(
"http://sts/token_endpoint",
expect.objectContaining({
body: expect.any(URLSearchParams),
Expand All @@ -233,8 +233,8 @@ describe("TokenClient", () => {
await subject.exchangeCredentials({ username: "u", password: "p" });

// assert
expect(getTokenEndpointMock).toBeCalledWith(false);
expect(postFormMock).toBeCalledWith(
expect(getTokenEndpointMock).toHaveBeenCalledWith(false);
expect(postFormMock).toHaveBeenCalledWith(
"http://sts/token_endpoint",
expect.objectContaining({
body: expect.any(URLSearchParams),
Expand Down Expand Up @@ -300,8 +300,8 @@ describe("TokenClient", () => {
await subject.exchangeRefreshToken({ refresh_token: "refresh_token" });

// assert
expect(getTokenEndpointMock).toBeCalledWith(false);
expect(postFormMock).toBeCalledWith(
expect(getTokenEndpointMock).toHaveBeenCalledWith(false);
expect(postFormMock).toHaveBeenCalledWith(
"http://sts/token_endpoint",
expect.objectContaining({
body: expect.any(URLSearchParams),
Expand All @@ -325,8 +325,8 @@ describe("TokenClient", () => {
await subject.exchangeRefreshToken({ refresh_token: "refresh_token" });

// assert
expect(getTokenEndpointMock).toBeCalledWith(false);
expect(postFormMock).toBeCalledWith(
expect(getTokenEndpointMock).toHaveBeenCalledWith(false);
expect(postFormMock).toHaveBeenCalledWith(
"http://sts/token_endpoint",
expect.objectContaining({
body: expect.any(URLSearchParams),
Expand All @@ -350,8 +350,8 @@ describe("TokenClient", () => {
await subject.exchangeRefreshToken({ refresh_token: "refresh_token" });

// assert
expect(getTokenEndpointMock).toBeCalledWith(false);
expect(postFormMock).toBeCalledWith(
expect(getTokenEndpointMock).toHaveBeenCalledWith(false);
expect(postFormMock).toHaveBeenCalledWith(
"http://sts/token_endpoint",
expect.objectContaining({
body: expect.any(URLSearchParams),
Expand Down Expand Up @@ -384,8 +384,8 @@ describe("TokenClient", () => {
await subject.revoke({ token: "token", token_type_hint: "access_token" });

// assert
expect(getTokenEndpointMock).toBeCalledWith(false);
expect(postFormMock).toBeCalledWith(
expect(getTokenEndpointMock).toHaveBeenCalledWith(false);
expect(postFormMock).toHaveBeenCalledWith(
"http://sts/revoke_endpoint",
expect.objectContaining({
body: expect.any(URLSearchParams),
Expand Down
4 changes: 2 additions & 2 deletions src/UserInfoService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ describe("UserInfoService", () => {
await subject.getClaims("token");

// assert
expect(getJsonMock).toBeCalledWith(
expect(getJsonMock).toHaveBeenCalledWith(
"http://sts/userinfo",
expect.objectContaining({
token: "token",
Expand Down Expand Up @@ -112,7 +112,7 @@ describe("UserInfoService", () => {
await subject.getClaims("token");

// assert
expect(getJsonMock).toBeCalledWith(
expect(getJsonMock).toHaveBeenCalledWith(
"http://sts/userinfo",
expect.objectContaining({
credentials: "include",
Expand Down
Loading

0 comments on commit 009f770

Please sign in to comment.