|
| 1 | +package policy |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | +) |
| 10 | + |
| 11 | +func TestPolicy_PreservesUnknownFields(t *testing.T) { |
| 12 | + // Simulate the policy.json with many fields not in the struct |
| 13 | + // Only ExtensionInstallForcelist and ExtensionSettings are in the struct |
| 14 | + input := `{ |
| 15 | + "PasswordManagerEnabled": false, |
| 16 | + "AutofillCreditCardEnabled": false, |
| 17 | + "TranslateEnabled": false, |
| 18 | + "DefaultNotificationsSetting": 2, |
| 19 | + "DefaultGeolocationSetting": 2, |
| 20 | + "DefaultSearchProviderEnabled": true, |
| 21 | + "DefaultSearchProviderName": "DuckDuckGo", |
| 22 | + "DefaultSearchProviderSearchURL": "https://duckduckgo.com/?q={searchTerms}", |
| 23 | + "DefaultSearchProviderSuggestURL": "https://duckduckgo.com/ac/?q={searchTerms}", |
| 24 | + "NewTabPageLocation": "https://start.duckduckgo.com/", |
| 25 | + "SomeOtherUnknownField": {"nested": "value"}, |
| 26 | + "ExtensionSettings": { |
| 27 | + "*": { |
| 28 | + "allowed_types": ["extension"], |
| 29 | + "install_sources": ["*"] |
| 30 | + } |
| 31 | + } |
| 32 | + }` |
| 33 | + |
| 34 | + var policy Policy |
| 35 | + err := json.Unmarshal([]byte(input), &policy) |
| 36 | + require.NoError(t, err) |
| 37 | + |
| 38 | + // Verify known struct fields are parsed correctly |
| 39 | + assert.NotNil(t, policy.ExtensionSettings) |
| 40 | + assert.Contains(t, policy.ExtensionSettings, "*") |
| 41 | + |
| 42 | + // Verify all non-struct fields are captured as unknown |
| 43 | + // All fields except ExtensionSettings should be in unknownFields |
| 44 | + expectedUnknownFields := []string{ |
| 45 | + "PasswordManagerEnabled", |
| 46 | + "AutofillCreditCardEnabled", |
| 47 | + "TranslateEnabled", |
| 48 | + "DefaultNotificationsSetting", |
| 49 | + "DefaultGeolocationSetting", |
| 50 | + "DefaultSearchProviderEnabled", |
| 51 | + "DefaultSearchProviderName", |
| 52 | + "DefaultSearchProviderSearchURL", |
| 53 | + "DefaultSearchProviderSuggestURL", |
| 54 | + "NewTabPageLocation", |
| 55 | + "SomeOtherUnknownField", |
| 56 | + } |
| 57 | + for _, field := range expectedUnknownFields { |
| 58 | + assert.Contains(t, policy.unknownFields, field, "field %s should be preserved", field) |
| 59 | + } |
| 60 | + |
| 61 | + // Now marshal it back |
| 62 | + output, err := json.Marshal(&policy) |
| 63 | + require.NoError(t, err) |
| 64 | + |
| 65 | + // Unmarshal into a map to verify all fields are present |
| 66 | + var result map[string]interface{} |
| 67 | + err = json.Unmarshal(output, &result) |
| 68 | + require.NoError(t, err) |
| 69 | + |
| 70 | + // Verify all fields are preserved |
| 71 | + assert.Equal(t, float64(2), result["DefaultGeolocationSetting"]) |
| 72 | + assert.Equal(t, float64(2), result["DefaultNotificationsSetting"]) |
| 73 | + assert.Equal(t, false, result["PasswordManagerEnabled"]) |
| 74 | + assert.Equal(t, "DuckDuckGo", result["DefaultSearchProviderName"]) |
| 75 | + assert.NotNil(t, result["SomeOtherUnknownField"]) |
| 76 | +} |
| 77 | + |
| 78 | +func TestPolicy_ModifyAndPreserveUnknownFields(t *testing.T) { |
| 79 | + // This test simulates the AddExtension read-modify-write cycle |
| 80 | + input := `{ |
| 81 | + "PasswordManagerEnabled": false, |
| 82 | + "DefaultGeolocationSetting": 2, |
| 83 | + "DefaultNotificationsSetting": 2, |
| 84 | + "ExtensionSettings": {} |
| 85 | + }` |
| 86 | + |
| 87 | + var policy Policy |
| 88 | + err := json.Unmarshal([]byte(input), &policy) |
| 89 | + require.NoError(t, err) |
| 90 | + |
| 91 | + // Add an extension setting (this is what AddExtension does) |
| 92 | + policy.ExtensionSettings["test-extension"] = ExtensionSetting{ |
| 93 | + UpdateUrl: "http://127.0.0.1:10001/extensions/test/update.xml", |
| 94 | + } |
| 95 | + |
| 96 | + // Marshal it back |
| 97 | + output, err := json.Marshal(&policy) |
| 98 | + require.NoError(t, err) |
| 99 | + |
| 100 | + // Verify the unknown fields are still there |
| 101 | + var result map[string]interface{} |
| 102 | + err = json.Unmarshal(output, &result) |
| 103 | + require.NoError(t, err) |
| 104 | + |
| 105 | + assert.Equal(t, float64(2), result["DefaultGeolocationSetting"], "DefaultGeolocationSetting should be preserved") |
| 106 | + assert.Equal(t, float64(2), result["DefaultNotificationsSetting"], "DefaultNotificationsSetting should be preserved") |
| 107 | + assert.Equal(t, false, result["PasswordManagerEnabled"], "PasswordManagerEnabled should be preserved") |
| 108 | + |
| 109 | + // Verify extension was added |
| 110 | + extSettings, ok := result["ExtensionSettings"].(map[string]interface{}) |
| 111 | + require.True(t, ok) |
| 112 | + assert.Contains(t, extSettings, "test-extension") |
| 113 | +} |
| 114 | + |
| 115 | +func TestPolicy_ExtensionInstallForcelist(t *testing.T) { |
| 116 | + // Test that ExtensionInstallForcelist works correctly |
| 117 | + input := `{ |
| 118 | + "DefaultGeolocationSetting": 2, |
| 119 | + "ExtensionInstallForcelist": ["existing-ext;http://example.com/update.xml"], |
| 120 | + "ExtensionSettings": {} |
| 121 | + }` |
| 122 | + |
| 123 | + var policy Policy |
| 124 | + err := json.Unmarshal([]byte(input), &policy) |
| 125 | + require.NoError(t, err) |
| 126 | + |
| 127 | + // Verify forcelist is parsed |
| 128 | + assert.Len(t, policy.ExtensionInstallForcelist, 1) |
| 129 | + assert.Equal(t, "existing-ext;http://example.com/update.xml", policy.ExtensionInstallForcelist[0]) |
| 130 | + |
| 131 | + // Add to forcelist |
| 132 | + policy.ExtensionInstallForcelist = append(policy.ExtensionInstallForcelist, "new-ext;http://example.com/new.xml") |
| 133 | + |
| 134 | + // Marshal back |
| 135 | + output, err := json.Marshal(&policy) |
| 136 | + require.NoError(t, err) |
| 137 | + |
| 138 | + var result map[string]interface{} |
| 139 | + err = json.Unmarshal(output, &result) |
| 140 | + require.NoError(t, err) |
| 141 | + |
| 142 | + // Both unknown field and forcelist should be present |
| 143 | + assert.Equal(t, float64(2), result["DefaultGeolocationSetting"]) |
| 144 | + forcelist, ok := result["ExtensionInstallForcelist"].([]interface{}) |
| 145 | + require.True(t, ok) |
| 146 | + assert.Len(t, forcelist, 2) |
| 147 | +} |
| 148 | + |
| 149 | +func TestPolicy_EmptyPolicy(t *testing.T) { |
| 150 | + // Test with minimal input |
| 151 | + input := `{}` |
| 152 | + |
| 153 | + var policy Policy |
| 154 | + err := json.Unmarshal([]byte(input), &policy) |
| 155 | + require.NoError(t, err) |
| 156 | + |
| 157 | + assert.Empty(t, policy.unknownFields) |
| 158 | + assert.Nil(t, policy.ExtensionSettings) |
| 159 | + assert.Nil(t, policy.ExtensionInstallForcelist) |
| 160 | + |
| 161 | + output, err := json.Marshal(&policy) |
| 162 | + require.NoError(t, err) |
| 163 | + |
| 164 | + var result map[string]interface{} |
| 165 | + err = json.Unmarshal(output, &result) |
| 166 | + require.NoError(t, err) |
| 167 | + |
| 168 | + // Should have empty ExtensionSettings as null/missing |
| 169 | + assert.Nil(t, result["ExtensionSettings"]) |
| 170 | +} |
0 commit comments