Пример #1
0
    async def create_user(cls: Any, db: Any, user: User) -> Optional[str]:
        """Create user function.

        Args:
            db (Any): the db
            user (User): a user instanse to be created

        Returns:
            Optional[str]: The id of the created user. None otherwise.

        Raises:
            IllegalValueException: input object has illegal values
        """
        # Validation:
        if user.id:
            raise IllegalValueException(
                "Cannot create user with input id-") from None
        if user.username == "admin":
            raise IllegalValueException(
                'Cannot create user with username "admin".') from None
        # create id
        id = create_id()
        user.id = id
        # insert new user
        new_user = user.to_dict()
        result = await UsersAdapter.create_user(db, new_user)
        logging.debug(f"inserted user with id: {id}")
        if result:
            return id
        return None
Пример #2
0
 def test_put_user(self):
     user = User(first_name='First',
                 last_name='Last',
                 birth_date=datetime.date.today(),
                 email='*****@*****.**',
                 password='******',
                 phone='4988888')
     user.save()
     user.first_name = 'UpdateFirstName'
     response = self.client.put('/api/users/'+str(user.id), data=json.dumps(user.to_dict()), content_type='application/json')
     self.assertStatus(response, 200)
     self.assertEqual(response.json['first_name'], 'UpdateFirstName')
Пример #3
0
 def test_put_user(self):
     user = User(first_name='First',
                 last_name='Last',
                 birth_date=datetime.date.today(),
                 email='*****@*****.**',
                 password='******',
                 phone='4988888')
     user.save()
     user.first_name = 'UpdateFirstName'
     response = self.client.put('/api/users/' + str(user.id),
                                data=json.dumps(user.to_dict()),
                                content_type='application/json')
     self.assertStatus(response, 200)
     self.assertEqual(response.json['first_name'], 'UpdateFirstName')
Пример #4
0
 async def update_user(cls: Any, db: Any, id: str,
                       user: User) -> Optional[str]:
     """Get user function."""
     # Validation:
     if user.username == "admin":
         raise IllegalValueException(
             'Cannot change username to "admin".') from None
     # get old document
     old_user = await UsersAdapter.get_user_by_id(db, id)
     # update the user if found:
     if old_user:
         if user.id != old_user["id"]:
             raise IllegalValueException(
                 "Cannot change id for user.") from None
         new_user = user.to_dict()
         result = await UsersAdapter.update_user(db, id, new_user)
         return result
     raise UserNotFoundException(f"User with id {id} not found.") from None