diff --git a/instances/tests/test_index_host_alerts.py b/instances/tests/test_index_host_alerts.py index a81bf425..898176e6 100644 --- a/instances/tests/test_index_host_alerts.py +++ b/instances/tests/test_index_host_alerts.py @@ -68,3 +68,17 @@ class IndexHostAlertTests(TestCase): response = self.get(viewer, down=set()) self.assertContains(response, "mine-host") self.assertNotContains(response, reverse("overview", args=[self.mine.id])) + + def test_a_users_list_reads_only_the_hosts_of_their_vms(self): + read = [] + + def status(compute): + read.append(compute.name) + return True + + self.client.force_login(self.user) + with patch("instances.views.utils.refr") as refr, patch.object(Compute, "status", property(status)), \ + patch.object(Instance, "info", new_callable=PropertyMock, return_value=None): + self.assertEqual(self.client.get(reverse("instances:index")).status_code, 200) + self.assertEqual([call.args[0].name for call in refr.call_args_list], ["mine-host"]) + self.assertNotIn("other-host", read) diff --git a/instances/views.py b/instances/views.py index b843fe0a..e34415d0 100755 --- a/instances/views.py +++ b/instances/views.py @@ -79,7 +79,8 @@ STATS_SAMPLE_SIGNER = signing.TimestampSigner(salt="instances.stats-sample") def index(request): instances = None - computes = list(Compute.objects.all().order_by("name")) + # a user who sees no hosts makes the panel read only those of their own VMs + computes = _listed_computes(request.user, list(Compute.objects.all().order_by("name"))) for compute in computes: utils.refr(compute) @@ -98,8 +99,7 @@ def index(request): ).order_by("name").prefetch_related("userinstance_set__user")) # other users get no host names or connection errors, only whether # a host of their own VMs is down - own = {inst.compute_id for inst in instances} - own_hosts_down = any(c.status is not True for c in computes if c.pk in own) + own_hosts_down = any(c.status is not True for c in computes) utils.read_list_rows(instances) return render(