Skip to content

Commit

Permalink
Merge pull request #64 from LulzLoL231/LulzLoL231/issue63
Browse files Browse the repository at this point in the history
[TESTS]: API ssh-ключей
  • Loading branch information
LulzLoL231 authored Mar 12, 2023
2 parents ff19e93 + 202bdcd commit 2c1ad8b
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 18 deletions.
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "timeweb-cloud"
version = "0.12.1"
version = "0.12.2"
description = "Timeweb Cloud API wrapper"
authors = ["Maxim Mosin <max@mosin.pw>"]
license = "MIT"
Expand Down Expand Up @@ -42,7 +42,7 @@ requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

[tool.bumpver]
current_version = "0.12.1"
current_version = "0.12.2"
version_pattern = "MAJOR.MINOR.PATCH"
commit_message = "Bump version {old_version} -> {new_version}"
commit = false
Expand Down
2 changes: 1 addition & 1 deletion src/timeweb/__meta.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# -*- coding: utf-8 -*-
'''Timeweb Cloud package metadata'''
__version__ = '0.12.1'
__version__ = '0.12.2'
__author__ = 'Maxim Mosin <max@mosin.pw>'
13 changes: 8 additions & 5 deletions src/timeweb/async_api/ssh_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,17 @@ async def update(
Returns:
schemas.SSHKeyResponse: Обновленный SSH-ключ.
'''
data: dict[str, str] = {}
if name:
data['name'] = name
if body:
data['body'] = body
if is_default:
data['is_default'] = is_default
key = await self._request(
'PATCH',
f'/ssh-keys/{ssh_key_id}',
json={
'name': name,
'body': body,
'is_default': is_default,
},
json=data
)
return schemas.SSHKeyResponse(**key.json())

Expand Down
9 changes: 4 additions & 5 deletions src/timeweb/schemas/ssh_keys/ssh_keys.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-
'''Модели для работы с SSH ключами'''
from uuid import UUID
from datetime import datetime

from pydantic import Field
Expand All @@ -12,25 +11,25 @@ class Server(BaseData):
'''Модель сервера.
Attributes:
id (UUID): Уникальный идентификатор сервера.
id (int): Уникальный идентификатор сервера.
name (str): Название сервера.
'''
id: UUID = Field(..., description='Уникальный идентификатор сервера.')
id: int = Field(..., description='Уникальный идентификатор сервера.')
name: str = Field(..., description='Название сервера.')


class SSHKey(BaseData):
'''Модель SSH ключа.
Attributes:
id (UUID): Уникальный идентификатор SSH ключа.
id (int): Уникальный идентификатор SSH ключа.
name (str): Название SSH ключа.
body (str): Тело SSH-ключа.
created_at (datetime): Дата и время создания SSH ключа.
used_by (list[Server]): Массив серверов, на которых используется SSH ключ.
is_default (bool): Будет ли выбираться SSH-ключ по умолчанию при создании сервера
'''
id: UUID = Field(..., description='Уникальный идентификатор SSH ключа.')
id: int = Field(..., description='Уникальный идентификатор SSH ключа.')
name: str = Field(..., description='Название SSH ключа.')
body: str = Field(..., description='Тело SSH-ключа.')
created_at: datetime = Field(
Expand Down
13 changes: 8 additions & 5 deletions src/timeweb/sync_api/ssh_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,17 @@ def update(
Returns:
schemas.SSHKeyResponse: Обновленный SSH-ключ.
'''
data: dict[str, str] = {}
if name:
data['name'] = name
if body:
data['body'] = body
if is_default:
data['is_default'] = is_default
key = self._request(
'PATCH',
f'/ssh-keys/{ssh_key_id}',
json={
'name': name,
'body': body,
'is_default': is_default,
},
json=data
)
return schemas.SSHKeyResponse(**key.json())

Expand Down
6 changes: 6 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

class Config(BaseSettings):
token: str = Field(..., env='TIMEWEB_TOKEN')
ssh_key: str = Field(..., env='TEST_SSH_KEY')

class Config:
env_file = 'tests/.env'
Expand All @@ -36,3 +37,8 @@ def my_ip() -> str:
if resp.is_success:
return resp.text.strip()
raise RuntimeError('Can\'t fetch IP address!')


@pytest.fixture()
def test_ssh_key() -> str:
return Config().ssh_key
53 changes: 53 additions & 0 deletions tests/test_sshkeys.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# -*- coding: utf-8 -*-
from timeweb import Timeweb
from timeweb.schemas import ssh_keys as schemas
from timeweb.schemas.ssh_keys.ssh_keys import SSHKey


KEY_NAME = 'TESTKEY_PYTEST'
KEY_NEWNAME = 'TESTKEY_PYTEST_NEW'


def search_key(name: str, ssh_keys: list[SSHKey]) -> SSHKey:
array = list(
filter(lambda t: t.name == name, ssh_keys)
)
return array.pop(0)


def test_get_keys(tw: Timeweb):
keys = tw.ssh_keys.get_keys()
assert isinstance(keys, schemas.SSHKeysArray)


def test_create_key(tw: Timeweb, test_ssh_key: str):
key = tw.ssh_keys.create(KEY_NAME, test_ssh_key, False)
assert isinstance(key, schemas.CreateSSHKeyResponse)
assert key.ssh_key.name == KEY_NAME
assert key.ssh_key.body == test_ssh_key


def test_get_key(tw: Timeweb, test_ssh_key: str):
keys = tw.ssh_keys.get_keys()
found_key = search_key(KEY_NAME, keys.ssh_keys)
key = tw.ssh_keys.get(found_key.id)
assert isinstance(key, schemas.SSHKeyResponse)
assert key.ssh_key.id == found_key.id
assert key.ssh_key.name == KEY_NAME
assert key.ssh_key.body == test_ssh_key


def test_update_key(tw: Timeweb):
keys = tw.ssh_keys.get_keys()
found_key = search_key(KEY_NAME, keys.ssh_keys)
updated = tw.ssh_keys.update(found_key.id, KEY_NEWNAME)
assert found_key.id == updated.ssh_key.id
assert found_key.name != updated.ssh_key.name
assert updated.ssh_key.name == KEY_NEWNAME


def test_delete_key(tw: Timeweb):
keys = tw.ssh_keys.get_keys()
found_key = search_key(KEY_NEWNAME, keys.ssh_keys)
status = tw.ssh_keys.delete(found_key.id)
assert status is True

0 comments on commit 2c1ad8b

Please sign in to comment.