github.com/openfga/openfga@v1.5.4-rc1/internal/condition/types/ipaddress_test.go (about)

     1  package types
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/google/cel-go/common/types"
     7  	"github.com/google/cel-go/common/types/ref"
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  func TestIPaddressCELBinaryBinding(t *testing.T) {
    12  	addr, err := ParseIPAddress("192.168.1.1")
    13  	require.NoError(t, err)
    14  
    15  	tests := []struct {
    16  		name   string
    17  		lhs    ref.Val
    18  		rhs    ref.Val
    19  		result ref.Val
    20  	}{
    21  		{
    22  			name:   "ip_in_cidr",
    23  			lhs:    addr,
    24  			rhs:    types.String("192.168.1.0/24"),
    25  			result: types.Bool(true),
    26  		},
    27  		{
    28  			name:   "ip_not_in_cidr",
    29  			lhs:    addr,
    30  			rhs:    types.String("10.0.0.0/8"),
    31  			result: types.Bool(false),
    32  		},
    33  		{
    34  			name:   "missing_cidr",
    35  			lhs:    addr,
    36  			rhs:    types.Bool(true),
    37  			result: types.NewErr("a CIDR string is required for comparison"),
    38  		},
    39  		{
    40  			name:   "malformed_cidr",
    41  			lhs:    addr,
    42  			rhs:    types.String("malformed"),
    43  			result: types.NewErr("'malformed' is a malformed CIDR string"),
    44  		},
    45  		{
    46  			name:   "missing_ip",
    47  			lhs:    types.String("10.0.0.1"),
    48  			rhs:    types.String("10.0.0.0/8"),
    49  			result: types.NewErr("an IPAddress parameter value is required for comparison"),
    50  		},
    51  	}
    52  
    53  	for _, test := range tests {
    54  		t.Run(test.name, func(t *testing.T) {
    55  			val := ipaddressCELBinaryBinding(test.lhs, test.rhs)
    56  			require.Equal(t, test.result, val)
    57  		})
    58  	}
    59  }