Ejemplo n.º 1
0
    def test_add_employment(self):
        """
        Test that we handle adding an employment correctly
        """
        employment_object = {
            "city": "NY",
            "state_or_territory": "NY",
            "country": "USA",
            "company_name": "XYZ-ABC",
            "position": "SSE",
            "industry": "IT",
            "end_date": "2016-05-17",
            "start_date": "2016-05-28"
        }

        user1 = UserFactory.create()
        user2 = UserFactory.create()
        serializer = ProfileSerializer(instance=user1.profile, data={
            'work_history': [employment_object], 'education': []
        })
        serializer.is_valid(raise_exception=True)
        serializer.save()

        assert user1.profile.work_history.count() == 1
        employment = user1.profile.work_history.first()
        employment_object['id'] = employment.id
        assert EmploymentSerializer(employment).data == employment_object

        # Other profile did not get the employment assigned to it
        assert user2.profile.work_history.count() == 0
    def test_delete_education(self):
        """
        Test that we delete Educations which aren't specified in the PATCH
        """
        with mute_signals(post_save):
            education1 = EducationFactory.create()
            EducationFactory.create(profile=education1.profile)
            # has a different profile
            education3 = EducationFactory.create()

        assert education1.profile.education.count() == 2
        education_object1 = EducationSerializer().to_representation(education1)
        serializer = ProfileSerializer(instance=education1.profile,
                                       data={
                                           'education': [education_object1],
                                           'work_history': []
                                       })
        serializer.is_valid(raise_exception=True)
        serializer.save()

        assert education1.profile.education.count() == 1
        assert education1.profile.education.first() == education1

        # Other profile is unaffected
        assert education3.profile.education.count() == 1
    def test_add_employment(self):
        """
        Test that we handle adding an employment correctly
        """
        employment_object = {
            "city": "NY",
            "state_or_territory": "NY",
            "country": "USA",
            "company_name": "XYZ-ABC",
            "position": "SSE",
            "industry": "IT",
            "end_date": "2016-05-17",
            "start_date": "2016-05-28"
        }

        user1 = UserFactory.create()
        user2 = UserFactory.create()
        serializer = ProfileSerializer(instance=user1.profile,
                                       data={
                                           'work_history': [employment_object],
                                           'education': []
                                       })
        serializer.is_valid(raise_exception=True)
        serializer.save()

        assert user1.profile.work_history.count() == 1
        employment = user1.profile.work_history.first()
        employment_object['id'] = employment.id
        assert EmploymentSerializer().to_representation(
            employment) == employment_object

        # Other profile did not get the employment assigned to it
        assert user2.profile.work_history.count() == 0
    def test_delete_employment(self):
        """
        Test that we delete employments which aren't specified in the PATCH
        """
        with mute_signals(post_save):
            employment1 = EmploymentFactory.create()
            EmploymentFactory.create(profile=employment1.profile)
            # has a different profile
            employment3 = EmploymentFactory.create()

        assert employment1.profile.work_history.count() == 2
        employment_object1 = EmploymentSerializer().to_representation(
            employment1)
        serializer = ProfileSerializer(instance=employment1.profile,
                                       data={
                                           'work_history':
                                           [employment_object1],
                                           'education': []
                                       })
        serializer.is_valid(raise_exception=True)
        serializer.save()

        assert employment1.profile.work_history.count() == 1
        assert employment1.profile.work_history.first() == employment1

        # Other profile is unaffected
        assert employment3.profile.work_history.count() == 1
Ejemplo n.º 5
0
    def test_add_education(self):
        """
        Test that we handle adding an Education correctly
        """
        education_object = {
            'degree_name': DOCTORATE,
            'graduation_date': '9876-04-23',
            'field_of_study': 'subject',
            'online_degree': True,
            'school_name': 'school_name',
            'school_city': 'school_city',
            'school_state_or_territory': 'school_state_or_territory',
            'school_country': 'school_country,'
        }

        user1 = UserFactory.create()
        user2 = UserFactory.create()
        serializer = ProfileSerializer(instance=user1.profile, data={
            'education': [education_object], 'work_history': []
        })
        serializer.is_valid(raise_exception=True)
        serializer.save()

        assert user1.profile.education.count() == 1
        education = user1.profile.education.first()
        education_object['id'] = education.id
        education_data = EducationSerializer(education).data
        assert education_data == education_object

        # Other profile did not get the education assigned to it
        assert user2.profile.education.count() == 0
    def test_add_education(self):
        """
        Test that we handle adding an Education correctly
        """
        education_object = {
            'degree_name': DOCTORATE,
            'graduation_date': '9876-04-23',
            'field_of_study': 'subject',
            'online_degree': True,
            'school_name': 'school_name',
            'school_city': 'school_city',
            'school_state_or_territory': 'school_state_or_territory',
            'school_country': 'school_country,'
        }

        user1 = UserFactory.create()
        user2 = UserFactory.create()
        serializer = ProfileSerializer(instance=user1.profile,
                                       data={
                                           'education': [education_object],
                                           'work_history': []
                                       })
        serializer.is_valid(raise_exception=True)
        serializer.save()

        assert user1.profile.education.count() == 1
        education = user1.profile.education.first()
        education_object['id'] = education.id
        assert EducationSerializer().to_representation(
            education) == education_object

        # Other profile did not get the education assigned to it
        assert user2.profile.education.count() == 0
Ejemplo n.º 7
0
    def post(self, request, *args, **kwargs):
        profile = request.user.base_profile()

        serializer = ProfileSerializer(data=request.data, instance=profile)

        if serializer.is_valid():
            data = serializer.save()
            response_data = update_template(
                **{
                    'as_json': False,
                    'status': status.HTTP_200_OK,
                    'request': request,
                    'result': ProfileSerializer(data).data
                })
            return Response(response_data, status=status.HTTP_200_OK)
        else:
            response_data = update_template(
                **{
                    'as_json': False,
                    'status': status.HTTP_500_INTERNAL_SERVER_ERROR,
                    'request': request,
                    'result': serializer.errors
                })
            return Response(response_data,
                            status=status.HTTP_500_INTERNAL_SERVER_ERROR)
Ejemplo n.º 8
0
    def test_update_employment_different_profile(self):
        """
        Make sure we can't edit an employment for a different profile
        """
        with mute_signals(post_save):
            employment1 = EmploymentFactory.create()
            employment2 = EmploymentFactory.create()
        employment_object = EmploymentSerializer(employment1).data
        employment_object['id'] = employment2.id

        serializer = ProfileSerializer(instance=employment1.profile, data={
            'work_history': [employment_object], 'education': []
        })
        serializer.is_valid(raise_exception=True)
        with self.assertRaises(ValidationError) as ex:
            serializer.save()
        assert ex.exception.detail == ["Work history {} does not exist".format(employment2.id)]
Ejemplo n.º 9
0
    def test_update_employment(self):
        """
        Test that we handle updating an employment correctly
        """
        with mute_signals(post_save):
            employment = EmploymentFactory.create()
        employment_object = EmploymentSerializer(employment).data
        employment_object['position'] = "SE"

        serializer = ProfileSerializer(instance=employment.profile, data={
            'work_history': [employment_object], 'education': []
        })
        serializer.is_valid(raise_exception=True)
        serializer.save()

        assert employment.profile.work_history.count() == 1
        employment = employment.profile.work_history.first()
        assert EmploymentSerializer(employment).data == employment_object
Ejemplo n.º 10
0
    def test_update_education(self):
        """
        Test that we handle updating an Education correctly
        """
        with mute_signals(post_save):
            education = EducationFactory.create()
        education_data = EducationSerializer(education).data
        education_data['degree_name'] = BACHELORS

        serializer = ProfileSerializer(instance=education.profile, data={
            'education': [education_data], 'work_history': []
        })
        serializer.is_valid(raise_exception=True)
        serializer.save()

        assert education.profile.education.count() == 1
        education = education.profile.education.first()
        assert EducationSerializer(education).data == education_data
Ejemplo n.º 11
0
 def patch(self, request, *args, **kwargs):
     profile = self.get_object()
     serializer = ProfileSerializer(profile,
                                    data=request.data,
                                    partial=True)
     if serializer.is_valid():
         serializer.save()
         return Response(status=HTTP_200_OK)
     return Response(data=serializer.errors, status=HTTP_400_BAD_REQUEST)
Ejemplo n.º 12
0
    def test_update_employment(self):
        """
        Test that we handle updating an employment correctly
        """
        with mute_signals(post_save):
            employment = EmploymentFactory.create()
        employment_object = EmploymentSerializer(employment).data
        employment_object['position'] = "SE"

        serializer = ProfileSerializer(instance=employment.profile,
                                       data={
                                           'work_history': [employment_object],
                                           'education': []
                                       })
        serializer.is_valid(raise_exception=True)
        serializer.save()

        assert employment.profile.work_history.count() == 1
        employment = employment.profile.work_history.first()
        assert EmploymentSerializer(employment).data == employment_object
Ejemplo n.º 13
0
    def test_update_education(self):
        """
        Test that we handle updating an Education correctly
        """
        with mute_signals(post_save):
            education = EducationFactory.create()
        education_data = EducationSerializer(education).data
        education_data['degree_name'] = BACHELORS

        serializer = ProfileSerializer(instance=education.profile,
                                       data={
                                           'education': [education_data],
                                           'work_history': []
                                       })
        serializer.is_valid(raise_exception=True)
        serializer.save()

        assert education.profile.education.count() == 1
        education = education.profile.education.first()
        assert EducationSerializer(education).data == education_data
Ejemplo n.º 14
0
 def patch(self, request):
     user = request.user
     profile = Profile.objects.get(user=user)
     serializer = ProfileSerializer(profile,
                                    data=request.data,
                                    partial=True)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data)
     else:
         return Response(serializer.errors,
                         status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 15
0
    def test_update_education_different_profile(self):
        """
        Make sure we can't edit an education for a different profile
        """
        with mute_signals(post_save):
            education1 = EducationFactory.create()
            education2 = EducationFactory.create()
        education_object = EducationSerializer().to_representation(education1)
        education_object['id'] = education2.id

        serializer = ProfileSerializer(instance=education1.profile,
                                       data={
                                           'education': [education_object],
                                           'work_history': []
                                       })
        serializer.is_valid(raise_exception=True)
        with self.assertRaises(ValidationError) as ex:
            serializer.save()
        assert ex.exception.detail == [
            "Education {} does not exist".format(education2.id)
        ]
Ejemplo n.º 16
0
    def test_update_employment_different_profile(self):
        """
        Make sure we can't edit an employment for a different profile
        """
        with mute_signals(post_save):
            employment1 = EmploymentFactory.create()
            employment2 = EmploymentFactory.create()
        employment_object = EmploymentSerializer(employment1).data
        employment_object['id'] = employment2.id

        serializer = ProfileSerializer(instance=employment1.profile,
                                       data={
                                           'work_history': [employment_object],
                                           'education': []
                                       })
        serializer.is_valid(raise_exception=True)
        with self.assertRaises(ValidationError) as ex:
            serializer.save()
        assert ex.exception.detail == [
            "Work history {} does not exist".format(employment2.id)
        ]
Ejemplo n.º 17
0
    def create(self, request):
        user_serializer = self.serializer_class(data=request.data,)
        profile_serializer = ProfileSerializer(data=request.data,)

        if user_serializer.is_valid() and profile_serializer.is_valid():
            place = request.data['place']
            user = user_serializer.save()
            user.set_password(request.data['password'])
            user.save()
            Profile.objects.create(place=place, user=user)

            return Response(user_serializer.data)
        else:
            return Response(profile_serializer.errors if user_serializer.is_valid() else user_serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 18
0
    def test_delete_education(self):
        """
        Test that we delete Educations which aren't specified in the PATCH
        """
        with mute_signals(post_save):
            education1 = EducationFactory.create()
            EducationFactory.create(profile=education1.profile)
            # has a different profile
            education3 = EducationFactory.create()

        assert education1.profile.education.count() == 2
        education_object1 = EducationSerializer(education1).data
        serializer = ProfileSerializer(instance=education1.profile, data={
            'education': [education_object1], 'work_history': []
        })
        serializer.is_valid(raise_exception=True)
        serializer.save()

        assert education1.profile.education.count() == 1
        assert education1.profile.education.first() == education1

        # Other profile is unaffected
        assert education3.profile.education.count() == 1
Ejemplo n.º 19
0
    def test_delete_employment(self):
        """
        Test that we delete employments which aren't specified in the PATCH
        """
        with mute_signals(post_save):
            employment1 = EmploymentFactory.create()
            EmploymentFactory.create(profile=employment1.profile)
            # has a different profile
            employment3 = EmploymentFactory.create()

        assert employment1.profile.work_history.count() == 2
        employment_object1 = EmploymentSerializer(employment1).data
        serializer = ProfileSerializer(instance=employment1.profile, data={
            'work_history': [employment_object1], 'education': []
        })
        serializer.is_valid(raise_exception=True)
        serializer.save()

        assert employment1.profile.work_history.count() == 1
        assert employment1.profile.work_history.first() == employment1

        # Other profile is unaffected
        assert employment3.profile.work_history.count() == 1
Ejemplo n.º 20
0
    def update(self, request, pk=None):
        user = self.get_object()
        profile = Profile.objects.filter(user_id=user.pk,)

        user_serializer = self.serializer_class(user, data=request.data,)
        profile_serializer = ProfileSerializer(profile, data=request.data,)

        if user_serializer.is_valid() and profile_serializer.is_valid():
            place = request.data['place']
            user_serializer.save()
            user.set_password(request.data['password'])
            user.save()
            profile.update(place=place, user=user)

            return Response(user_serializer.data)
        else:
            return Response(profile_serializer.errors if user_serializer.is_valid() else user_serializer.errors, status=status.HTTP_400_BAD_REQUEST)