Commit fbac44ce by muhammad-ammar

heartbeat endpoint

EDUCATOR-1632
parent 60c7bdc9
......@@ -50,5 +50,10 @@ urlpatterns = [
regex=r'^3playmedia/transcripts/handle/(?P<token>[\w]+)$',
view=transcripts.ThreePlayMediaCallbackHandlerView.as_view(),
name='3play_media_callback'
)
),
url(
r'^heartbeat/$',
view=views.heartbeat,
name='heartbeat'
),
]
""" Views tests """
import json
import responses
import responses
from ddt import data, ddt, unpack
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.db.utils import DatabaseError
from mock import patch
from rest_framework import status
from rest_framework.test import APITestCase
......@@ -221,3 +222,26 @@ class TranscriptCredentialsTest(APITestCase):
json.dumps({'error': error_message}),
status.HTTP_400_BAD_REQUEST
)
class HeartbeatTests(APITestCase):
"""
Tests for hearbeat endpoint.
"""
def test_heartbeat(self):
"""
Test that heartbeat endpoint gives expected response upon success.
"""
response = self.client.get(reverse('heartbeat'))
assert response.status_code == 200
assert json.loads(response.content) == {'OK': True}
@patch('django.db.backends.utils.CursorWrapper')
def test_heartbeat_failure_db(self, mocked_cursor_wrapper):
"""
Test that heartbeat endpoint gives expected response when there is an error.
"""
mocked_cursor_wrapper.side_effect = DatabaseError
response = self.client.get(reverse('heartbeat'))
assert response.status_code == 500
assert json.loads(response.content) == {'OK': False}
......@@ -2,26 +2,28 @@
import json
import logging
import requests
from django.http import HttpResponse
from django.http import HttpResponseRedirect
import requests
from django.db import connection
from django.db.utils import DatabaseError
from django.http import HttpResponse, HttpResponseRedirect, JsonResponse
from django.views.decorators.csrf import csrf_exempt
from rest_framework import filters, renderers, status, viewsets
from rest_framework.decorators import detail_route
from rest_framework.decorators import (api_view, detail_route,
permission_classes)
from rest_framework.permissions import AllowAny
from rest_framework.response import Response
from rest_framework.views import APIView
from api import token_finisher
from VEDA import utils
from VEDA_OS01.enums import TranscriptionProviderErrorType
from VEDA_OS01.models import Course, Video, URL, Encode, TranscriptCredentials, TranscriptProvider
from VEDA_OS01.serializers import CourseSerializer, EncodeSerializer, VideoSerializer, URLSerializer
from VEDA_OS01.models import (URL, Course, Encode, TranscriptCredentials,
TranscriptProvider, Video)
from VEDA_OS01.serializers import (CourseSerializer, EncodeSerializer,
URLSerializer, VideoSerializer)
from VEDA_OS01.transcripts import CIELO24_API_VERSION
LOGGER = logging.getLogger(__name__)
......@@ -284,3 +286,32 @@ def user_login(request):
return HttpResponseRedirect(request.path)
else:
return HttpResponseRedirect('../admin') # settings.LOGIN_REDIRECT_URL)
@api_view(['GET'])
@permission_classes([AllowAny])
def heartbeat(request): # pylint: disable=unused-argument
"""
View to check if database is reachable and ready to handle requests.
"""
try:
db_status()
except DatabaseError:
return JsonResponse(
{'OK': False},
status=status.HTTP_500_INTERNAL_SERVER_ERROR
)
return JsonResponse(
{'OK': True},
status=status.HTTP_200_OK
)
def db_status():
"""
Return database status.
"""
with connection.cursor() as cursor:
cursor.execute('SELECT 1')
cursor.fetchone()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment