11package account
22
33import (
4+ "encoding/json"
45 "testing"
56
67 "github.com/stretchr/testify/assert"
@@ -13,3 +14,88 @@ func TestContactType_String_Expected(t *testing.T) {
1314
1415 assert .Equal (t , "Accounts" , s )
1516}
17+
18+ func TestApplicationRestriction_UnmarshalJSON (t * testing.T ) {
19+ tests := []struct {
20+ name string
21+ jsonData string
22+ expected ApplicationRestriction
23+ expectError bool
24+ }{
25+ {
26+ name : "Empty array returns empty restriction" ,
27+ jsonData : `[]` ,
28+ expected : ApplicationRestriction {
29+ IPRestrictionType : "" ,
30+ IPRanges : nil ,
31+ },
32+ expectError : false ,
33+ },
34+ {
35+ name : "Valid object with allowlist unmarshals correctly" ,
36+ jsonData : `{"ip_restriction_type":"allowlist","ip_ranges":["198.51.100.1","198.51.100.2"]}` ,
37+ expected : ApplicationRestriction {
38+ IPRestrictionType : "allowlist" ,
39+ IPRanges : []string {"198.51.100.1" , "198.51.100.2" },
40+ },
41+ expectError : false ,
42+ },
43+ {
44+ name : "Valid object with denylist unmarshals correctly" ,
45+ jsonData : `{"ip_restriction_type":"denylist","ip_ranges":["198.51.100.3"]}` ,
46+ expected : ApplicationRestriction {
47+ IPRestrictionType : "denylist" ,
48+ IPRanges : []string {"198.51.100.3" },
49+ },
50+ expectError : false ,
51+ },
52+ {
53+ name : "Empty object unmarshals correctly" ,
54+ jsonData : `{"ip_restriction_type":"","ip_ranges":[]}` ,
55+ expected : ApplicationRestriction {
56+ IPRestrictionType : "" ,
57+ IPRanges : []string {},
58+ },
59+ expectError : false ,
60+ },
61+ {
62+ name : "Object with null ip_ranges unmarshals correctly" ,
63+ jsonData : `{"ip_restriction_type":"allowlist","ip_ranges":null}` ,
64+ expected : ApplicationRestriction {
65+ IPRestrictionType : "allowlist" ,
66+ IPRanges : nil ,
67+ },
68+ expectError : false ,
69+ },
70+ {
71+ name : "Invalid JSON returns error" ,
72+ jsonData : `{invalid json}` ,
73+ expected : ApplicationRestriction {},
74+ expectError : true ,
75+ },
76+ {
77+ name : "Array with content treated as empty restriction" ,
78+ jsonData : `["some","content"]` ,
79+ expected : ApplicationRestriction {
80+ IPRestrictionType : "" ,
81+ IPRanges : nil ,
82+ },
83+ expectError : false ,
84+ },
85+ }
86+
87+ for _ , tt := range tests {
88+ t .Run (tt .name , func (t * testing.T ) {
89+ var restriction ApplicationRestriction
90+ err := json .Unmarshal ([]byte (tt .jsonData ), & restriction )
91+
92+ if tt .expectError {
93+ assert .Error (t , err )
94+ } else {
95+ assert .NoError (t , err )
96+ assert .Equal (t , tt .expected .IPRestrictionType , restriction .IPRestrictionType )
97+ assert .Equal (t , tt .expected .IPRanges , restriction .IPRanges )
98+ }
99+ })
100+ }
101+ }
0 commit comments