示例#1
0
async def register_user(first_name: str, last_name: str, email: str,
                        password: str, config: Config):
    if not check_password(password):
        raise err.bad_request(
            "Please provide a password at least 8 characters long, containing letters and symbols"
        )

    password_hash = bcrypt.hashpw(password.encode(), bcrypt.gensalt())
    try:
        async with config.get_database_connection() as db:
            user_persistence = users.UsersPersistence(db)
            created_user_id = await user_persistence.create_user(
                first_name, last_name, email, password_hash)
            user = await user_persistence.get_user_by_id(created_user_id)
    except IntegrityError as e:
        logger.debug("IntegrityError registering user with email %s: %s",
                     email, e)
        raise err.bad_request("Email address is already in use")
    except Exception as e:
        logger.exception("Failed to get user with email %s from db: %s", email,
                         e)
        raise err.internal_server_error()

    return {
        "token": jwt.generate_jwt(user["user_id"], jwt.Aud.AUTH,
                                  config.jwt_key())
    }
示例#2
0
 async def test_get_user_by_email_does_not_exist(self):
     async with aiosqlite.connect(self.db_name) as db:
         db.row_factory = aiosqlite.Row
         user_model = users.UsersPersistence(db)
         stored_data = await user_model.get_user_by_email(
             "blahblahblahfakeblah")
         self.assertEqual(stored_data, {})
示例#3
0
 async def test_get_user_by_email(self):
     user = await self.random_new_user()
     async with aiosqlite.connect(self.db_name) as db:
         db.row_factory = aiosqlite.Row
         user_model = users.UsersPersistence(db)
         stored_data = await user_model.get_user_by_email(user["email"])
         for key in user:
             self.assertIn(key, stored_data)
             self.assertEqual(user[key], stored_data[key])
示例#4
0
 async def test_collision_on_email(self):
     async with aiosqlite.connect(self.db_name) as db:
         db.row_factory = aiosqlite.Row
         user_model = users.UsersPersistence(db)
         test_user = {
             "first_name": "MyFirst",
             "last_name": "TestUser",
             "email": "abc",
             "password_hash": "test"
         }
         await user_model.create_user(**test_user)
         with self.assertRaises(IntegrityError):
             await user_model.create_user(**test_user)
示例#5
0
 async def test_create_user(self):
     """tests that the correct data is persisted"""
     test_user = {
         "first_name": "MyFirst",
         "last_name": "TestUser",
         "email": "test",
         "password_hash": "test"
     }
     async with aiosqlite.connect(self.db_name) as db:
         db.row_factory = aiosqlite.Row
         user_model = users.UsersPersistence(db)
         user_id = await user_model.create_user(**test_user)
         stored_data = await user_model.get_user_by_id(user_id)
         self.assertEqual(user_id, stored_data["user_id"])
         for key in test_user:
             self.assertIn(key, stored_data)
             self.assertEqual(test_user[key], stored_data[key])
示例#6
0
async def me(user_id: int, config: Config) -> Dict:
    try:
        async with config.get_database_connection() as db:
            get_user = users.UsersPersistence(db).get_user_by_id(user_id)
            get_joined_rooms = rooms.RoomPersistence(
                db).get_joined_rooms_by_user(user_id)
            get_token = spotify.get_valid_token_for_user(user_id, config)
            (user, joined_rooms,
             token) = await asyncio.gather(get_user,
                                           get_joined_rooms,
                                           get_token,
                                           return_exceptions=False)
    except Exception as e:
        logger.exception("Failed to get user(id=%s) from db: %s", user_id, e)
        raise err.internal_server_error()
    del user["password_hash"]
    user["rooms"] = joined_rooms
    user["authed_with_spotify"] = isinstance(token, str)
    return user