Skip to content

Commit

Permalink
fix serializer/tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wh1te909 committed Feb 29, 2024
1 parent 12dee4d commit 7911235
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 24 deletions.
31 changes: 11 additions & 20 deletions api/tacticalrmm/core/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,28 @@
from .models import CodeSignToken, CoreSettings, CustomField, GlobalKVStore, URLAction


class CoreSettingsSerializer(serializers.ModelSerializer):
class HostedCoreMixin:
def to_representation(self, instance):
ret = super().to_representation(instance) # type: ignore
if getattr(settings, "HOSTED", False):
for field in ("mesh_site", "mesh_token", "mesh_username"):
ret[field] = "n/a"
return ret


class CoreSettingsSerializer(HostedCoreMixin, serializers.ModelSerializer):
all_timezones = serializers.SerializerMethodField("all_time_zones")
mesh_site = serializers.SerializerMethodField()
mesh_token = serializers.SerializerMethodField()
mesh_username = serializers.SerializerMethodField()

def all_time_zones(self, obj):
return ALL_TIMEZONES

def get_mesh_site(self, obj):
if getattr(settings, "HOSTED", False):
return "n/a"
return obj.mesh_site

def get_mesh_token(self, obj):
if getattr(settings, "HOSTED", False):
return "n/a"
return obj.mesh_token

def get_mesh_username(self, obj):
if getattr(settings, "HOSTED", False):
return "n/a"
return obj.mesh_username

class Meta:
model = CoreSettings
fields = "__all__"


# for audting
class CoreSerializer(serializers.ModelSerializer):
class CoreSerializer(HostedCoreMixin, serializers.ModelSerializer):
class Meta:
model = CoreSettings
fields = "__all__"
Expand Down
46 changes: 43 additions & 3 deletions api/tacticalrmm/core/tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
from unittest.mock import patch

import requests
Expand Down Expand Up @@ -109,18 +110,57 @@ def test_get_core_settings(self):

def test_edit_coresettings(self):
url = "/core/settings/"

# setup
baker.make("automation.Policy", _quantity=2)
# test normal request
data = {
"smtp_from_email": "newexample@example.com",
"mesh_token": "New_Mesh_Token",
"mesh_site": "https://mesh.example.com",
"mesh_username": "bob",
}
r = self.client.put(url, data)
self.assertEqual(r.status_code, 200)
self.assertEqual(get_core_settings().smtp_from_email, data["smtp_from_email"])
self.assertEqual(get_core_settings().mesh_token, data["mesh_token"])
core = get_core_settings()
self.assertEqual(core.smtp_from_email, "newexample@example.com")
self.assertEqual(core.mesh_token, "New_Mesh_Token")
self.assertEqual(core.mesh_site, "https://mesh.example.com")
self.assertEqual(core.mesh_username, "bob")

# test to_representation
r = self.client.get(url)
self.assertEqual(r.data["smtp_from_email"], "newexample@example.com")
self.assertEqual(r.data["mesh_token"], "New_Mesh_Token")
self.assertEqual(r.data["mesh_site"], "https://mesh.example.com")
self.assertEqual(r.data["mesh_username"], "bob")

self.check_not_authenticated("put", url)

@override_settings(HOSTED=True)
def test_hosted_edit_coresettings(self):
url = "/core/settings/"
baker.make("automation.Policy", _quantity=2)
data = {
"smtp_from_email": "newexample1@example.com",
"mesh_token": "abc123",
"mesh_site": "https://mesh15534.example.com",
"mesh_username": "jane",
}
r = self.client.put(url, data)
self.assertEqual(r.status_code, 200)
core = get_core_settings()
self.assertEqual(core.smtp_from_email, "newexample1@example.com")
self.assertIn("41410834b8bb4481446027f8", core.mesh_token) # type: ignore
if "GHACTIONS" in os.environ:
self.assertEqual(core.mesh_site, "https://example.com")
self.assertEqual(core.mesh_username, "pipeline")

# test to_representation
r = self.client.get(url)
self.assertEqual(r.data["smtp_from_email"], "newexample1@example.com")
self.assertEqual(r.data["mesh_token"], "n/a")
self.assertEqual(r.data["mesh_site"], "n/a")
self.assertEqual(r.data["mesh_username"], "n/a")

self.check_not_authenticated("put", url)

Expand Down
9 changes: 8 additions & 1 deletion api/tacticalrmm/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,15 @@ def get(self, request):
return Response(CoreSettingsSerializer(settings).data)

def put(self, request):
data = request.data.copy()

if getattr(settings, "HOSTED", False):
data.pop("mesh_site")
data.pop("mesh_token")
data.pop("mesh_username")

coresettings = CoreSettings.objects.first()
serializer = CoreSettingsSerializer(instance=coresettings, data=request.data)
serializer = CoreSettingsSerializer(instance=coresettings, data=data)
serializer.is_valid(raise_exception=True)
serializer.save()
sync_mesh_perms_task.delay()
Expand Down

0 comments on commit 7911235

Please sign in to comment.