-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(framework): change failing test case scenario
- Loading branch information
Showing
5 changed files
with
106 additions
and
92 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
packages/framework/tests/provider-visibility-issue/auth-listener.ts
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,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
23
packages/framework/tests/provider-visibility-issue/auth-module.ts
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,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], | ||
}) { } |
59 changes: 59 additions & 0 deletions
59
packages/framework/tests/provider-visibility-issue/provider-visibility-issue.spec.ts
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,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' | ||
}); | ||
}); |
6 changes: 6 additions & 0 deletions
6
packages/framework/tests/provider-visibility-issue/session-for-request.dto.ts
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,6 @@ | ||
export class SessionForRequest { | ||
constructor( | ||
public readonly sessionId: string, | ||
public readonly userId: string, | ||
) {} | ||
} |
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