def save_user(self, request, sociallogin, form=None): email = sociallogin.account.extra_data['email'] if sociallogin.account.provider == 'facebook_no_redirect': picture = sociallogin.account.extra_data['picture']['data']['url'] elif sociallogin.account.provider == 'google_no_redirect': picture = sociallogin.account.extra_data['picture'] else: picture = '' try: email_address = EmailAddress.objects.get(email=email) if not email_address.available: # 해당 이메일로 이미 가입한 유저가 있는지 확인 (이메일 회원가입으로 가입 후 구글 로그인으로 가입하는 경우) user = email_address.user sociallogin.user = user sociallogin.save(request) return user except EmailAddress.DoesNotExist: pass userprofile = { 'email': email, 'name': sociallogin.account.extra_data['name'], 'profile_image': picture } try: user = UserProfile.create_user( UserProfile.get_unique_username(sociallogin.account.extra_data['email']), userprofile) except ValidationError as e: res = HttpResponseBadRequest(str(e.detail)) raise ImmediateHttpResponse(res) Token.objects.create(user=user) sociallogin.user = user sociallogin.save(request) return user
class UserProfileTest(TestCase): def setUp(self): self.userprofile = UserProfile() def test_set_name(self): self.userprofile.name("Pedro Pereira Pinto") self.assertEqual(self.userprofile.user.first_name, "Pedro") self.assertEqual(self.userprofile.user.last_name, "Pereira Pinto") def test_create_user(self): self.assertFalse(hasattr(self.userprofile, 'user')) self.userprofile.create_user() self.assertTrue(hasattr(self.userprofile, 'user')) def test_get_full_name(self): name = "Pedro Pereira Pinto" self.userprofile.name(name) self.assertEqual(self.userprofile.full_name(), name) name = "Renan Calheiros" self.userprofile.name(name) self.assertEqual(self.userprofile.full_name(), name) def test_category(self): self.assertEqual(len(CATEGORY), 4) def test_registration_number(self): registration_number = "123456789" self.userprofile.registration_number = registration_number registration_size = len(registration_number) profile_registration_size = len(self.userprofile.registration_number) self.assertEqual(profile_registration_size, registration_size) def test_registration_number_bigger(self): registration_number = "0123456789" self.userprofile.registration_number = registration_number registration_size = len(registration_number) self.assertGreater(registration_size, 9) def test_registration_number_smaller(self): registration_number = "01234567" self.userprofile.registration_number = registration_number registration_size = len(registration_number) self.assertLess(registration_size, 9) def test_save(self): self.userprofile.name("Gustavo Rodrigues Coelho") self.userprofile.registration_number = "11/0030559" self.userprofile.user.username = "******" self.userprofile.user.email = "*****@*****.**" self.userprofile.save() self.assertTrue(self.userprofile.pk) def test_is_admin(self): userprofile = UserProfileFactory.create() self.assertFalse(userprofile.is_admin()) userprofile.make_as_admin() self.assertTrue(userprofile.is_admin()) def test_is_academic_staff(self): userprofile = UserProfileFactory.create() self.assertFalse(userprofile.is_academic_staff()) userprofile.make_as_academic_staff() self.assertTrue(userprofile.is_academic_staff()) def test_insert(self): user_form = UserForm() user_form.repeat_password = "******" user_form.registration_number = "123456789" user_form.category = 1 user_form.engineering = "Software" if(user_form.is_valid()): new_user = user_form.insert() self.assertEqual(new_user.engineering, "Software") self.assertEqual(new_user.user.password, "1234567") self.assertEqual(new_user.registration_number, "123456789") self.assertEqual(new_user.category, 1) def test_is_password_valid(self): self.userprofile.name("Gustavo Rodrigues Coelho") self.userprofile.registration_number = "11/0030559" self.userprofile.user.username = "******" self.userprofile.user.email = "*****@*****.**" self.userprofile.user.set_password("1234567") self.userprofile.save() password_form = PasswordForm() password_form.new_password = "******" password_form.renew_password = "******" if(password_form.is_valid()): valid = password_form.is_password_valid() self.assertTrue(valid) def test_clean(self): new_form = UserForm() if(new_form.is_valid()): clean = new_form.clean() self.assertTrue(clean)