<commit_before>from django.contrib.auth.models import User
from django.contrib.sessions.backends.base import SessionBase
from django.test import TestCase
from django.test.client import RequestFactory

from uwsgi_it_api.views import *
from uwsgi_it_api.views_metrics import *
from uwsgi_it_api.views_private import *

import base64
import datetime

class FakeSession(SessionBase):
    def create(self):
        return

    def delete(self, key=None):
        return

class ViewsTest(TestCase):
    def setUp(self):
        self.user = User.objects.create_user(
            username='test', email='<EMAIL>', password='<PASSWORD>')
        self.basic_auth = 'basic %s' % (base64.b64encode('test:top_secret'))
        self.server_address = "10.0.0.1"

        # customer api
        self.server, _ = Server.objects.get_or_create(
            name="server",
            address=self.server_address,
            hd="hd",
            memory=100,
            storage=100
        )
        self.customer, _ = Customer.objects.get_or_create(user=self.user)
        self.container, _ = Container.objects.get_or_create(
            customer=self.customer,
            server=self.server,
            memory=10,
            storage=10,
            name="container"
        )
        self.c_uid = self.container.uid
        self.domain, _ = Domain.objects.get_or_create(customer=self.customer, name="domain")
        self.d_uuid = self.domain.uuid
        self.tag, _ = Tag.objects.get_or_create(customer=self.customer, name="tag")
        self.container.tags.add(self.tag)
        self.domain.tags.add(self.tag)
        # metrics
        today = datetime.datetime.today()
        NetworkRXContainerMetric.objects.create(
            container=self.container,
            year=today.year,
            month=today.month,
            day=today.day,
        )
        NetworkTXContainerMetric.objects.create(
            container=self.container,
            year=today.year,
            month=today.month,
            day=today.day,
        )
        CPUContainerMetric.objects.create(
            container=self.container,
            year=today.year,
            month=today.month,
            day=today.day,
        )
        MemoryContainerMetric.objects.create(
            container=self.container,
            year=today.year,
            month=today.month,
            day=today.day,
        )
        IOReadContainerMetric.objects.create(
            container=self.container,
            year=today.year,
            month=today.month,
            day=today.day,
        )
        IOWriteContainerMetric.objects.create(
            container=self.container,
            year=today.year,
            month=today.month,
            day=today.day,
        )
        QuotaContainerMetric.objects.create(
            container=self.container,
            year=today.year,
            month=today.month,
            day=today.day,
        )
        HitsDomainMetric.objects.create(
            domain=self.domain,
            container=self.container,
            year=today.year,
            month=today.month,
            day=today.day,
        )
        NetworkRXDomainMetric.objects.create(
            domain=self.domain,
            container=self.container,
            year=today.year,
            month=today.month,
            day=today.day,
        )
        NetworkTXDomainMetric.objects.create(
            domain=self.domain,
            container=self.container,
            year=today.year,
            month=today.month,
            day=today.day,
        )

        self.factory = RequestFactory()

    def logged_get_response_for_view(self, path, view, kwargs=None, params={}):
        headers = {
            'HTTP_AUTHORIZATION': self.basic_auth,
            'HTTPS_DN': 'hithere',
            'REMOTE_ADDR': self.server_address,
        }
        request = self.factory.get(path, params, **headers)
        request.user = self.user
        request.session = FakeSession()
        if kwargs is None:
            kwargs = {}
        return view(request, **kwargs)

class ApiTest(ViewsTest):
    def test_me(self):
        response = self.logged_get_response_for_view('/me', me)
        self.assertEqual(response.status_code, 200)

    def test_me_containers(self):
        response = self.logged_get_response_for_view('/me/containers', containers)
        self.assertEqual(response.status_code, 200)

    def test_containers(self):
        response = self.logged_get_response_for_view('/containers', containers)
        self.assertEqual(response.status_code, 200)

    def test_containers_filters_tags(self):
        response = self.logged_get_response_for_view('/containers', containers, params={'tags': 'tag'})
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, '"uid": {}'.format(self.c_uid))
        response = self.logged_get_response_for_view('/containers', containers, params={'tags': 'fail'})
        self.assertEqual(response.status_code, 200)
        self.assertNotContains(response, '"uid": {}'.format(self.c_uid))

    def test_container(self):
        response = self.logged_get_response_for_view('/containers/1', container, {'id': self.c_uid})
        self.assertEqual(response.status_code, 200)

    def test_distros(self):
        response = self.logged_get_response_for_view('/distros', distros)
        self.assertEqual(response.status_code, 200)

    def test_domains(self):
        response = self.logged_get_response_for_view('/domains', domains)
        self.assertEqual(response.status_code, 200)

    def test_domains_filters_tags(self):
        response = self.logged_get_response_for_view('/domains', domains, params={'tags': 'tag'})
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, '"uuid": "{}"'.format(self.d_uuid))
        response = self.logged_get_response_for_view('/domains', domains, params={'tags': 'fail'})
        self.assertEqual(response.status_code, 200)
        self.assertNotContains(response, 'uuid: "{}"'.format(self.d_uuid))

    def test_domain(self):
        response = self.logged_get_response_for_view('/domains/1', domain, {'id': self.domain.pk})
        self.assertEqual(response.status_code, 200)

    def test_tags(self):
        response = self.logged_get_response_for_view('/tags', tags)
        self.assertEqual(response.status_code, 200)<commit_msg>add tests for loopboxes<commit_after>from django.contrib.auth.models import User
from django.contrib.sessions.backends.base import SessionBase
from django.test import TestCase
from django.test.client import RequestFactory

from uwsgi_it_api.views import *
from uwsgi_it_api.views_metrics import *
from uwsgi_it_api.views_private import *

import base64
import datetime

class FakeSession(SessionBase):
    def create(self):
        return

    def delete(self, key=None):
        return

class ViewsTest(TestCase):
    def setUp(self):
        self.user = User.objects.create_user(
            username='test', email='<EMAIL>', password='<PASSWORD>')
        self.basic_auth = 'basic %s' % (base64.b64encode('test:top_secret'))
        self.server_address = "10.0.0.1"

        # customer api
        self.server, _ = Server.objects.get_or_create(
            name="server",
            address=self.server_address,
            hd="hd",
            memory=100,
            storage=100
        )
        self.customer, _ = Customer.objects.get_or_create(user=self.user)
        self.container, _ = Container.objects.get_or_create(
            customer=self.customer,
            server=self.server,
            memory=10,
            storage=10,
            name="container"
        )
        self.container2, _ = Container.objects.get_or_create(
            customer=self.customer,
            server=self.server,
            memory=10,
            storage=10,
            name="container2"
        )
        self.c_uid = self.container.uid
        self.domain, _ = Domain.objects.get_or_create(customer=self.customer, name="domain")
        self.d_uuid = self.domain.uuid
        self.tag, _ = Tag.objects.get_or_create(customer=self.customer, name="tag")
        self.loopbox, _ = Loopbox.objects.get_or_create(container=self.container, filename='filename', mountpoint='mountpoint')
        self.loopbox2, _ = Loopbox.objects.get_or_create(container=self.container2, filename='filename2', mountpoint='mountpoint2')
        self.loopbox3, _ = Loopbox.objects.get_or_create(container=self.container, filename='filename3', mountpoint='mountpoint3')
        self.l_id = self.loopbox.id
        self.l2_id = self.loopbox2.id
        self.l3_id = self.loopbox3.id
        self.container.tags.add(self.tag)
        self.domain.tags.add(self.tag)
        self.loopbox.tags.add(self.tag)
        self.loopbox2.tags.add(self.tag)
        # metrics
        today = datetime.datetime.today()
        NetworkRXContainerMetric.objects.create(
            container=self.container,
            year=today.year,
            month=today.month,
            day=today.day,
        )
        NetworkTXContainerMetric.objects.create(
            container=self.container,
            year=today.year,
            month=today.month,
            day=today.day,
        )
        CPUContainerMetric.objects.create(
            container=self.container,
            year=today.year,
            month=today.month,
            day=today.day,
        )
        MemoryContainerMetric.objects.create(
            container=self.container,
            year=today.year,
            month=today.month,
            day=today.day,
        )
        IOReadContainerMetric.objects.create(
            container=self.container,
            year=today.year,
            month=today.month,
            day=today.day,
        )
        IOWriteContainerMetric.objects.create(
            container=self.container,
            year=today.year,
            month=today.month,
            day=today.day,
        )
        QuotaContainerMetric.objects.create(
            container=self.container,
            year=today.year,
            month=today.month,
            day=today.day,
        )
        HitsDomainMetric.objects.create(
            domain=self.domain,
            container=self.container,
            year=today.year,
            month=today.month,
            day=today.day,
        )
        NetworkRXDomainMetric.objects.create(
            domain=self.domain,
            container=self.container,
            year=today.year,
            month=today.month,
            day=today.day,
        )
        NetworkTXDomainMetric.objects.create(
            domain=self.domain,
            container=self.container,
            year=today.year,
            month=today.month,
            day=today.day,
        )

        self.factory = RequestFactory()

    def logged_get_response_for_view(self, path, view, kwargs=None, params={}):
        headers = {
            'HTTP_AUTHORIZATION': self.basic_auth,
            'HTTPS_DN': 'hithere',
            'REMOTE_ADDR': self.server_address,
        }
        request = self.factory.get(path, params, **headers)
        request.user = self.user
        request.session = FakeSession()
        if kwargs is None:
            kwargs = {}
        return view(request, **kwargs)

class ApiTest(ViewsTest):
    def test_me(self):
        response = self.logged_get_response_for_view('/me', me)
        self.assertEqual(response.status_code, 200)

    def test_me_containers(self):
        response = self.logged_get_response_for_view('/me/containers', containers)
        self.assertEqual(response.status_code, 200)

    def test_containers(self):
        response = self.logged_get_response_for_view('/containers', containers)
        self.assertEqual(response.status_code, 200)

    def test_containers_filters_tags(self):
        response = self.logged_get_response_for_view('/containers', containers, params={'tags': 'tag'})
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, '"uid": {}'.format(self.c_uid))
        response = self.logged_get_response_for_view('/containers', containers, params={'tags': 'fail'})
        self.assertEqual(response.status_code, 200)
        self.assertNotContains(response, '"uid": {}'.format(self.c_uid))

    def test_container(self):
        response = self.logged_get_response_for_view('/containers/1', container, {'id': self.c_uid})
        self.assertEqual(response.status_code, 200)

    def test_distros(self):
        response = self.logged_get_response_for_view('/distros', distros)
        self.assertEqual(response.status_code, 200)

    def test_domains(self):
        response = self.logged_get_response_for_view('/domains', domains)
        self.assertEqual(response.status_code, 200)

    def test_domains_filters_tags(self):
        response = self.logged_get_response_for_view('/domains', domains, params={'tags': 'tag'})
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, '"uuid": "{}"'.format(self.d_uuid))
        response = self.logged_get_response_for_view('/domains', domains, params={'tags': 'fail'})
        self.assertEqual(response.status_code, 200)
        self.assertNotContains(response, 'uuid: "{}"'.format(self.d_uuid))

    def test_domain(self):
        response = self.logged_get_response_for_view('/domains/1', domain, {'id': self.domain.pk})
        self.assertEqual(response.status_code, 200)

    def test_loopboxes(self):
        response = self.logged_get_response_for_view('/loopboxes', loopboxes)
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, '"id": {}'.format(self.l_id))
        self.assertContains(response, '"id": {}'.format(self.l2_id))
        self.assertContains(response, '"id": {}'.format(self.l3_id))

    def test_loopboxes_filters_per_container(self):
        response = self.logged_get_response_for_view('/loopboxes', loopboxes, params={'container': self.c_uid})
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, '"id": {}'.format(self.l_id))
        self.assertContains(response, '"id": {}'.format(self.l3_id))
        self.assertNotContains(response, '"id": {}'.format(self.l2_id))

    def test_loopboxes_filters_per_tag(self):
        response = self.logged_get_response_for_view('/loopboxes', loopboxes, params={'tags': 'tag'})
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, '"id": {}'.format(self.l_id))
        self.assertContains(response, '"id": {}'.format(self.l2_id))
        self.assertNotContains(response, '"id": {}'.format(self.l3_id))

    def test_loopboxes_filters_per_tag_and_container(self):
        response = self.logged_get_response_for_view('/loopboxes', loopboxes, params={'tags': 'tag', 'container': self.c_uid})
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, '"id": {}'.format(self.l_id))
        self.assertNotContains(response, '"id": {}'.format(self.l2_id))
        self.assertNotContains(response, '"id": {}'.format(self.l3_id))

    def test_loopbox(self):
        response = self.logged_get_response_for_view('/loopboxes/{}'.format(self.l_id), loopbox, {'id': self.l_id})
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, '"id": {}'.format(self.l_id))

    def test_tags(self):
        response = self.logged_get_response_for_view('/tags', tags)
        self.assertEqual(response.status_code, 200)
{'fraction_non_alphanumeric': 0.11874234907544617, 'fraction_numerical': 0.010244185297339089, 'mean_word_length': 2.55032021957914, 'pattern_counts': {'":': 19, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '3933679', 'n_tokens_mistral': 4713, 'n_tokens_neox': 4497, 'n_words': 665}