tests.py 3.46 KB
from django.test import TestCase
import os
import shutil
import time
import alert_mgmt.tasks

from django.conf import settings

from common.models import *

#from utils.Logger import *
#log = setupLogger("test", "test")

TEST_FILE = "unittest_voevent.xml"

TEST_FILE_PATH = os.path.join(alert_mgmt.tasks.VOEVENTS_PATH, TEST_FILE)

VOEVENTS_TO_SEND_PATH = "alert_manager/events_to_send"


class TestStrategyChange(TestCase):

    # In /src/misc/ folder !!!
    fixtures = ['tests/alert_mgr_test_TZ.json']

    def setUp(self):
        # (EP) BAD !!! Ca ne fonctionne que pcq la fixture ne contient que UNE alerte !!!
        self.alert = Alert.objects.get()
        # Pour info, une alerte n'a pas d'id propre, mais on peut la lire par son Request id:
        #self.alert = Alert.objects.get(pk=65)
        #print("alert read is", self.alert)
        #print("alert request read is", self.alert.request)
        self.strat1 = Alert.objects.get().strategyobs
        self.strat2 = StrategyObs.objects.exclude(id=self.strat1.id)[0]
        #print("strategies are", self.strat1, self.strat2)
        # A quoi ça sert ça, c'est déjà le cas non ???
        self.alert.strategyobs = self.strat1
        self.alert.save()

    def test_change_inexistant_strat(self):
        self.client.login(username="test@test.test", password="test")

        path = "/alert_mgmt/change_obs_strategy_validate/" + str(self.alert.id)
        response = self.client.post(path, {"strategy_choice": 42})
        self.assertTrue("error" in response.context.keys(), "There should be an error of non existant strategy")

    def test_change_inexistant_alert(self):
        self.client.login(username="test@test.test", password="test")
        path = "/alert_mgmt/change_obs_strategy_validate/" + str(self.alert.id + 1)
        response = self.client.post(path, {"strategy_choice": self.strat2.id})
        self.assertTrue("error" in response.context.keys(), "There should be an error of non existant alert")

    def test_change_same_strat(self):
        self.client.login(username="test@test.test", password="test")
        path = "/alert_mgmt/change_obs_strategy_validate/" + str(self.alert.id)
        response = self.client.post(path, {"strategy_choice": self.strat1.id})
        self.assertTrue("error" in response.context.keys(), "There should be an error of changing to same strategy")

    def test_change_all_working(self):
        print("\n===== test_change_all_working =====\n")

        # alert id is 65 with strategy strat1
        print("Alert request read is", self.alert.request)
        print("- its id is the same as its request id, which is", self.alert.id)
        print("- its current strategy is", self.alert.strategyobs)

        # strat1 and strat2
        print("Strategies available in fixture are", self.strat1, self.strat2)

        self.client.login(username="test@test.test", password="test")

        # change strategy to strat2
        path = "/alert_mgmt/change_obs_strategy_validate/" + str(self.alert.id)
        # call view alert_manager.views.change_obs_strategy_validate(65)
        response = self.client.post(path, {"strategy_choice": self.strat2.id})
        self.assertFalse("error" in response.context.keys(), "There shouldn't be an error")
        self.assertEqual(Alert.objects.count(), 2, "There should be 2 alerts after the strategy change")
        new_alert = Alert.objects.exclude(id=self.alert.id)[0]
        self.assertEqual(new_alert.strategyobs.id, self.strat2.id, "The new alert should have the 'strat2' strategy")