示例#1
0
    async def login(self):
        data = self.request.json

        self.data_validator_cls = LoginValidator
        try:
            self.data_validator_cls(**data).validate()
        except TypeError:
            self.response.status = CODE_BAD_REQUEST
            return
        except APIException as exc:
            self.response.status = CODE_BAD_REQUEST
            self.response.data = exc
            return

        user = UserEntity(**data)
        user.password = data.pop("password")
        users = await self.user_repo.filter(user.serialize())

        if not users:
            self.response.status = CODE_UNAUTHORIZED
            self.response.data = {"message": "Unauthorized."}
            return

        user = users[0]
        auth_token = self.token_manager.create(user)
        self.response.data = {"token": auth_token}
示例#2
0
    async def update(self, uuid: int):
        condition = await self._check_permission()
        if isinstance(condition, dict):
            self.process_exception(condition)
            return

        data = self.request.json

        self.data_validator_cls = UserValidator
        self.data_validator_cls(**data).validate()

        user = UserEntity(**data)
        user.password = data.pop("password")

        usecase = usecases.UpdateUserUsecase(self.response, self.user_repo, uuid, user)
        await usecase.execute()
示例#3
0
from unittest import TestCase
from unittest.mock import Mock

from src.infrastructure.databases import SQLiteDBClient
from src.core.entities import BlogPostEntity
from src.core.entities import UserEntity

user = UserEntity(
    **{
        "uuid": 1,
        "username": "******",
        "email": "*****@*****.**",
        "password": "******",
    })

blog = BlogPostEntity(**{
    "title": "Title",
    "context": "Context",
    "author": user
})


class TestSQLiteDBClient(TestCase):
    def setUp(self):
        self.client = SQLiteDBClient(":memory:")
        self.client.cursor = Mock()

    async def test_select_one(self):
        self.client.cursor.return_value.fetcone.return_value = blog
        item = await self.client.select_one("blog", 1)
        assert isinstance(item, BlogPostEntity)
示例#4
0
 async def create_superuser(self, username: str, email: str, password: str):
     user = UserEntity(username=username, email=email)
     user.password = password
     user.is_superadmin = True
     await self.user_repo.insert(user.serialize())
示例#5
0
 async def filter(self, user: dict):
     user = {k: user[k] for k in user if user.get(k)}
     user.pop("updated_at")
     data = await self.db.filter(self.table, user)
     return [UserEntity(**item) for item in data]
示例#6
0
 async def get_all(self, after=0, limit=DEFAULT_LIMIT) -> List[UserEntity]:
     data = await self.db.select_all(self.table, limit, after)
     return [UserEntity(**item) for item in data]
示例#7
0
 async def get_one(self, uuid: int) -> UserEntity:
     data = await self.db.select_one(self.table, uuid)
     return UserEntity(**data)
示例#8
0
from unittest import TestCase
from src.api.routers import api

from src.core.entities import UserEntity

USER_ENTITY_DATA = {
    "uuid": 1,
    "username": "******",
    "email": "*****@*****.**",
    "password": "******",
}

BLOG_ENTITY_DATA = {
    "title": "Title",
    "context": "Context",
    "author": UserEntity(**USER_ENTITY_DATA)
}


class TestBlogAPI(TestCase):
    async def test_blog_list_should_return_ok(self):
        request, response = await api.test_client.get('/posts')
        assert response.status == 200

    async def test_blog_get_should_return_ok(self):
        request, response = await api.test_client.get('/post/1')
        assert response.status == 200

    async def test_blog_patch_should_return_ok(self):
        request, response = await api.test_client.patch(
            '/post/1', {"title": 123})