Skip to content

Commit

Permalink
Merge pull request #569 from bandada-infra/fix/libs/credentials
Browse files Browse the repository at this point in the history
Improve and fix libs-credentials
  • Loading branch information
vplasencia authored Oct 9, 2024
2 parents bc7dfee + 488c308 commit a59f0c4
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 129 deletions.
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
135 changes: 66 additions & 69 deletions libs/credentials/src/validators/easAttestations/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,78 +1,41 @@
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()
}))

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",
attester: "0x63A35A52c0ac206108EBbf559E4C7109dAd281d3",
schemaId:
"0xe2636f31239f7948afdd9a9c477048b7fc2a089c347af60e3aa1251e5bf63e5c",
Expand All @@ -82,20 +45,22 @@ 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",
attester: "0x63A35A52c0ac206108EBbf559E4C7109dAd281d4",
schemaId:
"0xe2636f31239f7948afdd9a9c477048b7fc2a089c347af60e3aa1251e5bf63e5c",
Expand All @@ -105,20 +70,22 @@ 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",
attester: "0x63A35A52c0ac206108EBbf559E4C7109dAd281d3",
schemaId:
"0xe2636f31239f7948afdd9a9c477048b7fc2a089c347af60e3aa1251e5bf63e5d",
Expand All @@ -128,20 +95,22 @@ 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",
attester: "0x63A35A52c0ac206108EBbf559E4C7109dAd281d3",
schemaId:
"0xe2636f31239f7948afdd9a9c477048b7fc2a089c347af60e3aa1251e5bf63e5c",
Expand All @@ -151,20 +120,22 @@ 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",
attester: "0x63A35A52c0ac206108EBbf559E4C7109dAd281d3",
schemaId:
"0xe2636f31239f7948afdd9a9c477048b7fc2a089c347af60e3aa1251e5bf63e5c",
Expand All @@ -174,20 +145,22 @@ 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",
attester: "0x63A35A52c0ac206108EBbf559E4C7109dAd281d3",
schemaId:
"0xe2636f31239f7948afdd9a9c477048b7fc2a089c347af60e3aa1251e5bf63e5c",
Expand All @@ -197,39 +170,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 +222,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 +251,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

0 comments on commit a59f0c4

Please sign in to comment.