-
Notifications
You must be signed in to change notification settings - Fork 90
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
refactor(backend): add getOrCreate
method for grantReferenceService
#787
Changes from 11 commits
e6bfa1a
ba6afb5
577595d
39054c9
6129c0d
04bd508
a5a56c6
28860c8
9f63dd9
84a2e18
6378e3f
c6eaaa9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -9,6 +9,7 @@ import { Config } from '../../config/app' | |||||
import { GrantReferenceService } from './service' | ||||||
import { truncateTables } from '../../tests/tableManager' | ||||||
import { GrantReference } from './model' | ||||||
import { AccessAction } from '../auth/grant' | ||||||
|
||||||
describe('Grant Reference Service', (): void => { | ||||||
let deps: IocContract<AppServices> | ||||||
|
@@ -79,4 +80,60 @@ describe('Grant Reference Service', (): void => { | |||||
) | ||||||
}) | ||||||
}) | ||||||
|
||||||
describe('Get or Create Grant Reference', (): void => { | ||||||
test('returns undefined for a non-existing grant reference', async (): Promise<void> => { | ||||||
expect( | ||||||
await grantReferenceService.getOrCreate( | ||||||
{ id: uuid(), clientId: uuid() }, | ||||||
AccessAction.List | ||||||
) | ||||||
).toBeUndefined() | ||||||
}) | ||||||
|
||||||
test('throws an error when clientId does not match', async (): Promise<void> => { | ||||||
const id = uuid() | ||||||
|
||||||
await grantReferenceService.create({ | ||||||
id, | ||||||
clientId: uuid() | ||||||
}) | ||||||
|
||||||
await expect( | ||||||
async () => | ||||||
await grantReferenceService.getOrCreate( | ||||||
{ id, clientId: uuid() }, | ||||||
AccessAction.List | ||||||
) | ||||||
).rejects.toThrowError('does not match internal reference clientId') | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought we would have needed a RegEx here, but it seems to work?
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was thinking the same, but based on Jest docs, RegEx and strings are equivalent in this case. |
||||||
}) | ||||||
|
||||||
test('fetch an existing grant reference', async (): Promise<void> => { | ||||||
const existingRef = await grantReferenceService.create({ | ||||||
id: uuid(), | ||||||
clientId: uuid() | ||||||
}) | ||||||
|
||||||
const retrievedRef = await grantReferenceService.getOrCreate( | ||||||
existingRef, | ||||||
AccessAction.List | ||||||
) | ||||||
|
||||||
expect(retrievedRef).toEqual(existingRef) | ||||||
}) | ||||||
|
||||||
test('creates a grant reference', async (): Promise<void> => { | ||||||
const receivedRef = { | ||||||
id: uuid(), | ||||||
clientId: uuid() | ||||||
} | ||||||
|
||||||
const grantRef = await grantReferenceService.getOrCreate( | ||||||
receivedRef, | ||||||
AccessAction.Create | ||||||
) | ||||||
|
||||||
expect(grantRef).toEqual(receivedRef) | ||||||
}) | ||||||
}) | ||||||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import { Transaction, TransactionOrKnex } from 'objection' | ||
import { AccessAction } from '../auth/grant' | ||
import { GrantReference } from './model' | ||
|
||
export interface GrantReferenceService { | ||
|
@@ -8,13 +9,18 @@ export interface GrantReferenceService { | |
trx?: Transaction | ||
): Promise<GrantReference> | ||
lock(grantId: string, trx: TransactionOrKnex): Promise<void> | ||
getOrCreate( | ||
options: CreateGrantReferenceOptions, | ||
action: AccessAction | ||
): Promise<GrantReference> | ||
} | ||
|
||
export async function createGrantReferenceService(): Promise<GrantReferenceService> { | ||
return { | ||
get: (grantId, trx) => getGrantReference(grantId, trx), | ||
create: (options, trx) => createGrantReference(options, trx), | ||
lock: (grantId, trx) => lockGrantReference(grantId, trx) | ||
lock: (grantId, trx) => lockGrantReference(grantId, trx), | ||
getOrCreate: (options, action) => getOrCreateGrantReference(options, action) | ||
} | ||
} | ||
|
||
|
@@ -42,3 +48,26 @@ async function lockGrantReference(grantId: string, trx: TransactionOrKnex) { | |
.forNoKeyUpdate() | ||
.timeout(5000) | ||
} | ||
|
||
async function getOrCreateGrantReference( | ||
options: CreateGrantReferenceOptions, | ||
action: AccessAction | ||
) { | ||
const grant = await GrantReference.transaction(async (trx: Transaction) => { | ||
const grantRef = await getGrantReference(options.id, trx) | ||
if (grantRef) { | ||
if (grantRef.clientId !== options.clientId) { | ||
throw new Error( | ||
`clientID ${options.clientId} for grant ${options.id} does not match internal reference clientId ${grantRef.clientId}.` | ||
) | ||
} | ||
|
||
return grantRef | ||
} else if (action === AccessAction.Create) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this check should be moved out to the caller. |
||
// Grant and client ID's are only stored for create routes | ||
return await createGrantReference(options, trx) | ||
} | ||
}) | ||
|
||
return grant | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By moving the action check to the caller, the
clientId
checkrafiki/packages/backend/src/open_payments/grantReference/service.ts
Lines 58 to 63 in c6eaaa9
will probably need to be moved to the
getGrantRerefence
methodrafiki/packages/backend/src/open_payments/grantReference/service.ts
Lines 27 to 29 in c6eaaa9
and will have to take both arguments (
id
andclientId
)otherwise there will be no validation for the
clientId
when callinggrantReferenceService.get
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm leaning towards not passing
clientId
tograntReferenceService.get
(even though this is currently the only place calling it).What if we moved the
clientId
check out here?🤔 that looks a lot like how it was before...
Is there an argument for not actually using
getOrCreate
?It looks like the
AccessAction.Create
check was added after this issue was opened: