-
Notifications
You must be signed in to change notification settings - Fork 464
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: refactored provider filtering, added tests
- Loading branch information
1 parent
ed9a75d
commit 9aee965
Showing
6 changed files
with
224 additions
and
40 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* eslint-disable */ | ||
import { PLATFORM_ID, PROVIDER_ID, ProviderContext, RequestPayload } from "@gitcoin/passport-types"; | ||
import { SimpleProvider } from "../simpleProvider"; | ||
import { createProviders } from "../createProviders"; | ||
import { PlatformConfig } from "platforms"; | ||
|
||
jest.useFakeTimers(); // Use Jest's timer mocks | ||
|
||
const makeSimplePlatforms = ({ | ||
stampIsDeprecated, | ||
}: { | ||
stampIsDeprecated: boolean; | ||
}): Record<string, PlatformConfig> => ({ | ||
Simple: { | ||
providers: [new SimpleProvider()], | ||
PlatformDetails: { | ||
connectMessage: "", | ||
description: "", | ||
enablePlatformCardUpdate: false, | ||
icon: "", | ||
isEVM: false, | ||
name: "Simple", | ||
platform: "Simple" as PLATFORM_ID, | ||
website: "", | ||
}, | ||
ProviderConfig: [ | ||
{ | ||
platformGroup: "Simple", | ||
providers: [ | ||
{ | ||
title: "Simple", | ||
name: "Simple" as PROVIDER_ID, | ||
isDeprecated: stampIsDeprecated, | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
}); | ||
|
||
describe("createProviders", () => { | ||
const mockContext: ProviderContext = {}; | ||
|
||
const mockPayload: RequestPayload = { | ||
address: "0x0", | ||
proofs: { | ||
username: "test", | ||
valid: "true", | ||
}, | ||
type: "Simple", | ||
version: "", | ||
}; | ||
|
||
test.each([ | ||
{ | ||
stampIsDeprecated: true, | ||
}, | ||
{ | ||
stampIsDeprecated: false, | ||
}, | ||
])("should filter out deprecated providers(deprecated=$stampIsDeprecated)", async ({ stampIsDeprecated }) => { | ||
const platforms = makeSimplePlatforms({ stampIsDeprecated }); | ||
const providers = createProviders(platforms); | ||
|
||
const result = await providers.verify("Simple", mockPayload, mockContext); | ||
|
||
expect(result.valid).toEqual(!stampIsDeprecated); | ||
if (stampIsDeprecated) { | ||
expect(result.errors).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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Provider Utils | ||
import { Providers } from "./providers"; | ||
import { Provider } from "../types"; | ||
import { SimpleProvider } from "./simpleProvider"; | ||
import { SimpleEvmProvider } from "./simpleEvmProvider"; | ||
import { ClearTextSimpleProvider } from "./clearTextSimpleProvider"; | ||
import { ClearTextTwitterProvider, ClearTextGithubOrgProvider } from "../ClearText"; | ||
|
||
import { PlatformConfig } from "../platforms"; | ||
import { PROVIDER_ID } from "@gitcoin/passport-types"; | ||
|
||
export const createProviders = (platforms: Record<string, PlatformConfig>): Providers => { | ||
const platformProviders = Object.values(platforms) | ||
.map((platform) => platform.providers) | ||
.flat(); | ||
|
||
const deprecatedProviderIds = Object.values(platforms) | ||
.map(({ ProviderConfig }) => | ||
ProviderConfig.map(({ providers }) => providers.filter(({ isDeprecated }) => isDeprecated)) | ||
) | ||
.flat(3) | ||
.map(({ name }) => name); | ||
|
||
const providerIsNotDeprecated = ({ type }: Provider) => !deprecatedProviderIds.includes(type as PROVIDER_ID); | ||
|
||
return new Providers( | ||
[ | ||
// Example provider which verifies the payload when `payload.proofs.valid === "true"` | ||
new SimpleProvider(), | ||
new SimpleEvmProvider(), | ||
new ClearTextSimpleProvider(), | ||
new ClearTextTwitterProvider(), | ||
new ClearTextGithubOrgProvider(), | ||
...platformProviders, | ||
].filter(providerIsNotDeprecated) | ||
); | ||
}; |