import pytest

from django.test.utils import override_settings
from django.urls.exceptions import NoReverseMatch

from olympia import amo
from olympia.activity.models import ActivityLog, VersionLog
from olympia.amo.tests import (
    APITestClient,
    TestCase,
    addon_factory,
    reverse_ns,
    user_factory,
    version_factory,
)
from olympia.api.tests.utils import APIKeyAuthTestMixin
from olympia.constants.scanners import (
    CUSTOMS,
    LABEL_BAD,
    MAD,
    TRUE_POSITIVE,
    WAT,
    YARA,
)
from olympia.blocklist.models import Block
from olympia.blocklist.utils import block_activity_log_save
from olympia.scanners.models import ScannerResult
from olympia.scanners.serializers import ScannerResultSerializer


@pytest.mark.internal_routes_allowed
class TestScannerResultViewInternal(TestCase):
    client_class = APITestClient

    def setUp(self):
        super().setUp()

        self.user = user_factory()
        self.grant_permission(self.user, 'Admin:ScannersResultsView')
        self.client.login_api(self.user)
        self.url = reverse_ns('scanner-results', api_version='v5')

    def assert_json_results(self, response, expected_results):
        json = response.json()
        assert 'results' in json
        assert 'count' in json
        assert json['count'] == expected_results
        return json['results']

    def test_endpoint_requires_authentication(self):
        self.client.logout_api()
        response = self.client.get(self.url)
        assert response.status_code == 401

    def test_endpoint_requires_permissions(self):
        self.user = user_factory()