-
-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathSign.spec.ts
More file actions
1074 lines (908 loc) · 26.4 KB
/
Sign.spec.ts
File metadata and controls
1074 lines (908 loc) · 26.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* SPDX-FileCopyrightText: 2026 LibreCode coop and contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import { beforeEach, describe, expect, it, vi } from 'vitest'
import type { MockedFunction } from 'vitest'
import { setActivePinia, createPinia } from 'pinia'
import { mount } from '@vue/test-utils'
import { useSignMethodsStore } from '../../../store/signMethods.js'
import type { useSignStore } from '../../../store/sign.js'
type TokenMethodKey = 'smsToken' | 'whatsappToken' | 'signalToken' | 'telegramToken' | 'xmppToken'
type SignMethodSettings = {
needCode?: boolean
identifyMethod?: string
label?: string
hashOfIdentifier?: string
blurredEmail?: string
hasConfirmCode?: boolean
token?: string
hasSignatureFile?: boolean
}
type SignMethodsSettings = Partial<Record<TokenMethodKey, SignMethodSettings>> & {
emailToken?: SignMethodSettings
password?: SignMethodSettings
}
type SignMethodsStore = ReturnType<typeof useSignMethodsStore> & {
settings: SignMethodsSettings
}
type SignStore = ReturnType<typeof useSignStore>
type SubmitSignaturePayload = {
method: string
token: string
}
type SignComponent = {
data: () => {
signMethodsStore: SignMethodsStore
signStore: SignStore
loading: boolean
}
methods: {
signWithTokenCode: (token: string) => Promise<void>
submitSignature: (payload: SubmitSignaturePayload) => Promise<unknown>
}
}
type SignComponentWithConfirm = SignComponent & {
methods: SignComponent['methods'] & {
confirmSignDocument: () => boolean
}
}
type ActionHandler = {
showModal: (modalCode: string) => void
closeModal: (modalCode: string) => void
}
type ProceedWithSigningLogic = (store: SignMethodsStore, actionHandler: ActionHandler) => void
// Global mock for axios - prevents unhandled rejections during component mounting
vi.mock('@nextcloud/axios', () => {
const axiosInstanceMock = Object.assign(vi.fn().mockResolvedValue({
data: {
ocs: {
data: {
elements: [],
},
},
},
}), {
get: vi.fn().mockResolvedValue({
data: {
ocs: {
data: {},
},
},
}),
post: vi.fn().mockResolvedValue({
data: {
ocs: {
data: {},
},
},
}),
patch: vi.fn().mockResolvedValue({
data: {
ocs: {
data: {},
},
},
}),
delete: vi.fn().mockResolvedValue({
data: {
ocs: {
data: {},
},
},
}),
})
return {
default: axiosInstanceMock,
}
})
// Global mocks for other Nextcloud modules
vi.mock('@nextcloud/router', () => ({
generateOcsUrl: vi.fn((path) => `/ocs/v2.php/apps/libresign${path}`),
}))
vi.mock('@nextcloud/auth', () => ({
getCurrentUser: vi.fn(() => null),
}))
vi.mock('@nextcloud/initial-state', () => ({
loadState: vi.fn((app, key, defaultValue) => defaultValue),
}))
vi.mock('@nextcloud/capabilities', () => ({
getCapabilities: vi.fn(() => ({
libresign: {
config: {
'sign-elements': {
'can-create-signature': true,
},
},
},
})),
}))
vi.mock('vue-select', () => ({
default: {
name: 'VSelect',
props: ['modelValue'],
emits: ['update:modelValue'],
render: () => null,
},
}))
describe('Sign.vue - signWithTokenCode', () => {
let Sign: SignComponent
let signMethodsStore: SignMethodsStore
let signStore: SignStore
let submitSignatureSpy: MockedFunction<(payload: SubmitSignaturePayload) => Promise<unknown>>
beforeEach(async () => {
setActivePinia(createPinia())
// Import stores
const { useSignMethodsStore } = await import('../../../store/signMethods.js')
const { useSignStore } = await import('../../../store/sign.js')
signMethodsStore = useSignMethodsStore() as SignMethodsStore
signStore = useSignStore()
// Create a mock Sign component with the method we want to test
Sign = {
data() {
return {
signMethodsStore,
signStore,
loading: false,
}
},
methods: {
async signWithTokenCode(
this: {
signMethodsStore: SignMethodsStore
submitSignature: (payload: SubmitSignaturePayload) => Promise<unknown>
},
token: string,
) {
const tokenMethods: TokenMethodKey[] = ['smsToken', 'whatsappToken', 'signalToken', 'telegramToken', 'xmppToken']
const activeMethod = tokenMethods.find(method =>
Object.hasOwn(this.signMethodsStore.settings, method)
)
if (!activeMethod) {
throw new Error('No active token method found')
}
await this.submitSignature({
method: activeMethod,
token,
})
},
async submitSignature(payload: SubmitSignaturePayload) {
// Spy on this method
return submitSignatureSpy(payload)
},
},
}
submitSignatureSpy = vi.fn<(payload: SubmitSignaturePayload) => Promise<unknown>>()
.mockResolvedValue({ status: 'signed' })
})
describe('signWithTokenCode', () => {
it('detects SMS token method', async () => {
signMethodsStore.settings = {
smsToken: { needCode: true },
}
const instance = {
...Sign.data(),
...Sign.methods,
}
await instance.signWithTokenCode('123456')
expect(submitSignatureSpy).toHaveBeenCalledWith({
method: 'smsToken',
token: '123456',
})
})
it('detects WhatsApp token method', async () => {
signMethodsStore.settings = {
whatsappToken: { needCode: true },
}
const instance = {
...Sign.data(),
...Sign.methods,
}
await instance.signWithTokenCode('789012')
expect(submitSignatureSpy).toHaveBeenCalledWith({
method: 'whatsappToken',
token: '789012',
})
})
it('successfully processes WhatsApp token signing', async () => {
signMethodsStore.settings = {
whatsappToken: { needCode: true },
}
const instance = {
...Sign.data(),
...Sign.methods,
}
await instance.signWithTokenCode('654321')
expect(submitSignatureSpy).toHaveBeenCalledWith({
method: 'whatsappToken',
token: '654321',
})
})
it('detects Signal token method', async () => {
signMethodsStore.settings = {
signalToken: { needCode: true },
}
const instance = {
...Sign.data(),
...Sign.methods,
}
await instance.signWithTokenCode('456789')
expect(submitSignatureSpy).toHaveBeenCalledWith({
method: 'signalToken',
token: '456789',
})
})
it('detects Telegram token method', async () => {
signMethodsStore.settings = {
telegramToken: { needCode: true },
}
const instance = {
...Sign.data(),
...Sign.methods,
}
await instance.signWithTokenCode('012345')
expect(submitSignatureSpy).toHaveBeenCalledWith({
method: 'telegramToken',
token: '012345',
})
})
it('detects XMPP token method', async () => {
signMethodsStore.settings = {
xmppToken: { needCode: true },
}
const instance = {
...Sign.data(),
...Sign.methods,
}
await instance.signWithTokenCode('678901')
expect(submitSignatureSpy).toHaveBeenCalledWith({
method: 'xmppToken',
token: '678901',
})
})
it('prefers first token method when multiple are present', async () => {
signMethodsStore.settings = {
smsToken: { needCode: true },
whatsappToken: { needCode: true },
signalToken: { needCode: true },
}
const instance = {
...Sign.data(),
...Sign.methods,
}
await instance.signWithTokenCode('111111')
expect(submitSignatureSpy).toHaveBeenCalledWith({
method: 'smsToken',
token: '111111',
})
})
it('throws error when no token method is found', async () => {
signMethodsStore.settings = {
clickToSign: {},
password: {},
}
const instance = {
...Sign.data(),
...Sign.methods,
}
await expect(instance.signWithTokenCode('123456')).rejects.toThrow('No active token method found')
expect(submitSignatureSpy).not.toHaveBeenCalled()
})
it('throws error when settings is empty', async () => {
signMethodsStore.settings = {}
const instance = {
...Sign.data(),
...Sign.methods,
}
await expect(instance.signWithTokenCode('123456')).rejects.toThrow('No active token method found')
expect(submitSignatureSpy).not.toHaveBeenCalled()
})
it('passes token correctly to submitSignature', async () => {
signMethodsStore.settings = {
smsToken: { needCode: true },
}
const instance = {
...Sign.data(),
...Sign.methods,
}
const testToken = 'abc123xyz'
await instance.signWithTokenCode(testToken)
expect(submitSignatureSpy).toHaveBeenCalledWith({
method: 'smsToken',
token: testToken,
})
})
it('ignores non-token methods in settings', async () => {
signMethodsStore.settings = {
clickToSign: {},
emailToken: { needCode: true },
password: { hasSignatureFile: true },
smsToken: { needCode: true },
}
const instance = {
...Sign.data(),
...Sign.methods,
}
await instance.signWithTokenCode('123456')
expect(submitSignatureSpy).toHaveBeenCalledWith({
method: 'smsToken',
token: '123456',
})
})
})
describe('proceedWithSigning - Full flow with WhatsApp token', () => {
let proceedWithSigningLogic: ProceedWithSigningLogic
beforeEach(() => {
setActivePinia(createPinia())
// Function that simulates the proceedWithSigning logic
proceedWithSigningLogic = (store: SignMethodsStore, actionHandler: ActionHandler) => {
if (store.needClickToSign()) {
actionHandler.showModal('clickToSign')
} else if (store.needSignWithPassword()) {
actionHandler.showModal('password')
} else if (store.needTokenCode()) {
actionHandler.showModal('token')
}
}
})
it('shows token modal when WhatsApp token is needed', () => {
const store = useSignMethodsStore()
store.settings = {
whatsappToken: { needCode: true },
}
const actionHandler: ActionHandler = {
showModal: vi.fn(),
closeModal: vi.fn(),
}
proceedWithSigningLogic(store, actionHandler)
expect(actionHandler.showModal).toHaveBeenCalledWith('token')
})
it('shows password modal when password is needed (priority over token)', () => {
const store = useSignMethodsStore()
store.settings = {
password: { hasSignatureFile: true },
whatsappToken: { needCode: true },
}
const actionHandler: ActionHandler = {
showModal: vi.fn(),
closeModal: vi.fn(),
}
proceedWithSigningLogic(store, actionHandler)
expect(actionHandler.showModal).toHaveBeenCalledWith('password')
})
it('shows clickToSign modal when clickToSign is needed (highest priority)', () => {
const store = useSignMethodsStore()
store.settings = {
clickToSign: {},
password: { hasSignatureFile: true },
whatsappToken: { needCode: true },
}
const actionHandler: ActionHandler = {
showModal: vi.fn(),
closeModal: vi.fn(),
}
proceedWithSigningLogic(store, actionHandler)
expect(actionHandler.showModal).toHaveBeenCalledWith('clickToSign')
})
it('does nothing when no signing method is configured', () => {
const store = useSignMethodsStore()
store.settings = {}
const actionHandler: ActionHandler = {
showModal: vi.fn(),
closeModal: vi.fn(),
}
proceedWithSigningLogic(store, actionHandler)
expect(actionHandler.showModal).not.toHaveBeenCalled()
})
})
describe('Full signing flow with WhatsApp token', () => {
let Sign: SignComponentWithConfirm
let signMethodsStore: SignMethodsStore
let signStore: SignStore
let submitSignatureSpy: MockedFunction<(payload: SubmitSignaturePayload) => Promise<unknown>>
beforeEach(async () => {
setActivePinia(createPinia())
// Import stores
const { useSignStore } = await import('../../../store/sign.js')
signMethodsStore = useSignMethodsStore() as SignMethodsStore
signStore = useSignStore()
Sign = {
data() {
return {
signMethodsStore,
signStore,
loading: false,
}
},
methods: {
async signWithTokenCode(
this: {
signMethodsStore: SignMethodsStore
submitSignature: (payload: SubmitSignaturePayload) => Promise<unknown>
},
token: string,
) {
const tokenMethods: TokenMethodKey[] = ['smsToken', 'whatsappToken', 'signalToken', 'telegramToken', 'xmppToken']
const activeMethod = tokenMethods.find(method =>
Object.hasOwn(this.signMethodsStore.settings, method)
)
if (!activeMethod) {
throw new Error('No active token method found')
}
await this.submitSignature({
method: activeMethod,
token,
})
},
async submitSignature(payload: SubmitSignaturePayload) {
// Spy on this method
return submitSignatureSpy(payload)
},
confirmSignDocument(this: { signMethodsStore: SignMethodsStore }) {
// Simulate the logic
if (this.signMethodsStore.needTokenCode()) {
this.signMethodsStore.showModal('token')
return true
}
return false
},
},
}
submitSignatureSpy = vi.fn<(payload: SubmitSignaturePayload) => Promise<unknown>>()
.mockResolvedValue({ status: 'signed', data: { id: 1 } })
})
it('complete flow: click sign button -> token modal opens -> submit token', async () => {
signMethodsStore.settings = {
whatsappToken: { needCode: true },
}
const instance = {
...Sign.data(),
...Sign.methods,
}
// User clicks "Sign the document" button
const result = instance.confirmSignDocument()
expect(result).toBe(true)
expect(signMethodsStore.modal.token).toBe(true)
// User enters token and submits
await instance.signWithTokenCode('123456')
// Verify the submission happened
expect(submitSignatureSpy).toHaveBeenCalledWith({
method: 'whatsappToken',
token: '123456',
})
})
it('complete flow: click sign with multiple token methods enables first available', async () => {
signMethodsStore.settings = {
smsToken: { needCode: true },
whatsappToken: { needCode: true },
telegramToken: { needCode: true },
}
const instance = {
...Sign.data(),
...Sign.methods,
}
// User clicks "Sign the document" button
const result = instance.confirmSignDocument()
expect(result).toBe(true)
// User enters token - should use first method (SMS)
await instance.signWithTokenCode('999999')
// Verify the submission happened with SMS token
expect(submitSignatureSpy).toHaveBeenCalledWith({
method: 'smsToken',
token: '999999',
})
})
})
describe('signWithTokenCode - Correct identify method from signature methods', () => {
let Sign: SignComponent
let signMethodsStore: SignMethodsStore
let signStore: SignStore
let submitSignatureSpy: MockedFunction<(payload: SubmitSignaturePayload) => Promise<unknown>>
beforeEach(async () => {
setActivePinia(createPinia())
const { useSignStore } = await import('../../../store/sign.js')
signMethodsStore = useSignMethodsStore() as SignMethodsStore
signStore = useSignStore()
Sign = {
data() {
return {
signMethodsStore,
signStore,
loading: false,
}
},
methods: {
// CORRECTED implementation - extracts identify method from signature method
async signWithTokenCode(
this: {
signMethodsStore: SignMethodsStore
submitSignature: (payload: SubmitSignaturePayload) => Promise<unknown>
},
token: string,
) {
const tokenMethods: TokenMethodKey[] = ['smsToken', 'whatsappToken', 'signalToken', 'telegramToken', 'xmppToken']
const activeMethod = tokenMethods.find(method =>
Object.hasOwn(this.signMethodsStore.settings, method)
)
if (!activeMethod) {
throw new Error('No active token method found')
}
// Extract the identify method from the signature method
const signatureMethodData = this.signMethodsStore.settings[activeMethod]
const identifyMethod = signatureMethodData?.identifyMethod
await this.submitSignature({
method: identifyMethod,
token,
})
},
async submitSignature(payload: SubmitSignaturePayload) {
return submitSignatureSpy(payload)
},
},
}
submitSignatureSpy = vi.fn<(payload: SubmitSignaturePayload) => Promise<unknown>>()
.mockResolvedValue({ status: 'signed' })
})
it('FAILS: sends signature method name instead of identify method (whatsappToken vs whatsapp)', async () => {
signMethodsStore.settings = {
whatsappToken: {
label: 'WhatsApp token',
identifyMethod: 'whatsapp',
needCode: true,
},
}
const instance = {
...Sign.data(),
...Sign.methods,
}
await instance.signWithTokenCode('123456')
// This assertion FAILS with current implementation
// because it sends 'whatsappToken' instead of 'whatsapp'
expect(submitSignatureSpy).toHaveBeenCalledWith({
method: 'whatsapp', // Should be the identify method
token: '123456',
})
})
it('FAILS: sends sms token name instead of identify method (smsToken vs sms)', async () => {
signMethodsStore.settings = {
smsToken: {
label: 'SMS token',
identifyMethod: 'sms',
needCode: true,
},
}
const instance = {
...Sign.data(),
...Sign.methods,
}
await instance.signWithTokenCode('789012')
// This assertion FAILS with current implementation
expect(submitSignatureSpy).toHaveBeenCalledWith({
method: 'sms', // Should be the identify method, not 'smsToken'
token: '789012',
})
})
it('FAILS: multiple token methods - uses wrong method name', async () => {
signMethodsStore.settings = {
smsToken: {
label: 'SMS token',
identifyMethod: 'sms',
needCode: true,
},
whatsappToken: {
label: 'WhatsApp token',
identifyMethod: 'whatsapp',
needCode: true,
},
}
const instance = {
...Sign.data(),
...Sign.methods,
}
await instance.signWithTokenCode('555555')
// Should use 'sms' not 'smsToken'
expect(submitSignatureSpy).toHaveBeenCalledWith({
method: 'sms',
token: '555555',
})
})
it('FAILS: signal token - sends wrong method name', async () => {
signMethodsStore.settings = {
signalToken: {
label: 'Signal token',
identifyMethod: 'signal',
needCode: true,
},
}
const instance = {
...Sign.data(),
...Sign.methods,
}
await instance.signWithTokenCode('444444')
// Should send 'signal' not 'signalToken'
expect(submitSignatureSpy).toHaveBeenCalledWith({
method: 'signal',
token: '444444',
})
})
it('FAILS: backend expects identify method not signature method', async () => {
signMethodsStore.settings = {
whatsappToken: {
label: 'WhatsApp token',
identifyMethod: 'whatsapp',
needCode: true,
hashOfIdentifier: 'd41d8cd98f00b204e9800998ecf8427e',
},
}
const instance = {
...Sign.data(),
...Sign.methods,
}
await instance.signWithTokenCode('333333')
// Backend endpoint expects 'whatsapp' (identify method)
// not 'whatsappToken' (signature method)
expect(submitSignatureSpy).not.toHaveBeenCalledWith({
method: 'whatsappToken',
token: '333333',
})
expect(submitSignatureSpy).toHaveBeenCalledWith({
method: 'whatsapp',
token: '333333',
})
})
})
describe('signWithTokenCode - REAL component integration test', () => {
it('INTEGRATION: extracts and sends correct identify method from signature methods data', async () => {
const testCases = [
{
name: 'WhatsApp token',
signatureMethodKey: 'whatsappToken',
expectedIdentifyMethod: 'whatsapp',
token: '123456',
},
{
name: 'SMS token',
signatureMethodKey: 'smsToken',
expectedIdentifyMethod: 'sms',
token: '789012',
},
{
name: 'Signal token',
signatureMethodKey: 'signalToken',
expectedIdentifyMethod: 'signal',
token: '456789',
},
]
for (const testCase of testCases) {
setActivePinia(createPinia())
// Import REAL Sign component
const SignComponent = await import('../../../views/SignPDF/_partials/Sign.vue')
const realSign = SignComponent.default
const signMethodsStore = useSignMethodsStore()
// Set up signature method with identify method info
signMethodsStore.settings = {
[testCase.signatureMethodKey]: {
label: testCase.name,
identifyMethod: testCase.expectedIdentifyMethod,
needCode: true,
},
}
const submitSignatureMock = vi.fn().mockResolvedValue({ status: 'signed' })
// Mount REAL component
const wrapper = mount(realSign, {
global: {
stubs: {
NcButton: true,
NcDialog: true,
NcLoadingIcon: true,
TokenManager: true,
EmailManager: true,
UploadCertificate: true,
Documents: true,
Signatures: true,
Draw: true,
ManagePassword: true,
CreatePassword: true,
NcNoteCard: true,
NcPasswordField: true,
NcRichText: true,
},
mocks: {
$watch: vi.fn(),
$nextTick: vi.fn(),
},
},
props: {},
})
wrapper.vm.submitSignature = submitSignatureMock
// Call real signWithTokenCode method
await wrapper.vm.signWithTokenCode(testCase.token)
// VERIFY: Must send identify method, NOT signature method name
expect(submitSignatureMock).toHaveBeenCalledWith({
method: testCase.expectedIdentifyMethod, // 'whatsapp', 'sms', 'signal'
modalCode: 'token',
token: testCase.token,
})
// Double-check: Should NOT send the signature method key name
expect(submitSignatureMock).not.toHaveBeenCalledWith({
method: testCase.signatureMethodKey, // NOT 'whatsappToken', 'smsToken', etc
modalCode: 'token',
token: testCase.token,
})
}
})
})
describe('Sign.vue - envelope visible elements', () => {
it('includes elements from child files when document has no signers', async () => {
setActivePinia(createPinia())
const SignComponent = await import('../../../views/SignPDF/_partials/Sign.vue')
const realSign = SignComponent.default
const { useSignStore } = await import('../../../store/sign.js')
const { useSignatureElementsStore } = await import('../../../store/signatureElements.js')
const signStore = useSignStore()
const signatureElementsStore = useSignatureElementsStore()
signStore.document = {
id: 1,
nodeType: 'envelope',
signers: [],
files: [
{
id: 10,
signers: [
{ signRequestId: 501, me: true },
],
visibleElements: [
{ elementId: 201, fileId: 10, signRequestId: 501, type: 'signature' },
],
},
],
}
signatureElementsStore.signs.signature = {
id: 1,
type: 'signature',
file: { url: '/sig.png', nodeId: 11623 },
starred: 0,
createdAt: '2024-01-01',
}
const wrapper = mount(realSign, {
global: {
stubs: {
NcButton: true,
NcDialog: true,
NcLoadingIcon: true,
TokenManager: true,
EmailManager: true,
UploadCertificate: true,
Documents: true,
Signatures: true,
Draw: true,
ManagePassword: true,
CreatePassword: true,
NcNoteCard: true,
NcPasswordField: true,
NcRichText: true,
},
mocks: {
$watch: vi.fn(),
$nextTick: vi.fn(),
},
},
})
expect(wrapper.vm.elements).toEqual([
{ elementId: 201, fileId: 10, signRequestId: 501, type: 'signature' },
])
})
it('updates elements when signature is created dynamically', async () => {
const { default: realSign } = await import('../../../views/SignPDF/_partials/Sign.vue')
const { useSignStore } = await import('../../../store/sign.js')
const { useSignatureElementsStore } = await import('../../../store/signatureElements.js')
const signStore = useSignStore()
const signatureElementsStore = useSignatureElementsStore()
signStore.document = {
id: 1,
nodeType: 'envelope',
signers: [
{ signRequestId: 501, me: true },
],
files: [],
visibleElements: [
{ elementId: 201, signRequestId: 501, type: 'signature' },
],
}
// Initially, no signature exists
signatureElementsStore.signs.signature = {
id: 0,
type: '',
file: { url: '', nodeId: 0 },
starred: 0,
createdAt: '', // Empty createdAt means no signature
}
const wrapper = mount(realSign, {
global: {
stubs: {
NcButton: true,
NcDialog: true,
NcLoadingIcon: true,
TokenManager: true,
EmailManager: true,
UploadCertificate: true,
Documents: true,
Signatures: true,
Draw: true,
ManagePassword: true,
CreatePassword: true,
NcNoteCard: true,
NcPasswordField: true,
NcRichText: true,
},
mocks: {
$watch: vi.fn(),
},
},
})