Skip to content

Commit

Permalink
feat(audit): Updating audit to support auditing from auth object with…
Browse files Browse the repository at this point in the history
…out nationalId. (#6165)

* Updating audit to support auditing from auth object without nationalId.

* Removing comment. Sort imports.

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
saevarma and kodiakhq[bot] authored Jan 10, 2022
1 parent 9c010ae commit 1b7888c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 15 deletions.
8 changes: 4 additions & 4 deletions libs/nest/audit/src/lib/audit.interceptor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import { AuditService } from './audit.service'
import { Test, TestingModule } from '@nestjs/testing'
import { AuditInterceptor } from './audit.interceptor'
import { Audit } from './audit.decorator'
import { getCurrentUser } from '@island.is/auth-nest-tools'
import { getCurrentAuth } from '@island.is/auth-nest-tools'
import { of } from 'rxjs'
import MockInstance = jest.MockInstance

jest.mock('@island.is/auth-nest-tools', () => ({
getCurrentUser: jest.fn(),
getCurrentAuth: jest.fn(),
}))

const context: any = {
Expand Down Expand Up @@ -124,11 +124,11 @@ describe('AuditInterceptor', () => {
context.getClass.mockReturnValue(MyClass)
context.getHandler.mockReturnValue(MyClass.prototype.handler)
const user = 'Test user'
const getCurrentUserMock = (getCurrentUser as unknown) as MockInstance<
const getCurrentAuthMock = (getCurrentAuth as unknown) as MockInstance<
string,
unknown[]
>
getCurrentUserMock.mockReturnValue(user)
getCurrentAuthMock.mockReturnValue(user)

// Act
const observable = interceptor.intercept(context, next)
Expand Down
4 changes: 2 additions & 2 deletions libs/nest/audit/src/lib/audit.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { tap } from 'rxjs/operators'
import { AuditService, AuditTemplate } from './audit.service'
import { Reflector } from '@nestjs/core'
import { AUDIT_METADATA_KEY } from './audit.decorator'
import { getCurrentUser } from '@island.is/auth-nest-tools'
import { getCurrentAuth } from '@island.is/auth-nest-tools'

@Injectable()
export class AuditInterceptor implements NestInterceptor {
Expand All @@ -30,7 +30,7 @@ export class AuditInterceptor implements NestInterceptor {
return next.handle()
}

const user = getCurrentUser(context)
const user = getCurrentAuth(context)
const template = {
action: handler.name,
...decorators[0],
Expand Down
34 changes: 29 additions & 5 deletions libs/nest/audit/src/lib/audit.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { mock } from 'jest-mock-extended'

import { Test, TestingModule } from '@nestjs/testing'
import { mock } from 'jest-mock-extended'

import type { Logger } from '@island.is/logging'
import { LOGGER_PROVIDER } from '@island.is/logging'
import { AUDIT_OPTIONS } from '@island.is/nest/audit'
import type { Logger } from '@island.is/logging'
import type { Auth, User } from '@island.is/auth-nest-tools'

import { AuditService } from './audit.service'
import { AUDIT_OPTIONS, AuditOptions } from '@island.is/nest/audit'
import type { User } from '@island.is/auth-nest-tools'
import SpyInstance = jest.SpyInstance

jest.mock('@island.is/logging', () => {
Expand Down Expand Up @@ -142,6 +141,31 @@ describe('AuditService against Cloudwatch', () => {
}).toThrowError('Audit namespace is required')
})

it('supports auditing for auth without nationalId', () => {
// Arrange
const action = 'viewDetails'

const auth: Auth = {
scope: [],
authorization: '',
client: 'machine-client',
ip: '10.10.10.10',
}

// Act
service.audit({
user: auth,
action,
})

// Assert
expect(spy).toHaveBeenCalledWith({
client: [auth.client],
action: `${defaultNamespace}#${action}`,
ip: auth.ip,
})
})

it('supports auditing promises with audit templates', async () => {
// Arrange
const action = 'viewDetails'
Expand Down
8 changes: 4 additions & 4 deletions libs/nest/audit/src/lib/audit.service.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import winston from 'winston'
import WinstonCloudWatch from 'winston-cloudwatch'
import { format, TransformableInfo } from 'logform'
import { TransformableInfo } from 'logform'
import { createHash } from 'crypto'
import { Inject, Injectable } from '@nestjs/common'
import type { Logger } from '@island.is/logging'
import { LOGGER_PROVIDER } from '@island.is/logging'
import type { User } from '@island.is/auth-nest-tools'
import type { Auth } from '@island.is/auth-nest-tools'
import type { AuditOptions } from './audit.options'
import { AUDIT_OPTIONS } from './audit.options'

export interface AuditMessage {
user: User
user: Auth
action: string
namespace?: string
resources?: string | string[]
meta?: Record<string, unknown>
}

export type AuditTemplate<ResultType> = {
user: User
user: Auth
action: string
namespace?: string
resources?: string | string[] | ((result: ResultType) => string | string[])
Expand Down

0 comments on commit 1b7888c

Please sign in to comment.