@ -1,16 +1,19 @@
from django . test import TestCase , Client
import os
from django . urls import reverse
from django . test import TestCase , Client , RequestFactory
from django . urls import reverse , NoReverseMatch
from django . conf import settings
from django . conf import settings
from django . templatetags . static import static
from django . templatetags . static import static
from django . utils import timezone
from django . utils import timezone
from django . utils . translation import gettext_lazy as _
from django . core . files . uploadedfile import SimpleUploadedFile
from django . core . files . uploadedfile import SimpleUploadedFile
from unittest import mock
from unittest import mock
from importlib import reload
from importlib import reload
from members . models import Member , Group , DIVERSE
from members . models import Member , Group , DIVERSE
from startpage import urls
from startpage import urls
from startpage . views import redirect , handler500
from . models import Post , Section , Image
from . models import Post , Section , Image , Link , MemberOnPost
class BasicTestCase ( TestCase ) :
class BasicTestCase ( TestCase ) :
@ -25,7 +28,7 @@ class BasicTestCase(TestCase):
file = SimpleUploadedFile ( " post_image.jpg " , b " file_content " , content_type = " image/jpeg " )
file = SimpleUploadedFile ( " post_image.jpg " , b " file_content " , content_type = " image/jpeg " )
staff_post = Post . objects . create ( title = ' Staff ' , urlname = ' staff ' , website_text = ' This is our staff: Peter. ' ,
staff_post = Post . objects . create ( title = ' Staff ' , urlname = ' staff ' , website_text = ' This is our staff: Peter. ' ,
section = orga )
section = orga )
Image . objects . create ( post = staff_post , f = file )
self . image_with_file = Image . objects . create ( post = staff_post , f = file )
file = SimpleUploadedFile ( " member_image.jpg " , b " file_content " , content_type = " image/jpeg " )
file = SimpleUploadedFile ( " member_image.jpg " , b " file_content " , content_type = " image/jpeg " )
m = Member . objects . create ( prename = ' crazy ' , lastname = ' cool ' , birth_date = timezone . now ( ) . date ( ) ,
m = Member . objects . create ( prename = ' crazy ' , lastname = ' cool ' , birth_date = timezone . now ( ) . date ( ) ,
email = settings . TEST_MAIL , gender = DIVERSE ,
email = settings . TEST_MAIL , gender = DIVERSE ,
@ -39,6 +42,10 @@ class BasicTestCase(TestCase):
crazy_post . groups . add ( crazy_group )
crazy_post . groups . add ( crazy_group )
crazy_post . save ( )
crazy_post . save ( )
self . post_no_section = Post . objects . create ( title = ' No Section ' , urlname = ' no-section ' , section = None )
self . image_no_file = Image . objects . create ( post = staff_post )
self . test_link = Link . objects . create ( title = ' Test Link ' , url = ' https://example.com ' )
class ModelsTestCase ( BasicTestCase ) :
class ModelsTestCase ( BasicTestCase ) :
def test_str ( self ) :
def test_str ( self ) :
@ -66,6 +73,41 @@ class ModelsTestCase(BasicTestCase):
' /de/ {name} /last-trip ' . format ( name = settings . REPORTS_SECTION ) )
' /de/ {name} /last-trip ' . format ( name = settings . REPORTS_SECTION ) )
self . assertEqual ( post3 . absolute_urlname ( ) , reverse ( ' startpage:post ' , args = ( reports . urlname , ' last-trip ' ) ) )
self . assertEqual ( post3 . absolute_urlname ( ) , reverse ( ' startpage:post ' , args = ( reports . urlname , ' last-trip ' ) ) )
def test_post_absolute_section_none ( self ) :
""" Test Post.absolute_section when section is None """
self . assertEqual ( self . post_no_section . absolute_section ( ) , ' Aktuelles ' )
def test_post_absolute_urlname_no_section ( self ) :
""" Test Post.absolute_urlname when section is None """
expected_url = reverse ( ' startpage:post ' , args = ( ' aktuelles ' , ' no-section ' ) )
self . assertEqual ( self . post_no_section . absolute_urlname ( ) , expected_url )
def test_image_str_without_file ( self ) :
""" Test Image.__str__ when no file is associated """
self . assertEqual ( str ( self . image_no_file ) , str ( _ ( ' Empty ' ) ) )
def test_image_str_with_file ( self ) :
""" Test Image.__str__ when file is associated """
# The str should return basename of the file
expected = os . path . basename ( self . image_with_file . f . name )
self . assertEqual ( str ( self . image_with_file ) , expected )
def test_link_str ( self ) :
""" Test Link.__str__ method """
self . assertEqual ( str ( self . test_link ) , ' Test Link ' )
def test_section_absolute_urlname_no_reverse_match ( self ) :
""" Test Section.absolute_urlname when NoReverseMatch occurs """
section = Section . objects . get ( urlname = ' orga ' )
with mock . patch ( ' startpage.models.reverse ' , side_effect = NoReverseMatch ) :
self . assertEqual ( section . absolute_urlname ( ) , str ( _ ( ' deactivated ' ) ) )
def test_post_absolute_urlname_no_reverse_match ( self ) :
""" Test Post.absolute_urlname when NoReverseMatch occurs """
post = Post . objects . get ( urlname = ' staff ' )
with mock . patch ( ' startpage.models.reverse ' , side_effect = NoReverseMatch ) :
self . assertEqual ( post . absolute_urlname ( ) , str ( _ ( ' deactivated ' ) ) )
class ViewTestCase ( BasicTestCase ) :
class ViewTestCase ( BasicTestCase ) :
def test_index ( self ) :
def test_index ( self ) :
@ -137,9 +179,7 @@ class ViewTestCase(BasicTestCase):
def test_post_image ( self ) :
def test_post_image ( self ) :
c = Client ( )
c = Client ( )
staff_post = Post . objects . get ( urlname = ' staff ' )
url = self . image_with_file . f . url
img = Image . objects . get ( post = staff_post )
url = img . f . url
response = c . get ( ' /de ' + url )
response = c . get ( ' /de ' + url )
self . assertEqual ( response . status_code , 200 , ' Images on posts should be visible without login. ' )
self . assertEqual ( response . status_code , 200 , ' Images on posts should be visible without login. ' )
@ -155,3 +195,17 @@ class ViewTestCase(BasicTestCase):
url_names = [ pattern . name for pattern in urls . urlpatterns if hasattr ( pattern , ' name ' ) ]
url_names = [ pattern . name for pattern in urls . urlpatterns if hasattr ( pattern , ' name ' ) ]
self . assertIn ( ' index ' , url_names )
self . assertIn ( ' index ' , url_names )
self . assertEqual ( len ( urls . urlpatterns ) , 2 ) # Should have index and impressum only
self . assertEqual ( len ( urls . urlpatterns ) , 2 ) # Should have index and impressum only
def test_redirect_view ( self ) :
""" Test redirect view functionality """
request = RequestFactory ( ) . get ( ' / ' )
with mock . patch . object ( settings , ' STARTPAGE_REDIRECT_URL ' , ' https://example.com ' ) :
response = redirect ( request )
self . assertEqual ( response . status_code , 302 )
self . assertEqual ( response . url , ' https://example.com ' )
def test_handler500 ( self ) :
""" Test custom 500 error handler """
request = RequestFactory ( ) . get ( ' / ' )
response = handler500 ( request )
self . assertEqual ( response . status_code , 500 )