Skip to content

Commit

Permalink
test(framework): change failing test case scenario
Browse files Browse the repository at this point in the history
  • Loading branch information
alpharder committed Jul 10, 2024
1 parent 8776eb0 commit 30d9d23
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 92 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { eventDispatcher } from '@deepkit/event';
import { httpWorkflow } from '@deepkit/http';
import { AuthenticationModule } from './auth-module.js';
import { SessionForRequest } from './session-for-request.dto.js';

export class AuthenticationListener {
@eventDispatcher.listen(httpWorkflow.onAuth)
onServerMainBootstrap(event: typeof httpWorkflow.onAuth.event, module: AuthenticationModule) {
event.injectorContext.set(
SessionForRequest,
new SessionForRequest(
'sidValue',
'uidValue',
),
module
);
}
}
23 changes: 23 additions & 0 deletions packages/framework/tests/provider-visibility-issue/auth-module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { createModule } from '@deepkit/app';
import { SessionForRequest } from './session-for-request.dto.js';
import { AuthenticationListener } from './auth-listener.js';
import { http } from '@deepkit/http';


@http.controller('/auth')
class OAuth2Controller {
@http.GET('/whoami')
whoami(sess: SessionForRequest) {
// Trying to consume "SessionForRequest" within the same module it's provided – no luck
return sess;
}
}

export class AuthenticationModule extends createModule({
providers: [
{ provide: SessionForRequest, scope: 'http', useValue: undefined },
],
controllers: [OAuth2Controller],
listeners: [AuthenticationListener],
exports: [SessionForRequest],
}) { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { expect, test } from '@jest/globals';
import { http, HttpKernel, HttpRequest } from '@deepkit/http';
import { App, createModule } from '@deepkit/app';
import { FrameworkModule } from '../../src/module.js';
import { SessionForRequest } from './session-for-request.dto.js';
import { AuthenticationModule } from './auth-module.js';

test('provider-visibility-issue', async () => {
class IAMModule extends createModule({ exports: [AuthenticationModule] }) {
imports = [new AuthenticationModule()];
}


@http.controller('/another')
class AnotherDomainModuleController {
@http.GET('/action')
action(sess: SessionForRequest) {
// Trying to consume "SessionForRequest" within another module – still no luck
return sess;
}
}
class AnotherDomainModule extends createModule({}) {
controllers = [AnotherDomainModuleController];
}

class DomainModule extends createModule({ exports: [IAMModule] }) {
imports = [new IAMModule(), new AnotherDomainModule()];
}

class InfrastructureModule extends createModule({
// The whole issue gets "fixed" if DomainModule gets exported here, but what if I don't need to export it?
exports: [],
}) {
imports = [new DomainModule()];
}

const app = new App({
imports: [
new FrameworkModule({ debug: true }),
new InfrastructureModule(),
],
providers: [],
});

/**
* These fail due to the following error:
*
* Controller for route /auth/whoami parameter resolving error:
* DependenciesUnmetError: Parameter sess is required but provider returned undefined.
*/
expect((await app.get(HttpKernel).request(HttpRequest.GET('/auth/whoami'))).json).toMatchObject({
sessionId: 'sidValue',
userId: 'uidValue'
});
expect((await app.get(HttpKernel).request(HttpRequest.GET('/another/action'))).json).toMatchObject({
sessionId: 'sidValue',
userId: 'uidValue'
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export class SessionForRequest {
constructor(
public readonly sessionId: string,
public readonly userId: string,
) {}
}
92 changes: 0 additions & 92 deletions packages/framework/tests/service-container.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,95 +188,3 @@ test('database injection useClass with defaults', () => {
const registry = app.get(DatabaseRegistry);
expect(registry.getDatabases()).toHaveLength(1);
});


test('provider-visibility-issue', async () => {
class SessionForRequest {
constructor(
public readonly sessionId: string,
public readonly userId: string,
) {}
}

class AuthenticationListener {
@eventDispatcher.listen(httpWorkflow.onAuth)
onServerMainBootstrap(event: typeof httpWorkflow.onAuth.event) {
event.injectorContext.set(
SessionForRequest,
new SessionForRequest(
'sidValue',
'uidValue',
),
);
}
}

@http.controller('/auth')
class OAuth2Controller {
@http.GET('/whoami')
whoami(sess: SessionForRequest) {
// Trying to consume "SessionForRequest" within the same module it's provided – no luck
return sess;
}
}

class AuthenticationModule extends createModule({
providers: [
{ provide: SessionForRequest, scope: 'http', useValue: undefined },
],
controllers: [OAuth2Controller],
listeners: [AuthenticationListener],
exports: [SessionForRequest],
}) { }

class IAMModule extends createModule({ exports: [AuthenticationModule] }) {
imports = [new AuthenticationModule()];
}


@http.controller('/another')
class AnotherDomainModuleController {
@http.GET('/action')
action(sess: SessionForRequest) {
// Trying to consume "SessionForRequest" within another module – still no luck
return sess;
}
}
class AnotherDomainModule extends createModule({}) {
controllers = [AnotherDomainModuleController];
}

class DomainModule extends createModule({ exports: [IAMModule] }) {
imports = [new IAMModule(), new AnotherDomainModule()];
}

class InfrastructureModule extends createModule({
// The whole issue gets "fixed" if DomainModule gets exported here, but what if I don't need to export it?
exports: [],
}) {
imports = [new DomainModule()];
}

const app = new App({
imports: [
new FrameworkModule({ debug: true }),
new InfrastructureModule(),
],
providers: [],
});

/**
* These fail due to the following error:
*
* Controller for route /auth/whoami parameter resolving error:
* DependenciesUnmetError: Parameter sess is required but provider returned undefined.
*/
expect((await app.get(HttpKernel).request(HttpRequest.GET('/auth/whoami'))).json).toMatchObject({
sessionId: 'sidValue',
userId: 'uidValue'
});
expect((await app.get(HttpKernel).request(HttpRequest.GET('/another/action'))).json).toMatchObject({
sessionId: 'sidValue',
userId: 'uidValue'
});
});

0 comments on commit 30d9d23

Please sign in to comment.