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
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
def test_limited(self): # pylint: disable=no-self-use """ Test limited serializer """ profile = self.create_profile() assert ProfileLimitedSerializer().to_representation(profile) == { 'username': get_social_username(profile.user), 'first_name': profile.first_name, 'last_name': profile.last_name, 'preferred_name': profile.preferred_name, 'gender': profile.gender, 'account_privacy': profile.account_privacy, 'has_profile_image': profile.has_profile_image, 'profile_url_full': format_gravatar_url(profile.user.email, GravatarImgSize.FULL), 'profile_url_large': format_gravatar_url(profile.user.email, GravatarImgSize.LARGE), 'profile_url_medium': format_gravatar_url(profile.user.email, GravatarImgSize.MEDIUM), 'profile_url_small': format_gravatar_url(profile.user.email, GravatarImgSize.SMALL), 'country': profile.country, 'state_or_territory': profile.state_or_territory, 'city': profile.city, 'birth_country': profile.birth_country, 'preferred_language': profile.preferred_language, 'edx_level_of_education': profile.edx_level_of_education, 'education': [ EducationSerializer().to_representation(education) for education in profile.education.all() ], 'work_history': [ EmploymentSerializer().to_representation(work_history) for work_history in profile.work_history.all() ] }
def test_limited(self): """ Test limited serializer """ profile = self.create_profile() data = ProfileLimitedSerializer(profile).data assert data == { 'username': get_social_username(profile.user), 'first_name': profile.first_name, 'last_name': profile.last_name, 'full_name': profile.full_name, 'preferred_name': profile.preferred_name, 'gender': profile.gender, 'account_privacy': profile.account_privacy, 'country': profile.country, 'state_or_territory': profile.state_or_territory, 'city': profile.city, 'birth_country': profile.birth_country, 'preferred_language': profile.preferred_language, 'edx_level_of_education': profile.edx_level_of_education, 'education': EducationSerializer(profile.education.all(), many=True).data, 'work_history': (EmploymentSerializer(profile.work_history.all(), many=True).data), 'image_medium': profile.image_medium.url, 'about_me': profile.about_me, 'romanized_first_name': profile.romanized_first_name, 'romanized_last_name': profile.romanized_last_name, }
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) ]
def test_full(self): # pylint: disable=no-self-use """ Test full serializer """ profile = self.create_profile() assert ProfileSerializer().to_representation(profile) == { 'username': get_social_username(profile.user), 'first_name': profile.first_name, 'filled_out': profile.filled_out, 'agreed_to_terms_of_service': profile.agreed_to_terms_of_service, 'last_name': profile.last_name, 'preferred_name': profile.preferred_name, 'email_optin': profile.email_optin, 'email': profile.email, 'gender': profile.gender, 'date_of_birth': DateTimeField().to_representation(profile.date_of_birth), 'account_privacy': profile.account_privacy, 'has_profile_image': profile.has_profile_image, 'profile_url_full': format_gravatar_url(profile.user.email, GravatarImgSize.FULL), 'profile_url_large': format_gravatar_url(profile.user.email, GravatarImgSize.LARGE), 'profile_url_medium': format_gravatar_url(profile.user.email, GravatarImgSize.MEDIUM), 'profile_url_small': format_gravatar_url(profile.user.email, GravatarImgSize.SMALL), 'country': profile.country, 'state_or_territory': profile.state_or_territory, 'city': profile.city, 'birth_country': profile.birth_country, 'nationality': profile.nationality, 'preferred_language': profile.preferred_language, 'pretty_printed_student_id': profile.pretty_printed_student_id, 'edx_level_of_education': profile.edx_level_of_education, 'education': [ EducationSerializer().to_representation(education) for education in profile.education.all() ], 'work_history': [ EmploymentSerializer().to_representation(work_history) for work_history in profile.work_history.all() ] }
def test_full(self): """ Test full serializer """ birthdate = date(1980, 1, 2) profile = self.create_profile(date_of_birth=birthdate) data = ProfileSerializer(profile).data assert data == { 'username': get_social_username(profile.user), 'first_name': profile.first_name, 'full_name': profile.full_name, 'filled_out': profile.filled_out, 'agreed_to_terms_of_service': profile.agreed_to_terms_of_service, 'last_name': profile.last_name, 'preferred_name': profile.preferred_name, 'email_optin': profile.email_optin, 'email': profile.email, 'gender': profile.gender, 'date_of_birth': "1980-01-02", 'account_privacy': profile.account_privacy, 'country': profile.country, 'state_or_territory': profile.state_or_territory, 'city': profile.city, 'address': profile.address, 'postal_code': profile.postal_code, 'birth_country': profile.birth_country, 'nationality': profile.nationality, 'preferred_language': profile.preferred_language, 'pretty_printed_student_id': profile.pretty_printed_student_id, 'edx_level_of_education': profile.edx_level_of_education, 'education': EducationSerializer(profile.education.all(), many=True).data, 'work_history': (EmploymentSerializer(profile.work_history.all(), many=True).data), 'image': profile.image.url, 'image_small': profile.image_small.url, 'image_medium': profile.image_medium.url, 'about_me': profile.about_me, 'romanized_first_name': profile.romanized_first_name, 'romanized_last_name': profile.romanized_last_name, 'phone_number': profile.phone_number, 'student_id': profile.student_id, }