@@ -2,39 +2,37 @@ import { mockDeep } from 'jest-mock-extended';
22import { Client } from 'utils' ;
33import { PutClientCommandParameters } from '../../app/put-client' ;
44import { AppDependencies , createApp } from '../../app' ;
5+ import { ConflictException , ValidationException } from 'domain/exceptions/' ;
6+
7+ const input : PutClientCommandParameters = {
8+ clientId : 'test_client_id' ,
9+ clientName : 'test_client_name' ,
10+ meshMailboxSenderId : 'test_client_mesh_mailbox_id' ,
11+ meshMailboxReportsId : 'test_client_mesh_workflow_id_suffix' ,
12+ fallbackWaitTimeSeconds : 300 ,
13+ routingConfigId : '1234' ,
14+ } ;
515
6- function setup ( clientSpec : 'min' | 'max' ) {
7- const minimumClient : Client = {
8- clientId : 'test_client_id' ,
9- name : 'test_client_name' ,
10- meshMailboxId : 'test_client_mesh_mailbox_id' ,
11- meshWorkflowIdSuffix : 'test_client_mesh_workflow_id_suffix' ,
12- allowOdsOverride : true ,
13- } ;
14-
15- const maximumClient : Client = {
16+ const client : Client = {
1617 clientId : 'test_client_id' ,
17- name : 'test_client_name' ,
18- meshMailboxId : 'test_client_mesh_mailbox_id' ,
19- meshWorkflowIdSuffix : 'test_client_mesh_workflow_id_suffix' ,
20- meshWorkflowIdReceiveRequestAck :
21- 'test_client_mesh_workflow_id_receive_request_ack' ,
22- meshWorkflowIdCompletedRequestItemsReport :
23- 'test_client_mesh_workflow_id_completed_items_report' ,
24- senderOdsCode : 'A12345' ,
25- allowAlternativeContactDetails : true ,
26- unprefixedName : true ,
27- allowAnonymousPatient : true ,
28- ignoreSecurityFlag : true ,
29- allowRfrOverride : true ,
18+ clientName : 'test_client_name' ,
19+ meshMailboxSenderId : 'test_client_mesh_mailbox_id' ,
20+ meshMailboxReportsId : 'test_client_mesh_workflow_id_suffix' ,
21+ fallbackWaitTimeSeconds : 300 ,
22+ routingConfigId : '1234' ,
3023 } ;
3124
32- const client = clientSpec === 'min' ? minimumClient : maximumClient ;
33-
25+ function setup ( existingClients : Client [ ] = [ ] , createClientResponse = client ) {
3426 const mocks = mockDeep < AppDependencies > ( {
3527 domain : {
3628 client : {
37- createClient : jest . fn ( ( ) => client ) ,
29+ createClient : jest . fn ( ( ) => createClientResponse ) ,
30+ } ,
31+ } ,
32+ infra : {
33+ clientRepository : {
34+ putClient : jest . fn ( ) ,
35+ listClients : jest . fn ( ) . mockResolvedValue ( existingClients ) ,
3836 } ,
3937 } ,
4038 } ) ;
@@ -43,18 +41,11 @@ function setup(clientSpec: 'min' | 'max') {
4341}
4442
4543describe ( 'putClient' , ( ) => {
46- it ( 'creates a new client with minimum data , stores it in the client repository and returns it' , async ( ) => {
47- const { data, mocks } = setup ( 'min' ) ;
44+ it ( 'creates a new client when not existing clients , stores it in the client repository and returns it' , async ( ) => {
45+ const { data, mocks } = setup ( [ ] ) ;
4846
4947 const app = createApp ( mocks ) ;
50-
51- const input : PutClientCommandParameters = {
52- clientId : 'input_client_id' ,
53- name : 'input_client_name' ,
54- meshMailboxId : 'input_client_mesh_mailbox_id' ,
55- senderOdsCode : 'A12345' ,
56- } ;
57-
48+ delete ( input . clientId ) ; // simulate no clientId provided
5849 const result = await app . putClient ( input ) ;
5950
6051 expect ( mocks . domain . client . createClient ) . toHaveBeenCalledWith ( input ) ;
@@ -64,24 +55,16 @@ describe('putClient', () => {
6455 expect ( result ) . toBe ( data . client ) ;
6556 } ) ;
6657
67- it ( 'creates a new client with maximum data, stores it in the client repository and returns it' , async ( ) => {
68- const { data, mocks } = setup ( 'max' ) ;
58+ it ( 'creates a new client when existing clients, stores it in the client repository and returns it' , async ( ) => {
59+ const existingClient : Client = {
60+ ...client ,
61+ clientId : "existing_client_id" ,
62+ meshMailboxSenderId : "existing_mesh_mailbox_sender_id" ,
63+ } ;
64+ const { data, mocks } = setup ( [ existingClient ] ) ;
6965
7066 const app = createApp ( mocks ) ;
7167
72- const input : PutClientCommandParameters = {
73- clientId : 'input_client_id' ,
74- name : 'input_client_name' ,
75- meshMailboxId : 'input_client_mesh_mailbox_id' ,
76- senderOdsCode : 'A12345' ,
77- allowOdsOverride : true ,
78- allowAlternativeContactDetails : true ,
79- unprefixedName : true ,
80- allowAnonymousPatient : true ,
81- ignoreSecurityFlag : true ,
82- allowRfrOverride : true ,
83- } ;
84-
8568 const result = await app . putClient ( input ) ;
8669
8770 expect ( mocks . domain . client . createClient ) . toHaveBeenCalledWith ( input ) ;
@@ -91,24 +74,15 @@ describe('putClient', () => {
9174 expect ( result ) . toBe ( data . client ) ;
9275 } ) ;
9376
94- it ( 'Accepts a client that has a 3 character valid ODS code' , async ( ) => {
95- const { data, mocks } = setup ( 'max' ) ;
77+ it ( 'Updates client when it exists, stores it in the client repository and returns it' , async ( ) => {
78+ const existingClient : Client = {
79+ ...client ,
80+ meshMailboxSenderId : "existing_mesh_mailbox_sender_id" ,
81+ } ;
82+ const { data, mocks } = setup ( [ existingClient ] ) ;
9683
9784 const app = createApp ( mocks ) ;
9885
99- const input : PutClientCommandParameters = {
100- clientId : 'input_client_id' ,
101- name : 'input_client_name' ,
102- meshMailboxId : 'input_client_mesh_mailbox_id' ,
103- senderOdsCode : 'AB1' ,
104- allowOdsOverride : true ,
105- allowAlternativeContactDetails : true ,
106- unprefixedName : true ,
107- allowAnonymousPatient : true ,
108- ignoreSecurityFlag : true ,
109- allowRfrOverride : true ,
110- } ;
111-
11286 const result = await app . putClient ( input ) ;
11387
11488 expect ( mocks . domain . client . createClient ) . toHaveBeenCalledWith ( input ) ;
@@ -117,4 +91,35 @@ describe('putClient', () => {
11791 ) ;
11892 expect ( result ) . toBe ( data . client ) ;
11993 } ) ;
94+
95+ it ( 'throws an error when a different existing client has the same mailbox sender ID' , async ( ) => {
96+ const existingClient : Client = {
97+ ...client ,
98+ clientId : "existing_client_id" ,
99+ } ;
100+ const { mocks } = setup ( [ existingClient ] ) ;
101+
102+ const app = createApp ( mocks ) ;
103+
104+ await expect ( app . putClient ( input ) ) . rejects . toThrow ( ConflictException ) ;
105+
106+ expect ( mocks . domain . client . createClient ) . toHaveBeenCalledWith ( input ) ;
107+ expect ( mocks . infra . clientRepository . putClient ) . toHaveBeenCalledTimes ( 0 ) ;
108+ } ) ;
109+
110+ it ( 'throws an error when the incoming client is not valid' , async ( ) => {
111+ const newInvalidClient : Client = {
112+ ...client ,
113+ } ;
114+ delete ( newInvalidClient . meshMailboxSenderId ) ;
115+
116+ const { mocks } = setup ( [ ] , newInvalidClient ) ;
117+
118+ const app = createApp ( mocks ) ;
119+
120+ await expect ( app . putClient ( newInvalidClient ) ) . rejects . toThrow ( ValidationException ) ;
121+
122+ expect ( mocks . domain . client . createClient ) . toHaveBeenCalledWith ( newInvalidClient ) ;
123+ expect ( mocks . infra . clientRepository . putClient ) . toHaveBeenCalledTimes ( 0 ) ;
124+ } ) ;
120125} ) ;
0 commit comments