|
| 1 | +package consent_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/raystack/frontier/core/consent" |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | +) |
| 10 | + |
| 11 | +func TestConfig_Validate(t *testing.T) { |
| 12 | + t.Run("accepts a fully configured block", func(t *testing.T) { |
| 13 | + require.NoError(t, enabledConfig().Validate()) |
| 14 | + }) |
| 15 | + |
| 16 | + t.Run("accepts a disabled block", func(t *testing.T) { |
| 17 | + require.NoError(t, consent.Config{}.Validate()) |
| 18 | + }) |
| 19 | + |
| 20 | + t.Run("does not check a disabled block", func(t *testing.T) { |
| 21 | + // a half-written documents map on a deployment that has not turned |
| 22 | + // consent on yet is not an error; turning it on is what makes it one |
| 23 | + config := consent.Config{ |
| 24 | + Documents: map[string]consent.DocumentConfig{ |
| 25 | + "terms_of_service": {}, |
| 26 | + }, |
| 27 | + } |
| 28 | + |
| 29 | + require.NoError(t, config.Validate()) |
| 30 | + |
| 31 | + config.Enabled = true |
| 32 | + require.Error(t, config.Validate()) |
| 33 | + }) |
| 34 | + |
| 35 | + t.Run("rejects enabled with no documents", func(t *testing.T) { |
| 36 | + // silently disabling itself would look identical to a working |
| 37 | + // deployment while asking nobody to accept anything |
| 38 | + err := consent.Config{Enabled: true}.Validate() |
| 39 | + |
| 40 | + require.Error(t, err) |
| 41 | + assert.Contains(t, err.Error(), "no documents") |
| 42 | + }) |
| 43 | + |
| 44 | + t.Run("rejects an empty id", func(t *testing.T) { |
| 45 | + config := consent.Config{ |
| 46 | + Enabled: true, |
| 47 | + Documents: map[string]consent.DocumentConfig{ |
| 48 | + "": {Title: "Terms", Version: "1", URL: "https://example.org/terms"}, |
| 49 | + }, |
| 50 | + } |
| 51 | + |
| 52 | + err := config.Validate() |
| 53 | + |
| 54 | + require.Error(t, err) |
| 55 | + assert.Contains(t, err.Error(), "empty id") |
| 56 | + }) |
| 57 | + |
| 58 | + t.Run("rejects an empty version", func(t *testing.T) { |
| 59 | + config := enabledConfig() |
| 60 | + config.Documents["terms_of_service"] = consent.DocumentConfig{ |
| 61 | + Title: "Terms & Conditions", |
| 62 | + URL: "https://example.org/legal/terms", |
| 63 | + } |
| 64 | + |
| 65 | + err := config.Validate() |
| 66 | + |
| 67 | + require.Error(t, err) |
| 68 | + assert.Contains(t, err.Error(), "terms_of_service") |
| 69 | + assert.Contains(t, err.Error(), "empty version") |
| 70 | + }) |
| 71 | + |
| 72 | + t.Run("rejects an empty url", func(t *testing.T) { |
| 73 | + config := enabledConfig() |
| 74 | + config.Documents["privacy_policy"] = consent.DocumentConfig{ |
| 75 | + Title: "Privacy Policy", |
| 76 | + Version: "2026-04-01", |
| 77 | + } |
| 78 | + |
| 79 | + err := config.Validate() |
| 80 | + |
| 81 | + require.Error(t, err) |
| 82 | + assert.Contains(t, err.Error(), "privacy_policy") |
| 83 | + assert.Contains(t, err.Error(), "empty url") |
| 84 | + }) |
| 85 | + |
| 86 | + t.Run("rejects a url that does not parse", func(t *testing.T) { |
| 87 | + config := enabledConfig() |
| 88 | + config.Documents["eula"] = consent.DocumentConfig{ |
| 89 | + Title: "End User License Agreement", |
| 90 | + Version: "2026-02-14", |
| 91 | + URL: "://example.org/legal/eula", |
| 92 | + } |
| 93 | + |
| 94 | + err := config.Validate() |
| 95 | + |
| 96 | + require.Error(t, err) |
| 97 | + assert.Contains(t, err.Error(), "eula") |
| 98 | + }) |
| 99 | + |
| 100 | + t.Run("rejects a url the client cannot link to", func(t *testing.T) { |
| 101 | + // url.Parse accepts a bare path, and a document nobody can open is as |
| 102 | + // useless as one that does not parse at all |
| 103 | + config := enabledConfig() |
| 104 | + config.Documents["eula"] = consent.DocumentConfig{ |
| 105 | + Title: "End User License Agreement", |
| 106 | + Version: "2026-02-14", |
| 107 | + URL: "example.org/legal/eula", |
| 108 | + } |
| 109 | + |
| 110 | + err := config.Validate() |
| 111 | + |
| 112 | + require.Error(t, err) |
| 113 | + assert.Contains(t, err.Error(), "absolute url") |
| 114 | + }) |
| 115 | + |
| 116 | + t.Run("does not require a title", func(t *testing.T) { |
| 117 | + // the RFC requires ids, versions and URLs to be non-empty, and stops |
| 118 | + // there; an untitled document renders badly but serves correctly |
| 119 | + config := enabledConfig() |
| 120 | + config.Documents["eula"] = consent.DocumentConfig{ |
| 121 | + Version: "2026-02-14", |
| 122 | + URL: "https://example.org/legal/eula", |
| 123 | + } |
| 124 | + |
| 125 | + require.NoError(t, config.Validate()) |
| 126 | + }) |
| 127 | + |
| 128 | + t.Run("names the same document on every run", func(t *testing.T) { |
| 129 | + // map iteration is randomised, so a validator that walks it unsorted |
| 130 | + // reports a different document each boot |
| 131 | + config := consent.Config{ |
| 132 | + Enabled: true, |
| 133 | + Documents: map[string]consent.DocumentConfig{ |
| 134 | + "aaa_broken": {Version: "1"}, |
| 135 | + "zzz_broken": {Version: "1"}, |
| 136 | + }, |
| 137 | + } |
| 138 | + |
| 139 | + for i := 0; i < 20; i++ { |
| 140 | + err := config.Validate() |
| 141 | + require.Error(t, err) |
| 142 | + assert.Contains(t, err.Error(), "aaa_broken") |
| 143 | + } |
| 144 | + }) |
| 145 | +} |
0 commit comments