mirror of
				https://github.com/juanfont/headscale.git
				synced 2025-10-28 10:51:44 +01:00 
			
		
		
		
	
		
			
				
	
	
		
			69 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			69 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
package v2
 | 
						|
 | 
						|
import (
 | 
						|
	"testing"
 | 
						|
 | 
						|
	"github.com/google/go-cmp/cmp"
 | 
						|
	"github.com/juanfont/headscale/hscontrol/policy/matcher"
 | 
						|
	"github.com/juanfont/headscale/hscontrol/types"
 | 
						|
	"github.com/stretchr/testify/require"
 | 
						|
	"gorm.io/gorm"
 | 
						|
	"tailscale.com/tailcfg"
 | 
						|
)
 | 
						|
 | 
						|
func node(name, ipv4, ipv6 string, user types.User, hostinfo *tailcfg.Hostinfo) *types.Node {
 | 
						|
	return &types.Node{
 | 
						|
		ID:       0,
 | 
						|
		Hostname: name,
 | 
						|
		IPv4:     ap(ipv4),
 | 
						|
		IPv6:     ap(ipv6),
 | 
						|
		User:     user,
 | 
						|
		UserID:   user.ID,
 | 
						|
		Hostinfo: hostinfo,
 | 
						|
	}
 | 
						|
}
 | 
						|
 | 
						|
func TestPolicyManager(t *testing.T) {
 | 
						|
	users := types.Users{
 | 
						|
		{Model: gorm.Model{ID: 1}, Name: "testuser", Email: "testuser@headscale.net"},
 | 
						|
		{Model: gorm.Model{ID: 2}, Name: "otheruser", Email: "otheruser@headscale.net"},
 | 
						|
	}
 | 
						|
 | 
						|
	tests := []struct {
 | 
						|
		name         string
 | 
						|
		pol          string
 | 
						|
		nodes        types.Nodes
 | 
						|
		wantFilter   []tailcfg.FilterRule
 | 
						|
		wantMatchers []matcher.Match
 | 
						|
	}{
 | 
						|
		{
 | 
						|
			name:         "empty-policy",
 | 
						|
			pol:          "{}",
 | 
						|
			nodes:        types.Nodes{},
 | 
						|
			wantFilter:   nil,
 | 
						|
			wantMatchers: []matcher.Match{},
 | 
						|
		},
 | 
						|
	}
 | 
						|
 | 
						|
	for _, tt := range tests {
 | 
						|
		t.Run(tt.name, func(t *testing.T) {
 | 
						|
			pm, err := NewPolicyManager([]byte(tt.pol), users, tt.nodes.ViewSlice())
 | 
						|
			require.NoError(t, err)
 | 
						|
 | 
						|
			filter, matchers := pm.Filter()
 | 
						|
			if diff := cmp.Diff(tt.wantFilter, filter); diff != "" {
 | 
						|
				t.Errorf("Filter() filter mismatch (-want +got):\n%s", diff)
 | 
						|
			}
 | 
						|
			if diff := cmp.Diff(
 | 
						|
				tt.wantMatchers,
 | 
						|
				matchers,
 | 
						|
				cmp.AllowUnexported(matcher.Match{}),
 | 
						|
			); diff != "" {
 | 
						|
				t.Errorf("Filter() matchers mismatch (-want +got):\n%s", diff)
 | 
						|
			}
 | 
						|
 | 
						|
			// TODO(kradalby): Test SSH Policy
 | 
						|
		})
 | 
						|
	}
 | 
						|
}
 |