mongodb-dynamic-api 3.2.0 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +59 -0
- package/README.md +34 -3
- package/package.json +14 -11
- package/src/adapters/socket-adapter.d.ts +3 -3
- package/src/adapters/socket-adapter.js.map +1 -1
- package/src/builders/route-decorators/auth-decorators.builder.d.ts +1 -1
- package/src/builders/route-decorators/route-decorators.builder.d.ts +1 -1
- package/src/builders/route-decorators/route-decorators.builder.js.map +1 -1
- package/src/decorators/api-endpoint-visibility.decorator.js +1 -2
- package/src/decorators/api-endpoint-visibility.decorator.js.map +1 -1
- package/src/decorators/schema-options.decorator.js +2 -2
- package/src/decorators/schema-options.decorator.js.map +1 -1
- package/src/decorators/validator-pipe.decorator.js +1 -2
- package/src/decorators/validator-pipe.decorator.js.map +1 -1
- package/src/dynamic-api.module.js +4 -1
- package/src/dynamic-api.module.js.map +1 -1
- package/src/gateways/dynamic-api-broadcast.gateway.js +1 -2
- package/src/gateways/dynamic-api-broadcast.gateway.js.map +1 -1
- package/src/guards/base-policies.guard.d.ts +0 -25
- package/src/guards/jwt-socket.guard.js +1 -1
- package/src/guards/jwt-socket.guard.js.map +1 -1
- package/src/helpers/controller-ability-predicates.helper.js +1 -2
- package/src/helpers/controller-ability-predicates.helper.js.map +1 -1
- package/src/helpers/format.helper.js +6 -7
- package/src/helpers/format.helper.js.map +1 -1
- package/src/helpers/mixin-data.helper.js +1 -2
- package/src/helpers/mixin-data.helper.js.map +1 -1
- package/src/helpers/repository.helper.d.ts +0 -25
- package/src/helpers/repository.helper.js.map +1 -1
- package/src/helpers/route-decorators.helper.d.ts +1 -1
- package/src/helpers/route-decorators.helper.js +1 -2
- package/src/helpers/route-decorators.helper.js.map +1 -1
- package/src/helpers/route-description.helper.js +1 -2
- package/src/helpers/route-description.helper.js.map +1 -1
- package/src/helpers/schema.helper.d.ts +0 -25
- package/src/helpers/schema.helper.js +1 -2
- package/src/helpers/schema.helper.js.map +1 -1
- package/src/helpers/socket-config.helper.js +2 -3
- package/src/helpers/socket-config.helper.js.map +1 -1
- package/src/helpers/swagger-config.helper.js +1 -2
- package/src/helpers/swagger-config.helper.js.map +1 -1
- package/src/helpers/validation-config.helper.js +1 -2
- package/src/helpers/validation-config.helper.js.map +1 -1
- package/src/helpers/versioning-config.helper.js +2 -3
- package/src/helpers/versioning-config.helper.js.map +1 -1
- package/src/interceptors/dynamic-api-cache.interceptor.d.ts +3 -2
- package/src/interceptors/dynamic-api-cache.interceptor.js +4 -0
- package/src/interceptors/dynamic-api-cache.interceptor.js.map +1 -1
- package/src/interfaces/dynamic-api-decorator-builder.interface.d.ts +1 -1
- package/src/interfaces/dynamic-api-global-state.interface.d.ts +3 -25
- package/src/interfaces/dynamic-api-policy-handler.interface.d.ts +0 -25
- package/src/interfaces/dynamic-api-schema-options.interface.d.ts +0 -25
- package/src/interfaces/dynamic-api-service-callback.interface.d.ts +0 -25
- package/src/interfaces/dynamic-api-swagger-options.type.d.ts +1 -1
- package/src/mixins/entity-body.mixin.js +2 -2
- package/src/mixins/entity-body.mixin.js.map +1 -1
- package/src/mixins/entity-presenter.mixin.js +1 -2
- package/src/mixins/entity-presenter.mixin.js.map +1 -1
- package/src/mixins/policies-guard.mixin.js +2 -3
- package/src/mixins/policies-guard.mixin.js.map +1 -1
- package/src/models/base-entity.model.d.ts +0 -25
- package/src/modules/auth/auth-broadcast.helper.d.ts +3 -0
- package/src/modules/auth/auth-broadcast.helper.js +11 -0
- package/src/modules/auth/auth-broadcast.helper.js.map +1 -0
- package/src/modules/auth/auth-events.constants.d.ts +13 -0
- package/src/modules/auth/auth-events.constants.js +28 -0
- package/src/modules/auth/auth-events.constants.js.map +1 -0
- package/src/modules/auth/auth.helper.d.ts +19 -5
- package/src/modules/auth/auth.helper.js +20 -13
- package/src/modules/auth/auth.helper.js.map +1 -1
- package/src/modules/auth/auth.module.d.ts +5 -2
- package/src/modules/auth/auth.module.js +46 -7
- package/src/modules/auth/auth.module.js.map +1 -1
- package/src/modules/auth/guards/index.d.ts +2 -0
- package/src/modules/auth/guards/index.js +2 -0
- package/src/modules/auth/guards/index.js.map +1 -1
- package/src/modules/auth/guards/jwt-refresh/jwt-refresh.guard.d.ts +4 -0
- package/src/modules/auth/guards/jwt-refresh/jwt-refresh.guard.js +18 -0
- package/src/modules/auth/guards/jwt-refresh/jwt-refresh.guard.js.map +1 -0
- package/src/modules/auth/guards/jwt-socket-auth/jwt-socket-auth.guard.js +1 -1
- package/src/modules/auth/guards/jwt-socket-auth/jwt-socket-auth.guard.js.map +1 -1
- package/src/modules/auth/guards/jwt-socket-refresh/jwt-socket-refresh.guard.d.ts +8 -0
- package/src/modules/auth/guards/jwt-socket-refresh/jwt-socket-refresh.guard.js +62 -0
- package/src/modules/auth/guards/jwt-socket-refresh/jwt-socket-refresh.guard.js.map +1 -0
- package/src/modules/auth/index.d.ts +2 -0
- package/src/modules/auth/index.js +2 -0
- package/src/modules/auth/index.js.map +1 -1
- package/src/modules/auth/interfaces/auth-controller.interface.d.ts +19 -4
- package/src/modules/auth/interfaces/auth-gateway.interface.d.ts +2 -0
- package/src/modules/auth/interfaces/auth-options.interface.d.ts +19 -2
- package/src/modules/auth/interfaces/auth-service.interface.d.ts +3 -0
- package/src/modules/auth/mixins/auth-controller.mixin.d.ts +2 -2
- package/src/modules/auth/mixins/auth-controller.mixin.js +113 -20
- package/src/modules/auth/mixins/auth-controller.mixin.js.map +1 -1
- package/src/modules/auth/mixins/auth-gateway.mixin.d.ts +2 -2
- package/src/modules/auth/mixins/auth-gateway.mixin.js +66 -33
- package/src/modules/auth/mixins/auth-gateway.mixin.js.map +1 -1
- package/src/modules/auth/mixins/auth-policies-guard.mixin.js +2 -3
- package/src/modules/auth/mixins/auth-policies-guard.mixin.js.map +1 -1
- package/src/modules/auth/services/base-auth.service.d.ts +9 -25
- package/src/modules/auth/services/base-auth.service.js +74 -15
- package/src/modules/auth/services/base-auth.service.js.map +1 -1
- package/src/modules/auth/strategies/index.d.ts +1 -0
- package/src/modules/auth/strategies/index.js +1 -0
- package/src/modules/auth/strategies/index.js.map +1 -1
- package/src/modules/auth/strategies/jwt-refresh.strategy.d.ts +14 -0
- package/src/modules/auth/strategies/jwt-refresh.strategy.js +45 -0
- package/src/modules/auth/strategies/jwt-refresh.strategy.js.map +1 -0
- package/src/modules/auth/strategies/jwt.strategy.d.ts +3 -1
- package/src/modules/auth/strategies/jwt.strategy.js.map +1 -1
- package/src/routes/aggregate/aggregate-controller.mixin.js +1 -2
- package/src/routes/aggregate/aggregate-controller.mixin.js.map +1 -1
- package/src/routes/aggregate/aggregate-gateway.mixin.js +1 -2
- package/src/routes/aggregate/aggregate-gateway.mixin.js.map +1 -1
- package/src/routes/aggregate/aggregate-presenter.mixin.js +1 -2
- package/src/routes/aggregate/aggregate-presenter.mixin.js.map +1 -1
- package/src/routes/aggregate/aggregate.helper.js +3 -4
- package/src/routes/aggregate/aggregate.helper.js.map +1 -1
- package/src/routes/aggregate/base-aggregate.service.d.ts +0 -25
- package/src/routes/aggregate/base-aggregate.service.js.map +1 -1
- package/src/routes/create-many/base-create-many.service.d.ts +0 -25
- package/src/routes/create-many/base-create-many.service.js.map +1 -1
- package/src/routes/create-many/create-many-body.mixin.js +1 -2
- package/src/routes/create-many/create-many-body.mixin.js.map +1 -1
- package/src/routes/create-many/create-many-controller.mixin.js +1 -2
- package/src/routes/create-many/create-many-controller.mixin.js.map +1 -1
- package/src/routes/create-many/create-many-gateway.mixin.js +1 -2
- package/src/routes/create-many/create-many-gateway.mixin.js.map +1 -1
- package/src/routes/create-many/create-many-presenter.mixin.js +1 -2
- package/src/routes/create-many/create-many-presenter.mixin.js.map +1 -1
- package/src/routes/create-many/create-many.helper.js +3 -4
- package/src/routes/create-many/create-many.helper.js.map +1 -1
- package/src/routes/create-one/base-create-one.service.d.ts +0 -25
- package/src/routes/create-one/base-create-one.service.js.map +1 -1
- package/src/routes/create-one/create-one-controller.mixin.js +1 -2
- package/src/routes/create-one/create-one-controller.mixin.js.map +1 -1
- package/src/routes/create-one/create-one-gateway.mixin.js +1 -2
- package/src/routes/create-one/create-one-gateway.mixin.js.map +1 -1
- package/src/routes/create-one/create-one.helper.js +3 -4
- package/src/routes/create-one/create-one.helper.js.map +1 -1
- package/src/routes/delete-many/base-delete-many.service.d.ts +0 -25
- package/src/routes/delete-many/base-delete-many.service.js.map +1 -1
- package/src/routes/delete-many/delete-many-controller.mixin.js +1 -2
- package/src/routes/delete-many/delete-many-controller.mixin.js.map +1 -1
- package/src/routes/delete-many/delete-many-gateway.mixin.js +1 -2
- package/src/routes/delete-many/delete-many-gateway.mixin.js.map +1 -1
- package/src/routes/delete-many/delete-many.helper.js +3 -4
- package/src/routes/delete-many/delete-many.helper.js.map +1 -1
- package/src/routes/delete-one/base-delete-one.service.d.ts +0 -25
- package/src/routes/delete-one/base-delete-one.service.js.map +1 -1
- package/src/routes/delete-one/delete-one-controller.mixin.js +1 -2
- package/src/routes/delete-one/delete-one-controller.mixin.js.map +1 -1
- package/src/routes/delete-one/delete-one-gateway.mixin.js +1 -2
- package/src/routes/delete-one/delete-one-gateway.mixin.js.map +1 -1
- package/src/routes/delete-one/delete-one.helper.js +3 -4
- package/src/routes/delete-one/delete-one.helper.js.map +1 -1
- package/src/routes/duplicate-many/base-duplicate-many.service.d.ts +0 -25
- package/src/routes/duplicate-many/base-duplicate-many.service.js +4 -3
- package/src/routes/duplicate-many/base-duplicate-many.service.js.map +1 -1
- package/src/routes/duplicate-many/duplicate-many-controller.mixin.js +1 -2
- package/src/routes/duplicate-many/duplicate-many-controller.mixin.js.map +1 -1
- package/src/routes/duplicate-many/duplicate-many-gateway.mixin.js +1 -2
- package/src/routes/duplicate-many/duplicate-many-gateway.mixin.js.map +1 -1
- package/src/routes/duplicate-many/duplicate-many.helper.js +3 -4
- package/src/routes/duplicate-many/duplicate-many.helper.js.map +1 -1
- package/src/routes/duplicate-one/base-duplicate-one.service.d.ts +0 -25
- package/src/routes/duplicate-one/base-duplicate-one.service.js +3 -2
- package/src/routes/duplicate-one/base-duplicate-one.service.js.map +1 -1
- package/src/routes/duplicate-one/duplicate-one-controller.mixin.js +1 -2
- package/src/routes/duplicate-one/duplicate-one-controller.mixin.js.map +1 -1
- package/src/routes/duplicate-one/duplicate-one-gateway.mixin.js +1 -2
- package/src/routes/duplicate-one/duplicate-one-gateway.mixin.js.map +1 -1
- package/src/routes/duplicate-one/duplicate-one.helper.js +3 -4
- package/src/routes/duplicate-one/duplicate-one.helper.js.map +1 -1
- package/src/routes/get-many/base-get-many.service.d.ts +0 -25
- package/src/routes/get-many/base-get-many.service.js.map +1 -1
- package/src/routes/get-many/get-many-controller.mixin.js +1 -2
- package/src/routes/get-many/get-many-controller.mixin.js.map +1 -1
- package/src/routes/get-many/get-many-gateway.mixin.js +1 -2
- package/src/routes/get-many/get-many-gateway.mixin.js.map +1 -1
- package/src/routes/get-many/get-many.helper.js +3 -4
- package/src/routes/get-many/get-many.helper.js.map +1 -1
- package/src/routes/get-one/base-get-one.service.d.ts +0 -25
- package/src/routes/get-one/base-get-one.service.js.map +1 -1
- package/src/routes/get-one/get-one-controller.mixin.js +1 -2
- package/src/routes/get-one/get-one-controller.mixin.js.map +1 -1
- package/src/routes/get-one/get-one-gateway.mixin.js +1 -2
- package/src/routes/get-one/get-one-gateway.mixin.js.map +1 -1
- package/src/routes/get-one/get-one.helper.js +3 -4
- package/src/routes/get-one/get-one.helper.js.map +1 -1
- package/src/routes/replace-one/base-replace-one.service.d.ts +0 -25
- package/src/routes/replace-one/base-replace-one.service.js +3 -2
- package/src/routes/replace-one/base-replace-one.service.js.map +1 -1
- package/src/routes/replace-one/replace-one-controller.mixin.js +1 -2
- package/src/routes/replace-one/replace-one-controller.mixin.js.map +1 -1
- package/src/routes/replace-one/replace-one-gateway.mixin.js +1 -2
- package/src/routes/replace-one/replace-one-gateway.mixin.js.map +1 -1
- package/src/routes/replace-one/replace-one.helper.js +3 -4
- package/src/routes/replace-one/replace-one.helper.js.map +1 -1
- package/src/routes/update-many/base-update-many.service.d.ts +0 -25
- package/src/routes/update-many/base-update-many.service.js.map +1 -1
- package/src/routes/update-many/update-many-controller.mixin.js +1 -2
- package/src/routes/update-many/update-many-controller.mixin.js.map +1 -1
- package/src/routes/update-many/update-many-gateway.mixin.js +1 -2
- package/src/routes/update-many/update-many-gateway.mixin.js.map +1 -1
- package/src/routes/update-many/update-many.helper.js +3 -4
- package/src/routes/update-many/update-many.helper.js.map +1 -1
- package/src/routes/update-one/base-update-one.service.d.ts +0 -25
- package/src/routes/update-one/base-update-one.service.js.map +1 -1
- package/src/routes/update-one/update-one-controller.mixin.js +1 -2
- package/src/routes/update-one/update-one-controller.mixin.js.map +1 -1
- package/src/routes/update-one/update-one-gateway.mixin.js +1 -2
- package/src/routes/update-one/update-one-gateway.mixin.js.map +1 -1
- package/src/routes/update-one/update-one.helper.js +3 -4
- package/src/routes/update-one/update-one.helper.js.map +1 -1
- package/src/services/base/base.service.d.ts +3 -28
- package/src/services/base/base.service.js +26 -8
- package/src/services/base/base.service.js.map +1 -1
- package/src/services/dynamic-api-global-state/dynamic-api-global-state.service.d.ts +0 -25
- package/src/services/dynamic-api-global-state/dynamic-api-global-state.service.js +3 -0
- package/src/services/dynamic-api-global-state/dynamic-api-global-state.service.js.map +1 -1
- package/src/version.json +1 -1
- package/test/e2e.setup.d.ts +9 -34
- package/test/e2e.setup.js +8 -7
- package/test/e2e.setup.js.map +1 -1
- package/test/for-feature/base-entity.e2e-spec.js +316 -0
- package/test/for-feature/base-entity.e2e-spec.js.map +1 -0
- package/test/for-feature/http-broadcast.e2e-spec.js +187 -0
- package/test/for-feature/http-broadcast.e2e-spec.js.map +1 -0
- package/test/for-feature/soft-deletable-entity.e2e-spec.d.ts +1 -0
- package/test/for-feature/soft-deletable-entity.e2e-spec.js +335 -0
- package/test/for-feature/soft-deletable-entity.e2e-spec.js.map +1 -0
- package/test/for-feature/websockets.e2e-spec.d.ts +1 -0
- package/test/for-feature/websockets.e2e-spec.js +324 -0
- package/test/for-feature/websockets.e2e-spec.js.map +1 -0
- package/test/for-root/auth-api-basic.e2e-spec.d.ts +1 -0
- package/test/for-root/auth-api-basic.e2e-spec.js +338 -0
- package/test/for-root/auth-api-basic.e2e-spec.js.map +1 -0
- package/test/for-root/auth-api-broadcast.e2e-spec.d.ts +1 -0
- package/test/for-root/auth-api-broadcast.e2e-spec.js +82 -0
- package/test/for-root/auth-api-broadcast.e2e-spec.js.map +1 -0
- package/test/for-root/auth-api-login.e2e-spec.d.ts +1 -0
- package/test/for-root/auth-api-login.e2e-spec.js +103 -0
- package/test/for-root/auth-api-login.e2e-spec.js.map +1 -0
- package/test/for-root/auth-api-logout.e2e-spec.d.ts +1 -0
- package/test/for-root/auth-api-logout.e2e-spec.js +74 -0
- package/test/for-root/auth-api-logout.e2e-spec.js.map +1 -0
- package/test/for-root/auth-api-refresh-token-cookie.e2e-spec.d.ts +1 -0
- package/test/for-root/auth-api-refresh-token-cookie.e2e-spec.js +110 -0
- package/test/for-root/auth-api-refresh-token-cookie.e2e-spec.js.map +1 -0
- package/test/for-root/auth-api-refresh-token.e2e-spec.d.ts +1 -0
- package/test/for-root/auth-api-refresh-token.e2e-spec.js +153 -0
- package/test/for-root/auth-api-refresh-token.e2e-spec.js.map +1 -0
- package/test/for-root/auth-api-register.e2e-spec.d.ts +1 -0
- package/test/for-root/auth-api-register.e2e-spec.js +116 -0
- package/test/for-root/auth-api-register.e2e-spec.js.map +1 -0
- package/test/for-root/auth-api-reset-password.e2e-spec.d.ts +1 -0
- package/test/for-root/auth-api-reset-password.e2e-spec.js +168 -0
- package/test/for-root/auth-api-reset-password.e2e-spec.js.map +1 -0
- package/test/for-root/initialization.e2e-spec.d.ts +1 -0
- package/test/for-root/initialization.e2e-spec.js +84 -0
- package/test/for-root/initialization.e2e-spec.js.map +1 -0
- package/test/for-root/websockets-auth-basic.e2e-spec.d.ts +1 -0
- package/test/for-root/websockets-auth-basic.e2e-spec.js +313 -0
- package/test/for-root/websockets-auth-basic.e2e-spec.js.map +1 -0
- package/test/for-root/websockets-auth-broadcast.e2e-spec.d.ts +1 -0
- package/test/for-root/websockets-auth-broadcast.e2e-spec.js +86 -0
- package/test/for-root/websockets-auth-broadcast.e2e-spec.js.map +1 -0
- package/test/for-root/websockets-auth-login.e2e-spec.d.ts +1 -0
- package/test/for-root/websockets-auth-login.e2e-spec.js +107 -0
- package/test/for-root/websockets-auth-login.e2e-spec.js.map +1 -0
- package/test/for-root/websockets-auth-refresh-token.e2e-spec.d.ts +1 -0
- package/test/for-root/websockets-auth-refresh-token.e2e-spec.js +104 -0
- package/test/for-root/websockets-auth-refresh-token.e2e-spec.js.map +1 -0
- package/test/for-root/websockets-auth-register.e2e-spec.d.ts +1 -0
- package/test/for-root/websockets-auth-register.e2e-spec.js +131 -0
- package/test/for-root/websockets-auth-register.e2e-spec.js.map +1 -0
- package/test/for-root/websockets-auth-reset-password.e2e-spec.d.ts +1 -0
- package/test/for-root/websockets-auth-reset-password.e2e-spec.js +164 -0
- package/test/for-root/websockets-auth-reset-password.e2e-spec.js.map +1 -0
- package/test/shared/entities/basic-user.factory.d.ts +12 -0
- package/test/shared/entities/basic-user.factory.js +31 -0
- package/test/shared/entities/basic-user.factory.js.map +1 -0
- package/test/shared/entities/broadcast-user.factory.d.ts +13 -0
- package/test/shared/entities/broadcast-user.factory.js +35 -0
- package/test/shared/entities/broadcast-user.factory.js.map +1 -0
- package/test/shared/entities/index.d.ts +7 -0
- package/test/shared/entities/index.js +24 -0
- package/test/shared/entities/index.js.map +1 -0
- package/test/shared/entities/login-user.factory.d.ts +14 -0
- package/test/shared/entities/login-user.factory.js +39 -0
- package/test/shared/entities/login-user.factory.js.map +1 -0
- package/test/shared/entities/refresh-token-user.factory.d.ts +13 -0
- package/test/shared/entities/refresh-token-user.factory.js +35 -0
- package/test/shared/entities/refresh-token-user.factory.js.map +1 -0
- package/test/shared/entities/register-user.factory.d.ts +14 -0
- package/test/shared/entities/register-user.factory.js +39 -0
- package/test/shared/entities/register-user.factory.js.map +1 -0
- package/test/shared/entities/reset-password-user.factory.d.ts +14 -0
- package/test/shared/entities/reset-password-user.factory.js +39 -0
- package/test/shared/entities/reset-password-user.factory.js.map +1 -0
- package/test/shared/entities/validated-user.factory.d.ts +12 -0
- package/test/shared/entities/validated-user.factory.js +40 -0
- package/test/shared/entities/validated-user.factory.js.map +1 -0
- package/test/shared/fixtures/index.d.ts +2 -0
- package/test/shared/fixtures/index.js +19 -0
- package/test/shared/fixtures/index.js.map +1 -0
- package/test/shared/fixtures/login.fixtures.d.ts +16 -0
- package/test/shared/fixtures/login.fixtures.js +20 -0
- package/test/shared/fixtures/login.fixtures.js.map +1 -0
- package/test/shared/fixtures/register.fixtures.d.ts +10 -0
- package/test/shared/fixtures/register.fixtures.js +14 -0
- package/test/shared/fixtures/register.fixtures.js.map +1 -0
- package/test/shared/index.d.ts +4 -0
- package/test/shared/index.js +24 -0
- package/test/shared/index.js.map +1 -0
- package/test/shared/init-app.d.ts +3 -0
- package/test/shared/init-app.js +19 -0
- package/test/shared/init-app.js.map +1 -0
- package/test/shared/init-module.d.ts +4 -0
- package/test/shared/init-module.js +16 -0
- package/test/shared/init-module.js.map +1 -0
- package/test/utils.d.ts +0 -25
- package/tsconfig.tsbuildinfo +1 -1
- package/test/dynamic-api-for-feature.e2e-spec.js +0 -1100
- package/test/dynamic-api-for-feature.e2e-spec.js.map +0 -1
- package/test/dynamic-api-for-root.e2e-spec.js +0 -1573
- package/test/dynamic-api-for-root.e2e-spec.js.map +0 -1
- /package/test/{dynamic-api-for-feature.e2e-spec.d.ts → for-feature/base-entity.e2e-spec.d.ts} +0 -0
- /package/test/{dynamic-api-for-root.e2e-spec.d.ts → for-feature/http-broadcast.e2e-spec.d.ts} +0 -0
|
@@ -1,1573 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
-
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
-
};
|
|
8
|
-
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
-
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
const common_1 = require("@nestjs/common");
|
|
13
|
-
const jwt_1 = require("@nestjs/jwt");
|
|
14
|
-
const mongoose_1 = require("@nestjs/mongoose");
|
|
15
|
-
const testing_1 = require("@nestjs/testing");
|
|
16
|
-
const class_validator_1 = require("class-validator");
|
|
17
|
-
const mongoose_2 = require("mongoose");
|
|
18
|
-
const src_1 = require("../src");
|
|
19
|
-
const socket_adapter_1 = require("../src/adapters/socket-adapter");
|
|
20
|
-
const e2e_setup_1 = require("./e2e.setup");
|
|
21
|
-
require("dotenv/config");
|
|
22
|
-
const utils_1 = require("./utils");
|
|
23
|
-
describe('DynamicApiModule forRoot (e2e)', () => {
|
|
24
|
-
const uri = process.env.MONGO_DB_URL;
|
|
25
|
-
const initModule = async (dynamicApiForRootOptions, initFixtures, initMainCb, testGateway) => {
|
|
26
|
-
const moduleRef = await testing_1.Test.createTestingModule({
|
|
27
|
-
imports: [src_1.DynamicApiModule.forRoot(uri, dynamicApiForRootOptions)],
|
|
28
|
-
providers: testGateway ? [e2e_setup_1.TestGateway] : [],
|
|
29
|
-
}).compile();
|
|
30
|
-
return (0, e2e_setup_1.createTestingApp)(moduleRef, initFixtures, initMainCb);
|
|
31
|
-
};
|
|
32
|
-
beforeEach(() => {
|
|
33
|
-
src_1.DynamicApiModule.state['resetState']();
|
|
34
|
-
});
|
|
35
|
-
afterEach(async () => {
|
|
36
|
-
await (0, e2e_setup_1.closeTestingApp)(mongoose_2.default.connections);
|
|
37
|
-
});
|
|
38
|
-
it('should initialize dynamic api module state with default options', async () => {
|
|
39
|
-
const app = await initModule({});
|
|
40
|
-
expect(app).toBeDefined();
|
|
41
|
-
expect(src_1.DynamicApiModule.state.get()).toStrictEqual({
|
|
42
|
-
uri,
|
|
43
|
-
initialized: true,
|
|
44
|
-
isGlobalCacheEnabled: true,
|
|
45
|
-
connectionName: 'dynamic-api-connection',
|
|
46
|
-
cacheExcludedPaths: [],
|
|
47
|
-
credentials: null,
|
|
48
|
-
isAuthEnabled: false,
|
|
49
|
-
jwtExpirationTime: undefined,
|
|
50
|
-
jwtSecret: undefined,
|
|
51
|
-
routesConfig: {
|
|
52
|
-
defaults: [
|
|
53
|
-
'GetMany',
|
|
54
|
-
'GetOne',
|
|
55
|
-
'CreateMany',
|
|
56
|
-
'CreateOne',
|
|
57
|
-
'UpdateMany',
|
|
58
|
-
'UpdateOne',
|
|
59
|
-
'ReplaceOne',
|
|
60
|
-
'DuplicateMany',
|
|
61
|
-
'DuplicateOne',
|
|
62
|
-
'DeleteMany',
|
|
63
|
-
'DeleteOne',
|
|
64
|
-
],
|
|
65
|
-
excluded: [],
|
|
66
|
-
},
|
|
67
|
-
gatewayOptions: undefined,
|
|
68
|
-
});
|
|
69
|
-
});
|
|
70
|
-
it('should initialize dynamic api module state with custom options', async () => {
|
|
71
|
-
const app = await initModule({
|
|
72
|
-
useGlobalCache: false,
|
|
73
|
-
cacheOptions: {
|
|
74
|
-
excludePaths: ['/fake-path'],
|
|
75
|
-
},
|
|
76
|
-
routesConfig: {
|
|
77
|
-
defaults: ['GetMany', 'GetOne', 'CreateOne', 'UpdateOne', 'DeleteOne'],
|
|
78
|
-
excluded: ['CreateMany', 'UpdateMany', 'DeleteMany'],
|
|
79
|
-
},
|
|
80
|
-
});
|
|
81
|
-
expect(app).toBeDefined();
|
|
82
|
-
expect(src_1.DynamicApiModule.state.get()).toStrictEqual({
|
|
83
|
-
uri,
|
|
84
|
-
initialized: true,
|
|
85
|
-
isGlobalCacheEnabled: false,
|
|
86
|
-
connectionName: 'dynamic-api-connection',
|
|
87
|
-
cacheExcludedPaths: ['/fake-path'],
|
|
88
|
-
credentials: null,
|
|
89
|
-
isAuthEnabled: false,
|
|
90
|
-
jwtExpirationTime: undefined,
|
|
91
|
-
jwtSecret: undefined,
|
|
92
|
-
routesConfig: {
|
|
93
|
-
defaults: ['GetMany', 'GetOne', 'CreateOne', 'UpdateOne', 'DeleteOne'],
|
|
94
|
-
excluded: ['CreateMany', 'UpdateMany', 'DeleteMany'],
|
|
95
|
-
},
|
|
96
|
-
gatewayOptions: undefined,
|
|
97
|
-
});
|
|
98
|
-
});
|
|
99
|
-
describe('Authentication API', () => {
|
|
100
|
-
describe('useAuth when only userEntity is provided', () => {
|
|
101
|
-
let UserEntity = class UserEntity extends src_1.BaseEntity {
|
|
102
|
-
};
|
|
103
|
-
__decorate([
|
|
104
|
-
(0, mongoose_1.Prop)({ type: String, required: true }),
|
|
105
|
-
__metadata("design:type", String)
|
|
106
|
-
], UserEntity.prototype, "email", void 0);
|
|
107
|
-
__decorate([
|
|
108
|
-
(0, mongoose_1.Prop)({ type: String, required: true }),
|
|
109
|
-
__metadata("design:type", String)
|
|
110
|
-
], UserEntity.prototype, "password", void 0);
|
|
111
|
-
UserEntity = __decorate([
|
|
112
|
-
(0, mongoose_1.Schema)({ collection: 'users' })
|
|
113
|
-
], UserEntity);
|
|
114
|
-
let app;
|
|
115
|
-
beforeEach(async () => {
|
|
116
|
-
app = await initModule({ useAuth: { userEntity: UserEntity } });
|
|
117
|
-
});
|
|
118
|
-
it('should initialize dynamic api module state and authentication API with default options', async () => {
|
|
119
|
-
expect(app).toBeDefined();
|
|
120
|
-
expect(src_1.DynamicApiModule.state.get()).toStrictEqual({
|
|
121
|
-
uri,
|
|
122
|
-
initialized: true,
|
|
123
|
-
isGlobalCacheEnabled: true,
|
|
124
|
-
connectionName: 'dynamic-api-connection',
|
|
125
|
-
cacheExcludedPaths: [],
|
|
126
|
-
credentials: {
|
|
127
|
-
loginField: 'email',
|
|
128
|
-
passwordField: 'password',
|
|
129
|
-
},
|
|
130
|
-
isAuthEnabled: true,
|
|
131
|
-
jwtExpirationTime: '1d',
|
|
132
|
-
jwtSecret: 'dynamic-api-jwt-secret',
|
|
133
|
-
routesConfig: {
|
|
134
|
-
defaults: [
|
|
135
|
-
'GetMany',
|
|
136
|
-
'GetOne',
|
|
137
|
-
'CreateMany',
|
|
138
|
-
'CreateOne',
|
|
139
|
-
'UpdateMany',
|
|
140
|
-
'UpdateOne',
|
|
141
|
-
'ReplaceOne',
|
|
142
|
-
'DuplicateMany',
|
|
143
|
-
'DuplicateOne',
|
|
144
|
-
'DeleteMany',
|
|
145
|
-
'DeleteOne',
|
|
146
|
-
],
|
|
147
|
-
excluded: [],
|
|
148
|
-
},
|
|
149
|
-
gatewayOptions: undefined,
|
|
150
|
-
});
|
|
151
|
-
});
|
|
152
|
-
describe('POST /auth/register', () => {
|
|
153
|
-
it('should throw a bad request exception if email is missing', async () => {
|
|
154
|
-
const { body, status } = await e2e_setup_1.server.post('/auth/register', { username: 'unit-test', password: 'test-2' });
|
|
155
|
-
expect(status).toBe(400);
|
|
156
|
-
expect(body).toEqual({
|
|
157
|
-
error: 'Bad Request',
|
|
158
|
-
message: ['email property is required'],
|
|
159
|
-
statusCode: 400,
|
|
160
|
-
});
|
|
161
|
-
});
|
|
162
|
-
it('should throw a bad request exception if password is missing', async () => {
|
|
163
|
-
const { body, status } = await e2e_setup_1.server.post('/auth/register', { email: 'unit@test.co', pass: 'test-2' });
|
|
164
|
-
expect(status).toBe(400);
|
|
165
|
-
expect(body).toEqual({
|
|
166
|
-
error: 'Bad Request',
|
|
167
|
-
message: ['password property is required'],
|
|
168
|
-
statusCode: 400,
|
|
169
|
-
});
|
|
170
|
-
});
|
|
171
|
-
it('should create a new user and return access token', async () => {
|
|
172
|
-
const { body, status } = await e2e_setup_1.server.post('/auth/register', { email: 'unit@test.co', password: 'test' });
|
|
173
|
-
expect(status).toBe(201);
|
|
174
|
-
expect(body).toEqual({ accessToken: expect.any(String) });
|
|
175
|
-
});
|
|
176
|
-
});
|
|
177
|
-
describe('POST /auth/login', () => {
|
|
178
|
-
it('should throw an unauthorized exception if email is missing', async () => {
|
|
179
|
-
const { body, status } = await e2e_setup_1.server.post('/auth/login', { pass: 'test-2' });
|
|
180
|
-
expect(status).toBe(401);
|
|
181
|
-
expect(body).toEqual({
|
|
182
|
-
message: 'Unauthorized',
|
|
183
|
-
statusCode: 401,
|
|
184
|
-
});
|
|
185
|
-
});
|
|
186
|
-
it('should throw an unauthorized exception if password is missing', async () => {
|
|
187
|
-
const { body, status } = await e2e_setup_1.server.post('/auth/login', { email: 'unit@test.co' });
|
|
188
|
-
expect(status).toBe(401);
|
|
189
|
-
expect(body).toEqual({
|
|
190
|
-
message: 'Unauthorized',
|
|
191
|
-
statusCode: 401,
|
|
192
|
-
});
|
|
193
|
-
});
|
|
194
|
-
it('should return access token', async () => {
|
|
195
|
-
await e2e_setup_1.server.post('/auth/register', { email: 'unit@test.co', password: 'test' });
|
|
196
|
-
const { body, status } = await e2e_setup_1.server.post('/auth/login', { email: 'unit@test.co', password: 'test' });
|
|
197
|
-
expect(status).toBe(200);
|
|
198
|
-
expect(body).toEqual({ accessToken: expect.any(String) });
|
|
199
|
-
});
|
|
200
|
-
});
|
|
201
|
-
describe('GET /auth/account', () => {
|
|
202
|
-
it('should throw an unauthorized exception if access token is missing', async () => {
|
|
203
|
-
const { body, status } = await e2e_setup_1.server.get('/auth/account');
|
|
204
|
-
expect(status).toBe(401);
|
|
205
|
-
expect(body).toEqual({
|
|
206
|
-
message: 'Unauthorized',
|
|
207
|
-
statusCode: 401,
|
|
208
|
-
});
|
|
209
|
-
});
|
|
210
|
-
it('should return user account', async () => {
|
|
211
|
-
await e2e_setup_1.server.post('/auth/register', { email: 'unit@test.co', password: 'test' });
|
|
212
|
-
const { body: { accessToken } } = await e2e_setup_1.server.post('/auth/login', { email: 'unit@test.co', password: 'test' });
|
|
213
|
-
const headers = { Authorization: `Bearer ${accessToken}` };
|
|
214
|
-
const { body: account, status: accountStatus } = await e2e_setup_1.server.get('/auth/account', { headers });
|
|
215
|
-
expect(accountStatus).toBe(200);
|
|
216
|
-
expect(account).toEqual({ id: expect.any(String), email: 'unit@test.co' });
|
|
217
|
-
});
|
|
218
|
-
});
|
|
219
|
-
it('should throw a Service Unavailable exception when requesting reset password endpoint if reset password options are not configured', async () => {
|
|
220
|
-
const { body, status } = await e2e_setup_1.server.post('/auth/reset-password', { email: 'toto@test.co' });
|
|
221
|
-
expect(status).toBe(503);
|
|
222
|
-
expect(body).toEqual({
|
|
223
|
-
error: 'Service Unavailable',
|
|
224
|
-
message: 'This feature is not available',
|
|
225
|
-
statusCode: 503,
|
|
226
|
-
});
|
|
227
|
-
});
|
|
228
|
-
it('should throw a Service Unavailable exception when requesting change password endpoint if reset password options are not configured', async () => {
|
|
229
|
-
const { body, status } = await e2e_setup_1.server.patch('/auth/change-password', { newPassword: 'test' });
|
|
230
|
-
expect(status).toBe(503);
|
|
231
|
-
expect(body).toEqual({
|
|
232
|
-
error: 'Service Unavailable',
|
|
233
|
-
message: 'This feature is not available',
|
|
234
|
-
statusCode: 503,
|
|
235
|
-
});
|
|
236
|
-
});
|
|
237
|
-
});
|
|
238
|
-
describe('useAuth with jwt options', () => {
|
|
239
|
-
let jwtService;
|
|
240
|
-
let token;
|
|
241
|
-
let app;
|
|
242
|
-
let UserEntity = class UserEntity extends src_1.BaseEntity {
|
|
243
|
-
};
|
|
244
|
-
__decorate([
|
|
245
|
-
(0, mongoose_1.Prop)({ type: String, required: true }),
|
|
246
|
-
__metadata("design:type", String)
|
|
247
|
-
], UserEntity.prototype, "email", void 0);
|
|
248
|
-
__decorate([
|
|
249
|
-
(0, mongoose_1.Prop)({ type: String, required: true }),
|
|
250
|
-
__metadata("design:type", String)
|
|
251
|
-
], UserEntity.prototype, "password", void 0);
|
|
252
|
-
UserEntity = __decorate([
|
|
253
|
-
(0, mongoose_1.Schema)({ collection: 'users' })
|
|
254
|
-
], UserEntity);
|
|
255
|
-
beforeEach(async () => {
|
|
256
|
-
app = await initModule({
|
|
257
|
-
useAuth: {
|
|
258
|
-
userEntity: UserEntity,
|
|
259
|
-
jwt: {
|
|
260
|
-
secret: 'test-secret',
|
|
261
|
-
expiresIn: '4s',
|
|
262
|
-
},
|
|
263
|
-
},
|
|
264
|
-
});
|
|
265
|
-
jwtService = app.get(jwt_1.JwtService);
|
|
266
|
-
const { body: { accessToken } } = await e2e_setup_1.server.post('/auth/register', { email: 'unit@test.co', password: 'test' });
|
|
267
|
-
token = accessToken;
|
|
268
|
-
});
|
|
269
|
-
it('should initialize dynamic api module state and authentication API with jwt options', async () => {
|
|
270
|
-
expect(app).toBeDefined();
|
|
271
|
-
expect(src_1.DynamicApiModule.state.get()).toStrictEqual({
|
|
272
|
-
uri,
|
|
273
|
-
initialized: true,
|
|
274
|
-
isGlobalCacheEnabled: true,
|
|
275
|
-
connectionName: 'dynamic-api-connection',
|
|
276
|
-
cacheExcludedPaths: [],
|
|
277
|
-
credentials: {
|
|
278
|
-
loginField: 'email',
|
|
279
|
-
passwordField: 'password',
|
|
280
|
-
},
|
|
281
|
-
isAuthEnabled: true,
|
|
282
|
-
jwtExpirationTime: '4s',
|
|
283
|
-
jwtSecret: 'test-secret',
|
|
284
|
-
routesConfig: {
|
|
285
|
-
defaults: [
|
|
286
|
-
'GetMany',
|
|
287
|
-
'GetOne',
|
|
288
|
-
'CreateMany',
|
|
289
|
-
'CreateOne',
|
|
290
|
-
'UpdateMany',
|
|
291
|
-
'UpdateOne',
|
|
292
|
-
'ReplaceOne',
|
|
293
|
-
'DuplicateMany',
|
|
294
|
-
'DuplicateOne',
|
|
295
|
-
'DeleteMany',
|
|
296
|
-
'DeleteOne',
|
|
297
|
-
],
|
|
298
|
-
excluded: [],
|
|
299
|
-
},
|
|
300
|
-
gatewayOptions: undefined,
|
|
301
|
-
});
|
|
302
|
-
});
|
|
303
|
-
it('should throw an unauthorized exception if access token is expired', async () => {
|
|
304
|
-
await (0, utils_1.wait)(5000);
|
|
305
|
-
const headers = { Authorization: `Bearer ${token}` };
|
|
306
|
-
const { body, status } = await e2e_setup_1.server.get('/auth/account', { headers });
|
|
307
|
-
expect(status).toBe(401);
|
|
308
|
-
expect(body).toEqual({
|
|
309
|
-
message: 'Unauthorized',
|
|
310
|
-
statusCode: 401,
|
|
311
|
-
});
|
|
312
|
-
}, 6000);
|
|
313
|
-
it('should throw an unauthorized exception if secret is invalid', async () => {
|
|
314
|
-
const invalidToken = jwtService.sign({ email: 'u', password: 'p' }, { secret: 'invalid-secret' });
|
|
315
|
-
const headers = { Authorization: `Bearer ${invalidToken}` };
|
|
316
|
-
const { body, status } = await e2e_setup_1.server.get('/auth/account', { headers });
|
|
317
|
-
expect(status).toBe(401);
|
|
318
|
-
expect(body).toEqual({
|
|
319
|
-
message: 'Unauthorized',
|
|
320
|
-
statusCode: 401,
|
|
321
|
-
});
|
|
322
|
-
});
|
|
323
|
-
});
|
|
324
|
-
describe('useAuth with validation options', () => {
|
|
325
|
-
let app;
|
|
326
|
-
let UserEntity = class UserEntity extends src_1.BaseEntity {
|
|
327
|
-
};
|
|
328
|
-
__decorate([
|
|
329
|
-
(0, mongoose_1.Prop)({ type: String, required: true }),
|
|
330
|
-
(0, class_validator_1.IsEmail)(),
|
|
331
|
-
__metadata("design:type", String)
|
|
332
|
-
], UserEntity.prototype, "email", void 0);
|
|
333
|
-
__decorate([
|
|
334
|
-
(0, mongoose_1.Prop)({ type: String, required: true }),
|
|
335
|
-
(0, class_validator_1.IsStrongPassword)({
|
|
336
|
-
minLength: 6,
|
|
337
|
-
minLowercase: 1,
|
|
338
|
-
minUppercase: 1,
|
|
339
|
-
minNumbers: 1,
|
|
340
|
-
minSymbols: 1,
|
|
341
|
-
}),
|
|
342
|
-
__metadata("design:type", String)
|
|
343
|
-
], UserEntity.prototype, "password", void 0);
|
|
344
|
-
UserEntity = __decorate([
|
|
345
|
-
(0, mongoose_1.Schema)({ collection: 'users' })
|
|
346
|
-
], UserEntity);
|
|
347
|
-
beforeEach(async () => {
|
|
348
|
-
app = await initModule({
|
|
349
|
-
useAuth: {
|
|
350
|
-
userEntity: UserEntity,
|
|
351
|
-
validationPipeOptions: {
|
|
352
|
-
whitelist: true,
|
|
353
|
-
forbidNonWhitelisted: true,
|
|
354
|
-
transform: true,
|
|
355
|
-
},
|
|
356
|
-
},
|
|
357
|
-
});
|
|
358
|
-
});
|
|
359
|
-
describe('POST /auth/register', () => {
|
|
360
|
-
it('should throw a bad request exception if payload contains non whitelisted property', async () => {
|
|
361
|
-
const { body, status } = await e2e_setup_1.server.post('/auth/register', { email: 'unit@test.co', password: 'Test-2', role: 'ADMIN' });
|
|
362
|
-
expect(status).toBe(400);
|
|
363
|
-
expect(body).toEqual({
|
|
364
|
-
error: 'Bad Request',
|
|
365
|
-
message: ['property role should not exist'],
|
|
366
|
-
statusCode: 400,
|
|
367
|
-
});
|
|
368
|
-
});
|
|
369
|
-
it('should throw a bad request exception if validation fails', async () => {
|
|
370
|
-
const { body, status } = await e2e_setup_1.server.post('/auth/register', { email: 'unit.test.co', password: 'test-2' });
|
|
371
|
-
expect(status).toBe(400);
|
|
372
|
-
expect(body).toEqual({
|
|
373
|
-
error: 'Bad Request',
|
|
374
|
-
message: [
|
|
375
|
-
'email must be an email',
|
|
376
|
-
'password is not strong enough',
|
|
377
|
-
],
|
|
378
|
-
statusCode: 400,
|
|
379
|
-
});
|
|
380
|
-
});
|
|
381
|
-
it('should create a new user and return access token if the validation was successful', async () => {
|
|
382
|
-
const { body, status } = await e2e_setup_1.server.post('/auth/register', { email: 'unit@test.co', password: 'Test-2' });
|
|
383
|
-
expect(status).toBe(201);
|
|
384
|
-
expect(body).toEqual({ accessToken: expect.any(String) });
|
|
385
|
-
});
|
|
386
|
-
});
|
|
387
|
-
describe('POST /auth/login', () => {
|
|
388
|
-
beforeEach(async () => {
|
|
389
|
-
await e2e_setup_1.server.post('/auth/register', { email: 'unit@test.co', password: 'Test-2' });
|
|
390
|
-
});
|
|
391
|
-
it('should throw an unauthorized exception if payload contains non whitelisted property', async () => {
|
|
392
|
-
const { body, status } = await e2e_setup_1.server.post('/auth/login', { email: 'unit@test.co', password: 'Test-2', role: 'ADMIN' });
|
|
393
|
-
expect(status).toBe(400);
|
|
394
|
-
expect(body).toEqual({
|
|
395
|
-
error: 'Bad Request',
|
|
396
|
-
message: ['property role should not exist'],
|
|
397
|
-
statusCode: 400,
|
|
398
|
-
});
|
|
399
|
-
});
|
|
400
|
-
it('should throw an unauthorized exception if email is missing', async () => {
|
|
401
|
-
const { body, status } = await e2e_setup_1.server.post('/auth/login', { password: 'Test-2' });
|
|
402
|
-
expect(status).toBe(401);
|
|
403
|
-
expect(body).toEqual({
|
|
404
|
-
message: 'Unauthorized',
|
|
405
|
-
statusCode: 401,
|
|
406
|
-
});
|
|
407
|
-
});
|
|
408
|
-
it('should throw an unauthorized exception if password is missing', async () => {
|
|
409
|
-
const { body, status } = await e2e_setup_1.server.post('/auth/login', { email: 'unit@test.co' });
|
|
410
|
-
expect(status).toBe(401);
|
|
411
|
-
expect(body).toEqual({
|
|
412
|
-
message: 'Unauthorized',
|
|
413
|
-
statusCode: 401,
|
|
414
|
-
});
|
|
415
|
-
});
|
|
416
|
-
it('should return access token if the validation was successful', async () => {
|
|
417
|
-
await e2e_setup_1.server.post('/auth/register', { email: 'unit@test.co', password: 'test' });
|
|
418
|
-
const { body, status } = await e2e_setup_1.server.post('/auth/login', { email: 'unit@test.co', password: 'Test-2' });
|
|
419
|
-
expect(status).toBe(200);
|
|
420
|
-
expect(body).toEqual({ accessToken: expect.any(String) });
|
|
421
|
-
});
|
|
422
|
-
});
|
|
423
|
-
});
|
|
424
|
-
describe('POST /auth/register with register options', () => {
|
|
425
|
-
let User = class User extends src_1.BaseEntity {
|
|
426
|
-
constructor() {
|
|
427
|
-
super(...arguments);
|
|
428
|
-
this.role = 'user';
|
|
429
|
-
}
|
|
430
|
-
};
|
|
431
|
-
__decorate([
|
|
432
|
-
(0, mongoose_1.Prop)({ type: String, required: true }),
|
|
433
|
-
__metadata("design:type", String)
|
|
434
|
-
], User.prototype, "email", void 0);
|
|
435
|
-
__decorate([
|
|
436
|
-
(0, mongoose_1.Prop)({ type: String, required: true }),
|
|
437
|
-
__metadata("design:type", String)
|
|
438
|
-
], User.prototype, "password", void 0);
|
|
439
|
-
__decorate([
|
|
440
|
-
(0, mongoose_1.Prop)({ type: String, default: 'user' }),
|
|
441
|
-
__metadata("design:type", String)
|
|
442
|
-
], User.prototype, "role", void 0);
|
|
443
|
-
__decorate([
|
|
444
|
-
(0, mongoose_1.Prop)({ type: Boolean, default: false }),
|
|
445
|
-
__metadata("design:type", Boolean)
|
|
446
|
-
], User.prototype, "isVerified", void 0);
|
|
447
|
-
User = __decorate([
|
|
448
|
-
(0, mongoose_1.Schema)({ collection: 'users' })
|
|
449
|
-
], User);
|
|
450
|
-
const admin = { email: 'admin@test.co', password: 'admin', role: 'admin', isVerified: true };
|
|
451
|
-
const user = { email: 'user@test.co', password: 'user' };
|
|
452
|
-
beforeEach(async () => {
|
|
453
|
-
const bcryptService = new src_1.BcryptService();
|
|
454
|
-
const fixtures = async (_) => {
|
|
455
|
-
const model = await (0, utils_1.getModelFromEntity)(User);
|
|
456
|
-
await model.insertMany([
|
|
457
|
-
{ ...admin, password: await bcryptService.hashPassword(admin.password) },
|
|
458
|
-
{ ...user, password: await bcryptService.hashPassword(user.password) },
|
|
459
|
-
]);
|
|
460
|
-
};
|
|
461
|
-
await initModule({
|
|
462
|
-
useAuth: {
|
|
463
|
-
userEntity: User,
|
|
464
|
-
register: {
|
|
465
|
-
protected: true,
|
|
466
|
-
abilityPredicate: (user) => user.isVerified,
|
|
467
|
-
additionalFields: ['role'],
|
|
468
|
-
callback: async (user, { updateOneDocument }) => {
|
|
469
|
-
if (user.role !== 'admin') {
|
|
470
|
-
return;
|
|
471
|
-
}
|
|
472
|
-
await updateOneDocument(User, { _id: user.id }, { $set: { isVerified: true } });
|
|
473
|
-
},
|
|
474
|
-
},
|
|
475
|
-
login: {
|
|
476
|
-
additionalFields: ['role', 'isVerified'],
|
|
477
|
-
},
|
|
478
|
-
},
|
|
479
|
-
}, fixtures);
|
|
480
|
-
});
|
|
481
|
-
describe('protected', () => {
|
|
482
|
-
it('should throw an unauthorized exception if user is not logged in and protected is true', async () => {
|
|
483
|
-
const { body, status } = await e2e_setup_1.server.post('/auth/register', { email: 'unit@test.co', password: 'test' });
|
|
484
|
-
expect(status).toBe(401);
|
|
485
|
-
expect(body).toEqual({
|
|
486
|
-
message: 'Unauthorized',
|
|
487
|
-
statusCode: 401,
|
|
488
|
-
});
|
|
489
|
-
});
|
|
490
|
-
});
|
|
491
|
-
describe('abilityPredicate', () => {
|
|
492
|
-
it('should not create a new user if user is not verified', async () => {
|
|
493
|
-
const { email, password } = user;
|
|
494
|
-
const { body: { accessToken } } = await e2e_setup_1.server.post('/auth/login', { email, password });
|
|
495
|
-
const { body, status } = await e2e_setup_1.server.post('/auth/register', { email: 'unit@test.co', password: 'test' }, {
|
|
496
|
-
headers: { Authorization: `Bearer ${accessToken}` },
|
|
497
|
-
});
|
|
498
|
-
expect(status).toBe(403);
|
|
499
|
-
expect(body).toEqual({
|
|
500
|
-
error: 'Forbidden',
|
|
501
|
-
message: 'Access denied',
|
|
502
|
-
statusCode: 403,
|
|
503
|
-
});
|
|
504
|
-
});
|
|
505
|
-
it('should create a new user and return access token if user is verified', async () => {
|
|
506
|
-
const { email, password } = admin;
|
|
507
|
-
const { body: { accessToken } } = await e2e_setup_1.server.post('/auth/login', { email, password });
|
|
508
|
-
const { body, status } = await e2e_setup_1.server.post('/auth/register', { email: 'unit@test.co', password: 'test' }, {
|
|
509
|
-
headers: { Authorization: `Bearer ${accessToken}` },
|
|
510
|
-
});
|
|
511
|
-
expect(status).toBe(201);
|
|
512
|
-
expect(body).toEqual({ accessToken: expect.any(String) });
|
|
513
|
-
});
|
|
514
|
-
});
|
|
515
|
-
describe('additionalFields', () => {
|
|
516
|
-
it('should allow to register a new user with additional fields', async () => {
|
|
517
|
-
const { email, password } = admin;
|
|
518
|
-
const { body: { accessToken } } = await e2e_setup_1.server.post('/auth/login', { email, password });
|
|
519
|
-
const { body, status } = await e2e_setup_1.server.post('/auth/register', { email: 'client@test.co', password: 'client', role: 'client' }, {
|
|
520
|
-
headers: { Authorization: `Bearer ${accessToken}` },
|
|
521
|
-
});
|
|
522
|
-
expect(status).toBe(201);
|
|
523
|
-
expect(body).toEqual({ accessToken: expect.any(String) });
|
|
524
|
-
});
|
|
525
|
-
});
|
|
526
|
-
describe('callback', () => {
|
|
527
|
-
it('should not set isVerified to true if role is not admin', async () => {
|
|
528
|
-
const { email, password } = admin;
|
|
529
|
-
const { body: loginBody } = await e2e_setup_1.server.post('/auth/login', { email, password });
|
|
530
|
-
const { body: { accessToken } } = await e2e_setup_1.server.post('/auth/register', { email: 'client@test.co', password: 'client', role: 'client' }, {
|
|
531
|
-
headers: { Authorization: `Bearer ${loginBody.accessToken}` },
|
|
532
|
-
});
|
|
533
|
-
const { body, status } = await e2e_setup_1.server.get('/auth/account', { headers: { Authorization: `Bearer ${accessToken}` } });
|
|
534
|
-
expect(status).toBe(200);
|
|
535
|
-
expect(body).toHaveProperty('isVerified', false);
|
|
536
|
-
});
|
|
537
|
-
it('should set isVerified to true if role is admin', async () => {
|
|
538
|
-
const { email, password } = admin;
|
|
539
|
-
const { body: loginBody } = await e2e_setup_1.server.post('/auth/login', { email, password });
|
|
540
|
-
const { body: { accessToken } } = await e2e_setup_1.server.post('/auth/register', { email: 'admin2@test.co', password: 'admin2', role: 'admin' }, {
|
|
541
|
-
headers: { Authorization: `Bearer ${loginBody.accessToken}` },
|
|
542
|
-
});
|
|
543
|
-
const { body, status } = await e2e_setup_1.server.get('/auth/account', { headers: { Authorization: `Bearer ${accessToken}` } });
|
|
544
|
-
expect(status).toBe(200);
|
|
545
|
-
expect(body).toHaveProperty('isVerified', true);
|
|
546
|
-
});
|
|
547
|
-
});
|
|
548
|
-
});
|
|
549
|
-
describe('POST /auth/login with login options', () => {
|
|
550
|
-
let User = class User extends src_1.BaseEntity {
|
|
551
|
-
constructor() {
|
|
552
|
-
super(...arguments);
|
|
553
|
-
this.role = 'user';
|
|
554
|
-
}
|
|
555
|
-
};
|
|
556
|
-
__decorate([
|
|
557
|
-
(0, mongoose_1.Prop)({ type: String, required: true }),
|
|
558
|
-
__metadata("design:type", String)
|
|
559
|
-
], User.prototype, "username", void 0);
|
|
560
|
-
__decorate([
|
|
561
|
-
(0, mongoose_1.Prop)({ type: String, required: true }),
|
|
562
|
-
__metadata("design:type", String)
|
|
563
|
-
], User.prototype, "pass", void 0);
|
|
564
|
-
__decorate([
|
|
565
|
-
(0, mongoose_1.Prop)({ type: String, default: 'user' }),
|
|
566
|
-
__metadata("design:type", String)
|
|
567
|
-
], User.prototype, "role", void 0);
|
|
568
|
-
__decorate([
|
|
569
|
-
(0, mongoose_1.Prop)({ type: Boolean, default: false }),
|
|
570
|
-
__metadata("design:type", Boolean)
|
|
571
|
-
], User.prototype, "isVerified", void 0);
|
|
572
|
-
User = __decorate([
|
|
573
|
-
(0, mongoose_1.Schema)({ collection: 'users' })
|
|
574
|
-
], User);
|
|
575
|
-
const admin = { username: 'admin', pass: 'admin', role: 'admin', isVerified: true };
|
|
576
|
-
const user = { username: 'user', pass: 'user' };
|
|
577
|
-
const client = { username: 'client', pass: 'client', role: 'client', isVerified: true };
|
|
578
|
-
beforeEach(async () => {
|
|
579
|
-
const bcryptService = new src_1.BcryptService();
|
|
580
|
-
const fixtures = async (_) => {
|
|
581
|
-
const model = await (0, utils_1.getModelFromEntity)(User);
|
|
582
|
-
await model.insertMany([
|
|
583
|
-
{ ...admin, pass: await bcryptService.hashPassword(admin.pass) },
|
|
584
|
-
{ ...user, pass: await bcryptService.hashPassword(user.pass) },
|
|
585
|
-
{ ...client, pass: await bcryptService.hashPassword(client.pass) },
|
|
586
|
-
]);
|
|
587
|
-
};
|
|
588
|
-
await initModule({
|
|
589
|
-
useAuth: {
|
|
590
|
-
userEntity: User,
|
|
591
|
-
login: {
|
|
592
|
-
loginField: 'username',
|
|
593
|
-
passwordField: 'pass',
|
|
594
|
-
additionalFields: ['role', 'isVerified'],
|
|
595
|
-
abilityPredicate: (user) => user.role === 'admin' || user.role === 'user',
|
|
596
|
-
callback: async (user) => {
|
|
597
|
-
if (user.isVerified) {
|
|
598
|
-
return;
|
|
599
|
-
}
|
|
600
|
-
throw new common_1.UnauthorizedException(`Hello ${user.username}, you must verify your account first!`);
|
|
601
|
-
},
|
|
602
|
-
},
|
|
603
|
-
},
|
|
604
|
-
}, fixtures);
|
|
605
|
-
});
|
|
606
|
-
describe('loginField', () => {
|
|
607
|
-
it('should throw an unauthorized exception if loginField is missing', async () => {
|
|
608
|
-
const { body, status } = await e2e_setup_1.server.post('/auth/login', { pass: 'test' });
|
|
609
|
-
expect(status).toBe(401);
|
|
610
|
-
expect(body).toEqual({
|
|
611
|
-
message: 'Unauthorized',
|
|
612
|
-
statusCode: 401,
|
|
613
|
-
});
|
|
614
|
-
});
|
|
615
|
-
});
|
|
616
|
-
describe('passwordField', () => {
|
|
617
|
-
it('should throw an unauthorized exception if passwordField is missing', async () => {
|
|
618
|
-
const { body, status } = await e2e_setup_1.server.post('/auth/login', { username: 'unit' });
|
|
619
|
-
expect(status).toBe(401);
|
|
620
|
-
expect(body).toEqual({
|
|
621
|
-
message: 'Unauthorized',
|
|
622
|
-
statusCode: 401,
|
|
623
|
-
});
|
|
624
|
-
});
|
|
625
|
-
});
|
|
626
|
-
describe('abilityPredicate', () => {
|
|
627
|
-
it('should throw an forbidden exception if user role is not admin or user', async () => {
|
|
628
|
-
const { username, pass } = client;
|
|
629
|
-
const { body, status } = await e2e_setup_1.server.post('/auth/login', { username, pass });
|
|
630
|
-
expect(status).toBe(403);
|
|
631
|
-
expect(body).toEqual({
|
|
632
|
-
error: 'Forbidden',
|
|
633
|
-
message: 'Access denied',
|
|
634
|
-
statusCode: 403,
|
|
635
|
-
});
|
|
636
|
-
});
|
|
637
|
-
});
|
|
638
|
-
describe('callback', () => {
|
|
639
|
-
it('should throw an unauthorized exception if user is not verified', async () => {
|
|
640
|
-
const { username, pass } = user;
|
|
641
|
-
const { body, status } = await e2e_setup_1.server.post('/auth/login', { username, pass });
|
|
642
|
-
expect(status).toBe(401);
|
|
643
|
-
expect(body).toEqual({
|
|
644
|
-
error: 'Unauthorized',
|
|
645
|
-
message: `Hello ${username}, you must verify your account first!`,
|
|
646
|
-
statusCode: 401,
|
|
647
|
-
});
|
|
648
|
-
});
|
|
649
|
-
});
|
|
650
|
-
describe('additionalFields', () => {
|
|
651
|
-
it('should return additional fields', async () => {
|
|
652
|
-
const { username, pass } = admin;
|
|
653
|
-
const { body: { accessToken } } = await e2e_setup_1.server.post('/auth/login', { username, pass });
|
|
654
|
-
const { body, status } = await e2e_setup_1.server.get('/auth/account', { headers: { Authorization: `Bearer ${accessToken}` } });
|
|
655
|
-
expect(status).toBe(200);
|
|
656
|
-
expect(body).toEqual({ id: expect.any(String), username: 'admin', role: 'admin', isVerified: true });
|
|
657
|
-
});
|
|
658
|
-
});
|
|
659
|
-
});
|
|
660
|
-
describe('useAuth with resetPassword options', () => {
|
|
661
|
-
let User = class User extends src_1.BaseEntity {
|
|
662
|
-
};
|
|
663
|
-
__decorate([
|
|
664
|
-
(0, mongoose_1.Prop)({ type: String, required: true }),
|
|
665
|
-
__metadata("design:type", String)
|
|
666
|
-
], User.prototype, "email", void 0);
|
|
667
|
-
__decorate([
|
|
668
|
-
(0, mongoose_1.Prop)({ type: String, required: true }),
|
|
669
|
-
__metadata("design:type", String)
|
|
670
|
-
], User.prototype, "password", void 0);
|
|
671
|
-
__decorate([
|
|
672
|
-
(0, mongoose_1.Prop)({ type: Boolean, default: false }),
|
|
673
|
-
__metadata("design:type", Boolean)
|
|
674
|
-
], User.prototype, "isVerified", void 0);
|
|
675
|
-
__decorate([
|
|
676
|
-
(0, mongoose_1.Prop)({ type: String }),
|
|
677
|
-
__metadata("design:type", String)
|
|
678
|
-
], User.prototype, "resetPasswordToken", void 0);
|
|
679
|
-
User = __decorate([
|
|
680
|
-
(0, mongoose_1.Schema)({ collection: 'users' })
|
|
681
|
-
], User);
|
|
682
|
-
let model;
|
|
683
|
-
let user;
|
|
684
|
-
let client;
|
|
685
|
-
let app;
|
|
686
|
-
beforeEach(async () => {
|
|
687
|
-
user = { email: 'user@test.co', password: 'user', isVerified: true };
|
|
688
|
-
client = { email: 'client@test.co', password: 'client' };
|
|
689
|
-
const bcryptService = new src_1.BcryptService();
|
|
690
|
-
const fixtures = async (_) => {
|
|
691
|
-
model = await (0, utils_1.getModelFromEntity)(User);
|
|
692
|
-
await model.insertMany([
|
|
693
|
-
{ ...user, password: await bcryptService.hashPassword(user.password) },
|
|
694
|
-
{ ...client, password: await bcryptService.hashPassword(client.password) },
|
|
695
|
-
]);
|
|
696
|
-
};
|
|
697
|
-
app = await initModule({
|
|
698
|
-
useAuth: {
|
|
699
|
-
userEntity: User,
|
|
700
|
-
resetPassword: {
|
|
701
|
-
emailField: 'email',
|
|
702
|
-
expirationInMinutes: 1,
|
|
703
|
-
resetPasswordCallback: async ({ resetPasswordToken }, { updateUserByEmail }) => {
|
|
704
|
-
await updateUserByEmail({ $set: { resetPasswordToken } });
|
|
705
|
-
},
|
|
706
|
-
changePasswordAbilityPredicate: (user) => user.isVerified && !!user.resetPasswordToken,
|
|
707
|
-
changePasswordCallback: async (user, { updateOneDocument }) => {
|
|
708
|
-
await updateOneDocument(User, { _id: user.id }, { $unset: { resetPasswordToken: 1 } });
|
|
709
|
-
},
|
|
710
|
-
},
|
|
711
|
-
},
|
|
712
|
-
}, fixtures);
|
|
713
|
-
});
|
|
714
|
-
describe('POST /auth/reset-password', () => {
|
|
715
|
-
it('should throw a bad request exception if email is missing if no validation options are provided', async () => {
|
|
716
|
-
const { body, status } = await e2e_setup_1.server.post('/auth/reset-password', {});
|
|
717
|
-
expect(status).toBe(400);
|
|
718
|
-
expect(body).toEqual({
|
|
719
|
-
error: 'Bad Request',
|
|
720
|
-
message: 'Invalid or missing argument',
|
|
721
|
-
statusCode: 400,
|
|
722
|
-
});
|
|
723
|
-
});
|
|
724
|
-
it('should not throw a bad request exception if email is invalid if no validation options are provided', async () => {
|
|
725
|
-
const { body, status } = await e2e_setup_1.server.post('/auth/reset-password', { email: 'unit.test.co' });
|
|
726
|
-
expect(status).toBe(204);
|
|
727
|
-
expect(body).toEqual({});
|
|
728
|
-
});
|
|
729
|
-
it('should not throw an exception if email is not found', async () => {
|
|
730
|
-
const { body, status } = await e2e_setup_1.server.post('/auth/reset-password', { email: 'invalid@test.co' });
|
|
731
|
-
expect(status).toBe(204);
|
|
732
|
-
expect(body).toEqual({});
|
|
733
|
-
});
|
|
734
|
-
describe('resetPasswordCallback', () => {
|
|
735
|
-
it('should set resetPasswordToken if email is valid', async () => {
|
|
736
|
-
const { email } = user;
|
|
737
|
-
const { resetPasswordToken: resetPasswordTokenBeforeUpdate } = (await model.findOne({ email }).lean().exec());
|
|
738
|
-
const { status } = await e2e_setup_1.server.post('/auth/reset-password', { email });
|
|
739
|
-
const { resetPasswordToken: resetPasswordTokenAfterUpdate } = (await model.findOne({ email }).lean().exec());
|
|
740
|
-
expect(status).toBe(204);
|
|
741
|
-
expect(resetPasswordTokenBeforeUpdate).toStrictEqual(undefined);
|
|
742
|
-
expect(resetPasswordTokenAfterUpdate).toStrictEqual(expect.any(String));
|
|
743
|
-
});
|
|
744
|
-
});
|
|
745
|
-
});
|
|
746
|
-
describe('PATCH /auth/change-password', () => {
|
|
747
|
-
it('should throw a bad request exception if resetPasswordToken is missing', async () => {
|
|
748
|
-
const { body, status } = await e2e_setup_1.server.patch('/auth/change-password', { newPassword: 'test' });
|
|
749
|
-
expect(status).toBe(400);
|
|
750
|
-
expect(body).toEqual({
|
|
751
|
-
error: 'Bad Request',
|
|
752
|
-
message: 'Invalid or missing argument',
|
|
753
|
-
statusCode: 400,
|
|
754
|
-
});
|
|
755
|
-
});
|
|
756
|
-
it('should throw a bad request exception if newPassword is missing', async () => {
|
|
757
|
-
const { email } = user;
|
|
758
|
-
await e2e_setup_1.server.post('/auth/reset-password', { email });
|
|
759
|
-
const { resetPasswordToken: resetPasswordTokenAfterUpdate } = (await model.findOne({ email }).lean().exec());
|
|
760
|
-
const resetPasswordToken = resetPasswordTokenAfterUpdate;
|
|
761
|
-
const { body, status } = await e2e_setup_1.server.patch('/auth/change-password', { resetPasswordToken });
|
|
762
|
-
expect(status).toBe(400);
|
|
763
|
-
expect(body).toEqual({
|
|
764
|
-
error: 'Bad Request',
|
|
765
|
-
message: 'Invalid or missing argument',
|
|
766
|
-
statusCode: 400,
|
|
767
|
-
});
|
|
768
|
-
});
|
|
769
|
-
it('should throw an unauthorized exception if resetPasswordToken is invalid', async () => {
|
|
770
|
-
const { body, status } = await e2e_setup_1.server.patch('/auth/change-password', { resetPasswordToken: 'test', newPassword: 'newPassword' });
|
|
771
|
-
expect(status).toBe(400);
|
|
772
|
-
expect(body).toEqual({
|
|
773
|
-
error: 'Bad Request',
|
|
774
|
-
message: 'Invalid reset password token. Please redo the reset password process.',
|
|
775
|
-
statusCode: 400,
|
|
776
|
-
});
|
|
777
|
-
});
|
|
778
|
-
it('should throw an unauthorized exception if resetPasswordToken is expired', async () => {
|
|
779
|
-
const jwtService = app.get(jwt_1.JwtService);
|
|
780
|
-
const expiredResetPasswordToken = jwtService.sign({ email: user.email }, { expiresIn: 1 });
|
|
781
|
-
await (0, utils_1.wait)(500);
|
|
782
|
-
const { body, status } = await e2e_setup_1.server.patch('/auth/change-password', { resetPasswordToken: expiredResetPasswordToken, newPassword: 'newPassword' });
|
|
783
|
-
expect(status).toBe(401);
|
|
784
|
-
expect(body).toEqual({
|
|
785
|
-
error: 'Unauthorized',
|
|
786
|
-
message: 'Time to reset password has expired. Please redo the reset password process.',
|
|
787
|
-
statusCode: 401,
|
|
788
|
-
});
|
|
789
|
-
});
|
|
790
|
-
describe('changePasswordAbilityPredicate', () => {
|
|
791
|
-
let resetPasswordToken;
|
|
792
|
-
beforeEach(async () => {
|
|
793
|
-
await e2e_setup_1.server.post('/auth/reset-password', { email: client.email });
|
|
794
|
-
const { resetPasswordToken: token } = (await model.findOne({ email: client.email }).lean().exec());
|
|
795
|
-
resetPasswordToken = token;
|
|
796
|
-
});
|
|
797
|
-
it('should throw a forbidden exception if user is not allowed to change password', async () => {
|
|
798
|
-
expect(resetPasswordToken).toStrictEqual(expect.any(String));
|
|
799
|
-
const { body, status } = await e2e_setup_1.server.patch('/auth/change-password', { resetPasswordToken, newPassword: 'newPassword' });
|
|
800
|
-
expect(status).toBe(403);
|
|
801
|
-
expect(body).toEqual({
|
|
802
|
-
error: 'Forbidden',
|
|
803
|
-
message: 'You are not allowed to change your password.',
|
|
804
|
-
statusCode: 403,
|
|
805
|
-
});
|
|
806
|
-
});
|
|
807
|
-
});
|
|
808
|
-
describe('changePasswordCallback', () => {
|
|
809
|
-
let resetPasswordToken;
|
|
810
|
-
beforeEach(async () => {
|
|
811
|
-
await e2e_setup_1.server.post('/auth/reset-password', { email: user.email });
|
|
812
|
-
const { resetPasswordToken: token } = (await model.findOne({ email: user.email }).lean().exec());
|
|
813
|
-
resetPasswordToken = token;
|
|
814
|
-
});
|
|
815
|
-
it('should change password and unset resetPasswordToken if resetPasswordToken is valid', async () => {
|
|
816
|
-
expect(resetPasswordToken).toStrictEqual(expect.any(String));
|
|
817
|
-
const newPassword = 'newPassword';
|
|
818
|
-
const bcryptService = app.get(src_1.BcryptService);
|
|
819
|
-
const { password: passwordBeforeUpdate } = (await model.findOne({ email: user.email }).lean().exec());
|
|
820
|
-
const { status } = await e2e_setup_1.server.patch('/auth/change-password', { resetPasswordToken, newPassword });
|
|
821
|
-
const { password: passwordAfterUpdate, resetPasswordToken: tokenAfterUpdate } = (await model.findOne({ email: user.email }).lean().exec());
|
|
822
|
-
const isPreviousPassword = await bcryptService.comparePassword(user.password, passwordBeforeUpdate);
|
|
823
|
-
expect(isPreviousPassword).toBe(true);
|
|
824
|
-
const isNewPassword = await bcryptService.comparePassword(newPassword, passwordAfterUpdate);
|
|
825
|
-
expect(isNewPassword).toBe(true);
|
|
826
|
-
expect(status).toBe(204);
|
|
827
|
-
expect(tokenAfterUpdate).toStrictEqual(undefined);
|
|
828
|
-
});
|
|
829
|
-
});
|
|
830
|
-
});
|
|
831
|
-
});
|
|
832
|
-
});
|
|
833
|
-
describe('Websockets', () => {
|
|
834
|
-
it('should enable websockets globally', async () => {
|
|
835
|
-
await initModule({
|
|
836
|
-
webSocket: true,
|
|
837
|
-
}, undefined, async (app) => {
|
|
838
|
-
app.useWebSocketAdapter(new socket_adapter_1.SocketAdapter(app));
|
|
839
|
-
}, true);
|
|
840
|
-
expect(src_1.DynamicApiModule.state.get('gatewayOptions')).toStrictEqual({});
|
|
841
|
-
await e2e_setup_1.server.emit('test', { message: 'Hello' });
|
|
842
|
-
expect(e2e_setup_1.handleSocketResponse).toHaveBeenCalledTimes(1);
|
|
843
|
-
expect(e2e_setup_1.handleSocketResponse).toHaveBeenCalledWith({ message: 'Hello' });
|
|
844
|
-
expect(e2e_setup_1.handleSocketException).not.toHaveBeenCalled();
|
|
845
|
-
});
|
|
846
|
-
describe('Authentication EVENTS', () => {
|
|
847
|
-
describe('useAuth when only userEntity is provided', () => {
|
|
848
|
-
let UserEntity = class UserEntity extends src_1.BaseEntity {
|
|
849
|
-
};
|
|
850
|
-
__decorate([
|
|
851
|
-
(0, mongoose_1.Prop)({ type: String, required: true }),
|
|
852
|
-
__metadata("design:type", String)
|
|
853
|
-
], UserEntity.prototype, "email", void 0);
|
|
854
|
-
__decorate([
|
|
855
|
-
(0, mongoose_1.Prop)({ type: String, required: true }),
|
|
856
|
-
__metadata("design:type", String)
|
|
857
|
-
], UserEntity.prototype, "password", void 0);
|
|
858
|
-
UserEntity = __decorate([
|
|
859
|
-
(0, mongoose_1.Schema)({ collection: 'users' })
|
|
860
|
-
], UserEntity);
|
|
861
|
-
beforeEach(async () => {
|
|
862
|
-
await initModule({
|
|
863
|
-
useAuth: {
|
|
864
|
-
userEntity: UserEntity,
|
|
865
|
-
webSocket: true,
|
|
866
|
-
},
|
|
867
|
-
}, undefined, async (app) => {
|
|
868
|
-
app.useWebSocketAdapter(new socket_adapter_1.SocketAdapter(app));
|
|
869
|
-
});
|
|
870
|
-
});
|
|
871
|
-
describe('EVENT auth-register', () => {
|
|
872
|
-
it('should throw a ws exception if email is missing', async () => {
|
|
873
|
-
await e2e_setup_1.server.emit('auth-register', { username: 'unit-test', password: 'test-2' });
|
|
874
|
-
expect(e2e_setup_1.handleSocketException).toHaveBeenCalledTimes(1);
|
|
875
|
-
expect(e2e_setup_1.handleSocketException).toHaveBeenCalledWith({
|
|
876
|
-
message: ['email property is required'],
|
|
877
|
-
});
|
|
878
|
-
expect(e2e_setup_1.handleSocketResponse).not.toHaveBeenCalled();
|
|
879
|
-
});
|
|
880
|
-
it('should throw a ws exception if password is missing', async () => {
|
|
881
|
-
await e2e_setup_1.server.emit('auth-register', { email: 'unit@test.co', pass: 'test-2' });
|
|
882
|
-
expect(e2e_setup_1.handleSocketException).toHaveBeenCalledTimes(1);
|
|
883
|
-
expect(e2e_setup_1.handleSocketException).toHaveBeenCalledWith({
|
|
884
|
-
message: ['password property is required'],
|
|
885
|
-
});
|
|
886
|
-
expect(e2e_setup_1.handleSocketResponse).not.toHaveBeenCalled();
|
|
887
|
-
});
|
|
888
|
-
it('should create a new user and return access token', async () => {
|
|
889
|
-
await e2e_setup_1.server.emit('auth-register', { email: 'unit@test.co', password: 'test' });
|
|
890
|
-
expect(e2e_setup_1.handleSocketException).not.toHaveBeenCalled();
|
|
891
|
-
expect(e2e_setup_1.handleSocketResponse).toHaveBeenCalledTimes(1);
|
|
892
|
-
expect(e2e_setup_1.handleSocketResponse).toHaveBeenCalledWith({ accessToken: expect.any(String) });
|
|
893
|
-
});
|
|
894
|
-
});
|
|
895
|
-
describe('EVENT auth-login', () => {
|
|
896
|
-
it('should throw a ws exception if email is missing', async () => {
|
|
897
|
-
await e2e_setup_1.server.emit('auth-login', { password: 'test-2' });
|
|
898
|
-
expect(e2e_setup_1.handleSocketResponse).not.toHaveBeenCalled();
|
|
899
|
-
expect(e2e_setup_1.handleSocketException).toHaveBeenCalledTimes(1);
|
|
900
|
-
expect(e2e_setup_1.handleSocketException).toHaveBeenCalledWith({
|
|
901
|
-
message: 'Unauthorized',
|
|
902
|
-
});
|
|
903
|
-
});
|
|
904
|
-
it('should throw a ws exception if password is missing', async () => {
|
|
905
|
-
await e2e_setup_1.server.emit('auth-login', { email: 'unit@test.co' });
|
|
906
|
-
expect(e2e_setup_1.handleSocketResponse).not.toHaveBeenCalled();
|
|
907
|
-
expect(e2e_setup_1.handleSocketException).toHaveBeenCalledTimes(1);
|
|
908
|
-
expect(e2e_setup_1.handleSocketException).toHaveBeenCalledWith({
|
|
909
|
-
message: 'Unauthorized',
|
|
910
|
-
});
|
|
911
|
-
});
|
|
912
|
-
it('should return access token', async () => {
|
|
913
|
-
await e2e_setup_1.server.emit('auth-register', { email: 'unit@test.co', password: 'test' });
|
|
914
|
-
await e2e_setup_1.server.emit('auth-login', { email: 'unit@test.co', password: 'test' });
|
|
915
|
-
expect(e2e_setup_1.handleSocketException).not.toHaveBeenCalled();
|
|
916
|
-
expect(e2e_setup_1.handleSocketResponse).toHaveBeenCalledTimes(2);
|
|
917
|
-
expect(e2e_setup_1.handleSocketResponse).toHaveBeenNthCalledWith(2, { accessToken: expect.any(String) });
|
|
918
|
-
});
|
|
919
|
-
});
|
|
920
|
-
describe('EVENT auth-get-account', () => {
|
|
921
|
-
it('should throw a ws exception if access token is missing', async () => {
|
|
922
|
-
await e2e_setup_1.server.emit('auth-get-account');
|
|
923
|
-
expect(e2e_setup_1.handleSocketException).toHaveBeenCalledTimes(1);
|
|
924
|
-
expect(e2e_setup_1.handleSocketException).toHaveBeenCalledWith({
|
|
925
|
-
message: 'Unauthorized',
|
|
926
|
-
});
|
|
927
|
-
expect(e2e_setup_1.handleSocketResponse).not.toHaveBeenCalled();
|
|
928
|
-
});
|
|
929
|
-
it('should return user account', async () => {
|
|
930
|
-
const { accessToken } = await e2e_setup_1.server.emit('auth-register', { email: 'unit@test.co', password: 'test' });
|
|
931
|
-
await e2e_setup_1.server.emit('auth-get-account', undefined, { accessToken });
|
|
932
|
-
expect(e2e_setup_1.handleSocketException).not.toHaveBeenCalled();
|
|
933
|
-
expect(e2e_setup_1.handleSocketResponse).toHaveBeenCalledTimes(2);
|
|
934
|
-
expect(e2e_setup_1.handleSocketResponse).toHaveBeenNthCalledWith(1, { accessToken: expect.any(String) });
|
|
935
|
-
expect(e2e_setup_1.handleSocketResponse).toHaveBeenNthCalledWith(2, { email: 'unit@test.co', id: expect.any(String) });
|
|
936
|
-
});
|
|
937
|
-
});
|
|
938
|
-
it('should throw a ws exception when receiving reset password event if reset password options are not configured', async () => {
|
|
939
|
-
await e2e_setup_1.server.emit('auth-reset-password', { email: 'toto@test.co' });
|
|
940
|
-
expect(e2e_setup_1.handleSocketException).toHaveBeenCalledTimes(1);
|
|
941
|
-
expect(e2e_setup_1.handleSocketException).toHaveBeenCalledWith({
|
|
942
|
-
message: 'This feature is not available',
|
|
943
|
-
});
|
|
944
|
-
expect(e2e_setup_1.handleSocketResponse).not.toHaveBeenCalled();
|
|
945
|
-
});
|
|
946
|
-
it('should throw a ws exception when receiving change password event if reset password options are not configured', async () => {
|
|
947
|
-
await e2e_setup_1.server.emit('auth-change-password', { newPassword: 'test' });
|
|
948
|
-
expect(e2e_setup_1.handleSocketException).toHaveBeenCalledTimes(1);
|
|
949
|
-
expect(e2e_setup_1.handleSocketException).toHaveBeenCalledWith({
|
|
950
|
-
message: 'This feature is not available',
|
|
951
|
-
});
|
|
952
|
-
expect(e2e_setup_1.handleSocketResponse).not.toHaveBeenCalled();
|
|
953
|
-
});
|
|
954
|
-
});
|
|
955
|
-
describe('useAuth with jwt options', () => {
|
|
956
|
-
let jwtService;
|
|
957
|
-
let accessToken;
|
|
958
|
-
let app;
|
|
959
|
-
let UserEntity = class UserEntity extends src_1.BaseEntity {
|
|
960
|
-
};
|
|
961
|
-
__decorate([
|
|
962
|
-
(0, mongoose_1.Prop)({ type: String, required: true }),
|
|
963
|
-
__metadata("design:type", String)
|
|
964
|
-
], UserEntity.prototype, "email", void 0);
|
|
965
|
-
__decorate([
|
|
966
|
-
(0, mongoose_1.Prop)({ type: String, required: true }),
|
|
967
|
-
__metadata("design:type", String)
|
|
968
|
-
], UserEntity.prototype, "password", void 0);
|
|
969
|
-
UserEntity = __decorate([
|
|
970
|
-
(0, mongoose_1.Schema)({ collection: 'users' })
|
|
971
|
-
], UserEntity);
|
|
972
|
-
beforeEach(async () => {
|
|
973
|
-
app = await initModule({
|
|
974
|
-
useAuth: {
|
|
975
|
-
userEntity: UserEntity,
|
|
976
|
-
webSocket: true,
|
|
977
|
-
jwt: {
|
|
978
|
-
secret: 'test-secret',
|
|
979
|
-
expiresIn: '3s',
|
|
980
|
-
},
|
|
981
|
-
},
|
|
982
|
-
}, undefined, async (_) => {
|
|
983
|
-
_.useWebSocketAdapter(new socket_adapter_1.SocketAdapter(_));
|
|
984
|
-
});
|
|
985
|
-
jwtService = app.get(jwt_1.JwtService);
|
|
986
|
-
const { accessToken: token } = await e2e_setup_1.server.emit('auth-register', { email: 'unit@test.co', password: 'test' });
|
|
987
|
-
accessToken = token;
|
|
988
|
-
});
|
|
989
|
-
it('should initialize state and authentication EVENTS with jwt options', async () => {
|
|
990
|
-
expect(app).toBeDefined();
|
|
991
|
-
expect(src_1.DynamicApiModule.state.get()).toStrictEqual({
|
|
992
|
-
uri,
|
|
993
|
-
initialized: true,
|
|
994
|
-
isGlobalCacheEnabled: true,
|
|
995
|
-
connectionName: 'dynamic-api-connection',
|
|
996
|
-
cacheExcludedPaths: [],
|
|
997
|
-
credentials: {
|
|
998
|
-
loginField: 'email',
|
|
999
|
-
passwordField: 'password',
|
|
1000
|
-
},
|
|
1001
|
-
isAuthEnabled: true,
|
|
1002
|
-
jwtExpirationTime: '3s',
|
|
1003
|
-
jwtSecret: 'test-secret',
|
|
1004
|
-
routesConfig: {
|
|
1005
|
-
defaults: [
|
|
1006
|
-
'GetMany',
|
|
1007
|
-
'GetOne',
|
|
1008
|
-
'CreateMany',
|
|
1009
|
-
'CreateOne',
|
|
1010
|
-
'UpdateMany',
|
|
1011
|
-
'UpdateOne',
|
|
1012
|
-
'ReplaceOne',
|
|
1013
|
-
'DuplicateMany',
|
|
1014
|
-
'DuplicateOne',
|
|
1015
|
-
'DeleteMany',
|
|
1016
|
-
'DeleteOne',
|
|
1017
|
-
],
|
|
1018
|
-
excluded: [],
|
|
1019
|
-
},
|
|
1020
|
-
gatewayOptions: undefined,
|
|
1021
|
-
});
|
|
1022
|
-
});
|
|
1023
|
-
it('should throw a ws exception if access token is expired', async () => {
|
|
1024
|
-
e2e_setup_1.handleSocketResponse.mockReset();
|
|
1025
|
-
await (0, utils_1.wait)(4000);
|
|
1026
|
-
await e2e_setup_1.server.emit('auth-get-account', undefined, { accessToken });
|
|
1027
|
-
expect(e2e_setup_1.handleSocketException).toHaveBeenCalledTimes(1);
|
|
1028
|
-
expect(e2e_setup_1.handleSocketException).toHaveBeenCalledWith({
|
|
1029
|
-
message: 'Unauthorized',
|
|
1030
|
-
});
|
|
1031
|
-
expect(e2e_setup_1.handleSocketResponse).not.toHaveBeenCalled();
|
|
1032
|
-
});
|
|
1033
|
-
it('should throw a ws exception if secret is invalid', async () => {
|
|
1034
|
-
e2e_setup_1.handleSocketResponse.mockReset();
|
|
1035
|
-
const invalidToken = jwtService.sign({ email: 'u', password: 'p' }, { secret: 'invalid-secret' });
|
|
1036
|
-
await e2e_setup_1.server.emit('auth-get-account', undefined, { accessToken: invalidToken });
|
|
1037
|
-
expect(e2e_setup_1.handleSocketException).toHaveBeenCalledTimes(1);
|
|
1038
|
-
expect(e2e_setup_1.handleSocketException).toHaveBeenCalledWith({
|
|
1039
|
-
message: 'Unauthorized',
|
|
1040
|
-
});
|
|
1041
|
-
expect(e2e_setup_1.handleSocketResponse).not.toHaveBeenCalled();
|
|
1042
|
-
});
|
|
1043
|
-
});
|
|
1044
|
-
describe('useAuth with validation options', () => {
|
|
1045
|
-
let UserEntity = class UserEntity extends src_1.BaseEntity {
|
|
1046
|
-
};
|
|
1047
|
-
__decorate([
|
|
1048
|
-
(0, mongoose_1.Prop)({ type: String, required: true }),
|
|
1049
|
-
(0, class_validator_1.IsEmail)(),
|
|
1050
|
-
__metadata("design:type", String)
|
|
1051
|
-
], UserEntity.prototype, "email", void 0);
|
|
1052
|
-
__decorate([
|
|
1053
|
-
(0, mongoose_1.Prop)({ type: String, required: true }),
|
|
1054
|
-
(0, class_validator_1.IsStrongPassword)({
|
|
1055
|
-
minLength: 6,
|
|
1056
|
-
minLowercase: 1,
|
|
1057
|
-
minUppercase: 1,
|
|
1058
|
-
minNumbers: 1,
|
|
1059
|
-
minSymbols: 1,
|
|
1060
|
-
}),
|
|
1061
|
-
__metadata("design:type", String)
|
|
1062
|
-
], UserEntity.prototype, "password", void 0);
|
|
1063
|
-
UserEntity = __decorate([
|
|
1064
|
-
(0, mongoose_1.Schema)({ collection: 'users' })
|
|
1065
|
-
], UserEntity);
|
|
1066
|
-
beforeEach(async () => {
|
|
1067
|
-
await initModule({
|
|
1068
|
-
useAuth: {
|
|
1069
|
-
userEntity: UserEntity,
|
|
1070
|
-
validationPipeOptions: {
|
|
1071
|
-
whitelist: true,
|
|
1072
|
-
forbidNonWhitelisted: true,
|
|
1073
|
-
transform: true,
|
|
1074
|
-
},
|
|
1075
|
-
},
|
|
1076
|
-
webSocket: true,
|
|
1077
|
-
}, undefined, async (_) => {
|
|
1078
|
-
_.useWebSocketAdapter(new socket_adapter_1.SocketAdapter(_));
|
|
1079
|
-
});
|
|
1080
|
-
e2e_setup_1.handleSocketResponse.mockReset();
|
|
1081
|
-
});
|
|
1082
|
-
describe('EVENT auth-register', () => {
|
|
1083
|
-
it('should throw a ws exception if payload contains non whitelisted property', async () => {
|
|
1084
|
-
await e2e_setup_1.server.emit('auth-register', { email: 'unit@test.co', password: 'Test-2', role: 'ADMIN' });
|
|
1085
|
-
expect(e2e_setup_1.handleSocketException).toHaveBeenCalledTimes(1);
|
|
1086
|
-
expect(e2e_setup_1.handleSocketException).toHaveBeenCalledWith({
|
|
1087
|
-
message: ['property role should not exist'],
|
|
1088
|
-
});
|
|
1089
|
-
expect(e2e_setup_1.handleSocketResponse).not.toHaveBeenCalled();
|
|
1090
|
-
});
|
|
1091
|
-
it('should throw a ws exception if validation fails', async () => {
|
|
1092
|
-
await e2e_setup_1.server.emit('auth-register', { email: 'unit.test.co', password: 'test-2' });
|
|
1093
|
-
expect(e2e_setup_1.handleSocketException).toHaveBeenCalledTimes(1);
|
|
1094
|
-
expect(e2e_setup_1.handleSocketException).toHaveBeenCalledWith({
|
|
1095
|
-
message: [
|
|
1096
|
-
'email must be an email',
|
|
1097
|
-
'password is not strong enough',
|
|
1098
|
-
],
|
|
1099
|
-
});
|
|
1100
|
-
expect(e2e_setup_1.handleSocketResponse).not.toHaveBeenCalled();
|
|
1101
|
-
});
|
|
1102
|
-
it('should create a new user and emit access token if the validation was successful', async () => {
|
|
1103
|
-
await e2e_setup_1.server.emit('auth-register', { email: 'unit@test.co', password: 'Test-2' });
|
|
1104
|
-
expect(e2e_setup_1.handleSocketException).not.toHaveBeenCalled();
|
|
1105
|
-
expect(e2e_setup_1.handleSocketResponse).toHaveBeenCalledTimes(1);
|
|
1106
|
-
expect(e2e_setup_1.handleSocketResponse).toHaveBeenCalledWith({
|
|
1107
|
-
accessToken: expect.any(String),
|
|
1108
|
-
});
|
|
1109
|
-
});
|
|
1110
|
-
});
|
|
1111
|
-
describe('EVENT auth-login', () => {
|
|
1112
|
-
beforeEach(async () => {
|
|
1113
|
-
await e2e_setup_1.server.emit('auth-register', { email: 'unit@test.co', password: 'Test-2' });
|
|
1114
|
-
e2e_setup_1.handleSocketResponse.mockReset();
|
|
1115
|
-
});
|
|
1116
|
-
it('should throw a ws exception if payload contains non whitelisted property', async () => {
|
|
1117
|
-
await e2e_setup_1.server.emit('auth-login', { email: 'unit@test.co', password: 'Test-2', role: 'ADMIN' });
|
|
1118
|
-
expect(e2e_setup_1.handleSocketException).toHaveBeenCalledTimes(1);
|
|
1119
|
-
expect(e2e_setup_1.handleSocketException).toHaveBeenCalledWith({
|
|
1120
|
-
message: ['property role should not exist'],
|
|
1121
|
-
});
|
|
1122
|
-
expect(e2e_setup_1.handleSocketResponse).not.toHaveBeenCalled();
|
|
1123
|
-
});
|
|
1124
|
-
it('should throw a ws exception if email is missing', async () => {
|
|
1125
|
-
await e2e_setup_1.server.emit('auth-login', { password: 'Test-2' });
|
|
1126
|
-
expect(e2e_setup_1.handleSocketException).toHaveBeenCalledTimes(1);
|
|
1127
|
-
expect(e2e_setup_1.handleSocketException).toHaveBeenCalledWith({
|
|
1128
|
-
message: ['email must be an email'],
|
|
1129
|
-
});
|
|
1130
|
-
expect(e2e_setup_1.handleSocketResponse).not.toHaveBeenCalled();
|
|
1131
|
-
});
|
|
1132
|
-
it('should throw a ws exception if password is missing', async () => {
|
|
1133
|
-
await e2e_setup_1.server.emit('auth-login', { email: 'unit@test.co' });
|
|
1134
|
-
expect(e2e_setup_1.handleSocketException).toHaveBeenCalledTimes(1);
|
|
1135
|
-
expect(e2e_setup_1.handleSocketException).toHaveBeenCalledWith({
|
|
1136
|
-
message: ['password is not strong enough'],
|
|
1137
|
-
});
|
|
1138
|
-
expect(e2e_setup_1.handleSocketResponse).not.toHaveBeenCalled();
|
|
1139
|
-
});
|
|
1140
|
-
it('should emit access token if the validation was successful', async () => {
|
|
1141
|
-
await e2e_setup_1.server.emit('auth-login', { email: 'unit@test.co', password: 'Test-2' });
|
|
1142
|
-
expect(e2e_setup_1.handleSocketException).not.toHaveBeenCalled();
|
|
1143
|
-
expect(e2e_setup_1.handleSocketResponse).toHaveBeenCalledTimes(1);
|
|
1144
|
-
expect(e2e_setup_1.handleSocketResponse).toHaveBeenCalledWith({
|
|
1145
|
-
accessToken: expect.any(String),
|
|
1146
|
-
});
|
|
1147
|
-
});
|
|
1148
|
-
});
|
|
1149
|
-
});
|
|
1150
|
-
describe('EVENT auth-register with register options', () => {
|
|
1151
|
-
let User = class User extends src_1.BaseEntity {
|
|
1152
|
-
constructor() {
|
|
1153
|
-
super(...arguments);
|
|
1154
|
-
this.role = 'user';
|
|
1155
|
-
}
|
|
1156
|
-
};
|
|
1157
|
-
__decorate([
|
|
1158
|
-
(0, mongoose_1.Prop)({ type: String, required: true }),
|
|
1159
|
-
__metadata("design:type", String)
|
|
1160
|
-
], User.prototype, "email", void 0);
|
|
1161
|
-
__decorate([
|
|
1162
|
-
(0, mongoose_1.Prop)({ type: String, required: true }),
|
|
1163
|
-
__metadata("design:type", String)
|
|
1164
|
-
], User.prototype, "password", void 0);
|
|
1165
|
-
__decorate([
|
|
1166
|
-
(0, mongoose_1.Prop)({ type: String, default: 'user' }),
|
|
1167
|
-
__metadata("design:type", String)
|
|
1168
|
-
], User.prototype, "role", void 0);
|
|
1169
|
-
__decorate([
|
|
1170
|
-
(0, mongoose_1.Prop)({ type: Boolean, default: false }),
|
|
1171
|
-
__metadata("design:type", Boolean)
|
|
1172
|
-
], User.prototype, "isVerified", void 0);
|
|
1173
|
-
User = __decorate([
|
|
1174
|
-
(0, mongoose_1.Schema)({ collection: 'users' })
|
|
1175
|
-
], User);
|
|
1176
|
-
const admin = { email: 'admin@test.co', password: 'admin', role: 'admin', isVerified: true };
|
|
1177
|
-
const user = { email: 'user@test.co', password: 'user' };
|
|
1178
|
-
beforeEach(async () => {
|
|
1179
|
-
const bcryptService = new src_1.BcryptService();
|
|
1180
|
-
const fixtures = async (_) => {
|
|
1181
|
-
const model = await (0, utils_1.getModelFromEntity)(User);
|
|
1182
|
-
await model.insertMany([
|
|
1183
|
-
{ ...admin, password: await bcryptService.hashPassword(admin.password) },
|
|
1184
|
-
{ ...user, password: await bcryptService.hashPassword(user.password) },
|
|
1185
|
-
]);
|
|
1186
|
-
};
|
|
1187
|
-
await initModule({
|
|
1188
|
-
useAuth: {
|
|
1189
|
-
userEntity: User,
|
|
1190
|
-
register: {
|
|
1191
|
-
protected: true,
|
|
1192
|
-
abilityPredicate: (user) => user.isVerified,
|
|
1193
|
-
additionalFields: ['role'],
|
|
1194
|
-
callback: async (user, { updateOneDocument }) => {
|
|
1195
|
-
if (user.role !== 'admin') {
|
|
1196
|
-
return;
|
|
1197
|
-
}
|
|
1198
|
-
await updateOneDocument(User, { _id: user.id }, { $set: { isVerified: true } });
|
|
1199
|
-
},
|
|
1200
|
-
},
|
|
1201
|
-
login: {
|
|
1202
|
-
additionalFields: ['role', 'isVerified'],
|
|
1203
|
-
},
|
|
1204
|
-
},
|
|
1205
|
-
webSocket: true,
|
|
1206
|
-
}, fixtures, async (_) => {
|
|
1207
|
-
_.useWebSocketAdapter(new socket_adapter_1.SocketAdapter(_));
|
|
1208
|
-
});
|
|
1209
|
-
});
|
|
1210
|
-
describe('protected', () => {
|
|
1211
|
-
it('should throw a ws exception if user is not logged in and protected is true', async () => {
|
|
1212
|
-
await e2e_setup_1.server.emit('auth-register', { email: 'unit@test.co', password: 'test' });
|
|
1213
|
-
expect(e2e_setup_1.handleSocketException).toHaveBeenCalledTimes(1);
|
|
1214
|
-
expect(e2e_setup_1.handleSocketException).toHaveBeenCalledWith({
|
|
1215
|
-
message: 'Unauthorized',
|
|
1216
|
-
});
|
|
1217
|
-
expect(e2e_setup_1.handleSocketResponse).not.toHaveBeenCalled();
|
|
1218
|
-
});
|
|
1219
|
-
});
|
|
1220
|
-
describe('abilityPredicate', () => {
|
|
1221
|
-
it('should not create a new user if user is not verified', async () => {
|
|
1222
|
-
const { email, password } = user;
|
|
1223
|
-
const { accessToken } = await e2e_setup_1.server.emit('auth-login', { email, password });
|
|
1224
|
-
e2e_setup_1.handleSocketResponse.mockReset();
|
|
1225
|
-
await e2e_setup_1.server.emit('auth-register', { email: 'unit@test.co', password: 'test' }, {
|
|
1226
|
-
accessToken,
|
|
1227
|
-
});
|
|
1228
|
-
expect(e2e_setup_1.handleSocketException).toHaveBeenCalledTimes(1);
|
|
1229
|
-
expect(e2e_setup_1.handleSocketException).toHaveBeenCalledWith({
|
|
1230
|
-
message: 'Access denied',
|
|
1231
|
-
});
|
|
1232
|
-
expect(e2e_setup_1.handleSocketResponse).not.toHaveBeenCalled();
|
|
1233
|
-
});
|
|
1234
|
-
it('should create a new user and return access token if user is verified', async () => {
|
|
1235
|
-
const { email, password } = admin;
|
|
1236
|
-
const { accessToken } = await e2e_setup_1.server.emit('auth-login', { email, password });
|
|
1237
|
-
e2e_setup_1.handleSocketResponse.mockReset();
|
|
1238
|
-
await e2e_setup_1.server.emit('auth-register', { email: 'unit@test.co', password: 'test' }, {
|
|
1239
|
-
accessToken,
|
|
1240
|
-
});
|
|
1241
|
-
expect(e2e_setup_1.handleSocketException).not.toHaveBeenCalled();
|
|
1242
|
-
expect(e2e_setup_1.handleSocketResponse).toHaveBeenCalledTimes(1);
|
|
1243
|
-
expect(e2e_setup_1.handleSocketResponse).toHaveBeenCalledWith({ accessToken: expect.any(String) });
|
|
1244
|
-
});
|
|
1245
|
-
});
|
|
1246
|
-
describe('additionalFields', () => {
|
|
1247
|
-
it('should allow to register a new user with additional fields', async () => {
|
|
1248
|
-
const { email, password } = admin;
|
|
1249
|
-
const { accessToken } = await e2e_setup_1.server.emit('auth-login', { email, password });
|
|
1250
|
-
e2e_setup_1.handleSocketResponse.mockReset();
|
|
1251
|
-
await e2e_setup_1.server.emit('auth-register', { email: 'client@test.co', password: 'client', role: 'client' }, {
|
|
1252
|
-
accessToken,
|
|
1253
|
-
});
|
|
1254
|
-
expect(e2e_setup_1.handleSocketException).not.toHaveBeenCalled();
|
|
1255
|
-
expect(e2e_setup_1.handleSocketResponse).toHaveBeenCalledTimes(1);
|
|
1256
|
-
expect(e2e_setup_1.handleSocketResponse).toHaveBeenCalledWith({ accessToken: expect.any(String) });
|
|
1257
|
-
});
|
|
1258
|
-
});
|
|
1259
|
-
describe('callback', () => {
|
|
1260
|
-
let adminAccessToken;
|
|
1261
|
-
beforeEach(async () => {
|
|
1262
|
-
const { email, password } = admin;
|
|
1263
|
-
const { accessToken } = await e2e_setup_1.server.emit('auth-login', { email, password });
|
|
1264
|
-
adminAccessToken = accessToken;
|
|
1265
|
-
e2e_setup_1.handleSocketResponse.mockReset();
|
|
1266
|
-
});
|
|
1267
|
-
it('should not set isVerified to true if role is not admin', async () => {
|
|
1268
|
-
const { accessToken: clientAccessToken } = await e2e_setup_1.server.emit('auth-register', { email: 'client@test.co', password: 'client', role: 'client' }, {
|
|
1269
|
-
accessToken: adminAccessToken,
|
|
1270
|
-
});
|
|
1271
|
-
e2e_setup_1.handleSocketResponse.mockReset();
|
|
1272
|
-
const body = await e2e_setup_1.server.emit('auth-get-account', undefined, { accessToken: clientAccessToken });
|
|
1273
|
-
expect(e2e_setup_1.handleSocketException).not.toHaveBeenCalled();
|
|
1274
|
-
expect(e2e_setup_1.handleSocketResponse).toHaveBeenCalledTimes(1);
|
|
1275
|
-
expect(body).toHaveProperty('isVerified', false);
|
|
1276
|
-
});
|
|
1277
|
-
it('should set isVerified to true if role is admin', async () => {
|
|
1278
|
-
const { accessToken: admin2AccessToken } = await e2e_setup_1.server.emit('auth-register', { email: 'admin2@test.co', password: 'admin2', role: 'admin' }, {
|
|
1279
|
-
accessToken: adminAccessToken,
|
|
1280
|
-
});
|
|
1281
|
-
e2e_setup_1.handleSocketResponse.mockReset();
|
|
1282
|
-
const body = await e2e_setup_1.server.emit('auth-get-account', undefined, { accessToken: admin2AccessToken });
|
|
1283
|
-
expect(e2e_setup_1.handleSocketException).not.toHaveBeenCalled();
|
|
1284
|
-
expect(e2e_setup_1.handleSocketResponse).toHaveBeenCalledTimes(1);
|
|
1285
|
-
expect(body).toHaveProperty('isVerified', true);
|
|
1286
|
-
});
|
|
1287
|
-
});
|
|
1288
|
-
});
|
|
1289
|
-
describe('EVENT auth-login with login options', () => {
|
|
1290
|
-
let User = class User extends src_1.BaseEntity {
|
|
1291
|
-
constructor() {
|
|
1292
|
-
super(...arguments);
|
|
1293
|
-
this.role = 'user';
|
|
1294
|
-
}
|
|
1295
|
-
};
|
|
1296
|
-
__decorate([
|
|
1297
|
-
(0, mongoose_1.Prop)({ type: String, required: true }),
|
|
1298
|
-
__metadata("design:type", String)
|
|
1299
|
-
], User.prototype, "username", void 0);
|
|
1300
|
-
__decorate([
|
|
1301
|
-
(0, mongoose_1.Prop)({ type: String, required: true }),
|
|
1302
|
-
__metadata("design:type", String)
|
|
1303
|
-
], User.prototype, "pass", void 0);
|
|
1304
|
-
__decorate([
|
|
1305
|
-
(0, mongoose_1.Prop)({ type: String, default: 'user' }),
|
|
1306
|
-
__metadata("design:type", String)
|
|
1307
|
-
], User.prototype, "role", void 0);
|
|
1308
|
-
__decorate([
|
|
1309
|
-
(0, mongoose_1.Prop)({ type: Boolean, default: false }),
|
|
1310
|
-
__metadata("design:type", Boolean)
|
|
1311
|
-
], User.prototype, "isVerified", void 0);
|
|
1312
|
-
User = __decorate([
|
|
1313
|
-
(0, mongoose_1.Schema)({ collection: 'users' })
|
|
1314
|
-
], User);
|
|
1315
|
-
const admin = { username: 'admin', pass: 'admin', role: 'admin', isVerified: true };
|
|
1316
|
-
const user = { username: 'user', pass: 'user' };
|
|
1317
|
-
const client = { username: 'client', pass: 'client', role: 'client', isVerified: true };
|
|
1318
|
-
beforeEach(async () => {
|
|
1319
|
-
const bcryptService = new src_1.BcryptService();
|
|
1320
|
-
const fixtures = async (_) => {
|
|
1321
|
-
const model = await (0, utils_1.getModelFromEntity)(User);
|
|
1322
|
-
await model.insertMany([
|
|
1323
|
-
{ ...admin, pass: await bcryptService.hashPassword(admin.pass) },
|
|
1324
|
-
{ ...user, pass: await bcryptService.hashPassword(user.pass) },
|
|
1325
|
-
{ ...client, pass: await bcryptService.hashPassword(client.pass) },
|
|
1326
|
-
]);
|
|
1327
|
-
};
|
|
1328
|
-
await initModule({
|
|
1329
|
-
useAuth: {
|
|
1330
|
-
userEntity: User,
|
|
1331
|
-
login: {
|
|
1332
|
-
loginField: 'username',
|
|
1333
|
-
passwordField: 'pass',
|
|
1334
|
-
additionalFields: ['role', 'isVerified'],
|
|
1335
|
-
abilityPredicate: (user) => user.role === 'admin' || user.role === 'user',
|
|
1336
|
-
callback: async (user) => {
|
|
1337
|
-
if (user.isVerified) {
|
|
1338
|
-
return;
|
|
1339
|
-
}
|
|
1340
|
-
throw new common_1.UnauthorizedException(`Hello ${user.username}, you must verify your account first!`);
|
|
1341
|
-
},
|
|
1342
|
-
},
|
|
1343
|
-
},
|
|
1344
|
-
webSocket: true,
|
|
1345
|
-
}, fixtures, async (_) => {
|
|
1346
|
-
_.useWebSocketAdapter(new socket_adapter_1.SocketAdapter(_));
|
|
1347
|
-
});
|
|
1348
|
-
});
|
|
1349
|
-
describe('loginField', () => {
|
|
1350
|
-
it('should throw a ws exception if loginField is missing', async () => {
|
|
1351
|
-
await e2e_setup_1.server.emit('auth-login', { pass: 'test' });
|
|
1352
|
-
expect(e2e_setup_1.handleSocketException).toHaveBeenCalledTimes(1);
|
|
1353
|
-
expect(e2e_setup_1.handleSocketException).toHaveBeenCalledWith({
|
|
1354
|
-
message: 'Unauthorized',
|
|
1355
|
-
});
|
|
1356
|
-
expect(e2e_setup_1.handleSocketResponse).not.toHaveBeenCalled();
|
|
1357
|
-
});
|
|
1358
|
-
});
|
|
1359
|
-
describe('passwordField', () => {
|
|
1360
|
-
it('should throw an unauthorized exception if passwordField is missing', async () => {
|
|
1361
|
-
await e2e_setup_1.server.emit('auth-login', { username: 'unit' });
|
|
1362
|
-
expect(e2e_setup_1.handleSocketException).toHaveBeenCalledTimes(1);
|
|
1363
|
-
expect(e2e_setup_1.handleSocketException).toHaveBeenCalledWith({
|
|
1364
|
-
message: 'Unauthorized',
|
|
1365
|
-
});
|
|
1366
|
-
expect(e2e_setup_1.handleSocketResponse).not.toHaveBeenCalled();
|
|
1367
|
-
});
|
|
1368
|
-
});
|
|
1369
|
-
describe('abilityPredicate', () => {
|
|
1370
|
-
it('should throw a ws exception if user role is not admin or user', async () => {
|
|
1371
|
-
const { username, pass } = client;
|
|
1372
|
-
await e2e_setup_1.server.emit('auth-login', { username, pass });
|
|
1373
|
-
expect(e2e_setup_1.handleSocketException).toHaveBeenCalledTimes(1);
|
|
1374
|
-
expect(e2e_setup_1.handleSocketException).toHaveBeenCalledWith({
|
|
1375
|
-
message: 'Access denied',
|
|
1376
|
-
});
|
|
1377
|
-
expect(e2e_setup_1.handleSocketResponse).not.toHaveBeenCalled();
|
|
1378
|
-
});
|
|
1379
|
-
});
|
|
1380
|
-
describe('callback', () => {
|
|
1381
|
-
it('should throw a ws exception if user is not verified', async () => {
|
|
1382
|
-
const { username, pass } = user;
|
|
1383
|
-
await e2e_setup_1.server.emit('auth-login', { username, pass });
|
|
1384
|
-
expect(e2e_setup_1.handleSocketException).toHaveBeenCalledTimes(1);
|
|
1385
|
-
expect(e2e_setup_1.handleSocketException).toHaveBeenCalledWith({
|
|
1386
|
-
message: 'Hello user, you must verify your account first!',
|
|
1387
|
-
});
|
|
1388
|
-
expect(e2e_setup_1.handleSocketResponse).not.toHaveBeenCalled();
|
|
1389
|
-
});
|
|
1390
|
-
});
|
|
1391
|
-
describe('additionalFields', () => {
|
|
1392
|
-
it('should return additional fields', async () => {
|
|
1393
|
-
const { username, pass } = admin;
|
|
1394
|
-
const { accessToken } = await e2e_setup_1.server.emit('auth-login', { username, pass });
|
|
1395
|
-
e2e_setup_1.handleSocketResponse.mockReset();
|
|
1396
|
-
await e2e_setup_1.server.emit('auth-get-account', undefined, { accessToken });
|
|
1397
|
-
expect(e2e_setup_1.handleSocketException).not.toHaveBeenCalled();
|
|
1398
|
-
expect(e2e_setup_1.handleSocketResponse).toHaveBeenCalledTimes(1);
|
|
1399
|
-
expect(e2e_setup_1.handleSocketResponse).toHaveBeenCalledWith({ id: expect.any(String), username: 'admin', role: 'admin', isVerified: true });
|
|
1400
|
-
});
|
|
1401
|
-
});
|
|
1402
|
-
});
|
|
1403
|
-
describe('useAuth with resetPassword options', () => {
|
|
1404
|
-
let User = class User extends src_1.BaseEntity {
|
|
1405
|
-
};
|
|
1406
|
-
__decorate([
|
|
1407
|
-
(0, mongoose_1.Prop)({ type: String, required: true }),
|
|
1408
|
-
__metadata("design:type", String)
|
|
1409
|
-
], User.prototype, "email", void 0);
|
|
1410
|
-
__decorate([
|
|
1411
|
-
(0, mongoose_1.Prop)({ type: String, required: true }),
|
|
1412
|
-
__metadata("design:type", String)
|
|
1413
|
-
], User.prototype, "password", void 0);
|
|
1414
|
-
__decorate([
|
|
1415
|
-
(0, mongoose_1.Prop)({ type: Boolean, default: false }),
|
|
1416
|
-
__metadata("design:type", Boolean)
|
|
1417
|
-
], User.prototype, "isVerified", void 0);
|
|
1418
|
-
__decorate([
|
|
1419
|
-
(0, mongoose_1.Prop)({ type: String }),
|
|
1420
|
-
__metadata("design:type", String)
|
|
1421
|
-
], User.prototype, "resetPasswordToken", void 0);
|
|
1422
|
-
User = __decorate([
|
|
1423
|
-
(0, mongoose_1.Schema)({ collection: 'users' })
|
|
1424
|
-
], User);
|
|
1425
|
-
let model;
|
|
1426
|
-
let user;
|
|
1427
|
-
let client;
|
|
1428
|
-
let app;
|
|
1429
|
-
beforeEach(async () => {
|
|
1430
|
-
user = { email: 'user@test.co', password: 'user', isVerified: true };
|
|
1431
|
-
client = { email: 'client@test.co', password: 'client' };
|
|
1432
|
-
const bcryptService = new src_1.BcryptService();
|
|
1433
|
-
const fixtures = async (_) => {
|
|
1434
|
-
model = await (0, utils_1.getModelFromEntity)(User);
|
|
1435
|
-
await model.insertMany([
|
|
1436
|
-
{ ...user, password: await bcryptService.hashPassword(user.password) },
|
|
1437
|
-
{ ...client, password: await bcryptService.hashPassword(client.password) },
|
|
1438
|
-
]);
|
|
1439
|
-
};
|
|
1440
|
-
app = await initModule({
|
|
1441
|
-
useAuth: {
|
|
1442
|
-
userEntity: User,
|
|
1443
|
-
resetPassword: {
|
|
1444
|
-
emailField: 'email',
|
|
1445
|
-
expirationInMinutes: 1,
|
|
1446
|
-
resetPasswordCallback: async ({ resetPasswordToken }, { updateUserByEmail }) => {
|
|
1447
|
-
await updateUserByEmail({ $set: { resetPasswordToken } });
|
|
1448
|
-
},
|
|
1449
|
-
changePasswordAbilityPredicate: (user) => user.isVerified && !!user.resetPasswordToken,
|
|
1450
|
-
changePasswordCallback: async (user, { updateOneDocument }) => {
|
|
1451
|
-
await updateOneDocument(User, { _id: user.id }, { $unset: { resetPasswordToken: 1 } });
|
|
1452
|
-
},
|
|
1453
|
-
},
|
|
1454
|
-
},
|
|
1455
|
-
webSocket: true,
|
|
1456
|
-
}, fixtures, async (_) => {
|
|
1457
|
-
_.useWebSocketAdapter(new socket_adapter_1.SocketAdapter(_));
|
|
1458
|
-
});
|
|
1459
|
-
});
|
|
1460
|
-
describe('EVENT auth-reset-password', () => {
|
|
1461
|
-
it('should throw a ws exception if email is missing if no validation options are provided', async () => {
|
|
1462
|
-
await e2e_setup_1.server.emit('auth-reset-password', {});
|
|
1463
|
-
expect(e2e_setup_1.handleSocketException).toHaveBeenCalledTimes(1);
|
|
1464
|
-
expect(e2e_setup_1.handleSocketException).toHaveBeenCalledWith({
|
|
1465
|
-
message: 'Invalid or missing argument',
|
|
1466
|
-
});
|
|
1467
|
-
expect(e2e_setup_1.handleSocketResponse).not.toHaveBeenCalled();
|
|
1468
|
-
});
|
|
1469
|
-
it('should not throw a ws exception if email is invalid if no validation options are provided', async () => {
|
|
1470
|
-
await e2e_setup_1.server.emit('auth-reset-password', { email: 'unit.test.co' });
|
|
1471
|
-
expect(e2e_setup_1.handleSocketException).not.toHaveBeenCalled();
|
|
1472
|
-
expect(e2e_setup_1.handleSocketResponse).toHaveBeenCalledTimes(1);
|
|
1473
|
-
});
|
|
1474
|
-
it('should not throw a ws exception if email is not found', async () => {
|
|
1475
|
-
await e2e_setup_1.server.emit('auth-reset-password', { email: 'invalid@test.co' });
|
|
1476
|
-
expect(e2e_setup_1.handleSocketException).not.toHaveBeenCalled();
|
|
1477
|
-
expect(e2e_setup_1.handleSocketResponse).toHaveBeenCalledTimes(1);
|
|
1478
|
-
});
|
|
1479
|
-
describe('resetPasswordCallback', () => {
|
|
1480
|
-
it('should set resetPasswordToken if email is valid', async () => {
|
|
1481
|
-
const { email } = user;
|
|
1482
|
-
const { resetPasswordToken: resetPasswordTokenBeforeUpdate } = (await model.findOne({ email }).lean().exec());
|
|
1483
|
-
await e2e_setup_1.server.emit('auth-reset-password', { email });
|
|
1484
|
-
const { resetPasswordToken: resetPasswordTokenAfterUpdate } = (await model.findOne({ email }).lean().exec());
|
|
1485
|
-
expect(e2e_setup_1.handleSocketException).not.toHaveBeenCalled();
|
|
1486
|
-
expect(e2e_setup_1.handleSocketResponse).toHaveBeenCalledTimes(1);
|
|
1487
|
-
expect(resetPasswordTokenBeforeUpdate).toStrictEqual(undefined);
|
|
1488
|
-
expect(resetPasswordTokenAfterUpdate).toStrictEqual(expect.any(String));
|
|
1489
|
-
});
|
|
1490
|
-
});
|
|
1491
|
-
});
|
|
1492
|
-
describe('EVENT auth-change-password', () => {
|
|
1493
|
-
it('should throw a ws exception if resetPasswordToken is missing', async () => {
|
|
1494
|
-
await e2e_setup_1.server.emit('auth-change-password', { newPassword: 'test' });
|
|
1495
|
-
expect(e2e_setup_1.handleSocketException).toHaveBeenCalledTimes(1);
|
|
1496
|
-
expect(e2e_setup_1.handleSocketException).toHaveBeenCalledWith({
|
|
1497
|
-
message: 'Invalid or missing argument',
|
|
1498
|
-
});
|
|
1499
|
-
expect(e2e_setup_1.handleSocketResponse).not.toHaveBeenCalled();
|
|
1500
|
-
});
|
|
1501
|
-
it('should throw a ws exception if newPassword is missing', async () => {
|
|
1502
|
-
await e2e_setup_1.server.emit('auth-change-password', { resetPasswordToken: 'resetPasswordToken' });
|
|
1503
|
-
expect(e2e_setup_1.handleSocketException).toHaveBeenCalledTimes(1);
|
|
1504
|
-
expect(e2e_setup_1.handleSocketException).toHaveBeenCalledWith({
|
|
1505
|
-
message: 'Invalid or missing argument',
|
|
1506
|
-
});
|
|
1507
|
-
expect(e2e_setup_1.handleSocketResponse).not.toHaveBeenCalled();
|
|
1508
|
-
});
|
|
1509
|
-
it('should throw a ws exception if resetPasswordToken is invalid', async () => {
|
|
1510
|
-
await e2e_setup_1.server.emit('auth-change-password', { resetPasswordToken: 'test', newPassword: 'newPassword' });
|
|
1511
|
-
expect(e2e_setup_1.handleSocketException).toHaveBeenCalledTimes(1);
|
|
1512
|
-
expect(e2e_setup_1.handleSocketException).toHaveBeenCalledWith({
|
|
1513
|
-
message: 'Invalid reset password token. Please redo the reset password process.',
|
|
1514
|
-
});
|
|
1515
|
-
expect(e2e_setup_1.handleSocketResponse).not.toHaveBeenCalled();
|
|
1516
|
-
});
|
|
1517
|
-
it('should throw a ws exception if resetPasswordToken is expired', async () => {
|
|
1518
|
-
const jwtService = app.get(jwt_1.JwtService);
|
|
1519
|
-
const expiredResetPasswordToken = jwtService.sign({ email: user.email }, { expiresIn: 1 });
|
|
1520
|
-
await (0, utils_1.wait)(500);
|
|
1521
|
-
await e2e_setup_1.server.emit('auth-change-password', { resetPasswordToken: expiredResetPasswordToken, newPassword: 'newPassword' });
|
|
1522
|
-
expect(e2e_setup_1.handleSocketException).toHaveBeenCalledTimes(1);
|
|
1523
|
-
expect(e2e_setup_1.handleSocketException).toHaveBeenCalledWith({
|
|
1524
|
-
message: 'Time to reset password has expired. Please redo the reset password process.',
|
|
1525
|
-
});
|
|
1526
|
-
expect(e2e_setup_1.handleSocketResponse).not.toHaveBeenCalled();
|
|
1527
|
-
});
|
|
1528
|
-
describe('changePasswordAbilityPredicate', () => {
|
|
1529
|
-
let resetPasswordToken;
|
|
1530
|
-
beforeEach(async () => {
|
|
1531
|
-
await e2e_setup_1.server.emit('auth-reset-password', { email: client.email });
|
|
1532
|
-
e2e_setup_1.handleSocketResponse.mockReset();
|
|
1533
|
-
const { resetPasswordToken: token } = (await model.findOne({ email: client.email }).lean().exec());
|
|
1534
|
-
resetPasswordToken = token;
|
|
1535
|
-
});
|
|
1536
|
-
it('should throw a ws exception if user is not allowed to change password', async () => {
|
|
1537
|
-
await e2e_setup_1.server.emit('auth-change-password', { resetPasswordToken, newPassword: 'newPassword' });
|
|
1538
|
-
expect(e2e_setup_1.handleSocketException).toHaveBeenCalledTimes(1);
|
|
1539
|
-
expect(e2e_setup_1.handleSocketException).toHaveBeenCalledWith({
|
|
1540
|
-
message: 'You are not allowed to change your password.',
|
|
1541
|
-
});
|
|
1542
|
-
expect(e2e_setup_1.handleSocketResponse).not.toHaveBeenCalled();
|
|
1543
|
-
});
|
|
1544
|
-
});
|
|
1545
|
-
describe('changePasswordCallback', () => {
|
|
1546
|
-
let resetPasswordToken;
|
|
1547
|
-
beforeEach(async () => {
|
|
1548
|
-
await e2e_setup_1.server.emit('auth-reset-password', { email: user.email });
|
|
1549
|
-
e2e_setup_1.handleSocketResponse.mockReset();
|
|
1550
|
-
const { resetPasswordToken: token } = (await model.findOne({ email: user.email }).lean().exec());
|
|
1551
|
-
resetPasswordToken = token;
|
|
1552
|
-
});
|
|
1553
|
-
it('should change password and unset resetPasswordToken if resetPasswordToken is valid', async () => {
|
|
1554
|
-
const newPassword = 'newPassword';
|
|
1555
|
-
const bcryptService = app.get(src_1.BcryptService);
|
|
1556
|
-
const { password: passwordBeforeUpdate } = (await model.findOne({ email: user.email }).lean().exec());
|
|
1557
|
-
await e2e_setup_1.server.emit('auth-change-password', { resetPasswordToken, newPassword });
|
|
1558
|
-
const { password: passwordAfterUpdate, resetPasswordToken: tokenAfterUpdate } = (await model.findOne({ email: user.email }).lean().exec());
|
|
1559
|
-
const isPreviousPassword = await bcryptService.comparePassword(user.password, passwordBeforeUpdate);
|
|
1560
|
-
expect(isPreviousPassword).toBe(true);
|
|
1561
|
-
const isNewPassword = await bcryptService.comparePassword(newPassword, passwordAfterUpdate);
|
|
1562
|
-
expect(isNewPassword).toBe(true);
|
|
1563
|
-
expect(tokenAfterUpdate).toStrictEqual(undefined);
|
|
1564
|
-
expect(e2e_setup_1.handleSocketException).not.toHaveBeenCalled();
|
|
1565
|
-
expect(e2e_setup_1.handleSocketResponse).toHaveBeenCalledTimes(1);
|
|
1566
|
-
});
|
|
1567
|
-
});
|
|
1568
|
-
});
|
|
1569
|
-
});
|
|
1570
|
-
});
|
|
1571
|
-
});
|
|
1572
|
-
});
|
|
1573
|
-
//# sourceMappingURL=dynamic-api-for-root.e2e-spec.js.map
|