Repository: medtagger/MedTagger

Issue:
Log in user after registration
## Current Behavior

User needs to log in after first registration.

## Expected Behavior

User should be logged into MedTagger right after filling registration form.

## Steps to Reproduce the Problem

  1. Register new user.
  2. You will be redirected to the login page.
  3. Type your login once again...



Discussion:

Resolving patch:
diff --git a/backend/medtagger/api/auth/business.py b/backend/medtagger/api/auth/business.py
--- a/backend/medtagger/api/auth/business.py
+++ b/backend/medtagger/api/auth/business.py
@@ -1,11 +1,12 @@
 """Module responsible for business logic in all Auth endpoint."""
+from typing import Tuple
 from medtagger.api import InvalidArgumentsException
 from medtagger.api.security import hash_password, verify_user_password, generate_auth_token
 from medtagger.database.models import User
 from medtagger.repositories import roles as RolesRepository, users as UsersRepository
 
 
-def create_user(email: str, password: str, first_name: str, last_name: str) -> int:
+def create_user(email: str, password: str, first_name: str, last_name: str) -> Tuple[int, str]:
     """Create user with the given user information. Password is being hashed.
 
     :param email: user email in string format
@@ -13,7 +14,7 @@ def create_user(email: str, password: str, first_name: str, last_name: str) -> i
     :param first_name: user first name in string format
     :param last_name: user last name in string format
 
-    :return: id of the new user
+    :return: tuple with user id and authentication token
     """
     user = UsersRepository.get_user_by_email(email)
     if user:
@@ -24,7 +25,9 @@ def create_user(email: str, password: str, first_name: str, last_name: str) -> i
     if not role:
         raise InvalidArgumentsException('Role does not exist.')
     new_user.roles.append(role)
-    return UsersRepository.add_new_user(new_user)
+    user_id = UsersRepository.add_new_user(new_user)
+    user_token = generate_auth_token(new_user)
+    return user_id, user_token
 
 
 def sign_in_user(email: str, password: str) -> str:
diff --git a/backend/medtagger/api/auth/service.py b/backend/medtagger/api/auth/service.py
--- a/backend/medtagger/api/auth/service.py
+++ b/backend/medtagger/api/auth/service.py
@@ -21,8 +21,8 @@ class Register(Resource):
     def post() -> Any:
         """Register the user."""
         user = request.json
-        user_id = create_user(user['email'], user['password'], user['firstName'], user['lastName'])
-        return {'id': user_id}, 201
+        user_id, user_token = create_user(user['email'], user['password'], user['firstName'], user['lastName'])
+        return {'id': user_id, 'token': user_token}, 201
 
 
 @auth_ns.route('/sign-in')


Test patch:
diff --git a/backend/tests/functional_tests/conftest.py b/backend/tests/functional_tests/conftest.py
--- a/backend/tests/functional_tests/conftest.py
+++ b/backend/tests/functional_tests/conftest.py
@@ -8,12 +8,12 @@
 from medtagger import storage
 from medtagger.api import InvalidArgumentsException
 from medtagger.api.auth.business import create_user, sign_in_user
-from medtagger.api.users.business import set_user_role
 from medtagger.api.rest import app
+from medtagger.api.users.business import set_user_role
 from medtagger.database import Base, session, db_session
 from medtagger.database.fixtures import apply_all_fixtures
-from medtagger.storage import models
 from medtagger.repositories import roles as RolesRepository
+from medtagger.storage import models
 
 logger = logging.getLogger(__name__)
 
@@ -60,7 +60,7 @@ def get_token_for_logged_in_user(role: str) -> str:
     role = RolesRepository.get_role_with_name(role)
     if role is None:
         raise InvalidArgumentsException('Role does not exist.')
-    user_id = create_user(email, password, first_name, last_name)
+    user_id, _ = create_user(email, password, first_name, last_name)
     set_user_role(user_id, role.name)
 
     with app.app_context():
diff --git a/backend/tests/functional_tests/test_users.py b/backend/tests/functional_tests/test_users.py
--- a/backend/tests/functional_tests/test_users.py
+++ b/backend/tests/functional_tests/test_users.py
@@ -10,7 +10,6 @@
     label_tags as LabelTagsRepository,
     tasks as TasksRepository,
 )
-
 from tests.functional_tests import get_api_client, get_headers
 
 EXAMPLE_USER_EMAIL = 'test@mail.com'
@@ -36,14 +35,6 @@ def test_basic_user_flow(prepare_environment: Any) -> None:
     assert response.status_code == 201
     json_response = json.loads(response.data)
     assert isinstance(json_response, dict)
-
-    # Step 2. User logs in
-    payload = {'email': EXAMPLE_USER_EMAIL, 'password': EXAMPLE_USER_PASSWORD}
-    response = api_client.post('/api/v1/auth/sign-in', data=json.dumps(payload),
-                               headers=get_headers(json=True))
-    assert response.status_code == 200
-    json_response = json.loads(response.data)
-    assert isinstance(json_response, dict)
     user_token = json_response['token']
     assert isinstance(user_token, str)
     assert len(user_token) > 100
@@ -64,9 +55,10 @@ def test_upgrade_to_doctor_role(prepare_environment: Any) -> None:
     """Test for upgrading volunteer's to doctor's role."""
     api_client = get_api_client()
 
-    admin_id = create_user(ADMIN_EMAIL, ADMIN_PASSWORD, ADMIN_FIRST_NAME, ADMIN_LAST_NAME)
-    volunteer_id = create_user(EXAMPLE_USER_EMAIL, EXAMPLE_USER_PASSWORD, EXAMPLE_USER_FIRST_NAME,
-                               EXAMPLE_USER_LAST_NAME)
+    admin_id, _ = create_user(ADMIN_EMAIL, ADMIN_PASSWORD, ADMIN_FIRST_NAME, ADMIN_LAST_NAME)
+    volunteer_id, _ = create_user(EXAMPLE_USER_EMAIL, EXAMPLE_USER_PASSWORD, EXAMPLE_USER_FIRST_NAME,
+                                  EXAMPLE_USER_LAST_NAME)
+
     set_user_role(admin_id, 'admin')
     set_user_role(volunteer_id, 'volunteer')
 
@@ -119,7 +111,7 @@ def test_ownership(prepare_environment: Any, synchronous_celery: Any) -> None:
     DatasetsRepository.add_new_dataset('LUNGS', 'Lungs')
     task = TasksRepository.add_task('FIND_NODULES', 'Find Nodules', 'path/to/image', ['LUNGS'], [])
     LabelTagsRepository.add_new_tag('EXAMPLE_TAG', 'Example Tag', [LabelTool.RECTANGLE], task.id)
-    admin_id = create_user(ADMIN_EMAIL, ADMIN_PASSWORD, ADMIN_FIRST_NAME, ADMIN_LAST_NAME)
+    admin_id, _ = create_user(ADMIN_EMAIL, ADMIN_PASSWORD, ADMIN_FIRST_NAME, ADMIN_LAST_NAME)
     set_user_role(admin_id, 'admin')
 
     # Step 2. Admin user logs in