1 from datetime import timedelta

2

3 from django.utils import timezone

4

5 from meshadmin.server.networks.models import CA, Host

6

7

8 class TestCAModel:

9 def test_days_until_expiry(self, test_network):

10 test_net = test_network(name="testnet", cidr="100.100.64.0/24")

11 expired_ca = CA.objects.create(

12 network=test_net,

13 name="expired_ca",

14 cert="test_cert",

15 key="test_key",

16 cert_print={

17 "details": {

18 "notAfter": (timezone.now() - timedelta(days=1)).isoformat()

19 }

20 },

21 )

22 assert expired_ca.days_until_expiry == 0

23 warning_ca = CA.objects.create(

24 network=test_net,

25 name="warning_ca",

26 cert="test_cert",

27 key="test_key",

28 cert_print={

29 "details": {

30 "notAfter": (timezone.now() + timedelta(days=15)).isoformat()

31 }

32 },

33 )

34 assert warning_ca.days_until_expiry == 14

35 no_expiry_ca = CA.objects.create(

36 network=test_net,

37 name="no_expiry_ca",

38 cert="test_cert",

39 key="test_key",

40 cert_print={"details": {}},

41 )

42 assert no_expiry_ca.days_until_expiry is None

43 invalid_date_ca = CA.objects.create(

44 network=test_net,

45 name="invalid_date_ca",

46 cert="test_cert",

47 key="test_key",

48 cert_print={"details": {"notAfter": "invalid-date"}},

49 )

50 assert invalid_date_ca.days_until_expiry is None

51

52

53 class TestHostModel:

54 def test_is_cli_version_outdated(self, test_network):

55 test_net = test_network(name="testnet", cidr="100.100.64.0/24")

56 host = Host.objects.create(name="test_host", network=test_net)

57 assert host.is_cli_version_outdated == "Unknown"

58 host.cli_version = "0.0.0"

59 host.save()

60 host.refresh_from_db()

61 assert host.is_cli_version_outdated is True