<commit_before>from __future__ import unicode_literals

from django.test import TestCase

from nodeconductor.backup import views, models, backup_registry
from nodeconductor.backup.tests import factories
from nodeconductor.structure.tests import factories as structure_factories


class BackupPermissionFilterTest(TestCase):

    def setUp(self):
        self.filter = views.BackupPermissionFilter()
        self.user = structure_factories.UserFactory.create(is_staff=True, is_superuser=True)

    def test_get_user_visible_model_instances_ids(self):
        backup_ids = [factories.BackupFactory().pk for i in range(5)]
        self.assertSequenceEqual(
            backup_ids, self.filter._get_user_visible_model_instances_ids(self.user, models.Backup))

    def test_filter_queryset(self):
        # only for test lets make backupschedule backupable
        backup_registry.BACKUP_REGISTRY = {'Schedule': 'backup_backupschedule'}
        backupable = factories.BackupScheduleFactory()
        factories.BackupFactory(backup_source=backupable)
        mocked_request = type(str('MockedRequest'), (object,), {'user': self.user})()
        # user can view backupable:
        self.filter._get_user_visible_model_instances_ids = lambda u, m: [backupable.id]
        filtered = self.filter.filter_queryset(mocked_request, models.Backup.objects.all(), None)
        self.assertEqual(len(models.Backup.objects.all()), len(filtered.values()))
        # user can`t view backupable:
        self.filter._get_user_visible_model_instances_ids = lambda u, m: []
        filtered = self.filter.filter_queryset(mocked_request, models.Backup.objects.all(), None)
        self.assertFalse(filtered)


class BackupViewSetTest(TestCase):

    def setUp(self):
        self.view = views.BackupViewSet()

    def test_restore(self):
        backup = factories.BackupFactory(state=models.Backup.States.READY)
        request = type(str('MockedRequest'), (object, ), {'POST': {'replace_original': True}})
        response = self.view.restore(request, backup.uuid)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(models.Backup.objects.get(pk=backup.pk).state, models.Backup.States.RESTORING)

    def test_delete(self):
        request = type(str('MockedRequest'), (object, ), {})
        backup = factories.BackupFactory(state=models.Backup.States.READY)
        response = self.view.delete(request, backup.uuid)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(models.Backup.objects.get(pk=backup.pk).state, models.Backup.States.DELETING)<commit_msg>added user to mocked request in view tests<commit_after>from __future__ import unicode_literals

from django.test import TestCase

from nodeconductor.backup import views, models, backup_registry
from nodeconductor.backup.tests import factories
from nodeconductor.structure.tests import factories as structure_factories


class BackupPermissionFilterTest(TestCase):

    def setUp(self):
        self.filter = views.BackupPermissionFilter()
        self.user = structure_factories.UserFactory.create(is_staff=True, is_superuser=True)

    def test_get_user_visible_model_instances_ids(self):
        backup_ids = [factories.BackupFactory().pk for i in range(5)]
        self.assertSequenceEqual(
            backup_ids, self.filter._get_user_visible_model_instances_ids(self.user, models.Backup))

    def test_filter_queryset(self):
        # only for test lets make backupschedule backupable
        backup_registry.BACKUP_REGISTRY = {'Schedule': 'backup_backupschedule'}
        backupable = factories.BackupScheduleFactory()
        factories.BackupFactory(backup_source=backupable)
        mocked_request = type(str('MockedRequest'), (object,), {'user': self.user})()
        # user can view backupable:
        self.filter._get_user_visible_model_instances_ids = lambda u, m: [backupable.id]
        filtered = self.filter.filter_queryset(mocked_request, models.Backup.objects.all(), None)
        self.assertEqual(len(models.Backup.objects.all()), len(filtered.values()))
        # user can`t view backupable:
        self.filter._get_user_visible_model_instances_ids = lambda u, m: []
        filtered = self.filter.filter_queryset(mocked_request, models.Backup.objects.all(), None)
        self.assertFalse(filtered)


class BackupViewSetTest(TestCase):

    def setUp(self):
        self.view = views.BackupViewSet()
        self.user = structure_factories.UserFactory.create(is_staff=True, is_superuser=True)

    def test_restore(self):
        backup = factories.BackupFactory(state=models.Backup.States.READY)
        request = type(str('MockedRequest'), (object, ), {'POST': {'replace_original': True}, 'user': self.user})
        response = self.view.restore(request, backup.uuid)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(models.Backup.objects.get(pk=backup.pk).state, models.Backup.States.RESTORING)

    def test_delete(self):
        request = type(str('MockedRequest'), (object, ), {'user': self.user})
        backup = factories.BackupFactory(state=models.Backup.States.READY)
        response = self.view.delete(request, backup.uuid)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(models.Backup.objects.get(pk=backup.pk).state, models.Backup.States.DELETING)
{'fraction_non_alphanumeric': 0.10877719429857464, 'fraction_numerical': 0.002625656414103526, 'mean_word_length': 4.127884615384615, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4643145', 'n_tokens_mistral': 1557, 'n_tokens_neox': 1530, 'n_words': 273}