-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #610 from golemfactory/feature/JST-468/make-use-of…
…-public-whitelist Feature/jst 468/make use of public whitelist
- Loading branch information
Showing
7 changed files
with
143 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { MockPropertyPolicy, imock, instance, when } from "@johanblumenberg/ts-mockito"; | ||
|
||
import { getHealthyProvidersWhiteList } from "./helpers"; | ||
|
||
const mockFetch = jest.spyOn(global, "fetch"); | ||
const response = imock<Response>(); | ||
|
||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
describe("Market Helpers", () => { | ||
describe("Getting public healthy providers whitelist", () => { | ||
describe("Positive cases", () => { | ||
test("Will return the list returned by the endpoint", async () => { | ||
// Given | ||
when(response.json()).thenResolve(["0xAAA", "0xBBB"]); | ||
mockFetch.mockResolvedValue(instance(response)); | ||
|
||
// When | ||
const data = await getHealthyProvidersWhiteList(); | ||
|
||
// Then | ||
expect(data).toEqual(["0xAAA", "0xBBB"]); | ||
}); | ||
}); | ||
|
||
describe("Negative cases", () => { | ||
test("It throws an error when the response from the API will not be a successful one (fetch -> response.ok)", async () => { | ||
// Given | ||
const mockResponse = imock<Response>(MockPropertyPolicy.StubAsProperty); | ||
when(mockResponse.ok).thenReturn(false); | ||
when(mockResponse.text()).thenResolve("{error:'test'}"); | ||
mockFetch.mockResolvedValue(instance(mockResponse)); | ||
|
||
// When, Then | ||
await expect(() => getHealthyProvidersWhiteList()).rejects.toThrow( | ||
"Failed to download healthy provider whitelist due to an error: Error: Request to download healthy provider whitelist failed: {error:'test'}", | ||
); | ||
}); | ||
|
||
test("It throws an error when executing of fetch will fail for any reason", async () => { | ||
// Given | ||
mockFetch.mockImplementation(() => { | ||
throw new Error("Something went wrong really bad!"); | ||
}); | ||
|
||
// When, Then | ||
await expect(() => getHealthyProvidersWhiteList()).rejects.toThrow( | ||
"Failed to download healthy provider whitelist due to an error: Error: Something went wrong really bad!", | ||
); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* Helps to obtain a whitelist of providers which were health-tested. | ||
* | ||
* Important: This helper requires internet access to function properly. | ||
* | ||
* @return An array with Golem Node IDs of the whitelisted providers. | ||
*/ | ||
export async function getHealthyProvidersWhiteList(): Promise<string[]> { | ||
try { | ||
const response = await fetch("https://provider-health.golem.network/v1/provider-whitelist"); | ||
|
||
if (response.ok) { | ||
return response.json(); | ||
} else { | ||
const body = await response.text(); | ||
|
||
throw new Error(`Request to download healthy provider whitelist failed: ${body}`); | ||
} | ||
} catch (err) { | ||
throw new Error(`Failed to download healthy provider whitelist due to an error: ${err}`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters