Skip to content

Commit

Permalink
fix: fixes failing audience query tests
Browse files Browse the repository at this point in the history
  • Loading branch information
walfly committed May 1, 2020
1 parent 0ab0a01 commit 40408dc
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 92 deletions.
10 changes: 5 additions & 5 deletions packages/daf-core/src/__tests__/entities.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createConnection, Connection, In } from 'typeorm'
import { createConnection, Connection, In, Raw } from 'typeorm'
import { Identity, Key, Message, Credential, Presentation, Claim } from '../index'
import { Entities } from '../index'
import { blake2bHex } from 'blakejs'
Expand Down Expand Up @@ -116,6 +116,8 @@ describe('daf-core', () => {
vp2.raw = 'mockJWT'
vp2.credentials = [vc]

await vp2.save()

const m = new Message()
m.from = id1
m.to = id2
Expand Down Expand Up @@ -157,12 +159,10 @@ describe('daf-core', () => {
expect(claims[0].value).toEqual('Alice')

const presentations = await Presentation.find({
where: {
audience: { did: 'did:test:333' },
},
relations: ["audience"],
where: Raw((alias) => `audience.did = "did:test:333"`)
})

console.log(presentations)
expect(presentations.length).toEqual(1)
})

Expand Down
18 changes: 15 additions & 3 deletions packages/daf-core/src/__tests__/resolvers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ describe('daf-core entities', () => {

afterAll(async () => {
await connection.close()
// fs.unlinkSync(databaseFile)
fs.unlinkSync(databaseFile)
})

test('search presentations by audience', async () => {
Expand All @@ -145,8 +145,20 @@ describe('daf-core entities', () => {
},
}

const messages = await Gql.Core.resolvers.Query.messages({}, query, { agent })
expect(messages.length).toBe(1)
let presentations = await Gql.Core.resolvers.Query.presentations({}, query, { agent })
expect(presentations.length).toBe(1)
// search when authenticated as the issuer
let authenticatedDid = 'did:test:111'

presentations = await Gql.Core.resolvers.Query.presentations({}, query, { agent, authenticatedDid })
expect(presentations.length).toBe(1)

// search when authenticated as another did
authenticatedDid = 'did:test:333'

presentations = await Gql.Core.resolvers.Query.presentations({}, query, { agent, authenticatedDid })
expect(presentations.length).toBe(0)

})

test('without auth it fetches all messages that match the query', async () => {
Expand Down
153 changes: 69 additions & 84 deletions packages/daf-core/src/graphql/graphql-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,48 @@ interface TypeOrmOrder {
[x: string]: 'ASC' | 'DESC'
}

function opToSQL(item: Where): any[] {
switch (item.op) {
case 'IsNull':
return ['IS NULL', '']
case 'Like':
if (item.value?.length != 1) throw Error('Operation Equal requires one value')
return ['LIKE :value', item.value[0]]
case 'Equal':
if (item.value?.length != 1) throw Error('Operation Equal requires one value')
return ['= :value', item.value[0]]
case 'Any':
case 'Between':
case 'LessThan':
case 'LessThanOrEqual':
case 'MoreThan':
case 'MoreThanOrEqual':
throw new Error(`${item.op} not compatable with DID argument`)
case 'In':
default:
return ['IN (:...value)', item.value]
}
}

function addAudienceQuery(input: FindInput, qb: SelectQueryBuilder<any>): SelectQueryBuilder<any> {
if (!Array.isArray(input.where)) {
return qb
}
const audienceWhere = input.where.find((item) => item.column === "audience")
if (!audienceWhere) {
return qb
}
const [op, value] = opToSQL(audienceWhere)
return qb.andWhere(`audience.did ${op}`, {value})
}

function createWhereObject(input: FindInput): any {
if (input?.where) {
const where = {}
for (const item of input.where) {
if (item.column === "audience") {
continue
}
switch (item.op) {
case 'Any':
where[item.column] = Any(item.value)
Expand Down Expand Up @@ -139,7 +177,7 @@ export interface FindArgs {
input?: FindInput
}

const messages = async (_: any, args: FindArgs, ctx: Context) => {
const messagesQuery = async (_: any, args: FindArgs, ctx: Context): Promise<SelectQueryBuilder<any>> => {
const where = createWhereObject(args.input)
let qb = (await ctx.agent.dbConnection)
.getRepository(Message)
Expand All @@ -157,30 +195,18 @@ const messages = async (_: any, args: FindArgs, ctx: Context) => {
}),
)
}
return qb.getMany()
return qb
}

const messages = async (_: any, args: FindArgs, ctx: Context) => {
return (await messagesQuery(_, args, ctx)).getMany()
}

const messagesCount = async (_: any, args: FindArgs, ctx: Context) => {
const where = createWhereObject(args.input)
let qb = (await ctx.agent.dbConnection)
.getRepository(Message)
.createQueryBuilder('message')
.leftJoinAndSelect('message.from', 'from')
.leftJoinAndSelect('message.to', 'to')
.where(where)
qb = decorateQB(qb, 'message', args.input)
if (ctx.authenticatedDid) {
qb = qb.andWhere(
new Brackets(qb => {
qb.where('message.to = :ident', { ident: ctx.authenticatedDid }).orWhere('message.from = :ident', {
ident: ctx.authenticatedDid,
})
}),
)
}
return (await qb.select('COUNT(message.id)').getRawOne())['COUNT("message"."id")']
return (await messagesQuery(_, args, ctx)).getCount()
}

const presentations = async (_: any, args: FindArgs, ctx: Context) => {
const presentationsQuery = async (_: any, args: FindArgs, ctx: Context) => {
const where = createWhereObject(args.input)
let qb = (await ctx.agent.dbConnection)
.getRepository(Presentation)
Expand All @@ -189,6 +215,7 @@ const presentations = async (_: any, args: FindArgs, ctx: Context) => {
.leftJoinAndSelect('presentation.audience', 'audience')
.where(where)
qb = decorateQB(qb, 'presentation', args.input)
qb = addAudienceQuery(args.input, qb)
if (ctx.authenticatedDid) {
qb = qb.andWhere(
new Brackets(qb => {
Expand All @@ -198,31 +225,18 @@ const presentations = async (_: any, args: FindArgs, ctx: Context) => {
}),
)
}
return qb.getMany()
return qb
}

const presentations = async (_: any, args: FindArgs, ctx: Context) => {
return (await presentationsQuery(_, args, ctx)).getMany()
}

const presentationsCount = async (_: any, args: FindArgs, ctx: Context) => {
const where = createWhereObject(args.input)
let qb = (await ctx.agent.dbConnection)
.getRepository(Presentation)
.createQueryBuilder('presentation')
.leftJoinAndSelect('presentation.issuer', 'issuer')
.leftJoinAndSelect('presentation.audience', 'audience')
.where(where)
qb = decorateQB(qb, 'presentation', args.input)
if (ctx.authenticatedDid) {
qb = qb.andWhere(
new Brackets(qb => {
qb.where('audience.did = :ident', {
ident: ctx.authenticatedDid,
}).orWhere('presentation.issuer = :ident', { ident: ctx.authenticatedDid })
}),
)
}
return qb.select('COUNT(presentation.hash)').getRawOne()
return (await presentationsQuery(_, args, ctx)).getCount()
}

const credentials = async (_: any, args: FindArgs, ctx: Context) => {
const credentialsQuery = async (_: any, args: FindArgs, ctx: Context) => {
const where = createWhereObject(args.input)
let qb = (await ctx.agent.dbConnection)
.getRepository(Credential)
Expand All @@ -243,34 +257,19 @@ const credentials = async (_: any, args: FindArgs, ctx: Context) => {
}),
)
}
return qb.getMany()
return qb
}

const credentials = async (_: any, args: FindArgs, ctx: Context) => {
return (await credentialsQuery(_, args, ctx)).getMany()
}

const credentialsCount = async (_: any, args: FindArgs, ctx: Context) => {
const where = createWhereObject(args.input)
let qb = (await ctx.agent.dbConnection)
.getRepository(Credential)
.createQueryBuilder('credential')
.leftJoinAndSelect('credential.issuer', 'issuer')
.leftJoinAndSelect('credential.subject', 'subject')
.where(where)
qb = decorateQB(qb, 'credential', args.input)
if (ctx.authenticatedDid) {
qb = qb.andWhere(
new Brackets(qb => {
qb.where('credential.subject = :ident', { ident: ctx.authenticatedDid }).orWhere(
'credential.issuer = :ident',
{
ident: ctx.authenticatedDid,
},
)
}),
)
}
return qb.select('COUNT(credential.hash)').getRawOne()
return (await credentialsQuery(_, args, ctx)).getCount()
}

const claims = async (_: any, args: FindArgs, ctx: Context) => {

const claimsQuery = async (_: any, args: FindArgs, ctx: Context) => {
const where = createWhereObject(args.input)
let qb = (await ctx.agent.dbConnection)
.getRepository(Claim)
Expand All @@ -289,29 +288,15 @@ const claims = async (_: any, args: FindArgs, ctx: Context) => {
}),
)
}
return qb.getMany()
return qb
}

const claims = async (_: any, args: FindArgs, ctx: Context) => {
return (await claimsQuery(_, args, ctx)).getMany()
}

const claimsCount = async (_: any, args: FindArgs, ctx: Context) => {
const where = createWhereObject(args.input)
let qb = (await ctx.agent.dbConnection)
.getRepository(Claim)
.createQueryBuilder('claim')
.leftJoinAndSelect('claim.issuer', 'issuer')
.leftJoinAndSelect('claim.subject', 'subject')
.where(where)
qb = decorateQB(qb, 'claim', args.input)
qb = qb.leftJoinAndSelect('claim.credential', 'credential')
if (ctx.authenticatedDid) {
qb = qb.andWhere(
new Brackets(qb => {
qb.where('claim.subject = :ident', { ident: ctx.authenticatedDid }).orWhere('claim.issuer = :ident', {
ident: ctx.authenticatedDid,
})
}),
)
}
return qb.select('COUNT(claim.hash)').getRawOne()
return (await claimsQuery(_, args, ctx)).getCount()
}

export const resolvers = {
Expand Down

0 comments on commit 40408dc

Please sign in to comment.