[api] RESTful api for Subnet and IpAddress #30
Conversation
Pull Request Test Coverage Report for Build 98
💛 - Coveralls |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@anurag-ks where are the tests and the reusable views?
@nemesisdesign I first wanted to confirm these endpoints are okay or not and then move on to writing tests for these views. |
@anurag-ks you have to implement CRUD operations as described in http://openwisp.org/gsoc/ideas-2018.html |
7a51b50
to
c82473a
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good progress. See below.
- could you avoid
_create_subnet(dict(subnet="10.0.0.0/24"))
in favour of_create_subnet(subnet="10.0.0.0/24")
? See how is done here: https://github.com/openwisp/openwisp-controller/blob/connections/openwisp_controller/connection/tests/base.py#L18-L30 - please add tests for the sample app as well to ensure the generic views work with custom models as well.
- please add a test for the case in which a user is not logged-in
- see my inline comments as well
django_ipam/tests/base/test_api.py
Outdated
def test_read_subnet_api(self): | ||
subnet_id = self._create_subnet(dict(subnet="fdb6:21b:a477::9f7/64")).id | ||
response = self.client.get(reverse('ipam:subnet', args=(subnet_id,))) | ||
self.assertEqual(response.status_code, 200) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
perform a basic check of the contents also please
django_ipam/tests/base/test_api.py
Outdated
subnet_id = self._create_subnet(dict(subnet="10.0.0.0/32")).id | ||
response = self.client.delete(reverse('ipam:subnet', args=(subnet_id,))) | ||
self.assertEqual(response.status_code, 204) | ||
self.assertEqual(len(Subnet.objects.all()), 0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use Subnet.objects.count()
which is faster
django_ipam/tests/base/test_api.py
Outdated
data=json.dumps(data), | ||
content_type='application/json') | ||
self.assertEqual(response.status_code, 201) | ||
self.assertEqual(str(IpAddress.objects.first()), '10.0.0.2') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
access the ip_address
attribute because str()
representation may be changed and would break this test
django_ipam/tests/base/test_api.py
Outdated
subnet = self._create_subnet(dict(subnet="10.0.0.0/24")) | ||
ip_address = self._create_ipaddress(dict(ip_address="10.0.0.1", subnet=subnet)) | ||
response = self.client.get(reverse('ipam:ip_address', args=(ip_address.id,))) | ||
self.assertEqual(response.status_code, 200) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
perform a basic check of the contents please
django_ipam/tests/base/test_api.py
Outdated
ip_address = self._create_ipaddress(dict(ip_address="10.0.0.1", subnet=subnet)) | ||
response = self.client.delete(reverse('ipam:ip_address', args=(ip_address.id,))) | ||
self.assertEqual(response.status_code, 204) | ||
self.assertEqual(len(IpAddress.objects.all()), 0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use count as indicated in a previous comment
django_ipam/tests/base/test_api.py
Outdated
self._create_ipaddress(dict(ip_address="10.0.0.1", subnet=subnet)) | ||
self._create_ipaddress(dict(ip_address="10.0.0.2", subnet=subnet)) | ||
response = self.client.get(reverse('ipam:list_create_ip_address', args=(subnet.id,))) | ||
self.assertEqual(response.status_code, 200) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please perform a basic check of the contents too
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
some minor improvements to the tests are needed, see see my comments
@@ -13,4 +14,10 @@ class Meta: | |||
class IpAddressSerializer(serializers.ModelSerializer): | |||
class Meta: | |||
model = IpAddress | |||
fields = ('ip_address', 'subnet', 'description') | |||
fields = '__all__' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you should make some fields readonly like created
and modified
class SubnetSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = Subnet | ||
fields = '__all__' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you should make some fields readonly like created
and modified
django_ipam/tests/base/test_api.py
Outdated
subnet_id = self._create_subnet(subnet="fdb6:21b:a477::9f7/64").id | ||
response = self.client.get(reverse('ipam:subnet', args=(subnet_id,))) | ||
self.assertEqual(response.status_code, 200) | ||
self.assertContains(response, str(subnet_id)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is not good enough, you should check one of the attributes of the JSON response in response.data
.
django_ipam/tests/base/test_api.py
Outdated
ip_address = self._create_ipaddress(ip_address="10.0.0.1", subnet=subnet) | ||
response = self.client.get(reverse('ipam:ip_address', args=(ip_address.id,))) | ||
self.assertEqual(response.status_code, 200) | ||
self.assertContains(response, '10.0.0.1') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here
django_ipam/tests/base/test_api.py
Outdated
def test_update_subnet_api(self): | ||
data = { | ||
'description': 'Test Subnet' | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
write this as one line
django_ipam/tests/base/test_api.py
Outdated
response = self.client.get(reverse('ipam:list_create_ip_address', args=(subnet.id,))) | ||
self.assertEqual(response.status_code, 200) | ||
self.assertContains(response, '10.0.0.1') | ||
self.assertContains(response, '10.0.0.2') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here
django_ipam/tests/base/test_api.py
Outdated
'description': 'Test Subnet' | ||
} | ||
response = self.client.post(reverse('ipam:subnet_list_create'), | ||
data=json.dumps(data), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
simply use data=json.dumps({}),
and remove the definition of data
c680bd7
to
b742fd5
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, only docs are missing:
- explain how to use new api methods
- remember to explain how to authenticate
- explain how to extend the views, take inspiration from the docs of django-netjsongraph
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some improvements needed, see below.
README.rst
Outdated
|
||
.. code-block:: text | ||
|
||
<api-url> -H 'Cookie: csrftoken=<csrftoken>; sessionid=<session-id>' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no man, this is not good. the API must work without the CSRF mechanism, this is not a webpage.
Ensure the API works without the CSRF token
README.rst
Outdated
An api enpoint to retrieve or create IP addresses under a specific subnet | ||
|
||
GET | ||
++++ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be +++
README.rst
Outdated
@@ -168,6 +178,176 @@ Response | |||
"description": "optional description" | |||
} | |||
|
|||
IpAddress-Subnet List and Create View | |||
##################################### | |||
An api enpoint to retrieve or create IP addresses under a specific subnet |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
always leave a blank line after the title
README.rst
Outdated
|
||
GET | ||
++++ | ||
Returns the list of IP addresses under a particular subnet. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
always leave a blank line after the title
README.rst
Outdated
|
||
POST | ||
++++ | ||
Create an Ip Address record |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
always leave a blank line after the title
README.rst
Outdated
|
||
DELETE | ||
++++++ | ||
Delete a subnet instance |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Leave blank line after title, add final dot, double quote Subnet
(model class name).
README.rst
Outdated
|
||
PUT | ||
+++ | ||
Update details of a subnet instance |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here
README.rst
Outdated
|
||
GET | ||
+++ | ||
Get details of an IP address instance |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here
README.rst
Outdated
|
||
DELETE | ||
++++++ | ||
Delete an IP address instance |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same
README.rst
Outdated
|
||
PUT | ||
+++ | ||
Update details of an IP address instance |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
Implements and closes #10
This is a WIP PR, I wanted to confirm all the endpoints that we would be implementing in django-ipam.