Skip to content

Commit

Permalink
Release 0.13.2
Browse files Browse the repository at this point in the history
  • Loading branch information
wh1te909 committed Apr 25, 2022
2 parents 39e97c5 + d8dd3e1 commit 0999d98
Show file tree
Hide file tree
Showing 20 changed files with 129 additions and 101 deletions.
2 changes: 1 addition & 1 deletion api/tacticalrmm/accounts/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ def test_modify_api_key(self):
resp = self.client.put(url, data, format="json")
self.assertEqual(resp.status_code, 200)
apikey = APIKey.objects.get(pk=apikey.pk)
self.assertEquals(apikey.name, "New Name")
self.assertEqual(apikey.name, "New Name")

self.check_not_authenticated("put", url)

Expand Down
5 changes: 4 additions & 1 deletion api/tacticalrmm/agents/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,10 @@ def test_get_patch_policy(self):

def test_get_agent_versions(self):
url = "/agents/versions/"
r = self.client.get(url)

with self.assertNumQueries(1):
r = self.client.get(url)

self.assertEqual(r.status_code, 200)
assert any(i["hostname"] == self.agent.hostname for i in r.json()["agents"])

Expand Down
10 changes: 5 additions & 5 deletions api/tacticalrmm/agents/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,10 @@ def get(self, request):
else:
agents = (
Agent.objects.filter_by_role(request.user) # type: ignore
.select_related("site")
.defer(*AGENT_DEFER)
.select_related("site__client")
.filter(monitoring_type_filter)
.filter(client_site_filter)
.only("agent_id", "hostname", "site")
)
serializer = AgentHostnameSerializer(agents, many=True)

Expand Down Expand Up @@ -297,9 +297,9 @@ def post(self, request, agent_id):
@permission_classes([IsAuthenticated, AgentPerms])
def get_agent_versions(request):
agents = (
Agent.objects.filter_by_role(request.user) # type: ignore
.prefetch_related("site")
.only("pk", "hostname")
Agent.objects.defer(*AGENT_DEFER)
.filter_by_role(request.user) # type: ignore
.select_related("site__client")
)
return Response(
{
Expand Down
76 changes: 38 additions & 38 deletions api/tacticalrmm/alerts/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ def test_get_alerts(self):
data = {"top": 3}
resp = self.client.patch(url, data, format="json")
self.assertEqual(resp.status_code, 200)
self.assertEquals(resp.data["alerts"], AlertSerializer(alerts, many=True).data)
self.assertEquals(resp.data["alerts_count"], 10)
self.assertEqual(resp.data["alerts"], AlertSerializer(alerts, many=True).data)
self.assertEqual(resp.data["alerts_count"], 10)

# test filter data
# test data and result counts
Expand Down Expand Up @@ -409,87 +409,87 @@ def test_agent_gets_correct_alert_template(self):
core.server_policy = policy
core.save()

self.assertEquals(server.set_alert_template().pk, alert_templates[0].pk)
self.assertEquals(workstation.set_alert_template().pk, alert_templates[0].pk)
self.assertEqual(server.set_alert_template().pk, alert_templates[0].pk)
self.assertEqual(workstation.set_alert_template().pk, alert_templates[0].pk)

# assign second Alert Template to as default alert template
core.alert_template = alert_templates[1]
core.save()

self.assertEquals(workstation.set_alert_template().pk, alert_templates[1].pk)
self.assertEquals(server.set_alert_template().pk, alert_templates[1].pk)
self.assertEqual(workstation.set_alert_template().pk, alert_templates[1].pk)
self.assertEqual(server.set_alert_template().pk, alert_templates[1].pk)

# assign third Alert Template to client
workstation.client.alert_template = alert_templates[2]
server.client.alert_template = alert_templates[2]
workstation.client.save()
server.client.save()

self.assertEquals(workstation.set_alert_template().pk, alert_templates[2].pk)
self.assertEquals(server.set_alert_template().pk, alert_templates[2].pk)
self.assertEqual(workstation.set_alert_template().pk, alert_templates[2].pk)
self.assertEqual(server.set_alert_template().pk, alert_templates[2].pk)

# apply policy to client and should override
workstation.client.workstation_policy = policy
server.client.server_policy = policy
workstation.client.save()
server.client.save()

self.assertEquals(workstation.set_alert_template().pk, alert_templates[0].pk)
self.assertEquals(server.set_alert_template().pk, alert_templates[0].pk)
self.assertEqual(workstation.set_alert_template().pk, alert_templates[0].pk)
self.assertEqual(server.set_alert_template().pk, alert_templates[0].pk)

# assign fouth Alert Template to site
workstation.site.alert_template = alert_templates[3]
server.site.alert_template = alert_templates[3]
workstation.site.save()
server.site.save()

self.assertEquals(workstation.set_alert_template().pk, alert_templates[3].pk)
self.assertEquals(server.set_alert_template().pk, alert_templates[3].pk)
self.assertEqual(workstation.set_alert_template().pk, alert_templates[3].pk)
self.assertEqual(server.set_alert_template().pk, alert_templates[3].pk)

# apply policy to site
workstation.site.workstation_policy = policy
server.site.server_policy = policy
workstation.site.save()
server.site.save()

self.assertEquals(workstation.set_alert_template().pk, alert_templates[0].pk)
self.assertEquals(server.set_alert_template().pk, alert_templates[0].pk)
self.assertEqual(workstation.set_alert_template().pk, alert_templates[0].pk)
self.assertEqual(server.set_alert_template().pk, alert_templates[0].pk)

# apply policy to agents
workstation.policy = policy
server.policy = policy
workstation.save()
server.save()

self.assertEquals(workstation.set_alert_template().pk, alert_templates[0].pk)
self.assertEquals(server.set_alert_template().pk, alert_templates[0].pk)
self.assertEqual(workstation.set_alert_template().pk, alert_templates[0].pk)
self.assertEqual(server.set_alert_template().pk, alert_templates[0].pk)

# test disabling alert template
alert_templates[0].is_active = False
alert_templates[0].save()

self.assertEquals(workstation.set_alert_template().pk, alert_templates[3].pk)
self.assertEquals(server.set_alert_template().pk, alert_templates[3].pk)
self.assertEqual(workstation.set_alert_template().pk, alert_templates[3].pk)
self.assertEqual(server.set_alert_template().pk, alert_templates[3].pk)

# test policy exclusions
alert_templates[3].excluded_agents.set([workstation.pk])

self.assertEquals(workstation.set_alert_template().pk, alert_templates[2].pk)
self.assertEquals(server.set_alert_template().pk, alert_templates[3].pk)
self.assertEqual(workstation.set_alert_template().pk, alert_templates[2].pk)
self.assertEqual(server.set_alert_template().pk, alert_templates[3].pk)

# test workstation exclusions
alert_templates[2].exclude_workstations = True
alert_templates[2].save()

self.assertEquals(workstation.set_alert_template().pk, alert_templates[1].pk)
self.assertEquals(server.set_alert_template().pk, alert_templates[3].pk)
self.assertEqual(workstation.set_alert_template().pk, alert_templates[1].pk)
self.assertEqual(server.set_alert_template().pk, alert_templates[3].pk)

# test server exclusions
alert_templates[3].exclude_servers = True
alert_templates[3].save()

self.assertEquals(workstation.set_alert_template().pk, alert_templates[1].pk)
self.assertEquals(server.set_alert_template().pk, alert_templates[2].pk)
self.assertEqual(workstation.set_alert_template().pk, alert_templates[1].pk)
self.assertEqual(server.set_alert_template().pk, alert_templates[2].pk)

@patch("agents.tasks.sleep")
@patch("core.models.CoreSettings.send_mail")
Expand Down Expand Up @@ -523,7 +523,7 @@ def test_handle_agent_alerts(
# call outages task and no alert should be created
agent_outages_task()

self.assertEquals(Alert.objects.count(), 0)
self.assertEqual(Alert.objects.count(), 0)

# set overdue_dashboard_alert and alert should be created
agent_dashboard_alert.overdue_dashboard_alert = True
Expand Down Expand Up @@ -574,22 +574,22 @@ def test_handle_agent_alerts(
agent_outages_task()

# should have created 6 alerts
self.assertEquals(Alert.objects.count(), 6)
self.assertEqual(Alert.objects.count(), 6)

# other specific agents should have created alerts
self.assertEquals(Alert.objects.filter(agent=agent_dashboard_alert).count(), 1)
self.assertEquals(Alert.objects.filter(agent=agent_text_alert).count(), 1)
self.assertEquals(Alert.objects.filter(agent=agent_email_alert).count(), 1)
self.assertEquals(Alert.objects.filter(agent=agent_template_email).count(), 1)
self.assertEquals(
self.assertEqual(Alert.objects.filter(agent=agent_dashboard_alert).count(), 1)
self.assertEqual(Alert.objects.filter(agent=agent_text_alert).count(), 1)
self.assertEqual(Alert.objects.filter(agent=agent_email_alert).count(), 1)
self.assertEqual(Alert.objects.filter(agent=agent_template_email).count(), 1)
self.assertEqual(
Alert.objects.filter(agent=agent_template_dashboard).count(), 1
)
self.assertEquals(Alert.objects.filter(agent=agent_template_text).count(), 1)
self.assertEquals(Alert.objects.filter(agent=agent_template_blank).count(), 0)
self.assertEqual(Alert.objects.filter(agent=agent_template_text).count(), 1)
self.assertEqual(Alert.objects.filter(agent=agent_template_blank).count(), 0)

# check if email and text tasks were called
self.assertEquals(outage_email.call_count, 2)
self.assertEquals(outage_sms.call_count, 2)
self.assertEqual(outage_email.call_count, 2)
self.assertEqual(outage_sms.call_count, 2)

outage_sms.assert_any_call(
pk=Alert.objects.get(agent=agent_text_alert).pk, alert_interval=None
Expand Down Expand Up @@ -630,7 +630,7 @@ def test_handle_agent_alerts(

# calling agent outage task again shouldn't create duplicate alerts and won't send alerts
agent_outages_task()
self.assertEquals(Alert.objects.count(), 6)
self.assertEqual(Alert.objects.count(), 6)

# test periodic notification
# change email/text sent to sometime in the past
Expand Down Expand Up @@ -942,7 +942,7 @@ def test_handle_check_alerts(
send_email.assert_not_called()
send_sms.assert_not_called()

self.assertEquals(
self.assertEqual(
Alert.objects.filter(assigned_check=check_template_email).count(), 1
)

Expand Down Expand Up @@ -1244,7 +1244,7 @@ def test_handle_task_alerts(
send_email.assert_not_called()
send_sms.assert_not_called()

self.assertEquals(
self.assertEqual(
Alert.objects.filter(assigned_task=task_template_email).count(), 1
)

Expand Down
2 changes: 1 addition & 1 deletion api/tacticalrmm/apiv3/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def test_get_checks(self):
r = self.client.get(url)
self.assertEqual(r.status_code, 200)
self.assertEqual(r.data["check_interval"], 20)
self.assertEquals(len(r.data["checks"]), 2)
self.assertEqual(len(r.data["checks"]), 2)

url = "/api/v3/Maj34ACb324j234asdj2n34kASDjh34-DESKTOPTEST123/checkrunner/"
r = self.client.get(url)
Expand Down
4 changes: 2 additions & 2 deletions api/tacticalrmm/apiv3/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,9 @@ class TaskRunner(APIView):
permission_classes = [IsAuthenticated]

def get(self, request, pk, agentid):
_ = get_object_or_404(Agent, agent_id=agentid)
agent = get_object_or_404(Agent, agent_id=agentid)
task = get_object_or_404(AutomatedTask, pk=pk)
return Response(TaskGOGetSerializer(task).data)
return Response(TaskGOGetSerializer(task, context={"agent": agent}).data)

def patch(self, request, pk, agentid):
from alerts.models import Alert
Expand Down
20 changes: 10 additions & 10 deletions api/tacticalrmm/automation/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,29 +410,29 @@ def test_policy_related(self):
)

self.assertEqual(resp.status_code, 200)
self.assertEquals(len(resp.data["server_clients"]), 1)
self.assertEquals(len(resp.data["server_sites"]), 0)
self.assertEquals(len(resp.data["workstation_clients"]), 1)
self.assertEquals(len(resp.data["workstation_sites"]), 0)
self.assertEquals(len(resp.data["agents"]), 0)
self.assertEqual(len(resp.data["server_clients"]), 1)
self.assertEqual(len(resp.data["server_sites"]), 0)
self.assertEqual(len(resp.data["workstation_clients"]), 1)
self.assertEqual(len(resp.data["workstation_sites"]), 0)
self.assertEqual(len(resp.data["agents"]), 0)

# Add Site to Policy
policy.server_sites.add(server_agents[10].site)
policy.workstation_sites.add(workstation_agents[10].site)
resp = self.client.get(
f"/automation/policies/{policy.pk}/related/", format="json"
)
self.assertEquals(len(resp.data["server_sites"]), 1)
self.assertEquals(len(resp.data["workstation_sites"]), 1)
self.assertEquals(len(resp.data["agents"]), 0)
self.assertEqual(len(resp.data["server_sites"]), 1)
self.assertEqual(len(resp.data["workstation_sites"]), 1)
self.assertEqual(len(resp.data["agents"]), 0)

# Add Agent to Policy
policy.agents.add(server_agents[2])
policy.agents.add(workstation_agents[2])
resp = self.client.get(
f"/automation/policies/{policy.pk}/related/", format="json"
)
self.assertEquals(len(resp.data["agents"]), 2)
self.assertEqual(len(resp.data["agents"]), 2)

def test_getting_agent_policy_checks(self):

Expand All @@ -442,7 +442,7 @@ def test_getting_agent_policy_checks(self):
agent = baker.make_recipe("agents.agent", policy=policy)

# test policy assigned to agent
self.assertEquals(len(agent.get_checks_from_policies()), 7)
self.assertEqual(len(agent.get_checks_from_policies()), 7)

def test_getting_agent_policy_checks_with_enforced(self):
# setup data
Expand Down
4 changes: 3 additions & 1 deletion api/tacticalrmm/automation/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ class GetAddPolicies(APIView):
permission_classes = [IsAuthenticated, AutomationPolicyPerms]

def get(self, request):
policies = Policy.objects.all()
policies = Policy.objects.select_related("alert_template").prefetch_related(
"excluded_agents", "excluded_sites", "excluded_clients"
)

return Response(
PolicyTableSerializer(
Expand Down
5 changes: 3 additions & 2 deletions api/tacticalrmm/autotasks/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,14 @@ class TaskGOGetSerializer(serializers.ModelSerializer):
def get_task_actions(self, obj):
tmp = []
actions_to_remove = []
agent = self.context["agent"] if "agent" in self.context.keys() else obj.agent
for action in obj.actions:
if action["type"] == "cmd":
tmp.append(
{
"type": "cmd",
"command": Script.parse_script_args(
agent=obj.agent,
agent=agent,
shell=action["shell"],
args=[action["command"]],
)[0],
Expand All @@ -225,7 +226,7 @@ def get_task_actions(self, obj):
"script_name": script.name,
"code": script.code,
"script_args": Script.parse_script_args(
agent=obj.agent,
agent=agent,
shell=script.shell,
args=action["script_args"],
),
Expand Down
12 changes: 6 additions & 6 deletions api/tacticalrmm/checks/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -842,16 +842,16 @@ def test_handle_eventlog_check(self):

check_result = CheckResult.objects.get(assigned_check=check, agent=self.agent)

self.assertEquals(check.alert_severity, "warning")
self.assertEquals(check_result.status, "failing")
self.assertEqual(check.alert_severity, "warning")
self.assertEqual(check_result.status, "failing")

# test passing when contains
resp = self.client.patch(url, no_logs_data, format="json")
self.assertEqual(resp.status_code, 200)

check_result = CheckResult.objects.get(assigned_check=check, agent=self.agent)

self.assertEquals(check_result.status, "passing")
self.assertEqual(check_result.status, "passing")

# test failing when not contains and message and source
check.fail_when = "not_contains"
Expand All @@ -863,16 +863,16 @@ def test_handle_eventlog_check(self):

check_result = CheckResult.objects.get(assigned_check=check, agent=self.agent)

self.assertEquals(check_result.status, "failing")
self.assertEquals(check.alert_severity, "error")
self.assertEqual(check_result.status, "failing")
self.assertEqual(check.alert_severity, "error")

# test passing when contains with source and message
resp = self.client.patch(url, data, format="json")
self.assertEqual(resp.status_code, 200)

check_result = CheckResult.objects.get(assigned_check=check, agent=self.agent)

self.assertEquals(check_result.status, "passing")
self.assertEqual(check_result.status, "passing")


class TestCheckPermissions(TacticalTestCase):
Expand Down
Loading

0 comments on commit 0999d98

Please sign in to comment.