15
15
package parser
16
16
17
17
import (
18
+ "encoding/json"
18
19
"os"
19
20
"path/filepath"
21
+ "strings"
20
22
"testing"
21
23
22
24
"github.com/stretchr/testify/assert"
@@ -171,11 +173,11 @@ func TestFromFile(t *testing.T) {
171
173
assert .NotEmpty (t , operationState .Actions )
172
174
assert .Equal (t , "startApplicationWorkflowId" , operationState .Actions [0 ].SubFlowRef .WorkflowID )
173
175
assert .NotNil (t , w .Auth )
174
- assert . NotNil ( t , w .Auth .Defs )
175
- assert .Equal (t , len (w . Auth . Defs ), 1 )
176
- assert .Equal (t , "testAuth" , w . Auth . Defs [0 ].Name )
177
- assert .Equal (t , model .AuthTypeBearer , w . Auth . Defs [0 ].Scheme )
178
- bearerProperties := w . Auth . Defs [0 ].Properties .(* model.BearerAuthProperties ).Token
176
+ auth := w .Auth .([] * model. Auth )
177
+ assert .Equal (t , len (auth ), 1 )
178
+ assert .Equal (t , "testAuth" , auth [0 ].Name )
179
+ assert .Equal (t , model .AuthTypeBearer , auth [0 ].Scheme )
180
+ bearerProperties := auth [0 ].Properties .(* model.BearerAuthProperties ).Token
179
181
assert .Equal (t , "test_token" , bearerProperties )
180
182
},
181
183
}, {
@@ -194,15 +196,15 @@ func TestFromFile(t *testing.T) {
194
196
assert .NotEmpty (t , operationState .Actions )
195
197
assert .Equal (t , "startApplicationWorkflowId" , operationState .Actions [0 ].SubFlowRef .WorkflowID )
196
198
assert .NotNil (t , w .Auth )
197
- assert . NotNil ( t , w .Auth .Defs )
198
- assert .Equal (t , len (w . Auth . Defs ), 2 )
199
- assert .Equal (t , "testAuth" , w . Auth . Defs [0 ].Name )
200
- assert .Equal (t , model .AuthTypeBearer , w . Auth . Defs [0 ].Scheme )
201
- bearerProperties := w . Auth . Defs [0 ].Properties .(* model.BearerAuthProperties ).Token
199
+ auth := w .Auth .([] * model. Auth )
200
+ assert .Equal (t , len (auth ), 2 )
201
+ assert .Equal (t , "testAuth" , auth [0 ].Name )
202
+ assert .Equal (t , model .AuthTypeBearer , auth [0 ].Scheme )
203
+ bearerProperties := auth [0 ].Properties .(* model.BearerAuthProperties ).Token
202
204
assert .Equal (t , "test_token" , bearerProperties )
203
- assert .Equal (t , "testAuth2" , w . Auth . Defs [1 ].Name )
204
- assert .Equal (t , model .AuthTypeBasic , w . Auth . Defs [1 ].Scheme )
205
- basicProperties := w . Auth . Defs [1 ].Properties .(* model.BasicAuthProperties )
205
+ assert .Equal (t , "testAuth2" , auth [1 ].Name )
206
+ assert .Equal (t , model .AuthTypeBasic , auth [1 ].Scheme )
207
+ basicProperties := auth [1 ].Properties .(* model.BasicAuthProperties )
206
208
assert .Equal (t , "test_user" , basicProperties .Username )
207
209
assert .Equal (t , "test_pwd" , basicProperties .Password )
208
210
},
@@ -497,3 +499,138 @@ func TestFromFile(t *testing.T) {
497
499
)
498
500
}
499
501
}
502
+
503
+ func TestUnmarshalWorkflowBasicTests (t * testing.T ) {
504
+ t .Run ("BasicWorkflowYamlNoAuthDefs" , func (t * testing.T ) {
505
+ workflow , err := FromYAMLSource ([]byte (`
506
+ id: helloworld
507
+ version: '1.0.0'
508
+ specVersion: '0.8'
509
+ name: Hello World Workflow
510
+ description: Inject Hello World
511
+ start: Hello State
512
+ states:
513
+ - name: Hello State
514
+ type: inject
515
+ data:
516
+ result: Hello World!
517
+ end: true
518
+ ` ))
519
+ assert .Nil (t , err )
520
+ assert .NotNil (t , workflow )
521
+
522
+ b , err := json .Marshal (workflow )
523
+ assert .Nil (t , err )
524
+ assert .True (t , ! strings .Contains (string (b ), "auth" ))
525
+
526
+ workflow = nil
527
+ err = json .Unmarshal (b , & workflow )
528
+ assert .Nil (t , err )
529
+ })
530
+
531
+ t .Run ("BasicWorkflowBasicAuthJSONSource" , func (t * testing.T ) {
532
+ workflow , err := FromJSONSource ([]byte (`
533
+ {
534
+ "id": "applicantrequest",
535
+ "version": "1.0",
536
+ "name": "Applicant Request Decision Workflow",
537
+ "description": "Determine if applicant request is valid",
538
+ "start": "CheckApplication",
539
+ "specVersion": "0.8",
540
+ "auth": [
541
+ {
542
+ "name": "testAuth",
543
+ "scheme": "bearer",
544
+ "properties": {
545
+ "token": "test_token"
546
+ }
547
+ },
548
+ {
549
+ "name": "testAuth2",
550
+ "scheme": "basic",
551
+ "properties": {
552
+ "username": "test_user",
553
+ "password": "test_pwd"
554
+ }
555
+ }
556
+ ],
557
+ "states": [
558
+ {
559
+ "name": "Hello State",
560
+ "type": "inject",
561
+ "data": {
562
+ "result": "Hello World!"
563
+ },
564
+ "end": true
565
+ }
566
+ ]
567
+ }
568
+ ` ))
569
+ assert .Nil (t , err )
570
+ assert .NotNil (t , workflow .Auth )
571
+
572
+ b , _ := json .Marshal (workflow )
573
+ assert .Equal (t , "{\" id\" :\" applicantrequest\" ,\" name\" :\" Applicant Request Decision Workflow\" ,\" description\" :\" Determine if applicant request is valid\" ,\" version\" :\" 1.0\" ,\" start\" :{\" stateName\" :\" CheckApplication\" },\" specVersion\" :\" 0.8\" ,\" expressionLang\" :\" jq\" ,\" auth\" :[{\" name\" :\" testAuth\" ,\" scheme\" :\" bearer\" ,\" properties\" :{\" token\" :\" test_token\" }},{\" name\" :\" testAuth2\" ,\" scheme\" :\" basic\" ,\" properties\" :{\" username\" :\" test_user\" ,\" password\" :\" test_pwd\" }}],\" states\" :[{\" name\" :\" Hello State\" ,\" type\" :\" inject\" ,\" end\" :{},\" data\" :{\" result\" :\" Hello World!\" }}]}" ,
574
+ string (b ))
575
+
576
+ })
577
+
578
+ t .Run ("BasicWorkflowBasicAuthStringJSONSource" , func (t * testing.T ) {
579
+ workflow , err := FromJSONSource ([]byte (`
580
+ {
581
+ "id": "applicantrequest",
582
+ "version": "1.0",
583
+ "name": "Applicant Request Decision Workflow",
584
+ "description": "Determine if applicant request is valid",
585
+ "start": "CheckApplication",
586
+ "specVersion": "0.8",
587
+ "auth": "./testdata/workflows/urifiles/auth.json",
588
+ "states": [
589
+ {
590
+ "name": "Hello State",
591
+ "type": "inject",
592
+ "data": {
593
+ "result": "Hello World!"
594
+ },
595
+ "end": true
596
+ }
597
+ ]
598
+ }
599
+ ` ))
600
+ assert .Nil (t , err )
601
+ assert .NotNil (t , workflow .Auth )
602
+
603
+ b , _ := json .Marshal (workflow )
604
+ assert .Equal (t , "{\" id\" :\" applicantrequest\" ,\" name\" :\" Applicant Request Decision Workflow\" ,\" description\" :\" Determine if applicant request is valid\" ,\" version\" :\" 1.0\" ,\" start\" :{\" stateName\" :\" CheckApplication\" },\" specVersion\" :\" 0.8\" ,\" expressionLang\" :\" jq\" ,\" auth\" :[{\" name\" :\" testAuth\" ,\" scheme\" :\" bearer\" ,\" properties\" :{\" token\" :\" test_token\" }},{\" name\" :\" testAuth2\" ,\" scheme\" :\" basic\" ,\" properties\" :{\" username\" :\" test_user\" ,\" password\" :\" test_pwd\" }}],\" states\" :[{\" name\" :\" Hello State\" ,\" type\" :\" inject\" ,\" end\" :{},\" data\" :{\" result\" :\" Hello World!\" }}]}" ,
605
+ string (b ))
606
+
607
+ })
608
+
609
+ t .Run ("BasicWorkflowInteger" , func (t * testing.T ) {
610
+ workflow , err := FromJSONSource ([]byte (`
611
+ {
612
+ "id": "applicantrequest",
613
+ "version": "1.0",
614
+ "name": "Applicant Request Decision Workflow",
615
+ "description": "Determine if applicant request is valid",
616
+ "start": "CheckApplication",
617
+ "specVersion": "0.7",
618
+ "auth": 123,
619
+ "states": [
620
+ {
621
+ "name": "Hello State",
622
+ "type": "inject",
623
+ "data": {
624
+ "result": "Hello World!"
625
+ },
626
+ "end": true
627
+ }
628
+ ]
629
+ }
630
+ ` ))
631
+
632
+ assert .NotNil (t , err )
633
+ assert .Equal (t , "auth value '123' is not supported, it must be an object or string" , err .Error ())
634
+ assert .Nil (t , workflow )
635
+ })
636
+ }
0 commit comments