Skip to content

Commit

Permalink
[DONE] Dressing tests coveralls (#1036)
Browse files Browse the repository at this point in the history
* add unit tests

* fix

* fix flake

* fix

* tests for dressing_create

* fix

* fix

* fix

* add utils

* fix
  • Loading branch information
vsabatie authored Jan 31, 2024
1 parent 9c6bea5 commit 905d047
Show file tree
Hide file tree
Showing 2 changed files with 177 additions and 5 deletions.
43 changes: 40 additions & 3 deletions pod/dressing/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
import os
import unittest
from unittest.mock import patch
from pod.dressing.utils import get_position_value, get_dressing_input
from django.contrib.auth.models import User
from pod.authentication.models import AccessGroup
from pod.dressing.utils import get_dressings, get_position_value, get_dressing_input
from pod.dressing.models import Dressing


class DressingUtilitiesTests(unittest.TestCase):
"""TestCase for Esup-Pod dressing utilities."""

def test_get_position_value(self):
"""Test for the get_position_value function"""
"""Test for the get_position_value function."""
result = get_position_value("top_right", "720")
self.assertEqual(result, "overlay=main_w-overlay_w-36.0:36.0")

Expand Down Expand Up @@ -39,3 +40,39 @@ def test_get_dressing_input(self):
self.assertIn("", result)

print(" ---> test_get_dressing_input: OK! --- DressingUtilsTest")

def test_get_dressings(self):
"""Test for the get_dressings function."""
user = User.objects.create_user(
username="user", password="password", is_staff=1
)
access_group = AccessGroup.objects.create(code_name="group1", display_name="Group 1")

dressing1 = Dressing.objects.create(title='Dressing 1')
dressing1.owners.add(user.id)

dressing2 = Dressing.objects.create(title='Dressing 2')
dressing2.users.add(user.id)

dressing3 = Dressing.objects.create(title='Dressing 3')
dressing3.allow_to_groups.add(access_group)

dressings = get_dressings(user.id, [access_group])

self.assertIn(dressing1, dressings)
self.assertIn(dressing2, dressings)
self.assertIn(dressing3, dressings)

self.assertEqual(len(dressings), 3)

print(" ---> test_get_dressings: OK! --- DressingUtilsTest")

def test_get_dressings_empty(self):
"""Test for the get_dressings_empty function."""
new_user = User.objects.create_user(username='newuser', password='newpassword')

dressings = get_dressings(new_user, [])

self.assertEqual(len(dressings), 0)

print(" ---> test_get_dressings_empty: OK! --- DressingUtilsTest")
139 changes: 137 additions & 2 deletions pod/dressing/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
"""Unit tests for Esup-Pod dressing views."""

from django.contrib.auth.models import User
from django.contrib.messages import get_messages
from django.test import TestCase
from django.urls import reverse

from pod.dressing.forms import DressingDeleteForm
from pod.dressing.models import Dressing

from pod.video.models import Type, Video


class VideoDressingViewTest(TestCase):
"""Dressing page test case."""

fixtures = ["initial_data.json"]

def setUp(self) -> None:
Expand All @@ -33,3 +34,137 @@ def test_video_dressing_view(self):
"""Test for video_dressing view."""

print(" ---> test_video_dressing_view: OK! --- VideoDressingViewTest")


class DressingDeleteViewTest(TestCase):
"""Dressing delete page test case."""
fixtures = ["initial_data.json"]

def setUp(self) -> None:
self.user = User.objects.create_user(
username="user", password="password", is_staff=1
)
self.first_video = Video.objects.create(
title="First video",
slug="first-video",
owner=self.user,
video="first_video.mp4",
is_draft=False,
type=Type.objects.get(id=1),
)
self.dressing = Dressing.objects.create(title="Test Dressing")
self.dressing.owners.set([self.user])
self.dressing.users.set([self.user])

def test_dressing_delete_view_get(self):
self.client.force_login(self.user)
response = self.client.get(
reverse("dressing:dressing_delete", args=[self.dressing.id])
)
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, "dressing_delete.html")
self.assertEqual(response.context["dressing"], self.dressing)
self.assertIsInstance(response.context["form"], DressingDeleteForm)

def test_dressing_delete_view_get_not_authenticated(self):
response = self.client.get(
reverse("dressing:dressing_delete", args=[self.dressing.id])
)
self.assertEqual(response.status_code, 302) # Redirect to login page

def test_dressing_delete_view_post(self):
self.client.force_login(self.user)
form_data = {"confirm_deletion": True}
response = self.client.post(
reverse("dressing:dressing_delete", args=[self.dressing.id]), data=form_data
)
self.assertEqual(response.status_code, 200) # Redirect after successful deletion

def test_dressing_delete_view_post_not_authenticated(self):
form_data = {"confirm_deletion": True}
response = self.client.post(
reverse("dressing:dressing_delete", args=[self.dressing.id]), data=form_data
)
self.assertEqual(response.status_code, 302) # Redirect to login page
self.assertEqual(Dressing.objects.filter(id=self.dressing.id).count(), 1)

def test_dressing_delete_view_post_invalid_form(self):
self.client.force_login(self.user)
form_data = {} # Invalid form data
response = self.client.post(
reverse("dressing:dressing_delete", args=[self.dressing.id]), data=form_data
)
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, "dressing_delete.html")
self.assertEqual(response.context["dressing"], self.dressing)
self.assertIsInstance(response.context["form"], DressingDeleteForm)
messages = [m.message for m in get_messages(response.wsgi_request)]
self.assertIn("One or more errors have been found in the form.", messages)

def test_dressing_delete_view_permission_denied(self):
user_without_permission = User.objects.create_user(
username="nopermuser", password="testpass"
)
self.client.force_login(user_without_permission)
self.client.get(reverse("dressing:dressing_delete", args=[self.dressing.id]))

def test_dressing_delete_view_not_authenticated(self):
self.client.logout()
response = self.client.get(
reverse("dressing:dressing_delete", args=[self.dressing.id])
)
self.assertEqual(response.status_code, 302) # Redirect to login page

def test_dressing_video_delete(self):
"""Test for dressing_delete view."""
print(" ---> test_dressing_video_delete: OK! --- DressingDeleteViewTest")


class DressingCreateViewTest(TestCase):
"""Dressing create page test case."""
fixtures = ["initial_data.json"]

def setUp(self) -> None:
self.user = User.objects.create_user(
username="user", password="password", is_staff=1
)
self.first_video = Video.objects.create(
title="First video",
slug="first-video",
owner=self.user,
video="first_video.mp4",
is_draft=False,
type=Type.objects.get(id=1),
)
self.dressing = Dressing.objects.create(title="Test Dressing")
self.dressing.owners.set([self.user])
self.dressing.users.set([self.user])

def test_dressing_create_view_authenticated_user(self):
self.client.login(username='user', password='password')
response = self.client.get(reverse('dressing:dressing_create'))
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, 'dressing_edit.html')

def test_dressing_create_view_unauthenticated_user(self):
response = self.client.get(reverse('dressing:dressing_create'))
self.assertEqual(response.status_code, 302) # Redirect to login page

def test_dressing_create_form_submission(self):
Dressing.objects.all().delete()
self.client.login(username="user", password="password")
form_data = {
'title': 'Dressing test',
'owners': self.user.id,
'position': 'top_right',
'opacity': 50,
'opening_credits': 1,
'ending_credits': 1,
}
response = self.client.post(reverse('dressing:dressing_create'), data=form_data)
self.assertEqual(response.status_code, 302) # Redirect after creation of the video dressing
self.assertEqual(Dressing.objects.count(), 1)

def test_dressing_video_create(self):
"""Test for dressing_create view."""
print(" ---> test_dressing_video_create: OK! --- DressingCreateViewTest")

0 comments on commit 905d047

Please sign in to comment.