-
Notifications
You must be signed in to change notification settings - Fork 461
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
moved provider utils tests to their own file
- Loading branch information
1 parent
df917e0
commit 0dd74c2
Showing
4 changed files
with
119 additions
and
48 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* eslint-disable */ | ||
import { RequestPayload, ProviderContext } from "@gitcoin/passport-types"; | ||
import { ProviderExternalVerificationError } from "../../types"; | ||
import { Providers } from "../providers"; | ||
import { SimpleProvider, verifySimpleProvider } from "../simpleProvider"; | ||
|
||
jest.spyOn(console, "error").mockImplementation(() => {}); | ||
|
||
describe("Providers", function () { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
const mockContext: ProviderContext = {}; | ||
|
||
const mockPayload: RequestPayload = { | ||
address: "0x0", | ||
proofs: { | ||
username: "test", | ||
valid: "true", | ||
}, | ||
type: "Simple", | ||
version: "", | ||
}; | ||
|
||
it("should report unexpected errors even if not derived from Error", async () => { | ||
(verifySimpleProvider as jest.MockedFunction<typeof verifySimpleProvider>) = jest.fn().mockImplementation(() => { | ||
throw "I don't have an error type"; | ||
}); | ||
|
||
const unknownErrorMessagePartOne = "UNHANDLED ERROR: for type Simple and address 0x0 -"; | ||
const unknownErrorMessagePartTwo = "unable to parse, not derived from Error"; | ||
|
||
const provider = new SimpleProvider(); | ||
const providers = new Providers([provider]); | ||
const result = await providers.verify(mockPayload.type, mockPayload, mockContext); | ||
|
||
expect(console.error).toHaveBeenCalledWith(unknownErrorMessagePartOne, unknownErrorMessagePartTwo); | ||
expect(result).toEqual({ | ||
valid: false, | ||
error: ["There was an unexpected error during verification."], | ||
}); | ||
}); | ||
|
||
it("should report unexpected error details if derived from Error", async () => { | ||
(verifySimpleProvider as jest.MockedFunction<typeof verifySimpleProvider>) = jest.fn().mockImplementation(() => { | ||
class MyError extends Error { | ||
constructor(message: string) { | ||
super(message); | ||
this.name = "MyError"; | ||
} | ||
} | ||
throw new MyError("I'm an unhandled error"); | ||
}); | ||
|
||
const unknownErrorMessagePartOne = "UNHANDLED ERROR: for type Simple and address 0x0 -"; | ||
|
||
const provider = new SimpleProvider(); | ||
const providers = new Providers([provider]); | ||
const result = await providers.verify(mockPayload.type, mockPayload, mockContext); | ||
|
||
expect(console.error).toHaveBeenCalledWith(unknownErrorMessagePartOne, expect.stringContaining("MyError at")); | ||
|
||
expect(result).toEqual({ | ||
valid: false, | ||
error: [ | ||
expect.stringContaining( | ||
"There was an unexpected error during verification. MyError: I'm an unhandled error at" | ||
), | ||
], | ||
}); | ||
}); | ||
|
||
it("should not report expected errors", async () => { | ||
(verifySimpleProvider as jest.MockedFunction<typeof verifySimpleProvider>) = jest.fn().mockImplementation(() => { | ||
throw new ProviderExternalVerificationError("I'm an expected error"); | ||
}); | ||
|
||
const provider = new SimpleProvider(); | ||
const providers = new Providers([provider]); | ||
const result = await providers.verify(mockPayload.type, mockPayload, mockContext); | ||
|
||
expect(console.error).not.toHaveBeenCalled(); | ||
|
||
expect(result).toEqual({ | ||
valid: false, | ||
error: ["I'm an expected error"], | ||
}); | ||
}); | ||
|
||
it("should return missing provider error if type doesn't exist", async () => { | ||
const provider = new SimpleProvider(); | ||
const providers = new Providers([provider]); | ||
|
||
const result = await providers.verify("nonExistentType", mockPayload, mockContext); | ||
expect(result.valid).toEqual(false); | ||
expect(result.error).toEqual(["Missing provider"]); | ||
}); | ||
}); |
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