Skip to content

Commit

Permalink
Merge pull request #55 from jaroslavhuss/jarda-core
Browse files Browse the repository at this point in the history
Audit log and unit tests updated
  • Loading branch information
jaroslavhuss authored Jun 8, 2022
2 parents f78267a + 7669591 commit f32cc97
Show file tree
Hide file tree
Showing 17 changed files with 326 additions and 11 deletions.
2 changes: 1 addition & 1 deletion server/.gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# compiled output
#/dist
/node_modules

.env
# Logs
logs
*.log
Expand Down
4 changes: 3 additions & 1 deletion server/dist/app.module.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export declare class AppModule {
import { MiddlewareConsumer, NestModule } from '@nestjs/common';
export declare class AppModule implements NestModule {
configure(consumer: MiddlewareConsumer): void;
}
8 changes: 8 additions & 0 deletions server/dist/app.module.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion server/dist/app.module.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions server/dist/schemas/audit.schema.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/// <reference types="mongoose/types/pipelinestage" />
/// <reference types="mongoose/types/error" />
/// <reference types="mongoose/types/connection" />
import { Document } from 'mongoose';
export declare type AuditDocument = Audit & Document;
export declare class Audit {
createdAt: Date;
method: string;
originalUrl: string;
statusCode: number;
contentLength: string;
userAgent: string;
ip: string;
params: string;
body: string;
user: string;
}
export declare const AuditSchema: import("mongoose").Schema<Document<Audit, any, any>, import("mongoose").Model<Document<Audit, any, any>, any, any, any>, any, any>;
61 changes: 61 additions & 0 deletions server/dist/schemas/audit.schema.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions server/dist/schemas/audit.schema.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion server/dist/tsconfig.build.tsbuildinfo

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions server/dist/util/audit.logger.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { NestMiddleware } from '@nestjs/common';
import { Request, Response, NextFunction } from 'express';
import { AuditDocument } from 'src/schemas/audit.schema';
import { Model } from 'mongoose';
import { JwtService } from '@nestjs/jwt';
export declare class AuditLogger implements NestMiddleware {
private auditModel;
private jwt;
constructor(auditModel: Model<AuditDocument>, jwt: JwtService);
private logger;
use(request: Request, response: Response, next: NextFunction): void;
}
57 changes: 57 additions & 0 deletions server/dist/util/audit.logger.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions server/dist/util/audit.logger.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 12 additions & 4 deletions server/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { Module } from '@nestjs/common';
import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
import { AuthModule } from './auth/auth.module';
import { UserModule } from './user/user.module';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { MongooseModule } from '@nestjs/mongoose';
import { GatewayModule } from './gateway/gateway.module';

import { AuditLogger } from './util/audit.logger';
import { AuditSchema, Audit } from './schemas/audit.schema';
import { JwtModule } from '@nestjs/jwt';
//.env does not load here - but this string is no security harm
const STAGE: 'development' | 'production' = 'production';

@Module({
imports: [
imports: [
JwtModule.register({}),
AuthModule,
UserModule,
MongooseModule.forRootAsync({
Expand All @@ -26,8 +29,13 @@ const STAGE: 'development' | 'production' = 'production';
}),
inject: [ConfigService],
}),
MongooseModule.forFeature([{name:Audit.name, schema:AuditSchema}]),
ConfigModule.forRoot({ isGlobal: true, envFilePath: '.env' }),
GatewayModule,
],
})
export class AppModule {}
export class AppModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
consumer.apply(AuditLogger).forRoutes("*")
}
}
13 changes: 12 additions & 1 deletion server/src/auth/auth.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { Test, TestingModule } from '@nestjs/testing';
import { AuthController } from './auth.controller';
import {AuthService} from "./auth.service";

describe('AuthController', () => {
let controller: AuthController;
const mockAuthService = {};
const mockAuthService = {
signin:jest.fn(()=>({
access_token:"lskjdf",
refresh_token:"klsdjf"
}))
};

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
Expand All @@ -17,4 +23,9 @@ describe('AuthController', () => {
it('should be defined', () => {
expect(controller).toBeDefined();
});

it("should do something", async ()=>{
expect(await controller.signin({email:"[email protected]", password:"some pwd"})).toStrictEqual({ access_token:"lskjdf",
refresh_token:"klsdjf"})
})
});
38 changes: 38 additions & 0 deletions server/src/schemas/audit.schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document } from 'mongoose';

export type AuditDocument = Audit & Document;
@Schema()
export class Audit {
@Prop({ default: new Date() })
createdAt: Date;

@Prop()
method: string;

@Prop()
originalUrl: string;

@Prop()
statusCode: number;

@Prop()
contentLength: string;

@Prop()
userAgent:string;

@Prop()
ip:string

@Prop()
params:string

@Prop()
body:string

@Prop()
user:string
}

export const AuditSchema = SchemaFactory.createForClass(Audit);
42 changes: 41 additions & 1 deletion server/src/user/user.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { Test, TestingModule } from '@nestjs/testing';
import { UserController } from './user.controller';
import { UserService } from './user.service';

describe('UserController', () => {
let controller: UserController;
const mock = {
getAllUsers:jest.fn(async ()=>([
{}
]))
])),

updateUser:jest.fn(async()=>({})),
deleteUser:jest.fn(async()=>({}))
}

beforeEach(async () => {
Expand All @@ -25,4 +29,40 @@ const mock = {
it("should return all users", async ()=>{
expect(await controller.getAllUsers()).toEqual([{}])
})
const date = new Date()
it("should return a specific user", async()=>{
expect(controller.whoAmI({
email:"[email protected]",
name:"Jaroslav",
surname:"Huss",
createdAt:date,
updatedAt:date,
authLevel:"iot-admin",
isUserApproved:true,
password:"somefailstring",
lastLoggedIn:date,
refresh_token:"fake_token"
})).toStrictEqual({ email:"[email protected]",
name:"Jaroslav",
surname:"Huss",
createdAt:date,
updatedAt:date,
authLevel:"iot-admin",
isUserApproved:true,
lastLoggedIn:date,
refresh_token:"fake_token"})
})

it("should delete a user", async ()=>{
expect(await controller.deleteUser("fakeid")).toStrictEqual({})
})

it("should", async ()=>{
expect(await controller.updateUser({ email:"[email protected]",
name:"Jaroslav",
surname:"Huss",
authLevel:"iot-admin",
isUserApproved:true,
}, "fakeid")).toStrictEqual({})
})
});
Loading

0 comments on commit f32cc97

Please sign in to comment.