add testing and documentation

This commit is contained in:
admin 2025-08-21 20:11:50 +02:00
parent 97f7a139f7
commit 49ffc099fe
6 changed files with 4703 additions and 5 deletions

1
.gitignore vendored
View File

@ -1,4 +1,5 @@
.vscode .vscode
.idea .idea
coverage
dist dist
node_modules node_modules

42
README.md Normal file
View File

@ -0,0 +1,42 @@
# Password Hasher
This Service is for NestJs and can create a Hash of a Password and verify it.
## Install
```bash
npm i --save @apihub24/password-hasher
```
## Dependencies
| Dependency | Description |
| -------------- | ------------------------------------------------------------------------------------------------- |
| @nestjs/config | use the NestJs Config Module to get the Password Salt Rounds with key _PASSWORD_HASH_SALT_ROUNDS_ |
## Usage
```typescript
// register the PasswordHashService as Provider in a NestJs Module
import { PasswordHashService } from '@apihub24/password-hasher';
@Module({
// import the ConfigModule! not forget to set the PASSWORD_HASH_SALT_ROUNDS in Config otherwise 10 was used
imports: [ConfigModule.forRoot()]
providers: [PasswordHashService],
})
...
// inject the PasswordHashService
export class SomeOtherService {
constructor(@Inject(PasswordHashService) passwordHashService: PasswordHashService) {}
async test(password: string) {
// generate the hash
const hash = await this.passwordHashService(password);
// compare a plaintext Password with Hash
await this.passwordHashService.verify(password, hash);
}
}
```

4565
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,11 +1,15 @@
{ {
"name": "@apihub24/password-hasher", "name": "@apihub24/password-hasher",
"version": "1.0.2", "version": "1.0.3",
"description": "", "description": "",
"main": "dist/index.js", "main": "dist/index.js",
"types": "./dist/index.d.ts", "types": "./dist/index.d.ts",
"scripts": { "scripts": {
"build": "tsc" "build": "tsc",
"test": "jest",
"test:coverage": "jest --coverage",
"pub": "npm publish",
"rel": "npm i && npm run build && npm run test:coverage"
}, },
"dependencies": { "dependencies": {
"@nestjs/common": "^11.1.6", "@nestjs/common": "^11.1.6",
@ -14,7 +18,11 @@
"bcrypt": "^6.0.0" "bcrypt": "^6.0.0"
}, },
"devDependencies": { "devDependencies": {
"typescript": "^5.9.2" "@nestjs/testing": "^11.1.6",
"typescript": "^5.9.2",
"@types/jest": "^30.0.0",
"jest": "^30.0.0",
"ts-jest": "^29.4.1"
}, },
"keywords": [], "keywords": [],
"author": { "author": {
@ -25,5 +33,26 @@
"license": "MIT", "license": "MIT",
"publishConfig": { "publishConfig": {
"access": "public" "access": "public"
},
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": ".",
"testRegex": ".*\\.spec\\.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"collectCoverageFrom": [
"**/*.(t|j)s"
],
"coverageDirectory": "./coverage",
"testEnvironment": "node",
"roots": [
"<rootDir>/src/"
],
"moduleNameMapper": {}
} }
} }

View File

@ -0,0 +1,47 @@
import { ConfigModule } from "@nestjs/config";
import { Test, TestingModule } from "@nestjs/testing";
import { PasswordHashService } from "./password.hash.service";
describe("Password Service Tests", () => {
let module: TestingModule | null = null;
beforeAll(async () => {
module = await Test.createTestingModule({
imports: [ConfigModule.forRoot()],
providers: [PasswordHashService],
}).compile();
});
it("should create a Password Hash", async () => {
expect(module).toBeDefined();
const service = module?.get(PasswordHashService);
expect(service).toBeDefined();
const password = "12345";
const hash = await service?.hash(password);
expect(hash).toBeDefined();
expect(hash?.length).toBeGreaterThan(3);
});
it("can verify the right Password", async () => {
expect(module).toBeDefined();
const service = module?.get(PasswordHashService);
expect(service).toBeDefined();
const password = "12345";
const hash = await service?.hash(password);
expect(hash).toBeDefined();
expect(await service?.verify(password, hash ?? "")).toBeTruthy();
});
it("can not verify the wrong Password", async () => {
expect(module).toBeDefined();
const service = module?.get(PasswordHashService);
expect(service).toBeDefined();
const password = "12345";
const hash = await service?.hash(password);
expect(hash).toBeDefined();
expect(await service?.verify("abc", hash ?? "")).toBeFalsy();
});
});

View File

@ -0,0 +1,18 @@
import { Test, TestingModule } from "@nestjs/testing";
import { PasswordHasherModule } from "./password.hasher.module";
describe("PasswordHasherModule Tests", () => {
let module: TestingModule | null = null;
beforeAll(async () => {
module = await Test.createTestingModule({
imports: [PasswordHasherModule.forRoot()],
}).compile();
});
it("should get PasswordHasherService by injection Key @apihub24/password_service", () => {
expect(module).toBeDefined();
const service = module?.get("@apihub24/password_service");
expect(service).toBeDefined();
});
});