Commit be8135a5 by Matt Drayer Committed by Jonathan Piacenti

mattdrayer/api-invalid-test-cleanup: Profiler-driven refactoring

parent 738dcb7c
...@@ -790,7 +790,7 @@ class GroupsApiTests(ModuleStoreTestCase): ...@@ -790,7 +790,7 @@ class GroupsApiTests(ModuleStoreTestCase):
data = {'name': 'Alpha Group', 'type': 'test'} data = {'name': 'Alpha Group', 'type': 'test'}
alpha_response = self.do_post(self.base_groups_uri, data) alpha_response = self.do_post(self.base_groups_uri, data)
self.assertEqual(alpha_response.status_code, 201) self.assertEqual(alpha_response.status_code, 201)
test_uri = alpha_response.data['uri'] + '/groups/gaois89sdf98' test_uri = alpha_response.data['uri'] + '/groups/1234'
response = self.do_get(test_uri) response = self.do_get(test_uri)
self.assertEqual(response.status_code, 404) self.assertEqual(response.status_code, 404)
......
...@@ -465,7 +465,10 @@ class GroupsGroupsDetail(SecureAPIView): ...@@ -465,7 +465,10 @@ class GroupsGroupsDetail(SecureAPIView):
response_status = status.HTTP_404_NOT_FOUND response_status = status.HTTP_404_NOT_FOUND
from_group_relationship = GroupRelationship.objects.get(group__id=group_id) from_group_relationship = GroupRelationship.objects.get(group__id=group_id)
if from_group_relationship: if from_group_relationship:
to_group_relationship = GroupRelationship.objects.get(group__id=related_group_id) try:
to_group_relationship = GroupRelationship.objects.get(group__id=related_group_id)
except ObjectDoesNotExist:
return Response(response_data, status=status.HTTP_404_NOT_FOUND)
if to_group_relationship and str(to_group_relationship.parent_group_id) == str(group_id): if to_group_relationship and str(to_group_relationship.parent_group_id) == str(group_id):
response_data['relationship_type'] = RELATIONSHIP_TYPES['hierarchical'] response_data['relationship_type'] = RELATIONSHIP_TYPES['hierarchical']
response_status = status.HTTP_200_OK response_status = status.HTTP_200_OK
......
...@@ -6,15 +6,6 @@ from rest_framework import serializers ...@@ -6,15 +6,6 @@ from rest_framework import serializers
from api_manager.models import Organization from api_manager.models import Organization
class UserSerializer(serializers.HyperlinkedModelSerializer):
""" Serializer for model interactions """
class Meta:
""" Meta class for defining additional serializer characteristics """
model = User
fields = ('id', 'url', 'username', 'email')
class OrganizationSerializer(serializers.ModelSerializer): class OrganizationSerializer(serializers.ModelSerializer):
""" Serializer for Organization model interactions """ """ Serializer for Organization model interactions """
url = serializers.HyperlinkedIdentityField(view_name='organization-detail') url = serializers.HyperlinkedIdentityField(view_name='organization-detail')
......
...@@ -13,6 +13,7 @@ from django.core.cache import cache ...@@ -13,6 +13,7 @@ from django.core.cache import cache
from django.test import Client from django.test import Client
from django.test.utils import override_settings from django.test.utils import override_settings
from student.models import UserProfile
from student.tests.factories import CourseEnrollmentFactory from student.tests.factories import CourseEnrollmentFactory
from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase
from xmodule.modulestore.tests.factories import CourseFactory from xmodule.modulestore.tests.factories import CourseFactory
...@@ -54,6 +55,10 @@ class OrganizationsApiTests(ModuleStoreTestCase): ...@@ -54,6 +55,10 @@ class OrganizationsApiTests(ModuleStoreTestCase):
email=self.test_user_email, email=self.test_user_email,
username=self.test_user_username username=self.test_user_username
) )
profile = UserProfile(user=self.test_user)
profile.city = 'Boston'
profile.save()
self.course = CourseFactory.create() self.course = CourseFactory.create()
self.second_course = CourseFactory.create( self.second_course = CourseFactory.create(
number="899" number="899"
...@@ -99,7 +104,8 @@ class OrganizationsApiTests(ModuleStoreTestCase): ...@@ -99,7 +104,8 @@ class OrganizationsApiTests(ModuleStoreTestCase):
'username': 'test_user{}'.format(i), 'username': 'test_user{}'.format(i),
'password': 'test_pass', 'password': 'test_pass',
'first_name': 'John{}'.format(i), 'first_name': 'John{}'.format(i),
'last_name': 'Doe{}'.format(i) 'last_name': 'Doe{}'.format(i),
'city': 'Boston',
} }
response = self.do_post(self.base_users_uri, data) response = self.do_post(self.base_users_uri, data)
self.assertEqual(response.status_code, 201) self.assertEqual(response.status_code, 201)
...@@ -160,7 +166,7 @@ class OrganizationsApiTests(ModuleStoreTestCase): ...@@ -160,7 +166,7 @@ class OrganizationsApiTests(ModuleStoreTestCase):
self.assertIsNotNone(response.data['modified']) self.assertIsNotNone(response.data['modified'])
def test_organizations_detail_get_undefined(self): def test_organizations_detail_get_undefined(self):
test_uri = '{}/123456789/'.format(self.base_organizations_uri) test_uri = '{}123456789/'.format(self.base_organizations_uri)
response = self.do_get(test_uri) response = self.do_get(test_uri)
self.assertEqual(response.status_code, 404) self.assertEqual(response.status_code, 404)
......
...@@ -9,10 +9,11 @@ from rest_framework.decorators import action ...@@ -9,10 +9,11 @@ from rest_framework.decorators import action
from rest_framework.response import Response from rest_framework.response import Response
from api_manager.models import Organization from api_manager.models import Organization
from api_manager.users.serializers import UserSerializer
from api_manager.utils import str2bool from api_manager.utils import str2bool
from student.models import CourseEnrollment from student.models import CourseEnrollment
from .serializers import OrganizationSerializer, UserSerializer from .serializers import OrganizationSerializer
class OrganizationsViewSet(viewsets.ModelViewSet): class OrganizationsViewSet(viewsets.ModelViewSet):
......
...@@ -40,6 +40,7 @@ class SecureClient(Client): ...@@ -40,6 +40,7 @@ class SecureClient(Client):
super(SecureClient, self).__init__(*args, **kwargs) super(SecureClient, self).__init__(*args, **kwargs)
@override_settings(DEBUG=True)
@override_settings(EDX_API_KEY=TEST_API_KEY) @override_settings(EDX_API_KEY=TEST_API_KEY)
@override_settings(PASSWORD_MIN_LENGTH=4) @override_settings(PASSWORD_MIN_LENGTH=4)
@override_settings(API_PAGE_SIZE=10) @override_settings(API_PAGE_SIZE=10)
...@@ -508,7 +509,7 @@ class UsersApiTests(ModuleStoreTestCase): ...@@ -508,7 +509,7 @@ class UsersApiTests(ModuleStoreTestCase):
test_uri = self.users_base_uri test_uri = self.users_base_uri
local_username = self.test_username + str(randint(11, 99)) local_username = self.test_username + str(randint(11, 99))
data = {'email': self.test_email, 'username': local_username, 'password': data = {'email': self.test_email, 'username': local_username, 'password':
self.test_password, 'first_name': self.test_first_name, 'last_name': self.test_last_name} self.test_password, 'first_name': self.test_first_name, 'last_name': self.test_last_name, 'title': 'The King'}
response = self.do_post(test_uri, data) response = self.do_post(test_uri, data)
test_uri = test_uri + '/' + str(response.data['id']) test_uri = test_uri + '/' + str(response.data['id'])
response = self.do_get(test_uri) response = self.do_get(test_uri)
...@@ -532,7 +533,7 @@ class UsersApiTests(ModuleStoreTestCase): ...@@ -532,7 +533,7 @@ class UsersApiTests(ModuleStoreTestCase):
response = self.do_post(test_uri, data) response = self.do_post(test_uri, data)
user_id = response.data['id'] user_id = response.data['id']
test_uri = '{}/{}'.format(test_uri, str(user_id)) test_uri = '{}/{}'.format(test_uri, str(user_id))
fail_user_id_group_uri = '{}/{}/groups'.format(test_uri, '22') fail_user_id_group_uri = '{}/{}/groups'.format(self.users_base_uri, '22')
group_url = self.groups_base_uri group_url = self.groups_base_uri
group_name = 'Alpha Group' group_name = 'Alpha Group'
......
...@@ -310,6 +310,8 @@ class UsersList(SecureListAPIView): ...@@ -310,6 +310,8 @@ class UsersList(SecureListAPIView):
user.is_staff = is_staff user.is_staff = is_staff
user.save() user.save()
# Be sure to always create a UserProfile record when adding users
# Bad things happen with the UserSerializer if one does not exist
profile = UserProfile(user=user) profile = UserProfile(user=user)
profile.name = '{} {}'.format(first_name, last_name) profile.name = '{} {}'.format(first_name, last_name)
profile.city = city profile.city = city
......
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