diff --git a/api/djangormm/agents/tests.py b/api/djangormm/agents/tests.py index ce2e5389..752d87ef 100644 --- a/api/djangormm/agents/tests.py +++ b/api/djangormm/agents/tests.py @@ -1,15 +1,99 @@ from djangormm.test import BaseTestCase +from .serializers import AgentSerializer +from .models import Agent + class TestAgentViews(BaseTestCase): + def test_agents_list(self): + url = "/agents/listagents/" - def test_agents_list_GET(self): - response = self.client.get("/agents/listagents/") - self.assertEqual(response.status_code, 200) - self.client.logout() - response = self.client.get("/agents/listagents/") - self.assertEqual(response.status_code, 401) + r = self.client.get(url) + self.assertEqual(r.status_code, 200) - def test_agents_agent_detail_GET(self): - response = self.client.get(f"/agents/{self.agent.pk}/agentdetail/") - self.assertEqual(response.status_code, 200) \ No newline at end of file + self.check_not_authenticated("get", url) + + def test_agents_agent_detail(self): + url = f"/agents/{self.agent.pk}/agentdetail/" + + r = self.client.get(url) + self.assertEqual(r.status_code, 200) + + self.check_not_authenticated("get", url) + + def test_edit_agent(self): + url = "/agents/editagent/" + + edit = { + "pk": self.agent.pk, + "client": "Facebook", + "site": "NY Office", + "montype": "workstation", + "desc": "asjdk234andasd", + "overduetime": 300, + "pinginterval": 60, + "emailalert": True, + "textalert": False, + } + r = self.client.patch(url, edit, format="json") + self.assertEqual(r.status_code, 200) + + agent = Agent.objects.get(pk=self.agent.pk) + data = AgentSerializer(agent).data + self.assertEqual(data["site"], "NY Office") + + self.check_not_authenticated("patch", url) + + def test_meshcentral_tabs(self): + url = f"/agents/{self.agent.pk}/meshtabs/" + + r = self.client.get(url) + self.assertIn("viewmode", r.data["fileurl"]) + + self.check_not_authenticated("get", url) + + def test_takecontrols(self): + url = f"/agents/{self.agent.pk}/takecontrol/" + + r = self.client.get(url) + self.assertIn("hide", r.data) + + self.check_not_authenticated("get", url) + + def test_by_client(self): + url = "/agents/byclient/Google/" + + r = self.client.get(url) + self.assertEqual(r.status_code, 200) + self.assertTrue(r.data) + + url = f"/agents/byclient/Majh3 Akj34 ad/" + r = self.client.get(url) + self.assertFalse(r.data) # returns empty list + + self.check_not_authenticated("get", url) + + def test_by_site(self): + url = f"/agents/bysite/Google/Main Office/" + + r = self.client.get(url) + self.assertEqual(r.status_code, 200) + self.assertTrue(r.data) + + url = f"/agents/bysite/Google/Ajdaksd Office/" + r = self.client.get(url) + self.assertFalse(r.data) + + self.check_not_authenticated("get", url) + + def test_overdue_action(self): + url = "/agents/overdueaction/" + + payload = {"pk": self.agent.pk, "alertType": "email", "action": "enabled"} + r = self.client.post(url, payload, format="json") + self.assertEqual(r.status_code, 200) + + agent = Agent.objects.get(pk=self.agent.pk) + self.assertTrue(agent.overdue_email_alert) + + self.check_not_authenticated("post", url) diff --git a/api/djangormm/djangormm/test.py b/api/djangormm/djangormm/test.py index f73d7829..02f1782b 100644 --- a/api/djangormm/djangormm/test.py +++ b/api/djangormm/djangormm/test.py @@ -5,7 +5,7 @@ from rest_framework.test import force_authenticate from accounts.models import User from agents.models import Agent - +from clients.models import Client, Site class BaseTestCase(TestCase): def setUp(self): @@ -65,3 +65,27 @@ class BaseTestCase(TestCase): mesh_node_id="abcdefghijklmnopAABBCCDD77443355##!!AI%@#$%#*", status="online", ) + + Client.objects.create(client="Google") + Client.objects.create(client="Facebook") + google = Client.objects.get(client="Google") + facebook = Client.objects.get(client="Facebook") + Site.objects.create(client=google, site="Main Office") + Site.objects.create(client=google, site="LA Office") + Site.objects.create(client=google, site="MO Office") + Site.objects.create(client=facebook, site="Main Office") + Site.objects.create(client=facebook, site="NY Office") + + + + def check_not_authenticated(self, method, url): + self.client.logout() + switch = { + "get": self.client.get(url), + "post": self.client.post(url), + "put": self.client.put(url), + "patch": self.client.patch(url), + "delete": self.client.delete(url) + } + r = switch.get(method) + self.assertEqual(r.status_code, 401) \ No newline at end of file