tests.py 15.9 KB
from django.http import response
from django.test import TestCase
from common.models import PyrosUser,ScientificProgram,Period,SP_Period,SP_Period_User,SP_Period_Guest,UserLevel,Institute,Country
from django.urls import reverse
from django.core import mail
from django.conf import settings
import unittest,os
# Create your tests here.
class ScientificProgramTests(TestCase):
    fixtures = ['tests/scientific_program_TZ.json']
    def setUp(self) -> None:
        """
        institute = Institute.objects.get(id=1)
        country = Country.objects.create()

        self.usr1 = PyrosUser.objects.create(username="toto", country=country, institute=institute)
        UserLevel.objects.get(name = "Observer").pyros_users.add(self.usr1)
        self.usr1.save()
        self.usr2 = PyrosUser.objects.create(username="titi", country=country, institute=institute)
        UserLevel.objects.get(name = "Unit-PI").pyros_users.add(self.usr2)
        self.usr2.save()
        self.usr3 = PyrosUser.objects.create(username="tata", country=country, institute=institute)
        UserLevel.objects.get(name = "Operator").pyros_users.add(self.usr3)
        self.usr3.save()
        self.usr4 = PyrosUser.objects.create(username="tutu", country=country, institute=institute)
        UserLevel.objects.get(name = "TAC").pyros_users.add(self.usr4)
        self.usr4.save()
        """
        password = "password123"
        Period.objects.create()
        Period.objects.create(start_date=Period.objects.all().first().end_date)
        self.usr1 = PyrosUser.objects.get(username="observer")
        self.usr2 = PyrosUser.objects.get(username="unit_pi")
        self.usr3 = PyrosUser.objects.get(username="tac")
        self.usr4 = PyrosUser.objects.get(username="operator")
        self.usr5 = PyrosUser.objects.get(username="observer2")


        self.usr1.set_password(password)
        self.usr1.save()
        self.usr2.set_password(password)
        self.usr2.save()
        self.usr3.set_password(password)
        self.usr3.save()
        self.usr4.set_password(password)
        self.usr4.save()
        self.usr5.set_password(password)
        self.usr5.save()
    def test_SCP_observer_can_create_sp(self):
        self.client.login(username=self.usr1,password="password123")
        path = reverse('create_scientific_program')
        response = self.client.post(path, {"name": "test", "description_short": "sp short desc", "description_long": "sp long desc",
                                           "public_visibility": "All", "quota_minimal": 10, "quota_nominal": 15,
                                           "over_quota_duration": 5, "token": 3,
                                           "users":"test@example.com,test2@example.com,test3@example.com"
                                           })
        self.assertEqual(response.status_code,302)
        self.assertEqual(len(mail.outbox),3)
        self.assertIn("test@example.com",mail.outbox[0].recipients())
        self.assertIn("test2@example.com",mail.outbox[1].recipients())
        self.assertIn("test3@example.com",mail.outbox[2].recipients())
        domain = settings.DEFAULT_DOMAIN
        url = f"{domain}{reverse('sp_register',args=(ScientificProgram.objects.all().order_by('-id').first().pk,Period.objects.all().order_by('-id').first().pk))}"
        self.assertIn(url,mail.outbox[2].body)
        self.assertEqual(ScientificProgram.objects.all().count(),1)  
        self.assertEqual(list(SP_Period_Guest.objects.all().values_list("email",flat=True)),["test@example.com","test2@example.com","test3@example.com"])
        self.assertEqual(SP_Period.objects.get(scientific_program__name="test").status,SP_Period.STATUSES_DRAFT)                                         
        self.assertEqual(ScientificProgram.objects.get(name="test").sp_pi,self.usr1)
        self.client.get(reverse("user_logout"))
        # u2 shouldn't be able to create a new scientific program
        self.client.login(username=self.usr2,password="password123")
        path = reverse('create_scientific_program')
        response = self.client.post(path, {"name": "test2", "description_short": "sp short desc", "description_long": "sp long desc",
                                           "public_visibility": "All", "quota_minimal": 10, "quota_nominal": 15,
                                           "over_quota_duration": 5, "token": 3,
                                           "users":"test@example.com,test2@example.com,test3@example.com"
                                           })
        self.assertEqual(response.status_code,302)
        self.assertEqual(len(mail.outbox),3)
        self.assertEqual(ScientificProgram.objects.all().count(),1)

    def test_SCP_view_sp_list(self):
        self.client.login(username=self.usr1,password="password123")
        path = reverse('create_scientific_program')
        self.client.post(path, {"name": "test", "description_short": "sp short desc", "description_long": "sp long desc",
                                           "public_visibility": "All", "quota_minimal": 10, "quota_nominal": 15,
                                           "over_quota_duration": 5, "token": 3,
                                           "users":"test@example.com,test2@example.com,test3@example.com"
                                           })
        
        self.client.post(path, {"name": "sp2", "description_short": "sp short desc", "description_long": "sp long desc",
                                           "public_visibility": "All", "quota_minimal": 10, "quota_nominal": 15,
                                           "over_quota_duration": 5, "token": 3,
                                           "users":"test@example.com,test2@example.com,test3@example.com"
                                           })
        self.client.get(reverse("user_logout"))
        # login with another user to create a SP that user 1 shouldn't be able to see
        
        self.client.login(username=self.usr5,password="password123")
        self.client.post(path, {"name": "third_project", "description_short": "sp short desc", "description_long": "sp long desc",
                                           "public_visibility": "All", "quota_minimal": 10, "quota_nominal": 15,
                                           "over_quota_duration": 5, "token": 3,
                                           "users":"test@example.com,test2@example.com,test3@example.com"
                                           })
        self.client.get(reverse("user_logout"))
        # log in again as user 1 to see the scientific program list
        self.client.login(username=self.usr1,password="password123")
        path = reverse("scientific_program_list")
        response = self.client.get(path)
        self.assertContains(response,"test")
        self.assertContains(response,"sp2")
        self.assertNotContains(response,"third_project")
        self.client.get(reverse("user_logout"))

        # log in as unit pi (user 2)
        self.client.login(username=self.usr2,password="password123")
        path = reverse("scientific_program_list")
        response = self.client.get(path)
        self.assertContains(response,"test")
        self.assertContains(response,"sp2")
        self.assertContains(response,"third_project")

        self.client.get(reverse("user_logout"))

        # log in as operator (shoudln't be able to see anything)
        self.client.login(username=self.usr4,password="password123")
        path = reverse("scientific_program_list")
        response = self.client.get(path)
        # user is redirected to his previous page, however it was the first page he visited to he is redirected to a None page
        self.assertEqual(response.status_code,302)
        self.assertEqual(response.context,None)

    def test_SCP_view_sp_details(self):
        self.client.login(username=self.usr1,password="password123")
        path = reverse('create_scientific_program')
        self.client.post(path, {"name": "test", "description_short": "sp short desc", "description_long": "sp long desc",
                                           "public_visibility": "All", "quota_minimal": 10, "quota_nominal": 15,
                                           "over_quota_duration": 5, "token": 3,
                                           "users":"test@example.com,test2@example.com,test3@example.com"
                                           })
        
        self.client.post(path, {"name": "sp2", "description_short": "sp short desc", "description_long": "sp long desc",
                                           "public_visibility": "All", "quota_minimal": 10, "quota_nominal": 15,
                                           "over_quota_duration": 5, "token": 3,
                                           "users":"test@example.com,test2@example.com,test3@example.com"
                                           })
        self.client.get(reverse("user_logout"))
        # login with another user to create a SP that user 1 shouldn't be able to see
        
        self.client.login(username=self.usr5,password="password123")
        self.client.post(path, {"name": "third_project", "description_short": "sp short desc", "description_long": "sp long desc",
                                           "public_visibility": "All", "quota_minimal": 10, "quota_nominal": 15,
                                           "over_quota_duration": 5, "token": 3,
                                           "users":"test@example.com,test2@example.com,test3@example.com"
                                           })
        self.client.get(reverse("user_logout"))
        # log in as user 1
        self.client.login(username=self.usr1,password="password123")
        path = reverse("detail_scientific_program",args=[ScientificProgram.objects.get(name="test").id])
        response = self.client.get(path)
        self.assertEqual(response.status_code,200)
        self.assertContains(response,"sp long desc")
        self.assertContains(response,"test")

        path = reverse("detail_scientific_program",args=[ScientificProgram.objects.get(name="sp2").id])
        response = self.client.get(path)
        self.assertEqual(response.status_code,200)
        self.assertContains(response,"sp long desc")
        self.assertContains(response,"sp2")

        self.client.get(reverse("user_logout"))

         # log in as user 2
        self.client.login(username=self.usr2,password="password123")
        path = reverse("detail_scientific_program",args=[ScientificProgram.objects.get(name="test").id])
        response = self.client.get(path)
        self.assertEqual(response.status_code,200)
        self.assertContains(response,"sp long desc")
        self.assertContains(response,"test")

        path = reverse("detail_scientific_program",args=[ScientificProgram.objects.get(name="sp2").id])
        response = self.client.get(path)
        self.assertEqual(response.status_code,200)
        self.assertContains(response,"sp long desc")
        self.assertContains(response,"sp2")

        path = reverse("detail_scientific_program",args=[ScientificProgram.objects.get(name="third_project").id])
        response = self.client.get(path)
        self.assertEqual(response.status_code,200)
        self.assertContains(response,"sp long desc")
        self.assertContains(response,"third_project")

        self.client.get(reverse("user_logout"))

        # log in as user 4 (operator)

        self.client.login(username=self.usr4,password="password123")
        path = reverse("detail_scientific_program",args=[ScientificProgram.objects.get(name="test").id])
        response = self.client.get(path)
        # user is redirected to his previous page, however it was the first page he visited to he is redirected to a None page
        self.assertEqual(response.status_code,302)
        self.assertEqual(response.context,None)

        path = reverse("detail_scientific_program",args=[ScientificProgram.objects.get(name="sp2").id])
        response = self.client.get(path)
        # user is redirected to his previous page, however it was the first page he visited to he is redirected to a None page
        self.assertEqual(response.status_code,302)
        self.assertEqual(response.context,None)

        path = reverse("detail_scientific_program",args=[ScientificProgram.objects.get(name="third_project").id])
        response = self.client.get(path)
        # user is redirected to his previous page, however it was the first page he visited to he is redirected to a None page
        self.assertEqual(response.status_code,302)
        self.assertEqual(response.context,None)
        
    def test_SCP_sp_update(self):
        self.client.login(username=self.usr1,password="password123")
        path = reverse('create_scientific_program')
        self.client.post(path, {"name": "sp1", "description_short": "sp short desc", "description_long": "sp long desc",
                                           "public_visibility": "All", "quota_minimal": 10, "quota_nominal": 15,
                                           "over_quota_duration": 5, "token": 3,
                                           "users":"test@example.com,test2@example.com,test3@example.com"
                                           })
        
        self.client.post(path, {"name": "sp2", "description_short": "sp short desc", "description_long": "sp long desc",
                                           "public_visibility": "All", "quota_minimal": 10, "quota_nominal": 15,
                                           "over_quota_duration": 5, "token": 3,
                                           "users":"test@example.com,test2@example.com,test3@example.com"
                                           })
        self.client.post(path, {"name": "sp3", "description_short": "sp short desc", "description_long": "sp long desc",
                                           "public_visibility": "All", "quota_minimal": 10, "quota_nominal": 15,
                                           "over_quota_duration": 5, "token": 3,
                                           "users":"test@example.com,test2@example.com,test3@example.com"
                                           })
        self.client.post(path, {"name": "sp4", "description_short": "sp short desc", "description_long": "sp long desc",
                                           "public_visibility": "All", "quota_minimal": 10, "quota_nominal": 15,
                                           "over_quota_duration": 5, "token": 3,
                                           "users":"test@example.com,test2@example.com,test3@example.com"
                                           })
        self.client.get(reverse("user_logout"))
        # login with another user to create a SP that user 1 shouldn't be able to see
        
        self.client.login(username=self.usr5,password="password123")
        self.client.post(path, {"name": "sp5", "description_short": "sp short desc", "description_long": "sp long desc",
                                           "public_visibility": "All", "quota_minimal": 10, "quota_nominal": 15,
                                           "over_quota_duration": 5, "token": 3,
                                           "users":"test@example.com,test2@example.com,test3@example.com"
                                           })
        self.client.get(reverse("user_logout"))
        # log in as user 1
        self.client.login(username=self.usr1,password="password123")
        period = Period.objects.all().order_by('-id').first()
        sp1 = ScientificProgram.objects.get(name="sp1")
        path = reverse("edit_scientific_program_period",args=[sp1.id,period.id])
        self.assertEqual(SP_Period.objects.get(scientific_program=sp1,period=period).status,SP_Period.STATUSES_DRAFT)
        self.client.post(path, {"name": "sp4", "description_short": "sp modified short desc", "description_long": "sp long desc",
                                           "public_visibility": "All", "quota_minimal": 15, "quota_nominal": 20,
                                           "over_quota_duration": 5, "token": 3,
                                           "users":"test@example.com,test2@example.com"
                                           })