Repository: fossasia/open-event-server

Issue:
Server throws HTTP 500 for updating email
**Describe the bug**
<!-- A clear and concise description of what the bug is. -->
While updating contact details, HTTP 500 error is thrown if the user's email is changed.

**To Reproduce**
Steps to reproduce the behavior:
1. Send a `PATCH` request on `/users` endpoint, containing an email attribute

**Expected behavior**
<!-- A clear and concise description of what you expected to happen. -->
Email should be updated successfully.

**Stacktrace**
```
INFO:werkzeug:127.0.0.1 - - [01/Aug/2018 03:01:34] "PATCH /v1/users/2 HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/Users/binaryboy/Desktop/open-event-server/.venv/lib/python3.6/site-packages/flask/app.py", line 2309, in __call__
    return self.wsgi_app(environ, start_response)
  File "/Users/binaryboy/Desktop/open-event-server/app/__init__.py", line 67, in __call__
    return self.app(environ, start_response)
  File "/Users/binaryboy/Desktop/open-event-server/.venv/lib/python3.6/site-packages/flask/app.py", line 2295, in wsgi_app
    response = self.handle_exception(e)
  File "/Users/binaryboy/Desktop/open-event-server/.venv/lib/python3.6/site-packages/flask_cors/extension.py", line 161, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "/Users/binaryboy/Desktop/open-event-server/.venv/lib/python3.6/site-packages/flask_cors/extension.py", line 161, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "/Users/binaryboy/Desktop/open-event-server/.venv/lib/python3.6/site-packages/flask/app.py", line 1741, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/binaryboy/Desktop/open-event-server/.venv/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/Users/binaryboy/Desktop/open-event-server/.venv/lib/python3.6/site-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/Users/binaryboy/Desktop/open-event-server/.venv/lib/python3.6/site-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Users/binaryboy/Desktop/open-event-server/.venv/lib/python3.6/site-packages/flask_cors/extension.py", line 161, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "/Users/binaryboy/Desktop/open-event-server/.venv/lib/python3.6/site-packages/flask_cors/extension.py", line 161, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "/Users/binaryboy/Desktop/open-event-server/.venv/lib/python3.6/site-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/binaryboy/Desktop/open-event-server/.venv/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/Users/binaryboy/Desktop/open-event-server/.venv/lib/python3.6/site-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Users/binaryboy/Desktop/open-event-server/.venv/lib/python3.6/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/Users/binaryboy/Desktop/open-event-server/.venv/src/flask-rest-jsonapi/flask_rest_jsonapi/api.py", line 133, in decorated
    return self.check_permissions(view, view_args, view_kwargs, *args, **kwargs)
  File "/Users/binaryboy/Desktop/open-event-server/app/api/helpers/permission_manager.py", line 439, in permission_manager
    return permissions[args[0]](view, view_args, view_kwargs, *args, **kwargs)
  File "/Users/binaryboy/Desktop/open-event-server/app/api/helpers/permissions.py", line 45, in decorator
    return fn(*args, **kwargs)
  File "/Users/binaryboy/Desktop/open-event-server/app/api/helpers/permission_manager.py", line 112, in is_user_itself
    return view(*view_args, **view_kwargs)
  File "/Users/binaryboy/Desktop/open-event-server/.venv/src/flask-rest-jsonapi/flask_rest_jsonapi/decorators.py", line 32, in wrapper
    return func(*args, **kwargs)
  File "/Users/binaryboy/Desktop/open-event-server/.venv/lib/python3.6/site-packages/flask/views.py", line 88, in view
    return self.dispatch_request(*args, **kwargs)
  File "/Users/binaryboy/Desktop/open-event-server/.venv/src/flask-rest-jsonapi/flask_rest_jsonapi/resource.py", line 68, in dispatch_request
    response = method(*args, **kwargs)
  File "/Users/binaryboy/Desktop/open-event-server/.venv/src/flask-rest-jsonapi/flask_rest_jsonapi/decorators.py", line 56, in wrapper
    return func(*args, **kwargs)
  File "/Users/binaryboy/Desktop/open-event-server/.venv/src/flask-rest-jsonapi/flask_rest_jsonapi/resource.py", line 311, in patch
    self._data_layer.update_object(obj, data, kwargs)
  File "/Users/binaryboy/Desktop/open-event-server/.venv/src/flask-rest-jsonapi/flask_rest_jsonapi/data_layers/alchemy.py", line 144, in update_object
    self.after_update_object(obj, data, view_kwargs)
  File "/Users/binaryboy/Desktop/open-event-server/app/api/users.py", line 204, in after_update_object
    send_email_change_user_email(user, view_kwargs.get('email_changed'))
  File "/Users/binaryboy/Desktop/open-event-server/app/api/helpers/mail.py", line 276, in send_email_change_user_email
    hash = str(base64.b64encode(serializer.dumps([email, str_generator()])), 'utf-8')
  File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/base64.py", line 58, in b64encode
    encoded = binascii.b2a_base64(s, newline=False)
TypeError: a bytes-like object is required, not 'str'
```

Working on fixing this problem.


Discussion:

Resolving patch:
diff --git a/app/api/helpers/mail.py b/app/api/helpers/mail.py
--- a/app/api/helpers/mail.py
+++ b/app/api/helpers/mail.py
@@ -271,9 +271,9 @@ def send_import_mail(email, event_name=None, error_text=None, event_url=None):
 
 
 def send_email_change_user_email(user, email):
-    s = get_serializer()
-    hash = str(base64.b64encode(s.dumps([email, str_generator()])), 'utf-8')
-    link = make_frontend_url('/email/verify'.format(id=user.id), {'token': hash})
+    serializer = get_serializer()
+    hash_ = str(base64.b64encode(bytes(serializer.dumps([email, str_generator()]), 'utf-8')), 'utf-8')
+    link = make_frontend_url('/email/verify'.format(id=user.id), {'token': hash_})
     send_email_with_action(user.email, USER_CONFIRM, email=user.email, link=link)
     send_email_with_action(email, USER_CHANGE_EMAIL, email=email, new_email=user.email)
 


Test patch: