Commit 6a10861b by Clinton Blackburn

Merge pull request #10346 from edx/clintonb/credit-api-update

Updated CreditCourse update endpoint
parents e726d01d 98a39b92
...@@ -469,24 +469,48 @@ class CreditCourseViewSetTests(TestCase): ...@@ -469,24 +469,48 @@ class CreditCourseViewSetTests(TestCase):
response = self.client.get(self.path, **headers) response = self.client.get(self.path, **headers)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
def test_create(self): def assert_course_created(self, course_id, response):
""" Verify the endpoint supports creating new CreditCourse objects. """ """ Verify an API request created a new CreditCourse object. """
course_key = CourseKey.from_string('a/b/c')
enabled = True enabled = True
data = { data = {
'course_key': unicode(course_key), 'course_key': unicode(course_id),
'enabled': enabled 'enabled': enabled
} }
response = self.client.post(self.path, data=json.dumps(data), content_type=JSON)
self.assertEqual(response.status_code, 201) self.assertEqual(response.status_code, 201)
# Verify the API returns the serialized CreditCourse # Verify the API returns the serialized CreditCourse
self.assertDictEqual(json.loads(response.content), data) self.assertDictEqual(json.loads(response.content), data)
# Verify the CreditCourse was actually created # Verify the CreditCourse was actually created
course_key = CourseKey.from_string(course_id)
self.assertTrue(CreditCourse.objects.filter(course_key=course_key, enabled=enabled).exists()) self.assertTrue(CreditCourse.objects.filter(course_key=course_key, enabled=enabled).exists())
def test_create(self):
""" Verify the endpoint supports creating new CreditCourse objects. """
course_id = 'a/b/c'
enabled = True
data = {
'course_key': unicode(course_id),
'enabled': enabled
}
response = self.client.post(self.path, data=json.dumps(data), content_type=JSON)
self.assert_course_created(course_id, response)
def test_put_as_create(self):
""" Verify the update endpoint supports creating a new CreditCourse object. """
course_id = 'd/e/f'
enabled = True
data = {
'course_key': unicode(course_id),
'enabled': enabled
}
path = reverse('credit:creditcourse-detail', args=[course_id])
response = self.client.put(path, data=json.dumps(data), content_type=JSON)
self.assert_course_created(course_id, response)
def test_get(self): def test_get(self):
""" Verify the endpoint supports retrieving CreditCourse objects. """ """ Verify the endpoint supports retrieving CreditCourse objects. """
course_id = 'a/b/c' course_id = 'a/b/c'
......
""" """
Views for the credit Django app. Views for the credit Django app.
""" """
import json
import datetime import datetime
import json
import logging import logging
from django.conf import settings from django.conf import settings
...@@ -21,13 +21,15 @@ import pytz ...@@ -21,13 +21,15 @@ import pytz
from rest_framework import viewsets, mixins, permissions from rest_framework import viewsets, mixins, permissions
from rest_framework.authentication import SessionAuthentication from rest_framework.authentication import SessionAuthentication
from rest_framework_oauth.authentication import OAuth2Authentication from rest_framework_oauth.authentication import OAuth2Authentication
from util.json_request import JsonResponse
from util.date_utils import from_timestamp
from openedx.core.djangoapps.credit import api from openedx.core.djangoapps.credit import api
from openedx.core.djangoapps.credit.exceptions import CreditApiBadRequest, CreditRequestNotFound from openedx.core.djangoapps.credit.exceptions import CreditApiBadRequest, CreditRequestNotFound
from openedx.core.djangoapps.credit.models import CreditCourse from openedx.core.djangoapps.credit.models import CreditCourse
from openedx.core.djangoapps.credit.serializers import CreditCourseSerializer from openedx.core.djangoapps.credit.serializers import CreditCourseSerializer
from openedx.core.djangoapps.credit.signature import signature, get_shared_secret_key from openedx.core.djangoapps.credit.signature import signature, get_shared_secret_key
from openedx.core.lib.api.mixins import PutAsCreateMixin
from util.date_utils import from_timestamp
from util.json_request import JsonResponse
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
...@@ -371,7 +373,7 @@ def _validate_timestamp(timestamp_value, provider_id): ...@@ -371,7 +373,7 @@ def _validate_timestamp(timestamp_value, provider_id):
return HttpResponseForbidden(u"Timestamp is too far in the past.") return HttpResponseForbidden(u"Timestamp is too far in the past.")
class CreditCourseViewSet(mixins.CreateModelMixin, mixins.UpdateModelMixin, viewsets.ReadOnlyModelViewSet): class CreditCourseViewSet(PutAsCreateMixin, mixins.UpdateModelMixin, viewsets.ReadOnlyModelViewSet):
""" CreditCourse endpoints. """ """ CreditCourse endpoints. """
lookup_field = 'course_key' lookup_field = 'course_key'
......
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