Skip to content

Commit

Permalink
increase test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
JDTobin committed Nov 14, 2023
1 parent 9b8cdd6 commit cb0db79
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 5 deletions.
18 changes: 15 additions & 3 deletions app/es_api/tests/test_views_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def test_search_index_with_keyword(self):
Test that the /es-api/ endpoint succeeds when a valid
keyword is provided
"""
url = "%s?keyword=hello" % (reverse('es_api:search-index'))
url = "%s?keyword=hello&p=1&sort=1" % (reverse('es_api:search-index'))
with patch('es_api.views.XSEQueries') as query, \
patch('es_api.views.SearchFilter.objects') as sfObj, \
patch('es_api.views.XDSConfiguration.objects'):
Expand Down Expand Up @@ -83,7 +83,7 @@ def test_filters(self):

url = "%s?Course.CourseTitle=hi" % (reverse('es_api:filters')) + \
"&Course.CourseProviderName=" \
"test&CourseInstance.CourseLevel=3"
"test&CourseInstance.CourseLevel=3&p=1"
with patch('es_api.views.XSEQueries') as query, \
patch('es_api.views.XDSConfiguration.objects'):
result_json = json.dumps({"test": "value"})
Expand Down Expand Up @@ -141,6 +141,18 @@ def test_suggestions_exception(self):
self.assertEqual(response.status_code,
status.HTTP_500_INTERNAL_SERVER_ERROR)

def test_suggestions_missing(self):
"""
Test that the /es-api/suggest? endpoint returns a bad request
when an missing partial info
"""
url = "%s?partial=" % (reverse('es_api:suggest'))

response = self.client.get(url)

self.assertEqual(response.status_code,
status.HTTP_400_BAD_REQUEST)


@tag('unit')
class SearchDerivedTests(APITestCase):
Expand All @@ -158,7 +170,7 @@ def test_search_derived_with_reference(self):
Test that the /es-api/ endpoint succeeds when a valid
reference is provided
"""
url = "%s?reference=hello" % (reverse('es_api:search-derived'))
url = "%s?reference=hello&p=1" % (reverse('es_api:search-derived'))
with patch('es_api.views.XSEQueries') as query, \
patch('es_api.views.SearchFilter.objects') as sf1Obj, \
patch('es_api.views.XDSConfiguration.objects'):
Expand Down
5 changes: 5 additions & 0 deletions app/xds_api/tests/test_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ def setUp(self):
name="list 3",
description='list 3',
public=True)
self.list_4 = InterestList(owner=self.user_2,
name="list 4",
description='private list',
public=False)
self.filter_1 = SavedFilter(owner=self.user_1,
name="Devops",
query="randomQuery")
Expand All @@ -93,6 +97,7 @@ def setUp(self):
self.list_1.save()
self.list_2.save()
self.list_3.save()
self.list_4.save()
self.filter_1.save()
self.filter_2.save()
self.course_1 = Experience('1234')
Expand Down
46 changes: 44 additions & 2 deletions app/xds_api/tests/test_views_unit.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import json
from unittest.mock import patch
from unittest.mock import Mock, patch

from django.contrib.auth.models import Permission
from django.contrib.contenttypes.models import ContentType
Expand Down Expand Up @@ -125,6 +125,20 @@ def test_get_interest_list_authenticated(self):
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response_dict["name"], self.list_3.name)

def test_get_interest_list_authenticated_without_permission(self):
"""
Test that an authenticated user can't get another user's interest list.
"""
list_id = self.list_4.pk
url = reverse('xds_api:interest-list', args=(list_id,))

# login user
self.client.login(email=self.auth_email, password=self.auth_password)

response = self.client.get(url)

self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)

def test_get_interest_list_with_courses_authenticated(self):
"""
Test that an authenticated user can get an interest list by id,
Expand Down Expand Up @@ -235,6 +249,20 @@ def test_edit_interest_list_authenticated_not_owner(self):
response = self.client.patch(url, {'name': 'new name'})
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)

def test_edit_interest_list_authenticated_no_list(self):
"""
Test that an authenticated user cannot edit an interest list that
does not exist.
"""
list_id = 99
url = reverse('xds_api:interest-list', args=(list_id,))

# login user
self.client.login(email=self.auth_email, password=self.auth_password)

response = self.client.patch(url, {'name': 'new name'})
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

def test_edit_interest_list_authenticated_owner(self):
"""
Test that an authenticated user can edit an interest list that
Expand Down Expand Up @@ -318,6 +346,20 @@ def test_delete_interest_list_authenticated_not_owner(self):
response = self.client.delete(url)
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)

def test_delete_interest_list_authenticated_no_list(self):
"""
Test that an authenticated user cannot delete an interest list
that does not exist.
"""
list_id = 99
url = reverse('xds_api:interest-list', args=(list_id,))

# login user
self.client.login(email=self.auth_email, password=self.auth_password)

response = self.client.delete(url)
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

def test_delete_interest_list_authenticated_owner(self):
"""
Test that an authenticated user can delete an interest list
Expand Down Expand Up @@ -682,7 +724,7 @@ def test_get_spotlight_courses(self):
patch('xds_api.views.'
'get_spotlight_courses_api_url') as get_api_url:
get_api_url.return_value = "www.test.com"
http_resp = get_request.return_value
http_resp = Mock()
get_request.return_value = http_resp
http_resp.json.return_value = [{
"test": "value"
Expand Down

0 comments on commit cb0db79

Please sign in to comment.