logs tests

This commit is contained in:
sadnub 2020-09-25 10:32:56 -04:00
parent 3e01dcf3be
commit e79c61626a
3 changed files with 133 additions and 4 deletions

View File

@ -179,7 +179,7 @@ class CoreSettings(models.Model):
# readlines() returns an array. Get first item
mesh_settings["mesh_token"] = key[0].rstrip()
break
except IOError:
except (IOError, IndexError):
pass
counter = counter + 1

View File

@ -1,3 +1,105 @@
from django.test import TestCase
from tacticalrmm.test import PlainTestCase
from logs.models import AuditLog
from django.utils import timezone as djangotime
class TestAuditViews(PlainTestCase):
def setUp(self):
self.authenticate()
self.setup_coresettings()
def create_audit_records(self):
# create new records
AuditLog.audit_mesh_session("jim", "AgentHostname1")
AuditLog.audit_raw_command("jim", "AgentHostname2", "whoami", "batch")
AuditLog.audit_object_changed("jim", "policy", {}, {}, "Policy Name")
AuditLog.audit_object_add("jim", "policy", {}, "Another Policy")
AuditLog.audit_object_delete("jim", "policy", {}, "Another Policy")
AuditLog.audit_object_delete("james", "agent", {}, "AgentHostname1")
AuditLog.audit_script_run("james", "AgentHostname2", "Script Name")
AuditLog.audit_user_failed_login("james")
AuditLog.audit_user_failed_twofactor("james")
AuditLog.audit_user_login_successful("jim")
# create some old records
log1 = AuditLog.objects.create(
username="james",
agent="AgentHostname1",
action="execute_command",
object_type="agent",
message="jim issued powershell command on AgentHostname"
)
log2 = AuditLog.objects.create(
username="jim",
action="login",
object_type="user",
message="jim logged in successfully"
)
log3 = AuditLog.objects.create(
username="jim",
agent="AgentHostname2",
action="execute_script",
object_type="agent",
message='jim ran script: "Script Name" on AgentHostname'
)
log4 = AuditLog.objects.create(
username="james",
agent="AgentHostname1",
action="execute_command",
object_type="agent",
message="jim issued powershell command on AgentHostname"
)
log1.entry_time = djangotime.now() - djangotime.timedelta(days=15)
log1.save()
log2.entry_time = djangotime.now() - djangotime.timedelta(days=31)
log2.save()
log3.entry_time = djangotime.now() - djangotime.timedelta(days=130)
log3.save()
log4.entry_time = djangotime.now() - djangotime.timedelta(days=365)
log4.save()
def test_get_audit_logs(self):
url = "/logs/auditlogs/"
# create data
self.create_audit_records()
# test data and result counts
data = [
{
"filter": {"timeFilter": 30}, "count": 11
},
{
"filter": {"timeFilter": 45, "agentFilter": ["AgentHostname2"]}, "count": 2
},
{
"filter": {"userFilter": ["jim"], "agentFilter": ["AgentHostname1"]}, "count": 1
},
{
"filter": {"timeFilter": 180, "userFilter": ["james"], "agentFilter": ["AgentHostname1"]}, "count": 1
},
{
"filter": {}, "count": 14
},
{
"filter": {"agentFilter": ["DoesntExist"]}, "count": 0
},
{
"filter": {"timeFilter": 35, "userFilter": ["james", "jim"], "agentFilter": ["AgentHostname1", "AgentHostname2"]}, "count": 4
},
{
"filter": {"timeFilter": 35, "userFilter": ["james", "jim"]}, "count": 12
},
]
for req in data:
resp = self.client.patch(url, req["filter"], format="json")
self.assertEqual(resp.status_code, 200)
self.assertEqual(len(resp.data), req["count"])
self.check_not_authenticated("patch", url)
# Create your tests here.

View File

@ -1,7 +1,7 @@
import random
import string
from django.test import TestCase
from django.test import TestCase, override_settings
from django.utils import timezone as djangotime
from rest_framework.test import APIClient
@ -18,6 +18,33 @@ from checks.models import Check
from autotasks.models import AutomatedTask
class PlainTestCase(TestCase):
def authenticate(self):
self.john = User(username="john")
self.john.set_password("password")
self.john.save()
self.client = APIClient()
self.client.force_authenticate(user=self.john)
# fixes tests waiting 2 minutes for mesh token to appear
@override_settings(MESH_TOKEN_KEY="123456")
def setup_coresettings(self):
self.coresettings = CoreSettings.objects.create()
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)
class BaseTestCase(TestCase):
def setUp(self):