0.7.0 • Published 1 year ago

@nestjs-boilerplate/auth-testing v0.7.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

NestJS Boilerplate

NestJS Boilerplate is an open-source high-level framework for NestJS applications. It takes it up a notch by making it possible to solve repeatable tasks with less coding. It provides an architectural model based on Domain Driven Design and contains a variety of extra tools for a quick start developing NestJS applications. The NestJS Boilerplate aims to follow the principle of pluggable modules to extend functionality and less repeating common code from project to project. Inspired by Spring Framework, AspNet Boilerplate and Django.

Install

First

Install core package, testing package, user package, and auth package

Then

$ npm install @nestjs-boilerplate/auth-testing --save

Package description

Auth testing is a package that provides additional authentication tools for testing NestJS Boilerplate applications.

User factory

UserFactory class allows creating stub of the User entity.

import { UserFactory } from '@nestjs-boilerplate/auth-testing';

UserFactory.makeUser()
    .then(user => console.log(user));

Auth test utils

AuthTestUtils class contains a bunch of helpful methods for managing users, JWT tokens. TODO

MethodDescription
saveUserSaves provided user to the database
makeAndSaveUserMakes a stub of the user entity and saves it to the database. Returns the user instance
clearAllUsersClear all users from the database
generateJwtTokenGenerates JWT token
revokeJwtTokenAdds JWT token to a list of the blocked tokens
getJwtAuthHeaderReturns authorization header value from JWT token
generateResetPasswordTokenGenerates reset password token for provided user
import { TestingModuleBuilder } from '@nestjs/testing';
import { TestBootstrap } from '@nestjs-boilerplate/testing';
import { User } from '@nestjs-boilerplate/user';
import { UserFactory, AuthTestUtils } from '@nestjs-boilerplate/auth-testing';
import { AppModule } from './src/app.module';

describe('TestClass', () => {
    let app;
    let authTestUtils: AuthTestUtils;
    let user: User;

    beforeAll(async () => {
        app = await new TestBootstrap(AppModule)
            .startApplication();
        authTestUtils = new AuthTestUtils(app);
        user = await authTestUtils.makeAndSaveUser();
    });

    afterAll(async () => {
        await app.close();
    });

    afterEach(async () => {
        await authTestUtils.clearAllUsers();
    });
    
    // Your test cases...
});

Revoked tokens service

RevokedTokensService class extends BaseRevokedTokensService class and provides a mock implementation of the in-memory store of blocked tokens.

import { TestingModule, TestingModuleBuilder } from '@nestjs/testing';
import { TestBootstrap } from '@nestjs-boilerplate/testing';
import { BaseRevokedTokensService, RevokedTokensService } from '@nestjs-boilerplate/auth-testing';
import { AppModule } from './src/app.module';

describe('TestClass', () => {
    let revokedTokesService: BaseRevokedTokensService;
    let app;

    beforeAll(async () => {
        app = await new TestBootstrap(AppModule)
            .startApplication({
                onCreateTestingModule(builder: TestingModuleBuilder) {
                    return builder.overrideProvider(BaseRevokedTokensService)
                        .useValue(new RevokedTokensService());
                },
                onTestingModuleCreated: (testingModule: TestingModule) => {
                    revokedTokesService = app.get<BaseRevokedTokensService, RevokedTokensService>(
                        BaseRevokedTokensService,
                    );
                },
            });
    });

    afterAll(async () => {
        await app.close();
    });
    
    // Your test cases...
});

Keep in touch

Dmitry Kologrivko - dmitrykologrivko@gmail.com

License

MIT LICENSE

0.3.0

1 year ago

0.5.0

1 year ago

0.4.0

1 year ago

0.3.1

1 year ago

0.7.0

1 year ago

0.6.0

1 year ago

0.2.0

3 years ago