Skip to content

Commit f793420

Browse files
Fix for xml encoding issues with test
1 parent 15d42f4 commit f793420

File tree

2 files changed

+72
-23
lines changed

2 files changed

+72
-23
lines changed

cmd/rule/download.go

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
package rule
33

44
import (
5-
"bytes"
65
"context"
76
"encoding/json"
87
"encoding/xml"
@@ -27,9 +26,13 @@ type Rule struct {
2726
XMLName xml.Name `xml:"Rule"`
2827
Name string `xml:"name,attr"`
2928
Type string `xml:"type,attr"`
30-
Description string `xml:"Description,omitempty"`
31-
Signature *Signature
32-
Source string `xml:"Source"`
29+
Description struct {
30+
Content string `xml:",innerxml"`
31+
}
32+
Signature *Signature
33+
Source struct {
34+
Content string `xml:",cdata"`
35+
} `xml:"Source"`
3336
}
3437

3538
type Signature struct {
@@ -53,7 +56,9 @@ type Argument struct {
5356
XMLName xml.Name `xml:"Argument"`
5457
Name string `xml:"name,attr"`
5558
Type string `xml:"type,attr,omitempty"`
56-
Description string `xml:"Description,omitempty"`
59+
Description struct {
60+
Content string `xml:",innerxml"`
61+
}
5762
}
5863

5964
var cloudRuleTypes = []string{"AttributeGenerator", "AttributeGeneratorFromTemplate", "BeforeProvisioning", "BuildMap", "Correlation", "IdentityAttribute", "ManagerCorrelation"}
@@ -150,12 +155,12 @@ func saveCloudXMLRules(apiClient *sailpoint.APIClient, description string, inclu
150155
rule.Type = RuleType
151156

152157
if v.Object["description"] != nil {
153-
rule.Description = v.Object["description"].(string)
158+
rule.Description.Content = v.Object["description"].(string)
154159
} else {
155-
rule.Description = ""
160+
rule.Description.Content = ""
156161
}
157162

158-
rule.Source = "<![CDATA[\n" + v.Object["sourceCode"].(map[string]interface{})["script"].(string) + "\n]]>"
163+
rule.Source.Content = v.Object["sourceCode"].(map[string]interface{})["script"].(string)
159164

160165
var ruleSignature = &Signature{}
161166

@@ -175,9 +180,9 @@ func saveCloudXMLRules(apiClient *sailpoint.APIClient, description string, inclu
175180
}
176181

177182
if v.(map[string]interface{})["description"] != nil {
178-
argument.Description = v.(map[string]interface{})["description"].(string)
183+
argument.Description.Content = v.(map[string]interface{})["description"].(string)
179184
} else {
180-
argument.Description = ""
185+
argument.Description.Content = ""
181186
}
182187

183188
ruleSignature.Inputs.Argument = append(ruleSignature.Inputs.Argument, argument)
@@ -203,9 +208,9 @@ func saveCloudXMLRules(apiClient *sailpoint.APIClient, description string, inclu
203208
}
204209

205210
if v.(map[string]interface{})["description"] != nil {
206-
argument.Description = v.(map[string]interface{})["description"].(string)
211+
argument.Description.Content = v.(map[string]interface{})["description"].(string)
207212
} else {
208-
argument.Description = ""
213+
argument.Description.Content = ""
209214
}
210215

211216
ruleSignature.Returns.Argument = append(ruleSignature.Returns.Argument, argument)
@@ -225,9 +230,9 @@ func saveCloudXMLRules(apiClient *sailpoint.APIClient, description string, inclu
225230
}
226231

227232
if output["description"] != nil {
228-
argument.Description = output["description"].(string)
233+
argument.Description.Content = output["description"].(string)
229234
} else {
230-
argument.Description = ""
235+
argument.Description.Content = ""
231236
}
232237

233238
ruleSignature.Returns.Argument = append(ruleSignature.Returns.Argument, argument)
@@ -238,15 +243,7 @@ func saveCloudXMLRules(apiClient *sailpoint.APIClient, description string, inclu
238243

239244
out = []byte(xml.Header + SailPointHeader + string(out))
240245

241-
out = bytes.Replace(out, []byte("&#xA;"), []byte("\n"), -1)
242-
out = bytes.Replace(out, []byte("&#xD;"), []byte("\r"), -1)
243-
out = bytes.Replace(out, []byte("&#34;"), []byte("\""), -1)
244-
out = bytes.Replace(out, []byte("&amp;"), []byte("&"), -1)
245-
out = bytes.Replace(out, []byte("&#x9;"), []byte("\t"), -1)
246-
out = bytes.Replace(out, []byte("&lt;"), []byte("<"), -1)
247-
out = bytes.Replace(out, []byte("&gt;"), []byte(">"), -1)
248-
249-
err := output.WriteFile(destination+"/cloud", "Rule - "+rule.Type+" - "+rule.Name+".xml", out)
246+
err = output.WriteFile(destination+"/cloud", "Rule - "+rule.Type+" - "+rule.Name+".xml", out)
250247

251248
if err != nil {
252249
return err

cmd/rule/download_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package rule
2+
3+
import (
4+
"encoding/xml"
5+
"strings"
6+
"testing"
7+
)
8+
9+
func TestRuleXMLSerialization(t *testing.T) {
10+
// Define the Rule struct with characters that should remain unchanged
11+
rule := Rule{
12+
Name: "Test Rule",
13+
Type: "TestType",
14+
Description: struct {
15+
Content string `xml:",innerxml"`
16+
}{
17+
Content: `This contains "quotes", <tags>, and & special characters. * '`,
18+
},
19+
Source: struct {
20+
Content string `xml:",cdata"`
21+
}{
22+
Content: `if (x > '5') { return true; }`, // Should be wrapped in CDATA
23+
},
24+
}
25+
26+
// Marshal to XML
27+
xmlBytes, err := xml.MarshalIndent(rule, "", " ")
28+
if err != nil {
29+
t.Fatalf("Failed to marshal XML: %v", err)
30+
}
31+
32+
xmlString := string(xmlBytes)
33+
34+
// Ensure special characters remain as-is (not encoded)
35+
tests := []string{
36+
`"quotes"`, // Should not be converted to &quot;
37+
`<tags>`, // Should not be converted to &lt; or &gt;
38+
`& special`, // Should not be converted to &amp;
39+
`<![CDATA[if (x > '5') { return true; }]]>`, // CDATA should remain unchanged
40+
`*`, // Should remain as-is
41+
`'`, // Should remain as-is
42+
}
43+
44+
// Loop through test cases
45+
t.Log("Checking for expected string in XML output:", xmlString)
46+
47+
for _, expected := range tests {
48+
if !strings.Contains(xmlString, expected) {
49+
t.Errorf("Expected XML to contain %q but it was missing.\nActual XML:\n%s", expected, xmlString)
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)