def update_profile(self, request): """Updates user profile.""" serializer = UserUpdateSerializer( data=request.data, context={'request': request}, ) serializer.is_valid(raise_exception=True) UserService.update_profile(request.user, serializer.validated_data) request.user.refresh_from_db() return Response(self.get_serializer(request.user).data)
def post(self, request): """Registers an user using the info.""" serializer = RegisterSerializer(data=request.data) serializer.is_valid(raise_exception=True) if config.REGISTER_REQUIRES_EMAIL_CONFIRMATION: user = UserService.register_new_user( user_data=serializer.validated_data) AuthService.send_confirmation_email(user) return Response(AccountsResponses.CONFIRMATION_EMAIL_SENT) else: user = UserService.register_new_user( user_data=serializer.validated_data) return Response(SessionSerializer(user).data)
def process_google_token(cls, token: str) -> dict: """Returns google account info if is valid. https://developers.google.com/identity/sign-in/web/backend-auth """ session = requests.session() cached_session = cachecontrol.CacheControl(session) request = google_requests.Request(session=cached_session) try: account_info = id_token.verify_oauth2_token( token, request, settings.GOOGLE_CLIENT_ID, ) if account_info.get('iss') != cls.GOOGLE_ACCOUNTS_URL: raise AccountsErrorCodes.INVALID_GOOGLE_TOKEN_ISSUER return UserService.create_or_update_for_social_networks( email=account_info.get('email'), first_name=account_info.get('first_name'), last_name=account_info.get('last_name'), ) except (JSONDecodeError, TypeError, ValueError): raise AccountsErrorCodes.INVALID_GOOGLE_TOKEN_ID
def test_update_profile_without_photo(test_user): changes = { 'username': '******', 'first_name': 'corona', 'last_name': 'virus', } user = UserService.update_profile(test_user, changes) assert isinstance(user, User) assert user.username == changes.get('username') assert user.first_name == changes.get('first_name') assert user.last_name == changes.get('last_name')
def test_update_profile_with_photo(test_user): changes = { 'first_name': 'fulano', 'last_name': 'de tal', 'photo': generate_image() } user = UserService.update_profile(test_user, changes) assert isinstance(user, User) assert user.first_name == changes.get('first_name') assert user.last_name == changes.get('last_name') assert user.photo is not None assert isinstance(user.photo_url, str)
def test_create_or_update_for_social_networks_if_user_does_not_exists(test_user): email = test_user.email first_name = 'fulano' last_name = 'de cual' user = UserService.create_or_update_for_social_networks( email, first_name, last_name, ) assert isinstance(user, User) assert user == test_user assert user.first_name == first_name assert user.last_name == last_name assert user.is_active
def test_create_or_update_for_social_networks_if_user_not_exists(): email = '*****@*****.**' first_name = 'fulano' last_name = 'de cual' user = UserService.create_or_update_for_social_networks( email, first_name, last_name, ) assert isinstance(user, User) assert user.email == email assert user.first_name == first_name assert user.last_name == last_name assert user.is_active
def test_register_new_user(): plain_password = '******' new_user = { 'email': '*****@*****.**', 'password': plain_password, } is_active = True user = UserService.register_new_user(new_user, is_active) assert isinstance(user, User) assert user.email == new_user.get('email') assert user.username == new_user.get('email') assert user.is_active == is_active assert user.check_password(plain_password)
def process_facebook_token(cls, access_token: str) -> dict: """Returns google account info if is valid.""" try: response = requests.get( cls.make_facebook_profile_url(access_token)) user_data = response.json() if 'error' in user_data: raise AccountsErrorCodes.INVALID_FACEBOOK_ACCESS_TOKEN return UserService.create_or_update_for_social_networks( email=user_data.get('email'), first_name=user_data.get('first_name'), last_name=user_data.get('last_name'), ) except (ValueError, KeyError, TypeError): # noqa: WPS329 raise AccountsErrorCodes.INVALID_FACEBOOK_ACCESS_TOKEN