import factory
from rest_framework import test, status

from nodeconductor.logging.models import Alert
from nodeconductor.logging.tests.factories import AlertFactory
from nodeconductor.structure import SupportedServices
from nodeconductor.structure.models import CustomerRole
from nodeconductor.structure.tests import factories


class FilterAlertsByAggregateTest(test.APITransactionTestCase):

    def setUp(self):
        self.users = []
        self.customers = []
        self.projects = []

        self.project_alerts = []
        self.customer_alerts = []

        for i in range(2):
            customer = factories.CustomerFactory()
            user = factories.UserFactory()
            customer.add_user(user, CustomerRole.OWNER)

            project = factories.ProjectFactory(customer=customer)
            resource = self.create_resource(customer, project)
            spl = resource.service_project_link
            service = spl.service

            project_alerts = (
                AlertFactory(scope=project),
                AlertFactory(scope=resource),
                AlertFactory(scope=spl),
                AlertFactory(scope=service)
            )

            customer_alerts = project_alerts + (AlertFactory(scope=customer),)

            self.users.append(user)
            self.customers.append(customer)
            self.projects.append(project)

            self.project_alerts.append(project_alerts)
            self.customer_alerts.append(customer_alerts)

        Alert.objects.exclude(pk__in=[alert.pk for alerts in self.customer_alerts for alert in alerts]).delete()

    def test_alert_can_be_filtered_by_customer(self):
        for user, customer, alerts in zip(self.users, self.customers, self.customer_alerts):
            self.client.force_authenticate(user)
            query = {
                'aggregate': 'customer',
                'uuid': customer.uuid.hex
            }

            response = self.client.get(AlertFactory.get_list_url(), data=query)
            self.assertEqual(response.status_code, status.HTTP_200_OK)

            expected = set(alert.uuid.hex for alert in alerts)
            actual = set(alert['uuid'] for alert in response.data)
            self.assertEqual(expected, actual)

    def test_alert_can_be_filtered_by_project(self):
        for user, project, alerts in zip(self.users, self.projects, self.project_alerts):
            self.client.force_authenticate(user)
            query = {
                'aggregate': 'project',
                'uuid': project.uuid.hex
            }

            response = self.client.get(AlertFactory.get_list_url(), data=query)
            self.assertEqual(response.status_code, status.HTTP_200_OK)

            expected = set(alert.uuid.hex for alert in alerts)
            actual = set(alert['uuid'] for alert in response.data)
            self.assertEqual(expected, actual)

    def create_resource(self, customer, project):
        service_type, models = SupportedServices.get_service_models().items()[0]

        class ServiceFactory(factory.DjangoModelFactory):
            class Meta(object):
                model = models['service']

        class ServiceProjectLinkFactory(factory.DjangoModelFactory):
            class Meta(object):
                model = models['service_project_link']

        class ResourceFactory(factory.DjangoModelFactory):
            class Meta(object):
                model = models['resources'][0]

        settings = factories.ServiceSettingsFactory(customer=customer, type=service_type)
        service = ServiceFactory(customer=customer, settings=settings)
        spl = ServiceProjectLinkFactory(service=service, project=project)
        resource = ResourceFactory(service_project_link=spl)
        return resource

@@ -1497,37 +1497,51 @@
     
-Alert.objects.exclude(pk__in=
+# Cleanup other alerts%0A        alert_ids = 
 %5Bale
@@ -1601,16 +1601,63 @@
  alerts%5D
+%0A        Alert.objects.exclude(pk__in=alert_ids
 ).delete
@@ -1811,57 +1811,8 @@
 s):%0A
-            self.client.force_authenticate(user)%0A
     
@@ -1918,33 +1918,32 @@
 x%0A            %7D%0A
-%0A
             resp
@@ -1942,323 +1942,45 @@
     
-response = self.client.get(AlertFactory.get_list_url(), data=query)%0A            self.assertEqual(response.status_code, status.HTTP_200_OK)%0A%0A            expected = set(alert.uuid.hex for alert in alerts)%0A            actual = set(alert%5B'uuid'%5D for alert in response.data)%0A            self.assertEqual(expected, actual
+self.check_alerts(user, query, alerts
 )%0A%0A 
@@ -2287,21 +2287,162 @@
       %7D%0A
-%0A
     
+        self.check_alerts(user, query, alerts)%0A%0A    def check_alerts(self, user, query, alerts):%0A        self.client.force_authenticate(user)%0A
         
@@ -2509,36 +2509,32 @@
 =query)%0A        
-  
-  
 self.assertEqual
@@ -2585,20 +2585,16 @@
         
-    
 expected
@@ -2644,20 +2644,16 @@
         
- 
-   
 actual =
@@ -2699,20 +2699,16 @@
 e.data)%0A
-    
         

Refactor filter test (SAAS-721)