Skip to content

Commit

Permalink
test: fix all prime-core tests
Browse files Browse the repository at this point in the history
  • Loading branch information
birkir committed Sep 24, 2019
1 parent 4aef06c commit 38c4244
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 38 deletions.
4 changes: 1 addition & 3 deletions packages/prime-core/__tests__/modules/external.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@ describe('InternalModule', () => {
const context: any = { req: { headers: {} } };

const query = async (document, which = external, ctx = context) => {
const contextValue = await which.context(ctx);
contextValue.user = user;
return execute({
schema: which.schema,
contextValue,
contextValue: { user },
document,
});
};
Expand Down
22 changes: 12 additions & 10 deletions packages/prime-core/__tests__/modules/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,21 @@ describe('InternalModule', () => {
let queries: any;
let context: any;
let user: User;
let info: any;

beforeAll(async () => {
connection = await connect(process.env.TEST_DATABASE_URL);
internal = await createInternal(connection);
mutations = internal.resolvers.Mutation;
queries = internal.resolvers.Query;
user = getRepository(User).create({ username: 'test ' });
info = { session: { user } };
});

beforeEach(async () => {
const requestId = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER);
const container = Container.of(requestId);
context = { requestId, container, user };
context = { requestId, container, user, session: { user } };
container.set('context', context);

await connection.dropDatabase();
Expand All @@ -39,43 +41,43 @@ describe('InternalModule', () => {
describe('Webhook', () => {
it('should be able to create webhooks', async () => {
const input = { name: 'Hello', url: 'https://example.com', method: 'PUT' };
const webhook = await mutations.createWebhook.resolve({}, { input }, context);
const webhook = await mutations.createWebhook({}, { input }, context, info);
expect(webhook).toBeTruthy();
const result = await queries.Webhook.resolve({}, { id: webhook.id }, context);
const result = await queries.Webhook({}, { id: webhook.id }, context, info);
expect(result.id).toEqual(webhook.id);
});

it('should have no webhooks', async () => {
const result = await queries.allWebhooks.resolve({}, { order: 'id_ASC' }, context);
const result = await queries.allWebhooks({}, { order: 'id_ASC' }, context, info);
expect(result.edges).toHaveLength(0);
expect(result.totalCount).toEqual(0);
});

it('should be able to update webhooks', async () => {
const input = { name: 'Hello', url: 'https://example.com', method: 'PUT' };
const webhook = await mutations.createWebhook.resolve({}, { input }, context);
const webhook = await mutations.createWebhook({}, { input }, context, info);
expect(webhook).toBeTruthy();
const updatedUrl = 'http://noop.com';
const updateVariables = { id: webhook.id, input: { ...webhook, url: updatedUrl } };
const update = await mutations.updateWebhook.resolve({}, updateVariables, context);
const update = await mutations.updateWebhook({}, updateVariables, context, info);
expect(update.url).toBe(updatedUrl);
expect(update.name).toBe(webhook.name);
});

it('should be able to remove webhooks', async () => {
const input = { name: 'Hello', url: 'https://example.com', method: 'PUT' };
const { id } = await mutations.createWebhook.resolve({}, { input }, context);
const result = await mutations.removeWebhook.resolve({}, { id }, context);
const { id } = await mutations.createWebhook({}, { input }, context, info);
const result = await mutations.removeWebhook({}, { id }, context, info);
expect(result).toBeTruthy();
const webhook = await queries.Webhook.resolve({}, { id }, context);
const webhook = await queries.Webhook({}, { id }, context, info);
expect(webhook).toBeFalsy();
});
});

describe('Authentication', () => {
it('should be authorized', async () => {
try {
await queries.Webhook.resolve({}, { id: 'nah' }, { ...context, user: null });
await queries.Webhook({}, { id: 'nah' }, { ...context, user: null }, { session: {} });
throw new Error();
} catch (e) {
expect(e.message).toContain('Must be authenticated');
Expand Down
2 changes: 1 addition & 1 deletion packages/prime-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"@primecms/field-slice": "^0.3.4-beta.1",
"@primecms/field-string": "^0.3.4-beta.1",
"apollo-server-express": "^2.9.3",
"class-validator": "^0.9.0",
"class-validator": "0.9.1",
"cors": "^2.8.0",
"dataloader": "^1.4.0",
"debug": "^4.0.0",
Expand Down
3 changes: 2 additions & 1 deletion packages/prime-core/src/modules/external/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,8 @@ export const createExternal = async (connection: Connection) => {
name: 'prime-graphql',
extraSchemas: [graphqlSchema],
resolvers: unionResolvers,
async context({ req }) {
async context(session, currentContext) {
const { req } = session;
const requestId = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER);
log('requestId', requestId);
const container = Container.of(requestId);
Expand Down
1 change: 1 addition & 0 deletions packages/prime-core/src/modules/internal/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export const createInternal = async (connection: Connection) => {
const ctx = {
requestId,
container,
user: ((session || {}) as any).user,
ability: createAbility(currentContext),
};

Expand Down
33 changes: 18 additions & 15 deletions packages/prime-core/src/modules/internal/utils/Authorized.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,25 @@ import { UseMiddleware } from 'type-graphql';
import { Context } from '../../../interfaces/Context';

export function Authorized(ruleFn?: any) {
return UseMiddleware(async ({ args, context }: { args: any; context: Context }, next) => {
if (!context.user) {
throw new AuthenticationError('Must be authenticated');
}
return UseMiddleware(
async ({ args, context, info }: { args: any; context: Context; info: any }, next) => {
const { session = {} } = info || {};
if (!context.user && !session.user) {
throw new AuthenticationError('Must be authenticated');
}

if (ruleFn) {
ruleFn(
{
can: (action: string, subject: any, field?: string) => {
context.ability.throwUnlessCan(action, subject, field);
if (ruleFn) {
ruleFn(
{
can: (action: string, subject: any, field?: string) => {
context.ability.throwUnlessCan(action, subject, field);
},
},
},
args
);
}
args
);
}

return next();
});
return next();
}
);
}
16 changes: 8 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5096,6 +5096,14 @@ class-utils@^0.3.5:
isobject "^3.0.0"
static-extend "^0.1.1"

class-validator@0.9.1:
version "0.9.1"
resolved "https://registry.yarnpkg.com/class-validator/-/class-validator-0.9.1.tgz#d60e58c5d14abca0a41bce38cf792ad4c46d1531"
integrity sha512-3wApflrd3ywVZyx4jaasGoFt8pmo4aGLPPAEKCKCsTRWVGPilahD88q3jQjRQwja50rl9a7rsP5LAxJYwGK8/Q==
dependencies:
google-libphonenumber "^3.1.6"
validator "10.4.0"

class-validator@>=0.9.1:
version "0.10.0"
resolved "https://registry.yarnpkg.com/class-validator/-/class-validator-0.10.0.tgz#5e5f8108fa200d5ce1906cd5537af102c072dbcd"
Expand All @@ -5105,14 +5113,6 @@ class-validator@>=0.9.1:
google-libphonenumber "^3.1.6"
validator "11.1.0"

class-validator@^0.9.0:
version "0.9.1"
resolved "https://registry.yarnpkg.com/class-validator/-/class-validator-0.9.1.tgz#d60e58c5d14abca0a41bce38cf792ad4c46d1531"
integrity sha512-3wApflrd3ywVZyx4jaasGoFt8pmo4aGLPPAEKCKCsTRWVGPilahD88q3jQjRQwja50rl9a7rsP5LAxJYwGK8/Q==
dependencies:
google-libphonenumber "^3.1.6"
validator "10.4.0"

classnames@2.x, classnames@^2.2.0, classnames@^2.2.1, classnames@^2.2.3, classnames@^2.2.5, classnames@^2.2.6, classnames@~2.2.6:
version "2.2.6"
resolved "https://registry.yarnpkg.com/classnames/-/classnames-2.2.6.tgz#43935bffdd291f326dad0a205309b38d00f650ce"
Expand Down

0 comments on commit 38c4244

Please sign in to comment.