From 23e1b30e6535dca45410e4570c2f267c9343240b Mon Sep 17 00:00:00 2001 From: Rafael Taranto Date: Thu, 22 May 2025 10:16:15 +0100 Subject: [PATCH] tests: getAuthorizedStatus preparation for refactor --- .../admin-ui/src/pages/Customers/helper.jsx | 55 ++-- .../src/pages/Customers/helper.test.js | 296 ++++++++++++++++++ 2 files changed, 316 insertions(+), 35 deletions(-) create mode 100644 packages/admin-ui/src/pages/Customers/helper.test.js diff --git a/packages/admin-ui/src/pages/Customers/helper.jsx b/packages/admin-ui/src/pages/Customers/helper.jsx index 5a94df29..f30561d1 100644 --- a/packages/admin-ui/src/pages/Customers/helper.jsx +++ b/packages/admin-ui/src/pages/Customers/helper.jsx @@ -45,43 +45,28 @@ const getAuthorizedStatus = (it, triggers, customRequests) => { ) } - const pendingFieldStatus = R.map(ite => { - if (isManualField(ite)) { - if (uuidValidate(ite)) { - const request = R.find( - iter => iter.infoRequestId === ite, - it.customInfoRequests, - ) - return !R.isNil(request) && R.equals(request.override, 'automatic') + const getFieldsByStatus = status => + R.map(ite => { + if (isManualField(ite)) { + if (uuidValidate(ite)) { + const request = R.find( + iter => iter.infoRequestId === ite, + it.customInfoRequests, + ) + return !R.isNil(request) && R.equals(request.override, status) + } + + const regularFieldValue = R.includes(ite, fieldsWithPathSuffix) + ? it[`${ite}Path`] + : it[`${ite}`] + if (R.isNil(regularFieldValue)) return false + return R.equals(it[`${ite}Override`], status) } + return false + }, fields) - const regularFieldValue = R.includes(ite, fieldsWithPathSuffix) - ? it[`${ite}Path`] - : it[`${ite}`] - if (R.isNil(regularFieldValue)) return false - return R.equals(it[`${ite}Override`], 'automatic') - } - return false - }, fields) - - const rejectedFieldStatus = R.map(ite => { - if (isManualField(ite)) { - if (uuidValidate(ite)) { - const request = R.find( - iter => iter.infoRequestId === ite, - it.customInfoRequests, - ) - return !R.isNil(request) && R.equals(request.override, 'blocked') - } - - const regularFieldValue = R.includes(ite, fieldsWithPathSuffix) - ? it[`${ite}Path`] - : it[`${ite}`] - if (R.isNil(regularFieldValue)) return false - return R.equals(it[`${ite}Override`], 'blocked') - } - return false - }, fields) + const pendingFieldStatus = getFieldsByStatus('automatic') + const rejectedFieldStatus = getFieldsByStatus('blocked') if (it.authorizedOverride === CUSTOMER_BLOCKED) return { label: 'Blocked', type: 'error' } diff --git a/packages/admin-ui/src/pages/Customers/helper.test.js b/packages/admin-ui/src/pages/Customers/helper.test.js new file mode 100644 index 00000000..a3180eff --- /dev/null +++ b/packages/admin-ui/src/pages/Customers/helper.test.js @@ -0,0 +1,296 @@ +import { describe, it, expect } from 'vitest' +import { getAuthorizedStatus } from './helper' + +describe('getAuthorizedStatus', () => { + const mockTriggers = { + automation: 'automatic', + overrides: [], + } + + const mockCustomRequests = [{ id: 'custom-req-1' }, { id: 'custom-req-2' }] + + it('should return blocked status when authorizedOverride is blocked', () => { + const customer = { + authorizedOverride: 'blocked', + } + + const result = getAuthorizedStatus( + customer, + mockTriggers, + mockCustomRequests, + ) + + expect(result).toEqual({ + label: 'Blocked', + type: 'error', + }) + }) + + it('should return suspension status when customer is suspended with days > 0', () => { + const customer = { + authorizedOverride: null, + isSuspended: true, + daysSuspended: 5, + } + + const result = getAuthorizedStatus( + customer, + mockTriggers, + mockCustomRequests, + ) + + expect(result).toEqual({ + label: '5 day suspension', + type: 'warning', + }) + }) + + it('should return short suspension status when customer is suspended with days <= 0', () => { + const customer = { + authorizedOverride: null, + isSuspended: true, + daysSuspended: 0, + } + + const result = getAuthorizedStatus( + customer, + mockTriggers, + mockCustomRequests, + ) + + expect(result).toEqual({ + label: '< 1 day suspension', + type: 'warning', + }) + }) + + it('should return rejected status when any field has blocked override', () => { + const customer = { + authorizedOverride: null, + isSuspended: false, + emailOverride: 'blocked', + email: 'test@example.com', + } + + const triggers = { + automation: 'manual', + overrides: [], + } + + const result = getAuthorizedStatus(customer, triggers, mockCustomRequests) + + expect(result).toEqual({ + label: 'Rejected', + type: 'error', + }) + }) + + it('should return pending status when any field has automatic override', () => { + const customer = { + authorizedOverride: null, + isSuspended: false, + emailOverride: 'automatic', + email: 'test@example.com', + } + + const triggers = { + automation: 'manual', + overrides: [], + } + + const result = getAuthorizedStatus(customer, triggers, mockCustomRequests) + + expect(result).toEqual({ + label: 'Pending', + type: 'warning', + }) + }) + + it('should return authorized status when no blocking conditions exist', () => { + const customer = { + authorizedOverride: null, + isSuspended: false, + } + + const result = getAuthorizedStatus( + customer, + mockTriggers, + mockCustomRequests, + ) + + expect(result).toEqual({ + label: 'Authorized', + type: 'success', + }) + }) + + it('should handle customers with idCardData', () => { + const customer = { + authorizedOverride: null, + isSuspended: false, + idCardData: { firstName: 'John', lastName: 'Doe' }, + idCardDataOverride: 'automatic', + } + + const triggers = { + automation: 'manual', + overrides: [], + } + + const result = getAuthorizedStatus(customer, triggers, mockCustomRequests) + + expect(result).toEqual({ + label: 'Pending', + type: 'warning', + }) + }) + + it('should handle customers with photo fields using path suffix', () => { + const customer = { + authorizedOverride: null, + isSuspended: false, + frontCameraPath: '/path/to/photo.jpg', + frontCameraOverride: 'blocked', + } + + const triggers = { + automation: 'manual', + overrides: [], + } + + const result = getAuthorizedStatus(customer, triggers, mockCustomRequests) + + expect(result).toEqual({ + label: 'Rejected', + type: 'error', + }) + }) + + it('should handle custom info requests with UUID validation', () => { + const customer = { + authorizedOverride: null, + isSuspended: false, + customInfoRequests: [ + { + infoRequestId: '550e8400-e29b-41d4-a716-446655440000', + override: 'automatic', + }, + ], + } + + const triggers = { + automation: 'manual', + overrides: [], + } + + const customRequests = [{ id: '550e8400-e29b-41d4-a716-446655440000' }] + + const result = getAuthorizedStatus(customer, triggers, customRequests) + + expect(result).toEqual({ + label: 'Pending', + type: 'warning', + }) + }) + + it('should handle manual overrides for specific requirements', () => { + const customer = { + authorizedOverride: null, + isSuspended: false, + frontCameraPath: '/path/to/photo.jpg', + frontCameraOverride: 'automatic', + } + + const triggers = { + automation: 'automatic', + overrides: [ + { + requirement: 'facephoto', + automation: 'manual', + }, + ], + } + + const result = getAuthorizedStatus(customer, triggers, mockCustomRequests) + + expect(result).toEqual({ + label: 'Pending', + type: 'warning', + }) + }) + + it('should handle null or undefined triggers gracefully', () => { + const customer = { + authorizedOverride: null, + isSuspended: false, + } + + const triggers = { + automation: 'automatic', + overrides: [], + } + + const result = getAuthorizedStatus(customer, triggers, mockCustomRequests) + + expect(result).toEqual({ + label: 'Authorized', + type: 'success', + }) + }) + + it('should handle empty custom requests array', () => { + const customer = { + authorizedOverride: null, + isSuspended: false, + } + + const result = getAuthorizedStatus(customer, mockTriggers, []) + + expect(result).toEqual({ + label: 'Authorized', + type: 'success', + }) + }) + + it('should prioritize blocked status over suspension', () => { + const customer = { + authorizedOverride: 'blocked', + isSuspended: true, + daysSuspended: 5, + } + + const result = getAuthorizedStatus( + customer, + mockTriggers, + mockCustomRequests, + ) + + expect(result).toEqual({ + label: 'Blocked', + type: 'error', + }) + }) + + it('should prioritize rejection over pending status', () => { + const customer = { + authorizedOverride: null, + isSuspended: false, + emailOverride: 'blocked', + email: 'test@example.com', + usSsnOverride: 'automatic', + usSsn: '123-45-6789', + } + + const triggers = { + automation: 'manual', + overrides: [], + } + + const result = getAuthorizedStatus(customer, triggers, mockCustomRequests) + + expect(result).toEqual({ + label: 'Rejected', + type: 'error', + }) + }) +})