-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathprocessor_test.go
651 lines (590 loc) · 22.4 KB
/
processor_test.go
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
//go:build unit
package documents
import (
"context"
"testing"
"time"
coredocumentpb "github.com/centrifuge/centrifuge-protobufs/gen/go/coredocument"
p2ppb "github.com/centrifuge/centrifuge-protobufs/gen/go/p2p"
"github.com/centrifuge/go-centrifuge/anchors"
"github.com/centrifuge/go-centrifuge/config"
"github.com/centrifuge/go-centrifuge/contextutil"
"github.com/centrifuge/go-centrifuge/crypto"
"github.com/centrifuge/go-centrifuge/errors"
"github.com/centrifuge/go-centrifuge/identity"
testingcommons "github.com/centrifuge/go-centrifuge/testingutils/commons"
testingconfig "github.com/centrifuge/go-centrifuge/testingutils/config"
testingidentity "github.com/centrifuge/go-centrifuge/testingutils/identity"
"github.com/centrifuge/go-centrifuge/utils"
"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
type mockModel struct {
mock.Mock
Document
sigs []*coredocumentpb.Signature
}
func (m *mockModel) Scheme() string {
args := m.Called()
return args.String(0)
}
func (m *mockModel) SetStatus(st Status) error {
args := m.Called(st)
return args.Error(0)
}
func (m *mockModel) NFTs() []*coredocumentpb.NFT {
args := m.Called()
dr, _ := args.Get(0).([]*coredocumentpb.NFT)
return dr
}
func (m *mockModel) CalculateSigningRoot() ([]byte, error) {
args := m.Called()
sr, _ := args.Get(0).([]byte)
return sr, args.Error(1)
}
func (m *mockModel) CalculateDocumentRoot() ([]byte, error) {
args := m.Called()
dr, _ := args.Get(0).([]byte)
return dr, args.Error(1)
}
func (m *mockModel) CalculateSignaturesRoot() ([]byte, error) {
args := m.Called()
dr, _ := args.Get(0).([]byte)
return dr, args.Error(1)
}
func (m *mockModel) PreviousDocumentRoot() []byte {
args := m.Called()
dr, _ := args.Get(0).([]byte)
return dr
}
func (m *mockModel) AppendSignatures(sigs ...*coredocumentpb.Signature) {
m.Called(sigs)
m.sigs = sigs
}
func (m *mockModel) ID() []byte {
args := m.Called()
id, _ := args.Get(0).([]byte)
return id
}
func (m *mockModel) CurrentVersion() []byte {
args := m.Called()
id, _ := args.Get(0).([]byte)
return id
}
func (m *mockModel) CurrentVersionPreimage() []byte {
args := m.Called()
id, _ := args.Get(0).([]byte)
return id
}
func (m *mockModel) NextVersion() []byte {
args := m.Called()
id, _ := args.Get(0).([]byte)
return id
}
func (m *mockModel) PreviousVersion() []byte {
args := m.Called()
id, _ := args.Get(0).([]byte)
return id
}
func (m *mockModel) Signatures() []coredocumentpb.Signature {
m.Called()
var ss []coredocumentpb.Signature
for _, s := range m.sigs {
ss = append(ss, *s)
}
return ss
}
func (m *mockModel) AddUpdateLog(account identity.DID) error {
args := m.Called()
return args.Error(0)
}
func (m *mockModel) Author() (identity.DID, error) {
args := m.Called()
id, _ := args.Get(0).(identity.DID)
return id, args.Error(1)
}
func (m *mockModel) Timestamp() (time.Time, error) {
args := m.Called()
dr, _ := args.Get(0).(time.Time)
return dr, args.Error(1)
}
func (m *mockModel) GetCollaborators(filterIDs ...identity.DID) (CollaboratorsAccess, error) {
args := m.Called(filterIDs)
cas, _ := args.Get(0).(CollaboratorsAccess)
return cas, args.Error(1)
}
func (m *mockModel) GetSignerCollaborators(filterIDs ...identity.DID) ([]identity.DID, error) {
args := m.Called(filterIDs)
cids, _ := args.Get(0).([]identity.DID)
return cids, args.Error(1)
}
func (m *mockModel) PackCoreDocument() (coredocumentpb.CoreDocument, error) {
args := m.Called()
cd, _ := args.Get(0).(coredocumentpb.CoreDocument)
return cd, args.Error(1)
}
func (m *mockModel) CollaboratorCanUpdate(new Document, collaborator identity.DID) error {
args := m.Called(new, collaborator)
return args.Error(0)
}
func (m *mockModel) SetUsedAnchorRepoAddress(addr common.Address) {
m.Called(addr)
}
func (m *mockModel) AnchorRepoAddress() common.Address {
args := m.Called()
return args.Get(0).(common.Address)
}
func (m *mockModel) GetAttributes() []Attribute {
args := m.Called()
attrs, _ := args.Get(0).([]Attribute)
return attrs
}
func (m *mockModel) ExecuteComputeFields(timeout time.Duration) error {
args := m.Called(timeout)
return args.Error(0)
}
func (m *mockModel) GetComputeFieldsRules() []*coredocumentpb.TransitionRule {
args := m.Called()
rules, _ := args.Get(0).([]*coredocumentpb.TransitionRule)
return rules
}
func TestDefaultProcessor_PrepareForSignatureRequests(t *testing.T) {
srv := &testingcommons.MockIdentityService{}
dp := DefaultProcessor(srv, nil, nil, cfg).(defaultProcessor)
ctxh := testingconfig.CreateAccountContext(t, cfg)
// failed to get self
err := dp.PrepareForSignatureRequests(context.Background(), nil)
assert.Error(t, err)
assert.True(t, errors.IsOfType(contextutil.ErrSelfNotFound, err))
// failed compute field execution
model := new(mockModel)
model.On("AddUpdateLog").Return(nil)
model.On("SetUsedAnchorRepoAddress", cfg.GetContractAddress(config.AnchorRepo)).Return()
model.On("ExecuteComputeFields", computeFieldsTimeout).Return(errors.New("failed to execute compute fields")).Once()
err = dp.PrepareForSignatureRequests(ctxh, model)
model.AssertExpectations(t)
assert.Error(t, err)
// failed signing root
model.On("ExecuteComputeFields", computeFieldsTimeout).Return(nil)
model.On("CalculateSigningRoot").Return(nil, errors.New("failed signing root")).Once()
err = dp.PrepareForSignatureRequests(ctxh, model)
model.AssertExpectations(t)
assert.Error(t, err)
assert.Contains(t, err.Error(), "failed signing root")
// success
sr := utils.RandomSlice(32)
model.On("CalculateSigningRoot").Return(sr, nil)
model.On("AppendSignatures", mock.Anything).Return().Once()
err = dp.PrepareForSignatureRequests(ctxh, model)
model.AssertExpectations(t)
assert.Nil(t, err)
assert.NotNil(t, model.sigs)
assert.Len(t, model.sigs, 1)
sig := model.sigs[0]
self, err := contextutil.Account(ctxh)
assert.NoError(t, err)
keys, err := self.GetKeys()
assert.NoError(t, err)
assert.True(t, crypto.VerifyMessage(keys[identity.KeyPurposeSigning.Name].PublicKey, ConsensusSignaturePayload(sr, false), sig.Signature, crypto.CurveSecp256K1))
}
type p2pClient struct {
mock.Mock
Client
}
func (p *p2pClient) GetSignaturesForDocument(ctx context.Context, model Document) ([]*coredocumentpb.Signature, []error, error) {
args := p.Called(ctx, model)
sigs, _ := args.Get(0).([]*coredocumentpb.Signature)
return sigs, nil, args.Error(1)
}
func (p *p2pClient) SendAnchoredDocument(ctx context.Context, receiverID identity.DID, in *p2ppb.AnchorDocumentRequest) (*p2ppb.AnchorDocumentResponse, error) {
args := p.Called(ctx, receiverID, in)
resp, _ := args.Get(0).(*p2ppb.AnchorDocumentResponse)
return resp, args.Error(1)
}
func TestDefaultProcessor_RequestSignatures(t *testing.T) {
srv := &testingcommons.MockIdentityService{}
dp := DefaultProcessor(srv, nil, nil, cfg).(defaultProcessor)
ctxh := testingconfig.CreateAccountContext(t, cfg)
self, err := contextutil.Account(ctxh)
assert.NoError(t, err)
did := self.GetIdentityID()
sr := utils.RandomSlice(32)
sig, err := self.SignMsg(sr)
assert.NoError(t, err)
did1, err := identity.NewDIDFromBytes(did)
assert.NoError(t, err)
// data validations failed
model := new(mockModel)
model.On("ID").Return([]byte{})
model.On("CurrentVersion").Return([]byte{})
model.On("NextVersion").Return([]byte{})
model.On("CalculateSigningRoot").Return(nil, errors.New("error"))
model.On("Timestamp").Return(time.Now().UTC(), nil)
model.On("GetAttributes").Return(nil)
model.On("GetComputeFieldsRules").Return(nil)
err = dp.RequestSignatures(ctxh, model)
model.AssertExpectations(t)
assert.Error(t, err)
// key validation failed
model = new(mockModel)
id := utils.RandomSlice(32)
next := utils.RandomSlice(32)
model.On("ID").Return(id)
model.On("CurrentVersion").Return(id)
model.On("NextVersion").Return(next)
model.On("CalculateSigningRoot").Return(sr, nil)
model.On("Signatures").Return()
model.On("Author").Return(did1, nil)
model.On("Timestamp").Return(time.Now(), nil)
model.On("GetAttributes").Return(nil)
model.On("GetComputeFieldsRules").Return(nil)
model.On("GetSignerCollaborators", mock.Anything).Return([]identity.DID{did1, testingidentity.GenerateRandomDID()}, nil)
model.sigs = append(model.sigs, sig)
c := new(p2pClient)
srv.On("ValidateSignature", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(errors.New("cannot validate key")).Once()
err = dp.RequestSignatures(ctxh, model)
model.AssertExpectations(t)
c.AssertExpectations(t)
assert.Error(t, err)
assert.Contains(t, err.Error(), "cannot validate key")
// failed signature collection
model = new(mockModel)
model.On("ID").Return(id)
model.On("CurrentVersion").Return(id)
model.On("NextVersion").Return(next)
model.On("CalculateSigningRoot").Return(sr, nil)
model.On("Signatures").Return()
model.On("Author").Return(did1, nil)
model.On("Timestamp").Return(time.Now(), nil)
model.On("GetAttributes").Return(nil)
model.On("GetComputeFieldsRules").Return(nil)
model.On("GetSignerCollaborators", mock.Anything).Return([]identity.DID{did1, testingidentity.GenerateRandomDID()}, nil)
model.sigs = append(model.sigs, sig)
c = new(p2pClient)
srv.On("ValidateSignature", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil)
c.On("GetSignaturesForDocument", ctxh, model).Return(nil, errors.New("failed to get signatures")).Once()
dp.p2pClient = c
err = dp.RequestSignatures(ctxh, model)
model.AssertExpectations(t)
c.AssertExpectations(t)
assert.Error(t, err)
assert.Contains(t, err.Error(), "failed to get signatures")
// success
model = new(mockModel)
model.On("ID").Return(id)
model.On("CurrentVersion").Return(id)
model.On("NextVersion").Return(next)
model.On("CalculateSigningRoot").Return(sr, nil)
model.On("Signatures").Return()
model.On("AppendSignatures", []*coredocumentpb.Signature{sig}).Return().Once()
model.On("Author").Return(did1, nil)
model.On("GetSignerCollaborators", mock.Anything).Return([]identity.DID{did1, testingidentity.GenerateRandomDID()}, nil)
model.On("Timestamp").Return(time.Now(), nil)
model.On("GetAttributes").Return(nil)
model.On("GetComputeFieldsRules").Return(nil)
model.sigs = append(model.sigs, sig)
c = new(p2pClient)
c.On("GetSignaturesForDocument", ctxh, model).Return([]*coredocumentpb.Signature{sig}, nil).Once()
dp.p2pClient = c
err = dp.RequestSignatures(ctxh, model)
model.AssertExpectations(t)
c.AssertExpectations(t)
assert.Nil(t, err)
}
func TestDefaultProcessor_PrepareForAnchoring(t *testing.T) {
srv := &testingcommons.MockIdentityService{}
dp := DefaultProcessor(srv, nil, nil, cfg).(defaultProcessor)
ctxh := testingconfig.CreateAccountContext(t, cfg)
self, err := contextutil.Account(ctxh)
assert.NoError(t, err)
did := self.GetIdentityID()
sr := utils.RandomSlice(32)
payload := ConsensusSignaturePayload(sr, false)
sig, err := self.SignMsg(payload)
assert.NoError(t, err)
did1, err := identity.NewDIDFromBytes(did)
assert.NoError(t, err)
// validation failed
model := new(mockModel)
id := utils.RandomSlice(32)
next := utils.RandomSlice(32)
model.On("ID").Return(id)
model.On("CurrentVersion").Return(id)
model.On("NextVersion").Return(next)
model.On("CalculateSigningRoot").Return(sr, nil)
model.On("Signatures").Return()
model.On("Author").Return(did1, nil)
model.On("GetSignerCollaborators", mock.Anything).Return([]identity.DID{did1, testingidentity.GenerateRandomDID()}, nil)
tm := time.Now()
model.On("Timestamp").Return(tm, nil)
model.On("GetAttributes").Return(nil)
model.On("GetComputeFieldsRules").Return(nil)
model.sigs = append(model.sigs, sig)
srv = &testingcommons.MockIdentityService{}
cid, _ := identity.NewDIDFromBytes(did)
srv.On("ValidateSignature", cid, sig.PublicKey, sig.Signature, payload, tm).Return(errors.New("validation failed")).Once()
dp.identityService = srv
err = dp.PrepareForAnchoring(ctxh, model)
model.AssertExpectations(t)
srv.AssertExpectations(t)
assert.Error(t, err)
// success
model = new(mockModel)
model.On("ID").Return(id)
model.On("CurrentVersion").Return(id)
model.On("NextVersion").Return(next)
model.On("CalculateSigningRoot").Return(sr, nil)
model.On("Signatures").Return()
model.On("Author").Return(did1, nil)
model.On("GetSignerCollaborators", mock.Anything).Return([]identity.DID{did1, testingidentity.GenerateRandomDID()}, nil)
model.On("Timestamp").Return(tm, nil)
model.On("GetAttributes").Return(nil)
model.On("GetComputeFieldsRules").Return(nil)
model.sigs = append(model.sigs, sig)
srv = &testingcommons.MockIdentityService{}
srv.On("ValidateSignature", cid, sig.PublicKey, sig.Signature, payload, tm).Return(nil).Once()
dp.identityService = srv
err = dp.PrepareForAnchoring(ctxh, model)
model.AssertExpectations(t)
srv.AssertExpectations(t)
assert.NoError(t, err)
}
func TestDefaultProcessor_AnchorDocument(t *testing.T) {
srv := &testingcommons.MockIdentityService{}
dp := DefaultProcessor(srv, nil, nil, cfg).(defaultProcessor)
ctxh := testingconfig.CreateAccountContext(t, cfg)
self, err := contextutil.Account(ctxh)
assert.NoError(t, err)
did := self.GetIdentityID()
assert.NoError(t, err)
sr := utils.RandomSlice(32)
payload := ConsensusSignaturePayload(sr, false)
sig, err := self.SignMsg(payload)
assert.NoError(t, err)
did1, err := identity.NewDIDFromBytes(did)
assert.NoError(t, err)
// validations failed
id := utils.RandomSlice(32)
next := utils.RandomSlice(32)
model := new(mockModel)
model.On("ID").Return(id)
model.On("CurrentVersion").Return(id)
model.On("NextVersion").Return(next)
model.On("CalculateSigningRoot").Return(sr, nil)
model.On("Signatures").Return()
model.On("CalculateDocumentRoot").Return(nil, errors.New("error"))
model.On("Author").Return(did1, nil)
model.On("GetSignerCollaborators", mock.Anything).Return([]identity.DID{did1, testingidentity.GenerateRandomDID()}, nil)
tm := time.Now()
model.On("Timestamp").Return(tm, nil)
model.On("GetAttributes").Return(nil)
model.On("GetComputeFieldsRules").Return(nil)
model.sigs = append(model.sigs, sig)
srv = &testingcommons.MockIdentityService{}
cid, err := identity.NewDIDFromBytes(did)
assert.NoError(t, err)
srv.On("ValidateSignature", cid, sig.PublicKey, sig.Signature, payload, tm).Return(nil).Once()
dp.identityService = srv
err = dp.AnchorDocument(ctxh, model)
model.AssertExpectations(t)
srv.AssertExpectations(t)
assert.Error(t, err)
assert.Contains(t, err.Error(), "pre anchor validation failed")
id = utils.RandomSlice(32)
next = utils.RandomSlice(32)
model = new(mockModel)
model.On("ID").Return(id)
model.On("CurrentVersion").Return(id)
model.On("CurrentVersionPreimage").Return(id)
model.On("NextVersion").Return(next)
model.On("CalculateSigningRoot").Return(sr, nil)
model.On("Signatures").Return()
model.On("CalculateDocumentRoot").Return(utils.RandomSlice(32), nil)
model.On("Author").Return(did1, nil)
model.On("GetSignerCollaborators", mock.Anything).Return([]identity.DID{did1, testingidentity.GenerateRandomDID()}, nil)
model.On("Timestamp").Return(tm, nil)
model.On("CalculateSignaturesRoot").Return(nil, errors.New("error"))
model.On("GetAttributes").Return(nil)
model.On("GetComputeFieldsRules").Return(nil)
model.sigs = append(model.sigs, sig)
srv = &testingcommons.MockIdentityService{}
srv.On("ValidateSignature", did1, sig.PublicKey, sig.Signature, payload, tm).Return(nil).Once()
dp.identityService = srv
err = dp.AnchorDocument(ctxh, model)
model.AssertExpectations(t)
srv.AssertExpectations(t)
assert.Error(t, err)
assert.Contains(t, err.Error(), "failed to get signature root")
// success
model = new(mockModel)
model.On("ID").Return(id)
model.On("CurrentVersion").Return(id)
model.On("CurrentVersionPreimage").Return(id)
model.On("NextVersion").Return(next)
model.On("CalculateSigningRoot").Return(sr, nil)
model.On("CalculateSignaturesRoot").Return(utils.RandomSlice(32), nil)
model.On("Signatures").Return()
model.On("CalculateDocumentRoot").Return(utils.RandomSlice(32), nil)
model.On("Author").Return(did1, nil)
model.On("GetSignerCollaborators", mock.Anything).Return([]identity.DID{did1, testingidentity.GenerateRandomDID()}, nil)
model.On("Timestamp").Return(tm, nil)
model.On("GetAttributes").Return(nil)
model.On("GetComputeFieldsRules").Return(nil)
model.sigs = append(model.sigs, sig)
srv = &testingcommons.MockIdentityService{}
srv.On("ValidateSignature", cid, sig.PublicKey, sig.Signature, payload, tm).Return(nil).Once()
dp.identityService = srv
anchorSrv := new(anchors.MockAnchorService)
ch := make(chan error, 1)
ch <- nil
anchorSrv.On("CommitAnchor", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil).Once()
dp.anchorSrv = anchorSrv
err = dp.AnchorDocument(ctxh, model)
model.AssertExpectations(t)
srv.AssertExpectations(t)
anchorSrv.AssertExpectations(t)
assert.Nil(t, err)
}
func TestDefaultProcessor_SendDocument(t *testing.T) {
srv := &testingcommons.MockIdentityService{}
srv.On("ValidateSignature", mock.Anything, mock.Anything).Return(nil).Once()
dp := DefaultProcessor(srv, nil, nil, cfg).(defaultProcessor)
ctxh := testingconfig.CreateAccountContext(t, cfg)
self, err := contextutil.Account(ctxh)
assert.NoError(t, err)
didb := self.GetIdentityID()
assert.NoError(t, err)
did1, err := identity.NewDIDFromBytes(didb)
assert.NoError(t, err)
sr := utils.RandomSlice(32)
payload := ConsensusSignaturePayload(sr, false)
sig, err := self.SignMsg(payload)
assert.NoError(t, err)
zeros := [32]byte{}
zeroRoot, err := anchors.ToDocumentRoot(zeros[:])
// validations failed
id := utils.RandomSlice(32)
aid, err := anchors.ToAnchorID(id)
assert.NoError(t, err)
next := utils.RandomSlice(32)
nextAid, err := anchors.ToAnchorID(next)
assert.NoError(t, err)
model := new(mockModel)
model.On("ID").Return(id)
model.On("CurrentVersion").Return(id)
model.On("NextVersion").Return(next)
model.On("CalculateSigningRoot").Return(sr, nil)
model.On("Signatures").Return()
model.On("CalculateDocumentRoot").Return(utils.RandomSlice(32), nil)
model.On("Author").Return(did1, nil)
model.On("GetSignerCollaborators", mock.Anything).Return([]identity.DID{did1, testingidentity.GenerateRandomDID()}, nil)
tm := time.Now()
model.On("Timestamp").Return(tm, nil)
model.On("GetAttributes").Return(nil)
model.On("GetComputeFieldsRules").Return(nil)
model.sigs = append(model.sigs, sig)
srv = &testingcommons.MockIdentityService{}
cid, err := identity.NewDIDFromBytes(didb)
assert.NoError(t, err)
srv.On("ValidateSignature", cid, sig.PublicKey, sig.Signature, payload, tm).Return(nil).Once()
dp.identityService = srv
anchorSrv := new(anchors.MockAnchorService)
anchorSrv.On("GetAnchorData", nextAid).Return(zeroRoot, nil)
anchorSrv.On("GetAnchorData", aid).Return(nil, errors.New("error"))
dp.anchorSrv = anchorSrv
err = dp.SendDocument(ctxh, model)
model.AssertExpectations(t)
srv.AssertExpectations(t)
anchorSrv.AssertExpectations(t)
assert.Error(t, err)
assert.Contains(t, err.Error(), "post anchor validations failed")
// get collaborators failed
dr, err := anchors.ToDocumentRoot(utils.RandomSlice(32))
assert.NoError(t, err)
assert.NoError(t, err)
model = new(mockModel)
model.On("ID").Return(id)
model.On("CurrentVersion").Return(id)
model.On("NextVersion").Return(next)
model.On("CalculateSigningRoot").Return(sr, nil)
model.On("Signatures").Return()
model.On("CalculateDocumentRoot").Return(dr[:], nil)
model.On("GetSignerCollaborators", mock.Anything).Return(nil, errors.New("error")).Once()
model.On("Author").Return(did1, nil)
model.On("Timestamp").Return(tm, nil)
model.On("GetAttributes").Return(nil)
model.On("GetComputeFieldsRules").Return(nil)
model.sigs = append(model.sigs, sig)
srv = &testingcommons.MockIdentityService{}
dp.identityService = srv
anchorSrv = new(anchors.MockAnchorService)
anchorSrv.On("GetAnchorData", nextAid).Return(zeroRoot, nil)
anchorSrv.On("GetAnchorData", aid).Return(dr, nil)
dp.anchorSrv = anchorSrv
err = dp.SendDocument(ctxh, model)
model.AssertExpectations(t)
srv.AssertExpectations(t)
anchorSrv.AssertExpectations(t)
assert.Error(t, err)
// pack core document failed
model = new(mockModel)
model.On("ID").Return(id)
model.On("CurrentVersion").Return(id)
model.On("NextVersion").Return(next)
model.On("CalculateSigningRoot").Return(sr, nil)
model.On("Signatures").Return()
model.On("CalculateDocumentRoot").Return(dr[:], nil)
model.On("GetSignerCollaborators", mock.Anything).Return([]identity.DID{testingidentity.GenerateRandomDID()}, nil)
model.On("PackCoreDocument").Return(nil, errors.New("error")).Once()
model.On("Author").Return(did1, nil)
model.On("Timestamp").Return(tm, nil)
model.On("GetAttributes").Return(nil)
model.On("GetComputeFieldsRules").Return(nil)
model.sigs = append(model.sigs, sig)
srv = &testingcommons.MockIdentityService{}
srv.On("ValidateSignature", cid, sig.PublicKey, sig.Signature, payload, tm).Return(nil).Once()
dp.identityService = srv
anchorSrv = new(anchors.MockAnchorService)
anchorSrv.On("GetAnchorData", nextAid).Return(zeroRoot, errors.New("missing"))
anchorSrv.On("GetAnchorData", aid).Return(dr, nil)
dp.anchorSrv = anchorSrv
err = dp.SendDocument(ctxh, model)
model.AssertExpectations(t)
srv.AssertExpectations(t)
anchorSrv.AssertExpectations(t)
assert.Error(t, err)
// successful
cd := coredocumentpb.CoreDocument{}
did := testingidentity.GenerateRandomDID()
model = new(mockModel)
model.On("ID").Return(id)
model.On("CurrentVersion").Return(id)
model.On("NextVersion").Return(next)
model.On("CalculateSigningRoot").Return(sr, nil)
model.On("Signatures").Return()
model.On("CalculateDocumentRoot").Return(dr[:], nil)
model.On("GetSignerCollaborators", mock.Anything).Return([]identity.DID{did}, nil)
model.On("PackCoreDocument").Return(cd, nil).Once()
model.On("Author").Return(did1, nil)
model.On("Timestamp").Return(tm, nil)
model.On("GetAttributes").Return(nil)
model.On("GetComputeFieldsRules").Return(nil)
model.sigs = append(model.sigs, sig)
srv = &testingcommons.MockIdentityService{}
srv.On("ValidateSignature", cid, sig.PublicKey, sig.Signature, payload, tm).Return(nil).Once()
dp.identityService = srv
anchorSrv = new(anchors.MockAnchorService)
anchorSrv.On("GetAnchorData", aid).Return(dr, nil)
anchorSrv.On("GetAnchorData", nextAid).Return([32]byte{}, errors.New("missing"))
client := new(p2pClient)
client.On("SendAnchoredDocument", mock.Anything, did, mock.Anything).Return(&p2ppb.AnchorDocumentResponse{Accepted: true}, nil).Once()
dp.anchorSrv = anchorSrv
dp.p2pClient = client
err = dp.SendDocument(ctxh, model)
model.AssertExpectations(t)
srv.AssertExpectations(t)
anchorSrv.AssertExpectations(t)
client.AssertExpectations(t)
assert.NoError(t, err)
}