mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
Added tests for assigning tags during POST/PATCH
This commit is contained in:
60
netbox/extras/tests/test_tags.py
Normal file
60
netbox/extras/tests/test_tags.py
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
from django.contrib.auth.models import User
|
||||||
|
from django.urls import reverse
|
||||||
|
from rest_framework import status
|
||||||
|
from rest_framework.test import APITestCase
|
||||||
|
|
||||||
|
from dcim.models import Site
|
||||||
|
from users.models import Token
|
||||||
|
from utilities.testing import HttpStatusMixin
|
||||||
|
|
||||||
|
|
||||||
|
class TaggedItemTest(HttpStatusMixin, APITestCase):
|
||||||
|
"""
|
||||||
|
Test the application of Tags to and item (a Site, for example) upon creation (POST) and modification (PATCH).
|
||||||
|
"""
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
|
||||||
|
user = User.objects.create(username='testuser', is_superuser=True)
|
||||||
|
token = Token.objects.create(user=user)
|
||||||
|
self.header = {'HTTP_AUTHORIZATION': 'Token {}'.format(token.key)}
|
||||||
|
|
||||||
|
def test_create_tagged_item(self):
|
||||||
|
|
||||||
|
data = {
|
||||||
|
'name': 'Test Site',
|
||||||
|
'slug': 'test-site',
|
||||||
|
'tags': ['Foo', 'Bar', 'Baz']
|
||||||
|
}
|
||||||
|
|
||||||
|
url = reverse('dcim-api:site-list')
|
||||||
|
response = self.client.post(url, data, format='json', **self.header)
|
||||||
|
|
||||||
|
self.assertHttpStatus(response, status.HTTP_201_CREATED)
|
||||||
|
self.assertEqual(sorted(response.data['tags']), sorted(data['tags']))
|
||||||
|
site = Site.objects.get(pk=response.data['id'])
|
||||||
|
tags = [tag.name for tag in site.tags.all()]
|
||||||
|
self.assertEqual(sorted(tags), sorted(data['tags']))
|
||||||
|
|
||||||
|
def test_update_tagged_item(self):
|
||||||
|
|
||||||
|
site = Site.objects.create(
|
||||||
|
name='Test Site',
|
||||||
|
slug='test-site',
|
||||||
|
tags=['Foo', 'Bar', 'Baz']
|
||||||
|
)
|
||||||
|
|
||||||
|
data = {
|
||||||
|
'tags': ['Foo', 'Bar', 'New Tag']
|
||||||
|
}
|
||||||
|
|
||||||
|
url = reverse('dcim-api:site-detail', kwargs={'pk': site.pk})
|
||||||
|
response = self.client.patch(url, data, format='json', **self.header)
|
||||||
|
|
||||||
|
self.assertHttpStatus(response, status.HTTP_200_OK)
|
||||||
|
self.assertEqual(sorted(response.data['tags']), sorted(data['tags']))
|
||||||
|
site = Site.objects.get(pk=response.data['id'])
|
||||||
|
tags = [tag.name for tag in site.tags.all()]
|
||||||
|
self.assertEqual(sorted(tags), sorted(data['tags']))
|
Reference in New Issue
Block a user