|
| 1 | +package user |
| 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 TestSessionState_AllowedIPs_Ignored(t *testing.T) { |
| 12 | + // This test proves that 'allowed_ips' is NOT supported on the key level (SessionState). |
| 13 | + // When a user tries to pass 'allowed_ips' via the Gateway API or Dashboard, |
| 14 | + // it is completely ignored during JSON unmarshaling because the field does not exist. |
| 15 | + |
| 16 | + jsonPayload := []byte(`{ |
| 17 | + "allowance": 1000, |
| 18 | + "rate": 1000, |
| 19 | + "per": 60, |
| 20 | + "allowed_ips": ["192.168.1.1", "10.0.0.1"] |
| 21 | + }`) |
| 22 | + |
| 23 | + var session SessionState |
| 24 | + err := json.Unmarshal(jsonPayload, &session) |
| 25 | + require.NoError(t, err) |
| 26 | + |
| 27 | + // The standard fields are unmarshaled correctly |
| 28 | + assert.Equal(t, float64(1000), session.Allowance) |
| 29 | + assert.Equal(t, float64(1000), session.Rate) |
| 30 | + |
| 31 | + // But 'allowed_ips' is completely ignored. It is not in MetaData either, |
| 32 | + // because json.Unmarshal does not automatically put unknown fields into maps |
| 33 | + // unless explicitly handled by a custom UnmarshalJSON method. |
| 34 | + assert.Nil(t, session.MetaData) |
| 35 | + |
| 36 | + // Re-marshaling the session will NOT contain 'allowed_ips' |
| 37 | + marshaled, err := json.Marshal(session) |
| 38 | + require.NoError(t, err) |
| 39 | + assert.NotContains(t, string(marshaled), "allowed_ips") |
| 40 | +} |
| 41 | + |
| 42 | +func TestPolicy_AllowedIPs_Ignored(t *testing.T) { |
| 43 | + // This test proves that 'allowed_ips' is NOT supported on the policy level either. |
| 44 | + // If a user tries to pass 'allowed_ips' in a Policy definition, it is ignored. |
| 45 | + |
| 46 | + jsonPayload := []byte(`{ |
| 47 | + "name": "My Policy", |
| 48 | + "rate": 1000, |
| 49 | + "per": 60, |
| 50 | + "allowed_ips": ["192.168.1.1", "10.0.0.1"] |
| 51 | + }`) |
| 52 | + |
| 53 | + var policy Policy |
| 54 | + err := json.Unmarshal(jsonPayload, &policy) |
| 55 | + require.NoError(t, err) |
| 56 | + |
| 57 | + assert.Equal(t, "My Policy", policy.Name) |
| 58 | + assert.Equal(t, float64(1000), policy.Rate) |
| 59 | + |
| 60 | + // 'allowed_ips' is ignored |
| 61 | + marshaled, err := json.Marshal(policy) |
| 62 | + require.NoError(t, err) |
| 63 | + assert.NotContains(t, string(marshaled), "allowed_ips") |
| 64 | +} |
0 commit comments