<commit_before>from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.test import TestCase
from django.test.client import Client

from metashare import test_utils
from metashare.repository.models import resourceInfoType_model
from metashare.repository import views
from metashare.settings import DJANGO_BASE, ROOT_PATH
from metashare.test_utils import create_user


def _import_resource(fixture_name):
    """
    Imports the XML resource description with the given file name.
    
    The new resource is published and the ID is returned.
    """
    _result = test_utils.import_xml('{0}/repository/fixtures/{1}'
                                    .format(ROOT_PATH, fixture_name))
    result = _result[0].id
    resource = resourceInfoType_model.objects.get(pk=result)
    resource.storage_object.published = True
    resource.storage_object.save()
    return result


class ViewTest(TestCase):
    """
    Test the detail view
    """
    def setUp(self):
        """
        Set up the detail view
        """
        test_utils.setup_test_storage()
        self.resource_id = _import_resource('testfixture.xml')
        # set up test users with and without staff permissions.
        # These will live in the test database only, so will not
        # pollute the "normal" development db or the production db.
        # As a consequence, they need no valuable password.
        staffuser = create_user('staffuser', '<EMAIL>', 'secret')
        staffuser.is_staff = True
        staffuser.save()
        create_user('normaluser', '<EMAIL>', 'secret')

    def tearDown(self):
        """
        Clean up the test
        """
        resourceInfoType_model.objects.all().delete()
        User.objects.all().delete()

    def testView(self):
        """
        Tries to view a resource
        """
        client = Client()
        url = self.get_absolute_url()
        response = client.get(url, follow = True)
        self.assertTemplateUsed(response, 'repository/lr_view.html')
        self.assertNotContains(response, "Edit")

    def test_staff_user_sees_editor(self):
        """
        Tests whether a staff user can edit a resource (in seeing the edit button)
        """
        client = Client()
        client.login(username='staffuser', password='<PASSWORD>')
        url = self.get_absolute_url()
        response = client.get(url, follow = True)
        self.assertTemplateUsed(response, 'repository/lr_view.html')
        self.assertContains(response, "Editor")

    def test_normal_user_doesnt_see_editor(self):
        """
        Tests whether a normal user cannot edit a resource
        """
        client = Client()
        client.login(username='normaluser', password='<PASSWORD>')
        url = self.get_absolute_url()
        response = client.get(url, follow = True)
        self.assertTemplateUsed(response, 'repository/lr_view.html')
        self.assertNotContains(response, "Editor")

    def test_anonymous_doesnt_see_editor(self):
        """
        Tests whether an anonymous user cannot edit a resource
        """
        client = Client()
        url = self.get_absolute_url()
        response = client.get(url, follow = True)
        self.assertTemplateUsed(response, 'repository/lr_view.html')
        self.assertNotContains(response, "Editor")


class DownloadViewTest(TestCase):
    """
    Tests for the license selection, license agreement and download views.
    """
    def setUp(self):
        """
        Sets up some resources with which to test.
        """
        test_utils.setup_test_storage()
        # set up different test resources
        self.non_downloadable_resource_id = _import_resource('testfixture.xml')
        self.downloadable_resource_id_1 = \
            _import_resource('downloadable_1_license.xml')
        self.downloadable_resource_id_2 = \
            _import_resource('downloadable_3_licenses.xml')
        # set up test users with and without staff permissions
        staffuser = create_user('staffuser', '<EMAIL>', 'secret')
        staffuser.is_staff = True
        staffuser.save()
        create_user('normaluser', '<EMAIL>', 'secret')

    def tearDown(self):<commit_msg>Update tests of stable url for resources<commit_after>from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.test import TestCase
from django.test.client import Client

from metashare import test_utils
from metashare.repository.models import resourceInfoType_model
from metashare.repository import views
from metashare.settings import DJANGO_BASE, ROOT_PATH
from metashare.test_utils import create_user
from django.template.defaultfilters import slugify


def _import_resource(fixture_name):
    """
    Imports the XML resource description with the given file name.
    
    The new resource is published and the ID is returned.
    """
    _result = test_utils.import_xml('{0}/repository/fixtures/{1}'
                                    .format(ROOT_PATH, fixture_name))
    result = _result[0].id
    resource = resourceInfoType_model.objects.get(pk=result)
    resource.storage_object.published = True
    resource.storage_object.save()
    return result


class ViewTest(TestCase):
    """
    Test the detail view
    """
    def setUp(self):
        """
        Set up the detail view
        """
        test_utils.setup_test_storage()
        self.resource_id = _import_resource('testfixture.xml')
        resource = get_object_or_404(resourceInfoType_model, pk=object_id)
        # set up test users with and without staff permissions.
        # These will live in the test database only, so will not
        # pollute the "normal" development db or the production db.
        # As a consequence, they need no valuable password.
        staffuser = create_user('staffuser', '<EMAIL>', 'secret')
        staffuser.is_staff = True
        staffuser.save()
        create_user('normaluser', '<EMAIL>', 'secret')

    def tearDown(self):
        """
        Clean up the test
        """
        resourceInfoType_model.objects.all().delete()
        User.objects.all().delete()

    def testView(self):
        """
        Tries to view a resource
        """
        client = Client()
        url = resource.get_global_url()
        response = client.get(url, follow = True)
        self.assertTemplateUsed(response, 'repository/lr_view.html')
        self.assertNotContains(response, "Edit")

    def test_staff_user_sees_editor(self):
        """
        Tests whether a staff user can edit a resource (in seeing the edit button)
        """
        client = Client()
        client.login(username='staffuser', password='<PASSWORD>')
        url = resource.get_global_url()
        response = client.get(url, follow = True)
        self.assertTemplateUsed(response, 'repository/lr_view.html')
        self.assertContains(response, "Editor")

    def test_normal_user_doesnt_see_editor(self):
        """
        Tests whether a normal user cannot edit a resource
        """
        client = Client()
        client.login(username='normaluser', password='<PASSWORD>')
        url = resource.get_global_url()
        response = client.get(url, follow = True)
        self.assertTemplateUsed(response, 'repository/lr_view.html')
        self.assertNotContains(response, "Editor")

    def test_anonymous_doesnt_see_editor(self):
        """
        Tests whether an anonymous user cannot edit a resource
        """
        client = Client()
        url = resource.get_global_url()
        response = client.get(url, follow = True)
        self.assertTemplateUsed(response, 'repository/lr_view.html')
        self.assertNotContains(response, "Editor")


class DownloadViewTest(TestCase):
    """
    Tests for the license selection, license agreement and download views.
    """
    def setUp(self):
        """
        Sets up some resources with which to test.
        """
        test_utils.setup_test_storage()
        # set up different test resources
        self.non_downloadable_resource_id = _import_resource('testfixture.xml')
        self.downloadable_resource_id_1 = \
            _import_resource('downloadable_1_license.xml')
        self.downloadable_resource_id_2 = \
            _import_resource('downloadable_3_licenses.xml')
        # set up test users with and without staff permissions
        staffuser = create_user('staffuser', '<EMAIL>', '<PASSWORD>')
        staffuser.is_staff = True
        staffuser.save()
        create_user('normaluser', '<EMAIL>', 'secret')

    def tearDown(self):
{'fraction_non_alphanumeric': 0.0957657762743497, 'fraction_numerical': 0.00198296978887204, 'mean_word_length': 2.7721073471183457, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 10}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1068965', 'n_tokens_mistral': 2322, 'n_tokens_neox': 2306, 'n_words': 606}