Skip to content

Commit

Permalink
Add tests for authentication schema, improve code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
daffl committed Apr 4, 2022
1 parent 5b2a20a commit 5a49020
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 12 deletions.
1 change: 1 addition & 0 deletions packages/authentication/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
},
"devDependencies": {
"@feathersjs/memory": "^5.0.0-pre.17",
"@feathersjs/schema": "^5.0.0-pre.17",
"@types/lodash": "^4.14.178",
"@types/mocha": "^9.1.0",
"@types/node": "^17.0.15",
Expand Down
20 changes: 11 additions & 9 deletions packages/authentication/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,17 @@ export const authenticationSettingsSchema = {
},
jwt: {
type: 'object',
header: {
type: 'string',
default: 'Authorization',
description: 'The HTTP header containing the JWT'
},
schemes: {
type: 'array',
items: { type: 'string' },
description: 'An array of schemes to support'
properties: {
header: {
type: 'string',
default: 'Authorization',
description: 'The HTTP header containing the JWT'
},
schemes: {
type: 'array',
items: { type: 'string' },
description: 'An array of schemes to support'
}
}
},
local: {
Expand Down
17 changes: 17 additions & 0 deletions packages/authentication/test/core.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import assert from 'assert';
import { feathers, Application } from '@feathersjs/feathers';
import jwt from 'jsonwebtoken';
import { Infer, schema } from '@feathersjs/schema';

import { AuthenticationBase, AuthenticationRequest } from '../src/core';
import { authenticationSettingsSchema } from '../src/options';
import { Strategy1, Strategy2, MockRequest } from './fixtures';
import { ServerResponse } from 'http';

Expand Down Expand Up @@ -31,6 +33,21 @@ describe('authentication/core', () => {
});

describe('configuration', () => {
it('infers configuration from settings schema', async () => {
const settingsSchema = schema({
$id: 'AuthSettingsSchema',
...authenticationSettingsSchema
} as const);
type Settings = Infer<typeof settingsSchema>;
const config: Settings = {
entity: 'user',
secret: 'supersecret',
authStrategies: [ 'some', 'thing' ]
}

await settingsSchema.validate(config);
});

it('throws an error when app is not provided', () => {
try {
// @ts-ignore
Expand Down
2 changes: 0 additions & 2 deletions packages/schema/src/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ export type PropertyResolverMap<T, C> = {
}

export interface ResolverConfig<T, C> {
// TODO this should be `Schema<any>` but has recently produced an error, see
// https://github.com/ThomasAribart/json-schema-to-ts/issues/53
schema?: Schema<T>,
validate?: 'before'|'after'|false,
properties: PropertyResolverMap<T, C>
Expand Down
4 changes: 3 additions & 1 deletion packages/schema/test/fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { GeneralError } from '@feathersjs/errors';

import {
schema, resolve, Infer, resolveResult,
queryProperty, resolveQuery, resolveData
queryProperty, resolveQuery, resolveData, validateData, validateQuery
} from '../src';

export const userSchema = schema({
Expand Down Expand Up @@ -148,6 +148,7 @@ const app = feathers<ServiceTypes>()
.use('messages', memory());

app.service('messages').hooks([
validateQuery(messageQuerySchema),
resolveQuery(messageQueryResolver),
resolveResult(messageResultResolver)
]);
Expand All @@ -158,6 +159,7 @@ app.service('users').hooks([

app.service('users').hooks({
create: [
validateData(userSchema),
resolveData(userDataResolver)
]
});
Expand Down

0 comments on commit 5a49020

Please sign in to comment.