import http
import logging
from http import HTTPStatus

import pytest
from django.contrib.auth import get_user_model
from django.urls import reverse
from rest_framework.test import APIClient

from assets.models.assets import OperatingSystem, Asset
from perms.models import Perm

_log = logging.getLogger()

TIMEOUT = 10  # time before timeout exception appears
User = get_user_model()


@pytest.mark.django_db
class TestAssetPagesAccess(object):

    @pytest.fixture(autouse=True)
    def setup_tests(self, django_user_model, add_user_with_permissions):
        """ Fixture for preparing database for tests
        :param client: object to work with test session
        :param django_user_model: object to work with User model in pytest
        :param add_user_with_permissions: fixture for adding new user
        :return: prepared for tests database with:
            user 'user1' with perms.can_view_user permission
            user 'test_user' for testing view_user page features
        """
        username = 'foo'
        password = 'bar'

        add_user_with_permissions(username=username, password=password,
                                  is_superuser=True)
        add_user_with_permissions(username='test_no_perms', password='1')
        add_user_with_permissions(username='test_right_perms', password='1',
                                  permissions=[Perm.can_view_assets_list,
                                               Perm.can_view_asset,
                                               Perm.can_edit_asset])
        add_user_with_permissions(username='test_perms', password='1',
                                  permissions=[])
        os = OperatingSystem.objects.create(name='MACOS')
        for i in range(5):
            Asset.objects.create(name=f'test{i}', ip='1.1.1.1', os=os)

