Skip to content

Commit

Permalink
test: test guards
Browse files Browse the repository at this point in the history
  • Loading branch information
rauno56 committed Jul 6, 2021
1 parent 4b0917d commit dee9c41
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 32 deletions.
Expand Up @@ -79,54 +79,58 @@ describe('nestjs-core', () => {

after(() => {});

it('happy paths', async () => {
it('should capture requests', async () => {
const path = semver.intersects(LIB_VERSION, '<5.0.0') ? '/' : '/users';
const url = '/users';
const instance = 'UsersController';
const callback = 'getUsers';

assert.strictEqual(await request('/users'), 'Hello, world!\n');

assertSpans(memoryExporter.getFinishedSpans(), [
{ service: 'test', name: 'nest.factory.create', module: 'AppModule' },
{
service: 'test',
name: 'nest.guard.canActivate.UsersController(getUsers)',
name: `nest.guard.canActivate.${instance}(${callback})`,
method: 'GET',
url,
path,
instance: 'UsersController',
callback: 'getUsers',
instance,
callback,
parentSpanIdx: 2,
},
{
service: 'test',
name: 'UsersController(getUsers)',
name: `${instance}(${callback})`,
method: 'GET',
url,
path,
callback: 'getUsers',
callback,
},
{
service: 'test',
name: 'nest.interceptor.intercept',
method: 'GET',
url,
path,
instance: 'UsersController',
callback: 'getUsers',
instance,
callback,
parentSpanIdx: 2,
},
{
service: 'test',
name: 'getUsers',
callback: 'getUsers',
name: callback,
callback,
parentSpanIdx: 3,
},
]);
});

it('should properly capture errors', async () => {
it('should capture errors', async () => {
const path = semver.intersects(LIB_VERSION, '<5.0.0') ? '/' : '/errors';
const url = '/errors';
const instance = 'ErrorController';
const callback = 'getErrors';

assert.strictEqual(
await request('/errors'),
Expand All @@ -137,36 +141,36 @@ describe('nestjs-core', () => {
{ service: 'test', name: 'nest.factory.create', module: 'AppModule' },
{
service: 'test',
name: 'nest.guard.canActivate.ErrorController(getErrors)',
name: `nest.guard.canActivate.${instance}(${callback})`,
method: 'GET',
url,
path,
instance: 'ErrorController',
callback: 'getErrors',
instance,
callback,
parentSpanIdx: 2,
},
{
service: 'test',
name: 'ErrorController(getErrors)',
name: `${instance}(${callback})`,
method: 'GET',
url,
path,
callback: 'getErrors',
callback,
},
{
service: 'test',
name: 'nest.interceptor.intercept',
method: 'GET',
url,
path,
instance: 'ErrorController',
callback: 'getErrors',
instance,
callback,
parentSpanIdx: 2,
},
{
service: 'test',
name: 'getErrors',
callback: 'getErrors',
name: callback,
callback,
status: {
code: SpanStatusCode.ERROR,
message: 'custom error',
Expand All @@ -175,6 +179,53 @@ describe('nestjs-core', () => {
},
]);
});

it('should capture guards', async () => {
const path = semver.intersects(LIB_VERSION, '<5.0.0') ? '/' : '/guarded';
const url = '/guarded';
const instance = 'GuardedController';
const callback = 'getEndpoint';

assert.strictEqual(await request('/guarded'), 'Hello, guarded!\n');

assertSpans(memoryExporter.getFinishedSpans(), [
{ service: 'test', name: 'nest.factory.create', module: 'AppModule' },
{
service: 'test',
name: `MyGuard.tryActivate.${instance}(${callback})`,
method: 'GET',
url,
path,
instance,
callback,
parentSpanIdx: 2,
},
{
service: 'test',
name: `${instance}(${callback})`,
method: 'GET',
url,
path,
callback,
},
{
service: 'test',
name: 'nest.interceptor.intercept',
method: 'GET',
url,
path,
instance,
callback,
parentSpanIdx: 2,
},
{
service: 'test',
name: callback,
callback,
parentSpanIdx: 3,
},
]);
});
});

const assertSpans = (actualSpans: any[], expectedSpans: any[]) => {
Expand Down
Expand Up @@ -16,6 +16,7 @@
import * as http from 'http';
import * as semver from 'semver';
import { AddressInfo } from 'net';
import { Injectable, CanActivate, ExecutionContext } from '@nestjs/common';

// mimics the support for @decorators
const __decorate = function (
Expand Down Expand Up @@ -44,21 +45,60 @@ const __decorate = function (
};

export const setup = async version => {
let UsersController = class UsersController {};
let UsersController = class UsersController {
getUsers() {
return 'Hello, world!\n';
}
};
let GuardedController = class GuardedController {
getEndpoint() {
return 'Hello, guarded!\n';
}
};
let GuardedModule = class GuardedModule {};
let UsersModule = class UsersModule {};
let ErrorController = class ErrorController {};
let ErrorController = class ErrorController {
getErrors() {
throw new Error('custom error');
}
};
let ErrorModule = class ErrorModule {};
let AppModule = class AppModule {};
let MyGuard = class MyGuard implements CanActivate {
canActivate(context: ExecutionContext): boolean {
return true;
}
}

// const core = require(`../../versions/@nestjs/core@${version}`).get()
// const common = require(`../../versions/@nestjs/core@${version}/node_modules/@nestjs/common`)
const core = require('@nestjs/core');
const common = require('@nestjs/common');

MyGuard = __decorate([common.Injectable()], MyGuard);

GuardedController = __decorate([common.Controller('guarded'), common.UseGuards(MyGuard)], GuardedController);
Object.defineProperty(
GuardedController.prototype,
'getEndpoint',
__decorate(
[common.Get()],
GuardedController.prototype,
'getEndpoint',
Object.getOwnPropertyDescriptor(GuardedController.prototype, 'getEndpoint')
)
);

GuardedModule = __decorate(
[
common.Module({
controllers: [GuardedController],
}),
],
GuardedModule
);

UsersController = __decorate([common.Controller('users')], UsersController);
UsersController.prototype.getUsers = function getUsers() {
return 'Hello, world!\n';
};
Object.defineProperty(
UsersController.prototype,
'getUsers',
Expand All @@ -80,9 +120,6 @@ export const setup = async version => {
);

ErrorController = __decorate([common.Controller('errors')], ErrorController);
ErrorController.prototype.getErrors = function getErrors() {
throw new Error('custom error');
};
Object.defineProperty(
ErrorController.prototype,
'getErrors',
Expand All @@ -107,8 +144,8 @@ export const setup = async version => {
AppModule = __decorate(
[
common.Module({
imports: [UsersModule, ErrorModule],
controllers: [UsersController, ErrorController],
imports: [UsersModule, ErrorModule, GuardedModule],
controllers: [UsersController, ErrorController, GuardedController],
}),
],
AppModule
Expand All @@ -117,8 +154,8 @@ export const setup = async version => {
AppModule = __decorate(
[
common.Module({
modules: [UsersModule, ErrorModule],
controllers: [UsersController, ErrorController],
modules: [UsersModule, ErrorModule, GuardedModule],
controllers: [UsersController, ErrorController, GuardedController],
}),
],
AppModule
Expand Down

0 comments on commit dee9c41

Please sign in to comment.