From aad32c48662738f4365c3a75688ca085590b5a75 Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Thu, 29 Oct 2020 16:05:59 -0400 Subject: [PATCH] Add tests for UserConfig API endpoint --- netbox/users/tests/test_api.py | 58 ++++++++++++++++++++++++++++++++-- 1 file changed, 55 insertions(+), 3 deletions(-) diff --git a/netbox/users/tests/test_api.py b/netbox/users/tests/test_api.py index c4229bff9..7530b1d2d 100644 --- a/netbox/users/tests/test_api.py +++ b/netbox/users/tests/test_api.py @@ -1,11 +1,10 @@ from django.contrib.auth.models import Group, User from django.contrib.contenttypes.models import ContentType -from django.test import override_settings from django.urls import reverse -from rest_framework import status from users.models import ObjectPermission -from utilities.testing import APIViewTestCases, APITestCase, disable_warnings +from utilities.testing import APIViewTestCases, APITestCase +from utilities.utils import deepmerge class AppTest(APITestCase): @@ -132,3 +131,56 @@ class ObjectPermissionTest(APIViewTestCases.APIViewTestCase): 'constraints': {'name': 'TEST6'}, }, ] + + +class UserConfigTest(APITestCase): + + def test_get(self): + """ + Retrieve user configuration via GET request. + """ + userconfig = self.user.config + url = reverse('users-api:userconfig-list') + + response = self.client.get(url, **self.header) + self.assertEqual(response.data, {}) + + data = { + "a": 123, + "b": 456, + "c": 789, + } + userconfig.data = data + userconfig.save() + response = self.client.get(url, **self.header) + self.assertEqual(response.data, data) + + def test_patch(self): + """ + Set user config via PATCH requests. + """ + userconfig = self.user.config + url = reverse('users-api:userconfig-list') + + data = { + "a": { + "a1": "X", + "a2": "Y", + }, + "b": { + "b1": "Z", + } + } + response = self.client.patch(url, data=data, format='json', **self.header) + self.assertDictEqual(response.data, data) + userconfig.refresh_from_db() + self.assertDictEqual(userconfig.data, data) + + update_data = { + "c": 123 + } + response = self.client.patch(url, data=update_data, format='json', **self.header) + new_data = deepmerge(data, update_data) + self.assertDictEqual(response.data, new_data) + userconfig.refresh_from_db() + self.assertDictEqual(userconfig.data, new_data)