Skip to content

Commit

Permalink
[api] Improved tests for list api view
Browse files Browse the repository at this point in the history
  • Loading branch information
pandafy committed Jun 18, 2020
1 parent dcd0868 commit 7597720
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 23 deletions.
2 changes: 1 addition & 1 deletion openwisp_notifications/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class NotificationSerializer(serializers.ModelSerializer):

class Meta:
model = Notification
fields = ['id', 'message', 'unread', 'notification_url']
fields = ['id', 'message', 'unread', 'notification_url', 'email_subject']

def get_notification_url(self, obj):
url = reverse(
Expand Down
86 changes: 64 additions & 22 deletions openwisp_notifications/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,73 @@ def setUp(self):

def test_list_notification_api(self):
number_of_notifications = 20
url = reverse(f'{self.app_label}:api_list_notifications')
for _ in range(number_of_notifications):
notify.send(sender=self.admin, type='default', target=self.admin)

url = reverse(f'{self.app_label}:api_list_notifications')
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data['count'], number_of_notifications)
self.assertEqual(
response.data['next'],
f'http://testserver/api/v1/{self.app_label}/notifications/?page=2',
)
self.assertEqual(response.data['previous'], None)
self.assertEqual(len(response.data['results']), 10)

next_response = self.client.get(response.data['next'])
self.assertEqual(next_response.status_code, 200)
self.assertEqual(next_response.data['count'], number_of_notifications)
self.assertEqual(
next_response.data['next'], None,
)
self.assertEqual(
next_response.data['previous'],
f'http://testserver/api/v1/{self.app_label}/notifications/',
)
self.assertEqual(len(next_response.data['results']), 10)
with self.subTest('Test page query in notification list view'):
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data['count'], number_of_notifications)
self.assertEqual(
response.data['next'],
f'http://testserver/api/v1/{self.app_label}/notifications/?page=2',
)
self.assertEqual(response.data['previous'], None)
self.assertEqual(len(response.data['results']), 10)

next_response = self.client.get(response.data['next'])
self.assertEqual(next_response.status_code, 200)
self.assertEqual(next_response.data['count'], number_of_notifications)
self.assertEqual(
next_response.data['next'], None,
)
self.assertEqual(
next_response.data['previous'],
f'http://testserver/api/v1/{self.app_label}/notifications/',
)
self.assertEqual(len(next_response.data['results']), 10)

with self.subTest('Test page_size query'):
page_size = 5
url = f'{url}?page_size={page_size}'
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data['count'], number_of_notifications)
self.assertEqual(
response.data['next'],
f'http://testserver/api/v1/{self.app_label}/notifications/'
f'?page=2&page_size={page_size}',
)
self.assertEqual(response.data['previous'], None)
self.assertEqual(len(response.data['results']), page_size)

next_response = self.client.get(response.data['next'])
self.assertEqual(next_response.status_code, 200)
self.assertEqual(next_response.data['count'], number_of_notifications)
self.assertEqual(
next_response.data['next'],
f'http://testserver/api/v1/{self.app_label}/notifications/'
f'?page=3&page_size={page_size}',
)
self.assertEqual(
next_response.data['previous'],
f'http://testserver/api/v1/{self.app_label}/notifications/'
f'?page_size={page_size}',
)
self.assertEqual(len(next_response.data['results']), page_size)

with self.subTest('Test individual result object'):
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
n = response.data['results'][0]
self.assertIn('id', n)
self.assertIn('message', n)
self.assertTrue(n['unread'])
self.assertIn('notification_url', n)
self.assertEqual(
n['email_subject'], '[example.com] Default Notification Subject'
)

def test_read_notification_api(self):
notify.send(sender=self.admin, type='default', target=self.admin)
Expand Down

0 comments on commit 7597720

Please sign in to comment.