-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into feat/ocrvs-7978/qr-reader
- Loading branch information
Showing
22 changed files
with
516 additions
and
79 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
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
17 changes: 17 additions & 0 deletions
17
packages/events/src/router/__snapshots__/locations.set.test.ts.snap
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,17 @@ | ||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
||
exports[`Prevents sending empty payload 1`] = ` | ||
[TRPCError: [ | ||
{ | ||
"code": "too_small", | ||
"minimum": 1, | ||
"type": "array", | ||
"inclusive": true, | ||
"exact": false, | ||
"message": "Array must contain at least 1 element(s)", | ||
"path": [] | ||
} | ||
]] | ||
`; | ||
|
||
exports[`prevents unauthorized access from registrar 1`] = `[TRPCError: UNAUTHORIZED]`; |
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,45 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
* | ||
* OpenCRVS is also distributed under the terms of the Civil Registration | ||
* & Healthcare Disclaimer located at http://opencrvs.org/license. | ||
* | ||
* Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS. | ||
*/ | ||
import { createTestClient } from '@events/tests/utils' | ||
import { payloadGenerator } from '@events/tests/generators' | ||
import { userScopes } from '@opencrvs/commons' | ||
|
||
const nationalSystemAdminClient = createTestClient([ | ||
userScopes.nationalSystemAdmin | ||
]) | ||
const generator = payloadGenerator() | ||
|
||
test('Returns empty list when no locations are set', async () => { | ||
const locations = await nationalSystemAdminClient.locations.get() | ||
|
||
expect(locations).toEqual([]) | ||
}) | ||
|
||
test('Returns single location in right format', async () => { | ||
const setLocationPayload = [ | ||
{ id: '123-456-789', partOf: null, name: 'Location foobar' } | ||
] | ||
|
||
await nationalSystemAdminClient.locations.set(setLocationPayload) | ||
|
||
const locations = await nationalSystemAdminClient.locations.get() | ||
|
||
expect(locations).toHaveLength(1) | ||
expect(locations).toMatchObject(setLocationPayload) | ||
}) | ||
|
||
test('Returns multiple locations', async () => { | ||
await nationalSystemAdminClient.locations.set(generator.locations.set(5)) | ||
|
||
const locations = await nationalSystemAdminClient.locations.get() | ||
|
||
expect(locations).toHaveLength(5) | ||
}) |
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,95 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
* | ||
* OpenCRVS is also distributed under the terms of the Civil Registration | ||
* & Healthcare Disclaimer located at http://opencrvs.org/license. | ||
* | ||
* Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS. | ||
*/ | ||
import { createTestClient } from '@events/tests/utils' | ||
import { payloadGenerator } from '@events/tests/generators' | ||
import { userScopes } from '@opencrvs/commons' | ||
|
||
const nationalSystemAdminClient = createTestClient([ | ||
userScopes.nationalSystemAdmin | ||
]) | ||
|
||
const registrarClient = createTestClient() | ||
|
||
const generator = payloadGenerator() | ||
|
||
test('prevents unauthorized access from registrar', async () => { | ||
await expect( | ||
registrarClient.locations.set([]) | ||
).rejects.toThrowErrorMatchingSnapshot() | ||
}) | ||
|
||
test('Allows national system admin to set locations', async () => { | ||
await expect( | ||
nationalSystemAdminClient.locations.set(generator.locations.set(1)) | ||
).resolves.toEqual(undefined) | ||
}) | ||
|
||
test('Prevents sending empty payload', async () => { | ||
await expect( | ||
nationalSystemAdminClient.locations.set([]) | ||
).rejects.toThrowErrorMatchingSnapshot() | ||
}) | ||
|
||
test('Creates single location', async () => { | ||
const locationPayload = [ | ||
{ id: '123-456-789', partOf: null, name: 'Location foobar' } | ||
] | ||
|
||
await nationalSystemAdminClient.locations.set(locationPayload) | ||
|
||
const locations = await nationalSystemAdminClient.locations.get() | ||
|
||
expect(locations).toHaveLength(1) | ||
expect(locations).toMatchObject(locationPayload) | ||
}) | ||
|
||
test('Creates multiple locations', async () => { | ||
const parentId = 'parent-id' | ||
|
||
const locationPayload = generator.locations.set([ | ||
{ id: 'parentId' }, | ||
{ partOf: parentId }, | ||
{ partOf: parentId }, | ||
{} | ||
]) | ||
|
||
await nationalSystemAdminClient.locations.set(locationPayload) | ||
|
||
const locations = await nationalSystemAdminClient.locations.get() | ||
|
||
expect(locations).toEqual(locationPayload) | ||
}) | ||
|
||
test('Removes existing locations not in payload', async () => { | ||
const initialPayload = generator.locations.set(5) | ||
|
||
await nationalSystemAdminClient.locations.set(initialPayload) | ||
|
||
const initialLocations = await nationalSystemAdminClient.locations.get() | ||
expect(initialLocations).toHaveLength(initialPayload.length) | ||
|
||
const [removedLocation, ...remainingLocationsPayload] = initialPayload | ||
|
||
await nationalSystemAdminClient.locations.set(remainingLocationsPayload) | ||
|
||
const remainingLocationsAfterDeletion = | ||
await nationalSystemAdminClient.locations.get() | ||
|
||
expect(remainingLocationsAfterDeletion).toHaveLength( | ||
remainingLocationsPayload.length | ||
) | ||
|
||
expect( | ||
remainingLocationsAfterDeletion.some( | ||
(location) => location.id === removedLocation.id | ||
) | ||
).toBe(false) | ||
}) |
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,64 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
* | ||
* OpenCRVS is also distributed under the terms of the Civil Registration | ||
* & Healthcare Disclaimer located at http://opencrvs.org/license. | ||
* | ||
* Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS. | ||
*/ | ||
|
||
import { inScope, Scope, userScopes } from '@opencrvs/commons' | ||
import { TRPCError, AnyTRPCMiddlewareFunction } from '@trpc/server' | ||
|
||
import { z } from 'zod' | ||
|
||
const ContextSchema = z.object({ | ||
user: z.object({ | ||
id: z.string(), | ||
primaryOfficeId: z.string() | ||
}), | ||
token: z.string() | ||
}) | ||
|
||
export type Context = z.infer<typeof ContextSchema> | ||
|
||
/** | ||
* TRPC Middleware options with correct context. | ||
* Actual middleware type definition is only for internal use within TRPC. | ||
*/ | ||
type MiddlewareOptions = Omit< | ||
Parameters<AnyTRPCMiddlewareFunction>[0], | ||
'ctx' | ||
> & { ctx: Context } | ||
|
||
/** | ||
* Depending on how the API is called, there might or might not be Bearer keyword in the header. | ||
* To allow for usage with both direct HTTP calls and TRPC, ensure it's present to be able to use shared scope auth functions. | ||
*/ | ||
const setBearerForToken = (token: string) => { | ||
const bearer = 'Bearer' | ||
|
||
return token.startsWith(bearer) ? token : `${bearer} ${token}` | ||
} | ||
/** | ||
* @param scopes scopes that are allowed to access the resource | ||
* @returns TRPC compatible middleware function | ||
*/ | ||
const createScopeAuthMiddleware = | ||
(scopes: Scope[]) => (opts: MiddlewareOptions) => { | ||
if (inScope({ Authorization: setBearerForToken(opts.ctx.token) }, scopes)) { | ||
return opts.next() | ||
} | ||
|
||
throw new TRPCError({ code: 'UNAUTHORIZED' }) | ||
} | ||
|
||
const isNationalSystemAdminUser = createScopeAuthMiddleware([ | ||
userScopes.nationalSystemAdmin | ||
]) | ||
|
||
export const middleware = { | ||
isNationalSystemAdminUser | ||
} |
Oops, something went wrong.