<commit_before># The contents of this file are subject to the Mozilla Public License
# Version 2.0 (the "License"); you may not use this file except in
# compliance with the License. You may obtain a copy of the License at
#    http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS"basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
# for the specific language governing rights and limitations under the
# License.
#
# OS2Webscanner was developed by Magenta in collaboration with OS2 the
# Danish community of open source municipalities (http://www.os2web.dk/).
#
# The code is currently governed by OS2 the Danish community of open
# source municipalities ( http://www.os2web.dk/ )
"""Unit tests for OS2Webscanner.

These will pass when you run "manage.py test os2webscanner".
"""

import os
import pep8

from django.test import TestCase
from django.conf import settings

from os2webscanner.models import Domain, Organization, Scanner, Scan
from validate import validate_domain

install_directory = os.path.abspath(os.path.join(settings.BASE_DIR, '..'))


class ScannerTest(TestCase):

    """Test running a scan and domain validation."""
    # TODO: Capture the interaction so these tests can work without an
    # Internet connection! !!!

    @classmethod
    def setUpTestData(cls):
        """Setup some data to test with."""
        # Don't change the order of these, because Magenta needs
        # pk = 2 to pass the validation test
        cls.magenta = Organization(name="Magenta", pk=1)
        cls.magenta.save()
        cls.google = Organization(name="Google", pk=2)
        cls.google.save()

    def test_validate_domain(self):
        """Test validating domains."""
        # Make sure Google does not validate in any of the possible methods
        all_methods = [Domain.WEBSCANFILE, Domain.METAFIELD]
        # Make sure Magenta's website validates using all possible methods
        for validation_method in [Domain.WEBSCANFILE, Domain.METAFIELD]:
            domain = Domain(url="http://www.magenta.dk/",
                            validation_method=validation_method,
                            organization=self.magenta,
                            pk=1)
            domain.save()
            print "VALIDATING", validation_method
            self.assertTrue(validate_domain(domain))

        for validation_method in all_methods:
            domain = Domain(url="http://www.google.com/",
                            validation_method=validation_method,
                            organization=self.google,
                            pk=2)
            domain.save()
            self.assertFalse(validate_domain(domain))

    def test_run_scan(self):
        """Test running a scan."""
        domain = Domain(url="http://www.magenta.dk/",
                        organization=self.magenta,
                        validation_method=Domain.ROBOTSTXT,
                        validation_status=1)
        scanner = Scanner(organization=self.magenta, schedule="")
        scanner.save()
        domain.save()
        scanner.domains.add(domain)
        self.assertTrue(isinstance(scanner.run(test_only=True), Scan))
        self.assertFalse(isinstance(scanner.run(test_only=True), Scan))

<commit_msg>Converted to Python 3 code using 2to3 command<commit_after># The contents of this file are subject to the Mozilla Public License
# Version 2.0 (the "License"); you may not use this file except in
# compliance with the License. You may obtain a copy of the License at
#    http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS"basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
# for the specific language governing rights and limitations under the
# License.
#
# OS2Webscanner was developed by Magenta in collaboration with OS2 the
# Danish community of open source municipalities (http://www.os2web.dk/).
#
# The code is currently governed by OS2 the Danish community of open
# source municipalities ( http://www.os2web.dk/ )
"""Unit tests for OS2Webscanner.

These will pass when you run "manage.py test os2webscanner".
"""

import os
import pep8

from django.test import TestCase
from django.conf import settings

from os2webscanner.models import Domain, Organization, Scanner, Scan
from .validate import validate_domain

install_directory = os.path.abspath(os.path.join(settings.BASE_DIR, '..'))


class ScannerTest(TestCase):

    """Test running a scan and domain validation."""
    # TODO: Capture the interaction so these tests can work without an
    # Internet connection! !!!

    @classmethod
    def setUpTestData(cls):
        """Setup some data to test with."""
        # Don't change the order of these, because Magenta needs
        # pk = 2 to pass the validation test
        cls.magenta = Organization(name="Magenta", pk=1)
        cls.magenta.save()
        cls.google = Organization(name="Google", pk=2)
        cls.google.save()

    def test_validate_domain(self):
        """Test validating domains."""
        # Make sure Google does not validate in any of the possible methods
        all_methods = [Domain.WEBSCANFILE, Domain.METAFIELD]
        # Make sure Magenta's website validates using all possible methods
        for validation_method in [Domain.WEBSCANFILE, Domain.METAFIELD]:
            domain = Domain(url="http://www.magenta.dk/",
                            validation_method=validation_method,
                            organization=self.magenta,
                            pk=1)
            domain.save()
            print("VALIDATING", validation_method)
            self.assertTrue(validate_domain(domain))

        for validation_method in all_methods:
            domain = Domain(url="http://www.google.com/",
                            validation_method=validation_method,
                            organization=self.google,
                            pk=2)
            domain.save()
            self.assertFalse(validate_domain(domain))

    def test_run_scan(self):
        """Test running a scan."""
        domain = Domain(url="http://www.magenta.dk/",
                        organization=self.magenta,
                        validation_method=Domain.ROBOTSTXT,
                        validation_status=1)
        scanner = Scanner(organization=self.magenta, schedule="")
        scanner.save()
        domain.save()
        scanner.domains.add(domain)
        self.assertTrue(isinstance(scanner.run(test_only=True), Scan))
        self.assertFalse(isinstance(scanner.run(test_only=True), Scan))


{'fraction_non_alphanumeric': 0.09026522001205546, 'fraction_numerical': 0.005575647980711272, 'mean_word_length': 2.575969827586207, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 12, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1778066', 'n_tokens_mistral': 1785, 'n_tokens_neox': 1733, 'n_words': 554}