Add test for autoApprovers feature

This commit is contained in:
Benjamin George Roberts
2022-08-24 22:09:06 +10:00
parent 7653ad40d6
commit 60cc9ddb3b
4 changed files with 83 additions and 25 deletions

View File

@@ -1051,3 +1051,44 @@ func TestHeadscale_GenerateGivenName(t *testing.T) {
})
}
}
func (s *Suite) TestAutoApproveRoutes(c *check.C) {
err := app.LoadACLPolicy("./tests/acls/acl_policy_autoapprovers.hujson")
c.Assert(err, check.IsNil)
namespace, err := app.CreateNamespace("test")
c.Assert(err, check.IsNil)
pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil)
c.Assert(err, check.IsNil)
nodeKey := key.NewNode()
defaultRoute := netaddr.MustParseIPPrefix("0.0.0.0/0")
route1 := netaddr.MustParseIPPrefix("10.10.0.0/16")
route2 := netaddr.MustParseIPPrefix("10.11.0.0/16")
machine := Machine{
ID: 0,
MachineKey: "foo",
NodeKey: NodePublicKeyStripPrefix(nodeKey.Public()),
DiscoKey: "faa",
Hostname: "test",
NamespaceID: namespace.ID,
RegisterMethod: RegisterMethodAuthKey,
AuthKeyID: uint(pak.ID),
HostInfo: HostInfo{
RequestTags: []string{"tag:exit"},
RoutableIPs: []netaddr.IPPrefix{defaultRoute, route1, route2},
},
IPAddresses: []netaddr.IP{netaddr.MustParseIP("100.64.0.1")},
}
app.db.Save(&machine)
machine0ByID, err := app.GetMachineByID(0)
c.Assert(err, check.IsNil)
app.EnableAutoApprovedRoutes(machine0ByID)
c.Assert(machine0ByID.GetEnabledRoutes(), check.HasLen, 3)
}