tests.py 2.48 KB
from django.test import TestCase
from common.models import *
import os

SAVED_REQUESTS_FOLDER = "misc/saved_requests/"

class TestRoutineManager(TestCase):

    fixtures = ["tests/routine_mgr_test.json"]

    def setUp(self):
        pass

    def test_import_fake_file(self):
        self.client.login(username="test@test.test", password="test")
        path = "/routine_manager/import_request"
        response = self.client.post(path, {"request_file": "toto.xml"}, follow=True)
        self.assertTrue("error" in response.context.keys(), "There should be an error of non existant file")
        self.assertEqual(Request.objects.count(), 1, "There should still be only one request")

    def test_import_invalid_file(self):
        self.client.login(username="test@test.test", password="test")
        path = "/routine_manager/import_request"
        with open("manage.py") as file:
            response = self.client.post(path, {"request_file": file}, follow=True)
        self.assertTrue("error" in response.context.keys(), "There should be an error of invalid file")
        self.assertEqual(Request.objects.count(), 1, "There should still be only one request")

    def test_import(self):
        self.client.login(username="test@test.test", password="test")
        path = "/routine_manager/import_request"
        with open(SAVED_REQUESTS_FOLDER + "request_unittest.xml") as file:
            response = self.client.post(path, {"request_file": file}, follow=True)
        self.assertTrue("success" in response.context.keys(), "There should be a success message")
        self.assertEqual(Request.objects.count(), 2, "There should still be two request")

    def test_export_incomplete(self):
        req = Request.objects.get()
        req.complete = False
        req.save()
        self.client.login(username="test@test.test", password="test")
        path = "/routine_manager/export_request/" + str(req.id)
        response = self.client.get(path, follow=True)
        self.assertTrue("error" in response.context.keys(), "There should be an error of incomplete request")


    def test_export(self):
        req = Request.objects.get()
        self.client.login(username="test@test.test", password="test")
        path = "/routine_manager/export_request/" + str(req.id)
        response = self.client.get(path, follow=True)
        file_path = SAVED_REQUESTS_FOLDER + "request" + str(req.id) + ".xml"
        self.assertTrue(os.path.isfile(file_path), "There should be a new file %s" % (file_path,))
        os.remove(file_path)