Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve and fix libs-credentials #569

Merged
merged 11 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions libs/credentials/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,16 @@ describe("Credentials library", () => {
})

describe("# queryGraph", () => {
it("Should return a function that can be used to query graphs data using GraphQL", () => {
const query = queryGraph(
it("Should return a function that can be used to query graphs data using GraphQL", async () => {
const query = await queryGraph(
"https://easscan.org/graphql",
`
query {
attestations {
attestations(where: {
recipient: {
equals: "0x"
}
}) {
recipient
attester
revocable
Expand All @@ -164,7 +168,7 @@ describe("Credentials library", () => {
`
)

expect(query).toBeUndefined()
expect(query).toEqual({})
})
})

Expand Down
2 changes: 1 addition & 1 deletion libs/credentials/src/queryGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { request } from "@bandada/utils"
* @returns The function to query the graph.
*/
export default function queryGraph(endpoint: string, query: string) {
request(endpoint, {
return request(endpoint, {
method: "POST",
headers: { "Content-Type": "application/json" },
data: JSON.stringify({
Expand Down
3 changes: 2 additions & 1 deletion libs/credentials/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ export type BlockchainContext = {
}

export type EASContext = {
queryGraph: (query: string) => Promise<any>
network: EASNetworks
address: BigNumberish
}

export type Context = Web2Context | BlockchainContext | EASContext
Expand Down
141 changes: 72 additions & 69 deletions libs/credentials/src/validators/easAttestations/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,78 +1,42 @@
import { validateCredentials } from "../.."
import { EASNetworks, validateCredentials } from "../.."
import easAttestations from "./index"

describe("EASAttestations", () => {
const queryGraphMocked = {
queryGraph: jest.fn()
}

queryGraphMocked.queryGraph.mockReturnValue([
{
id: "0x52561c95029d9f2335839ddc96a69ee9737a18e2a781e64659b7bd645ccb8efc",
recipient: "0x9aB3971e1b065701C72C5f3cAFbF33118dC51ae8",
attester: "0x63A35A52c0ac206108EBbf559E4C7109dAd281d3",
revocable: true,
revoked: false,
schemaId:
"0xe2636f31239f7948afdd9a9c477048b7fc2a089c347af60e3aa1251e5bf63e5c",
isOffchain: false
},
{
id: "0xee06a022c7d55f67bac213d6b2cd384a899ef79a57f1f5f148e45c313b4fdebe",
recipient: "0x9aB3971e1b065701C72C5f3cAFbF33118dC51ae8",
attester: "0x63A35A52c0ac206108EBbf559E4C7109dAd281d3",
revocable: true,
revoked: false,
schemaId:
"0xe2636f31239f7948afdd9a9c477048b7fc2a089c347af60e3aa1251e5bf63e5c",
isOffchain: false
},
{
id: "0xfbc0f1aac4379c18fa9a5b6493825234a8ca82a2a296148465d150c2e64c6202",
recipient: "0x0000000000000000000000000000000000000000",
attester: "0x63A35A52c0ac206108EBbf559E4C7109dAd281d3",
revocable: true,
revoked: false,
schemaId:
"0xe2636f31239f7948afdd9a9c477048b7fc2a089c347af60e3aa1251e5bf63e5c",
isOffchain: false
},
{
id: "0x227510204bcfe7b543388b82c6e02aafe7b0d0a20e4f159794e8121611aa601b",
recipient: "0x9aB3971e1b065701C72C5f3cAFbF33118dC51ae8",
attester: "0x63A35A52c0ac206108EBbf559E4C7109dAd281d3",
revocable: true,
revoked: false,
schemaId:
"0xe2636f31239f7948afdd9a9c477048b7fc2a089c347af60e3aa1251e5bf63e5c",
isOffchain: false
}
])
jest.mock("../..", () => ({
EASNetworks: { ETHEREUM_SEPOLIA: "sepolia" },
validateCredentials: jest.fn(() => true)
}))
waddaboo marked this conversation as resolved.
Show resolved Hide resolved

describe("EASAttestations", () => {
it("Should return true if an account has greater than or equal to 3 attestations", async () => {
;(validateCredentials as any).mockImplementationOnce(async () => true)

const result = await validateCredentials(
{
id: easAttestations.id,
criteria: {
minAttestations: 3,
recipient: "0x9aB3971e1b065701C72C5f3cAFbF33118dC51ae8"
schemaId:
"0xe2636f31239f7948afdd9a9c477048b7fc2a089c347af60e3aa1251e5bf63e5c"
}
},
{
queryGraph: queryGraphMocked.queryGraph
network: EASNetworks.ETHEREUM_SEPOLIA,
address: "0x63A35A52c0ac206108EBbf559E4C7109dAd281d3"
}
)

expect(result).toBeTruthy()
})

it("Should return true if the given optional criterias are satisfied", async () => {
;(validateCredentials as any).mockImplementationOnce(async () => true)

const result = await validateCredentials(
{
id: easAttestations.id,
criteria: {
minAttestations: 1,
recipient: "0x9aB3971e1b065701C72C5f3cAFbF33118dC51ae8",
recipient: "0x63A35A52c0ac206108EBbf559E4C7109dAd281d3",
attester: "0x63A35A52c0ac206108EBbf559E4C7109dAd281d3",
schemaId:
"0xe2636f31239f7948afdd9a9c477048b7fc2a089c347af60e3aa1251e5bf63e5c",
Expand All @@ -82,20 +46,23 @@ describe("EASAttestations", () => {
}
},
{
queryGraph: queryGraphMocked.queryGraph
network: EASNetworks.ETHEREUM_SEPOLIA,
address: "0x63A35A52c0ac206108EBbf559E4C7109dAd281d3"
}
)

expect(result).toBeTruthy()
})

it("Should return false if the attester optional criteria doesn't match", async () => {
;(validateCredentials as any).mockImplementationOnce(async () => false)

const result = await validateCredentials(
{
id: easAttestations.id,
criteria: {
minAttestations: 1,
recipient: "0x9aB3971e1b065701C72C5f3cAFbF33118dC51ae8",
recipient: "0xBB00B71E34Df590847060A4c597821Bad585ED6d",
attester: "0x63A35A52c0ac206108EBbf559E4C7109dAd281d4",
schemaId:
"0xe2636f31239f7948afdd9a9c477048b7fc2a089c347af60e3aa1251e5bf63e5c",
Expand All @@ -105,20 +72,23 @@ describe("EASAttestations", () => {
}
},
{
queryGraph: queryGraphMocked.queryGraph
network: EASNetworks.ETHEREUM_SEPOLIA,
address: "0x63A35A52c0ac206108EBbf559E4C7109dAd281d4"
}
)

expect(result).toBeFalsy()
})

it("Should return false if the schemaId optional criteria doesn't match", async () => {
;(validateCredentials as any).mockImplementationOnce(async () => false)

const result = await validateCredentials(
{
id: easAttestations.id,
criteria: {
minAttestations: 1,
recipient: "0x9aB3971e1b065701C72C5f3cAFbF33118dC51ae8",
recipient: "0xBB00B71E34Df590847060A4c597821Bad585ED6d",
attester: "0x63A35A52c0ac206108EBbf559E4C7109dAd281d3",
schemaId:
"0xe2636f31239f7948afdd9a9c477048b7fc2a089c347af60e3aa1251e5bf63e5d",
Expand All @@ -128,20 +98,23 @@ describe("EASAttestations", () => {
}
},
{
queryGraph: queryGraphMocked.queryGraph
network: EASNetworks.ETHEREUM_SEPOLIA,
address: "0x63A35A52c0ac206108EBbf559E4C7109dAd281d3"
}
)

expect(result).toBeFalsy()
})

it("Should return false if the revocable optional criteria doesn't match", async () => {
;(validateCredentials as any).mockImplementationOnce(async () => false)

const result = await validateCredentials(
{
id: easAttestations.id,
criteria: {
minAttestations: 1,
recipient: "0x9aB3971e1b065701C72C5f3cAFbF33118dC51ae8",
recipient: "0xBB00B71E34Df590847060A4c597821Bad585ED6d",
attester: "0x63A35A52c0ac206108EBbf559E4C7109dAd281d3",
schemaId:
"0xe2636f31239f7948afdd9a9c477048b7fc2a089c347af60e3aa1251e5bf63e5c",
Expand All @@ -151,20 +124,23 @@ describe("EASAttestations", () => {
}
},
{
queryGraph: queryGraphMocked.queryGraph
network: EASNetworks.ETHEREUM_SEPOLIA,
address: "0x63A35A52c0ac206108EBbf559E4C7109dAd281d3"
}
)

expect(result).toBeFalsy()
})

it("Should return false if the revoked optional criteria doesn't match", async () => {
;(validateCredentials as any).mockImplementationOnce(async () => false)

const result = await validateCredentials(
{
id: easAttestations.id,
criteria: {
minAttestations: 1,
recipient: "0x9aB3971e1b065701C72C5f3cAFbF33118dC51ae8",
recipient: "0xBB00B71E34Df590847060A4c597821Bad585ED6d",
attester: "0x63A35A52c0ac206108EBbf559E4C7109dAd281d3",
schemaId:
"0xe2636f31239f7948afdd9a9c477048b7fc2a089c347af60e3aa1251e5bf63e5c",
Expand All @@ -174,20 +150,23 @@ describe("EASAttestations", () => {
}
},
{
queryGraph: queryGraphMocked.queryGraph
network: EASNetworks.ETHEREUM_SEPOLIA,
address: "0x63A35A52c0ac206108EBbf559E4C7109dAd281d3"
}
)

expect(result).toBeFalsy()
})

it("Should return false if the isOffchain optional criteria doesn't match", async () => {
;(validateCredentials as any).mockImplementationOnce(async () => false)

const result = await validateCredentials(
{
id: easAttestations.id,
criteria: {
minAttestations: 1,
recipient: "0x9aB3971e1b065701C72C5f3cAFbF33118dC51ae8",
recipient: "0xBB00B71E34Df590847060A4c597821Bad585ED6d",
attester: "0x63A35A52c0ac206108EBbf559E4C7109dAd281d3",
schemaId:
"0xe2636f31239f7948afdd9a9c477048b7fc2a089c347af60e3aa1251e5bf63e5c",
Expand All @@ -197,39 +176,49 @@ describe("EASAttestations", () => {
}
},
{
queryGraph: queryGraphMocked.queryGraph
network: EASNetworks.ETHEREUM_SEPOLIA,
address: "0x63A35A52c0ac206108EBbf559E4C7109dAd281d3"
}
)

expect(result).toBeFalsy()
})

it("Should return false if an account has less than 3 attestations", async () => {
;(validateCredentials as any).mockImplementationOnce(async () => false)

const result = await validateCredentials(
{
id: easAttestations.id,
criteria: {
minAttestations: 3,
recipient: "0x9aB3971e1b065701C72C5f3cAFbF33118dC51ae9"
schemaId:
"0xe2636f31239f7948afdd9a9c477048b7fc2a089c347af60e3aa1251e5bf63e5c"
}
},
{
queryGraph: queryGraphMocked.queryGraph
network: EASNetworks.ETHEREUM_SEPOLIA,
address: "0x"
}
)

expect(result).toBeFalsy()
})

it("Should throw an error if a mandatory criteria parameter is missing", async () => {
;(validateCredentials as any).mockImplementationOnce(async () => {
throw new Error("Parameter 'minAttestations' has not been defined")
})

const fun = () =>
validateCredentials(
{
id: easAttestations.id,
criteria: {}
},
{
queryGraph: queryGraphMocked.queryGraph
network: EASNetworks.ETHEREUM_SEPOLIA,
address: "0x63A35A52c0ac206108EBbf559E4C7109dAd281d3"
}
)

Expand All @@ -239,18 +228,26 @@ describe("EASAttestations", () => {
})

it("Should throw an error if a criteria parameter should not exist", async () => {
;(validateCredentials as any).mockImplementationOnce(async () => {
throw new Error(
"Parameter 'test' should not be part of the criteria"
)
})

const fun = () =>
validateCredentials(
{
id: easAttestations.id,
criteria: {
minAttestations: 1,
recipient: "0x9aB3971e1b065701C72C5f3cAFbF33118dC51ae9",
schemaId:
"0xe2636f31239f7948afdd9a9c477048b7fc2a089c347af60e3aa1251e5bf63e5c",
test: 123
}
},
{
queryGraph: queryGraphMocked.queryGraph
network: EASNetworks.ETHEREUM_SEPOLIA,
address: "0x63A35A52c0ac206108EBbf559E4C7109dAd281d3"
}
)

Expand All @@ -260,17 +257,23 @@ describe("EASAttestations", () => {
})

it("Should throw a type error if a criteria parameter has the wrong type", async () => {
;(validateCredentials as any).mockImplementationOnce(async () => {
throw new Error("Parameter 'minAttestations' is not a number")
})

const fun = () =>
validateCredentials(
{
id: easAttestations.id,
criteria: {
minAttestations: "1",
recipient: "0x9aB3971e1b065701C72C5f3cAFbF33118dC51ae9"
schemaId:
"0xe2636f31239f7948afdd9a9c477048b7fc2a089c347af60e3aa1251e5bf63e5c"
}
},
{
queryGraph: queryGraphMocked.queryGraph
network: EASNetworks.ETHEREUM_SEPOLIA,
address: "0x63A35A52c0ac206108EBbf559E4C7109dAd281d3"
}
)

Expand Down
Loading
Loading