Skip to content

Commit

Permalink
Fr error via gundb (#51)
Browse files Browse the repository at this point in the history
* add: improve fr edge cases messaging to gun

* fix: wrong boolean value for isNotDuplicate
  • Loading branch information
sirpy committed Aug 8, 2019
2 parents d722278 + 524b287 commit 992a1a3
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 2,123 deletions.
6 changes: 3 additions & 3 deletions jest.config.js
Expand Up @@ -54,7 +54,7 @@ module.exports = {
// forceCoverageMatch: [],

// A path to a module which exports an async function that is triggered once before all test suites
// globalSetup: './test/env.js',
// globalSetup: './jest/setup.js',

// A path to a module which exports an async function that is triggered once after all test suites
// globalTeardown: null,
Expand Down Expand Up @@ -153,7 +153,7 @@ module.exports = {
'/webpack.dev.config.js',
'/src/server/server-test.js',
'/src/server/server.config.js'
]
],

// The regexp pattern Jest uses to detect test files
// testRegex: "",
Expand Down Expand Up @@ -182,7 +182,7 @@ module.exports = {
// unmockedModulePathPatterns: undefined,

// Indicates whether each individual test should be reported during the run
// verbose: null,
verbose: true

// An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode
// watchPathIgnorePatterns: [],
Expand Down
51 changes: 25 additions & 26 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -43,9 +43,9 @@
"dependencies": {
"@babel/polyfill": "^7.4.4",
"@gooddollar/goodcontracts": "0.0.16",
"@sendgrid/mail": "^6.4.0",
"apidoc": "^0.17.7",
"apidoc-markdown": "^0.2.1",
"@sendgrid/mail": "^6.4.0",
"await-mutex": "^1.0.2",
"aws-sdk": "^2.466.0",
"bip39-light": "^1.0.7",
Expand All @@ -58,7 +58,7 @@
"express": "^4.17.1",
"express-pino-logger": "^4.0.0",
"flow": "^0.2.3",
"gun": "https://github.com/amark/gun.git#debug",
"gun": "^0.2019.612",
"hdkey": "^1.1.1",
"jsonwebtoken": "^8.5.1",
"lodash": "^4.17.11",
Expand Down
2,055 changes: 0 additions & 2,055 deletions publicdb-!-p2w.tmp

This file was deleted.

35 changes: 16 additions & 19 deletions src/server/verification/__tests__/verification.js
@@ -1,8 +1,21 @@
import Helper from '../faceRecognition/faceRecognitionHelper'
import verification from '../verification'
import { GunDBPublic, GunDB } from '../../gun/gun-middleware'

jest.mock('../faceRecognition/faceRecognitionHelper') // mock Helper
jest.genMockFromModule('../../gun/gun-middleware.js')
// jest.mock('../../gun/gun-middleware.js', () => ({
// __esModule: true, // this property makes it work
// default: 'mockedDefaultExport',
// GunDB: jest.fn()
// }))

describe('verification', () => {
let user
let verificationData

beforeAll(done => {
beforeAll(async done => {
await GunDBPublic.init()
verificationData = {
sessionId: 'fake-session-id',
enrollmentIdentifier: '0x9d5499D5099DE6Fe5A8f39874617dDFc967cA6e5',
Expand All @@ -23,55 +36,43 @@ describe('verification', () => {
done()
})

beforeEach(() => {
jest.mock('../faceRecognition/faceRecognitionHelper') // mock Helper
})
beforeEach(() => {})

afterEach(() => {
jest.resetModules()
jest.clearAllMocks()
})

test('Helper mocked succesfully', () => {
const Helper = require('../faceRecognition/faceRecognitionHelper').default
// console.log('helper', { Helper })
expect(Helper.prepareLivenessData.mock).toBeTruthy()
expect(Helper.prepareSearchData.mock).toBeTruthy()
expect(Helper.isDuplicatesExist.mock).toBeTruthy()
})

test('it doesnt throw error', async () => {
const verification = require('../verification').default
expect(() => {
verification.verifyUser(user, verificationData).not.toThrow()
})
})

test('it calls prepareSearchData', async () => {
const verification = require('../verification').default
const Helper = require('../faceRecognition/faceRecognitionHelper').default
verification.verifyUser(user, verificationData)
expect(Helper.prepareSearchData).toBeCalledTimes(1)
})

test('it calls isDuplicatesExist', async () => {
const verification = require('../verification').default
const Helper = require('../faceRecognition/faceRecognitionHelper').default
verification.verifyUser(user, verificationData)
expect(Helper.isDuplicatesExist).toBeCalledTimes(1)
})

test('it returns { ok: 1, livenessPassed: true, isDuplicatesExist: true} if Helper.isDuplicatesExist=true', async () => {
const verification = require('../verification').default
const Helper = require('../faceRecognition/faceRecognitionHelper').default
Helper.isLivenessPassed.mockResolvedValue(true)
Helper.isDuplicatesExist.mockResolvedValue(true)
const res = await verification.verifyUser(user, verificationData)
expect(res).toMatchObject({ ok: 1, isDuplicate: true })
})

test('it calls prepareEnrollmentData & enroll if liveness=true and isDuplicate=false', async () => {
const verification = require('../verification').default
const Helper = require('../faceRecognition/faceRecognitionHelper').default
Helper.isLivenessPassed.mockResolvedValue(true)
Helper.isDuplicatesExist.mockResolvedValue(false)
Helper.enroll.mockResolvedValue({ alreadyEnrolled: true })
Expand All @@ -80,8 +81,6 @@ describe('verification', () => {
})

test('it returns isVerified = true if liveness=true and isDuplicate=false and user was already enrolled', async () => {
const verification = require('../verification').default
const Helper = require('../faceRecognition/faceRecognitionHelper').default
Helper.isLivenessPassed.mockResolvedValue(true)
Helper.isDuplicatesExist.mockResolvedValue(false)
Helper.enroll.mockResolvedValue({ alreadyEnrolled: true })
Expand All @@ -90,8 +89,6 @@ describe('verification', () => {
})

test('it returns isVerified = true if liveness=true and isDuplicate=false and user was enrolled successfully', async () => {
const verification = require('../verification').default
const Helper = require('../faceRecognition/faceRecognitionHelper').default
Helper.isLivenessPassed.mockResolvedValue(true)
Helper.isDuplicatesExist.mockResolvedValue(false)
console.log('enrollmentIdentifier', verificationData.enrollmentIdentifier)
Expand Down
4 changes: 0 additions & 4 deletions src/server/verification/verification.js
Expand Up @@ -24,10 +24,6 @@ class Verifications implements VerificationAPI {
async verifyUser(user: UserRecord, verificationData: any) {
this.log.debug('Verifying user:', { user })
const sessionId = verificationData.sessionId
GunDBPublic.gun
.get(sessionId)
.get('isStarted')
.put(true)
this.log.debug('sessionId:', { sessionId })
const searchData = Helper.prepareSearchData(verificationData)
// log.info('searchData', { searchData })
Expand Down
33 changes: 19 additions & 14 deletions src/server/verification/verificationAPI.js
Expand Up @@ -39,7 +39,10 @@ const setup = (app: Router, verifier: VerificationAPI, storage: StorageAPI) => {
const { body, files, user } = req
log.debug({ user })
const sessionId = body.sessionId
GunDBPublic.gun.get(sessionId).put({}) // publish initialized data to subscribers
GunDBPublic.gun
.get(sessionId)
.get('isStarted')
.put(true) // publish initialized data to subscribers
log.debug('written FR status to gun', await GunDBPublic.gun.get(sessionId))

const verificationData = {
Expand All @@ -54,7 +57,8 @@ const setup = (app: Router, verifier: VerificationAPI, storage: StorageAPI) => {
.verifyUser(user, verificationData)
.catch(e => {
log.error('Facerecognition error:', e)
return { ok: 0, error: e.message }
GunDBPublic.gun.get(sessionId).put({ isNotDuplicate: false, isLive: false, isError: e.message })
return { ok: 1, error: e.message, isVerified: false }
})
.finally(() => {
//cleanup
Expand All @@ -63,6 +67,7 @@ const setup = (app: Router, verifier: VerificationAPI, storage: StorageAPI) => {
fsPromises.unlink(verificationData.auditTrailImageFile)
})
else {
GunDBPublic.gun.get(sessionId).put({ isNotDuplicate: true, isLive: true, isEnrolled: true }) // publish to subscribers
// mocked result for verified user or development mode
result = {
ok: 1,
Expand All @@ -78,18 +83,18 @@ const setup = (app: Router, verifier: VerificationAPI, storage: StorageAPI) => {
storage
.updateUser({ identifier: user.loggedInAs, isVerified: true })
.then(updatedUser => log.debug('updatedUser:', updatedUser))
]).catch(e => {
log.error('Whitelisting failed', e)
GunDBPublic.gun
.get(sessionId)
.get('isWhitelisted')
.put(false) // publish to subscribers
throw e
})
GunDBPublic.gun
.get(sessionId)
.get('isWhitelisted')
.put(true) // publish to subscribers
])
.then(
_ =>
GunDBPublic.gun
.get(sessionId)
.get('isWhitelisted')
.put(true) // publish to subscribers
)
.catch(e => {
log.error('Whitelisting failed', e)
GunDBPublic.gun.get(sessionId).put({ isWhitelisted: false, isError: e.message })
})
}
res.json(result)
})
Expand Down

0 comments on commit 992a1a3

Please sign in to comment.