예제 #1
0
async def test_post_user_endpoint(request_factory, patched_context, mocker):
    password = '******'
    hsh_ = generate_hash(password)

    json_data = {
        'login': '******',
        'password': password,
        'first_name': 'Alexey',
        'last_name': 'Dorin',
    }

    request = request_factory(method='post',
                              content_type='application/json',
                              json=json_data)

    db_user = DBUser(id=1,
                     login=json_data['login'],
                     first_name=json_data['first_name'],
                     last_name=json_data['last_name'],
                     password=hsh_)
    patched_query = mocker.patch('db.queries.user.create_user')
    patched_query.return_value = db_user

    endpoint = CreateUserEndpoint(None, patched_context, '/user', ['POST'])
    response = await endpoint(request)

    assert response.status == 201
예제 #2
0
async def test_post_auth_endpoint(request_factory, patched_context, mocker):
    password = '******'
    hsh_ = generate_hash(password)
    db_user = DBUser(
        id=1,
        login='******',
        password=hsh_
    )
    patched_query = mocker.patch('db.queries.user.get_user')
    patched_query.return_value = db_user

    json_data = {
        'login': '******',
        'password': password
    }

    request = request_factory(
        method='post',
        content_type='application/json',
        json=json_data
    )

    endpoint = AuthUserEndpoint(None, patched_context, '/auth', ['POST'])
    response = await endpoint(request)

    assert response.status == 200
def get_user(member: Member, g: DBGuild = None) -> DBUser:
    if g is None:
        g = DBGuild.objects.get(g_id=member.guild.id)

    try:
        u = DBUser.objects.get(u_id=member.id, g=g)
        u.d_name = member.display_name
        u.is_bot = member.bot
        u.avatar_url = member.avatar_url
        u.save()
    except DBUser.DoesNotExist:
        try:
            u = DBUser(u_id=member.id,
                       u_name=member.display_name,
                       g=g,
                       is_bot=member.bot,
                       avatar_url=member.avatar_url)
            u.save()
        except IntegrityError:
            u = DBUser.objects.get(u_id=member.id, g=g)
            u.d_name = member.display_name
            u.is_bot = member.bot
            u.avatar_url = member.avatar_url
            u.save()

    return u
예제 #4
0
async def test_patch_user_endpoint(request_factory, patched_context,
                                   request_header_authorization, mocker):
    password = '******'
    hsh_ = generate_hash(password)
    db_user = DBUser(id=1,
                     login='******',
                     first_name='Alexei',
                     last_name='Dori',
                     password=hsh_)
    json_data = {
        'first_name': 'Lech',
        'last_name': 'Dorin',
    }

    request = request_factory(method='patch',
                              headers=request_header_authorization,
                              content_type='application/json',
                              json=json_data)

    patched_query = mocker.patch('db.queries.user.get_user')
    patched_query.return_value = db_user

    db_user.first_name = json_data['first_name']
    db_user.last_name = json_data['last_name']

    patched_query = mocker.patch('db.queries.user.patch_user')
    patched_query.return_value = db_user

    endpoint = UserEndpoint(None,
                            patched_context,
                            '/user/1', ['PATCH'],
                            auth_required=True)
    response = await endpoint(request, uid=1)

    assert response.status == 200
예제 #5
0
def create_user(session: DBSession, user: RequestCreateUserDto) -> DBUser:
    new_user = DBUser(
        login=user.login,
        password=user.password,
    )

    if session.get_user_by_login(new_user.login) is not None:
        raise UserExistException

    session.add_model(new_user)

    return new_user
예제 #6
0
def create_user(session: DBSession, user: RequestCreateUserDto,
                hashed_password: bytes) -> DBUser:
    new_user = DBUser(login=user.login,
                      password=hashed_password,
                      first_name=user.first_name,
                      last_name=user.last_name)

    if session.get_user_by_login_with_deleted(new_user.login) is not None:
        raise DBUserExistsException

    session.add_model(new_user)

    return new_user
예제 #7
0
def create_user(
    session: DBSession, *, user: RequestCreateUserDto, hashed_password: bytes
) -> DBUser:
    new_user = DBUser(
        login=user.login,
        password=hashed_password,
        first_name=user.first_name,
        last_name=user.last_name,
    )

    try:
        if get_user_by_login(session, login=new_user.login):
            raise DBUserExists(new_user.login)
    except DBUserNotExists:
        pass

    session.add_model(new_user)

    return new_user
    def get_user(self, ctx: Context, u_id: Union[int, str]) -> Tuple[DBUser, Member]:

        m = None
        u_id = int(re.findall(r"\d+", u_id)[0]) if isinstance(u_id, str) else u_id

        for i in ctx.guild.members:
            if i.id == u_id:
                m = i
                break

        if m is not None:
            u = get_user(m, self.g)
        else:
            try:
                u = DBUser.objects.get(u_id=u_id, g=self.g)
            except DBUser.DoesNotExist:
                u = DBUser(u_id=u_id, g=self.g, u_name=m.display_name if m is not None else "")
                u.save()

        return u, m
예제 #9
0
def create_user(session: DBSession, user: RequestCreateUserDto,
                hashed_password: bytes, secret_word: bytes) -> DBUser:
    # создание модели DBUser
    new_user = DBUser(
        login=user.login,
        password=hashed_password,  # записываем в базу хэшированный пароль
        first_name=user.first_name,
        last_name=user.last_name,
        secret_word=secret_word,
    )

    # сначала попробуем получить пользователя по login перед созданием записи в БД
    # если не None, то получается, что пользователь с таким логином уже есть в БД -> рейзим исключение
    if session.get_user_by_login(new_user.login) is not None:
        raise DBUserAlreadyExistsException

    # добавляем модель в базу данных
    session.add_model(new_user)

    return new_user
예제 #10
0
async def test_get_user_endpoint(request_factory, patched_context,
                                 request_header_authorization, mocker):
    password = '******'
    hsh_ = generate_hash(password)

    request = request_factory(method='get',
                              headers=request_header_authorization)

    db_user = DBUser(id=1,
                     login='******',
                     first_name='Alexey',
                     last_name='Dorin',
                     password=hsh_)
    patched_query = mocker.patch('db.queries.user.get_user')
    patched_query.return_value = db_user

    endpoint = UserEndpoint(None,
                            patched_context,
                            '/user/1', ['GET'],
                            auth_required=True)
    response = await endpoint(request, uid=1)

    assert response.status == 200