Sith/core/tests.py

474 lines
16 KiB
Python
Raw Permalink Normal View History

# -*- coding:utf-8 -*
#
# Copyright 2016,2017
# - Skia <skia@libskia.so>
#
# Ce fichier fait partie du site de l'Association des Étudiants de l'UTBM,
# http://ae.utbm.fr.
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License a published by the Free Software
# Foundation; either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Sofware Foundation, Inc., 59 Temple
# Place - Suite 330, Boston, MA 02111-1307, USA.
#
#
import os
2016-01-28 09:13:45 +00:00
from django.test import Client, TestCase
from django.urls import reverse
2016-01-28 09:13:45 +00:00
from django.core.management import call_command
2015-11-19 10:23:08 +00:00
2016-12-14 17:05:19 +00:00
from core.models import User, Group, Page
from core.markdown import markdown
2015-11-19 10:23:08 +00:00
"""
to run these tests :
python3 manage.py test
"""
2017-06-12 07:42:03 +00:00
class UserRegistrationTest(TestCase):
2015-11-24 13:01:10 +00:00
def setUp(self):
try:
Group.objects.create(name="root")
2016-01-28 09:13:45 +00:00
except Exception as e:
print(e)
2015-11-24 13:01:10 +00:00
2015-11-19 10:23:08 +00:00
def test_register_user_form_ok(self):
"""
Should register a user correctly
"""
c = Client()
2018-10-04 19:29:19 +00:00
response = c.post(
reverse("core:register"),
{
"first_name": "Guy",
"last_name": "Carlier",
"email": "guy@git.an",
"date_of_birth": "12/6/1942",
"password1": "plop",
"password2": "plop",
"captcha_0": "dummy-value",
"captcha_1": "PASSED",
},
)
2015-11-19 10:23:08 +00:00
self.assertTrue(response.status_code == 200)
2018-10-04 19:29:19 +00:00
self.assertTrue("TEST_REGISTER_USER_FORM_OK" in str(response.content))
2015-11-19 10:23:08 +00:00
def test_register_user_form_fail_password(self):
"""
Should not register a user correctly
"""
c = Client()
2018-10-04 19:29:19 +00:00
response = c.post(
reverse("core:register"),
{
"first_name": "Guy",
"last_name": "Carlier",
"email": "bibou@git.an",
"date_of_birth": "12/6/1942",
"password1": "plop",
"password2": "plop2",
"captcha_0": "dummy-value",
"captcha_1": "PASSED",
},
)
2015-11-19 10:23:08 +00:00
self.assertTrue(response.status_code == 200)
2018-10-04 19:29:19 +00:00
self.assertTrue("TEST_REGISTER_USER_FORM_FAIL" in str(response.content))
2015-11-19 10:23:08 +00:00
def test_register_user_form_fail_email(self):
"""
Should not register a user correctly
"""
c = Client()
2018-10-04 19:29:19 +00:00
response = c.post(
reverse("core:register"),
{
"first_name": "Guy",
"last_name": "Carlier",
"email": "bibou.git.an",
"date_of_birth": "12/6/1942",
"password1": "plop",
"password2": "plop",
"captcha_0": "dummy-value",
"captcha_1": "PASSED",
},
)
2015-11-19 10:23:08 +00:00
self.assertTrue(response.status_code == 200)
2018-10-04 19:29:19 +00:00
self.assertTrue("TEST_REGISTER_USER_FORM_FAIL" in str(response.content))
2015-11-19 10:23:08 +00:00
def test_register_user_form_fail_missing_name(self):
"""
Should not register a user correctly
"""
c = Client()
2018-10-04 19:29:19 +00:00
response = c.post(
reverse("core:register"),
{
"first_name": "Guy",
"last_name": "",
"email": "bibou@git.an",
"date_of_birth": "12/6/1942",
"password1": "plop",
"password2": "plop",
"captcha_0": "dummy-value",
"captcha_1": "PASSED",
},
)
2015-11-22 17:23:21 +00:00
self.assertTrue(response.status_code == 200)
2018-10-04 19:29:19 +00:00
self.assertTrue("TEST_REGISTER_USER_FORM_FAIL" in str(response.content))
2015-11-22 17:23:21 +00:00
def test_register_user_form_fail_missing_date_of_birth(self):
"""
Should not register a user correctly
"""
c = Client()
2018-10-04 19:29:19 +00:00
response = c.post(
reverse("core:register"),
{
"first_name": "",
"last_name": "Carlier",
"email": "bibou@git.an",
"date_of_birth": "",
"password1": "plop",
"password2": "plop",
"captcha_0": "dummy-value",
"captcha_1": "PASSED",
},
)
2015-11-19 10:23:08 +00:00
self.assertTrue(response.status_code == 200)
2018-10-04 19:29:19 +00:00
self.assertTrue("TEST_REGISTER_USER_FORM_FAIL" in str(response.content))
2015-11-19 10:23:08 +00:00
def test_register_user_form_fail_missing_first_name(self):
"""
Should not register a user correctly
"""
c = Client()
2018-10-04 19:29:19 +00:00
response = c.post(
reverse("core:register"),
{
"first_name": "",
"last_name": "Carlier",
"email": "bibou@git.an",
"date_of_birth": "12/6/1942",
"password1": "plop",
"password2": "plop",
"captcha_0": "dummy-value",
"captcha_1": "PASSED",
},
)
2018-07-06 09:35:02 +00:00
self.assertTrue(response.status_code == 200)
2018-10-04 19:29:19 +00:00
self.assertTrue("TEST_REGISTER_USER_FORM_FAIL" in str(response.content))
2018-07-06 09:35:02 +00:00
def test_register_user_form_fail_wrong_captcha(self):
"""
Should not register a user correctly
"""
c = Client()
2018-10-04 19:29:19 +00:00
response = c.post(
reverse("core:register"),
{
"first_name": "Bibou",
"last_name": "Carlier",
"email": "bibou@git.an",
"date_of_birth": "12/6/1942",
"password1": "plop",
"password2": "plop",
"captcha_0": "dummy-value",
"captcha_1": "WRONG_CAPTCHA",
},
)
2015-11-19 10:23:08 +00:00
self.assertTrue(response.status_code == 200)
2018-10-04 19:29:19 +00:00
self.assertTrue("TEST_REGISTER_USER_FORM_FAIL" in str(response.content))
2015-11-19 10:23:08 +00:00
def test_register_user_form_fail_already_exists(self):
"""
Should not register a user correctly
"""
c = Client()
2018-10-04 19:29:19 +00:00
c.post(
reverse("core:register"),
{
"first_name": "Guy",
"last_name": "Carlier",
"email": "bibou@git.an",
"date_of_birth": "12/6/1942",
"password1": "plop",
"password2": "plop",
"captcha_0": "dummy-value",
"captcha_1": "PASSED",
},
)
response = c.post(
reverse("core:register"),
{
"first_name": "Bibou",
"last_name": "Carlier",
"email": "bibou@git.an",
"date_of_birth": "12/6/1942",
"password1": "plop",
"password2": "plop",
"captcha_0": "dummy-value",
"captcha_1": "PASSED",
},
)
2015-11-19 10:23:08 +00:00
self.assertTrue(response.status_code == 200)
2018-10-04 19:29:19 +00:00
self.assertTrue("TEST_REGISTER_USER_FORM_FAIL" in str(response.content))
2015-11-18 08:44:06 +00:00
2015-11-19 13:44:48 +00:00
def test_login_success(self):
"""
Should login a user correctly
"""
c = Client()
2018-10-04 19:29:19 +00:00
c.post(
reverse("core:register"),
{
"first_name": "Guy",
"last_name": "Carlier",
"email": "bibou@git.an",
"date_of_birth": "12/6/1942",
"password1": "plop",
"password2": "plop",
"captcha_0": "dummy-value",
"captcha_1": "PASSED",
},
)
response = c.post(
reverse("core:login"), {"username": "gcarlier", "password": "plop"}
)
2015-12-02 16:14:47 +00:00
self.assertTrue(response.status_code == 302)
2018-10-04 19:29:19 +00:00
# self.assertTrue('Hello, world' in str(response.content))
2015-11-19 13:44:48 +00:00
def test_login_fail(self):
"""
Should not login a user correctly
"""
c = Client()
2018-10-04 19:29:19 +00:00
c.post(
reverse("core:register"),
{
"first_name": "Guy",
"last_name": "Carlier",
"email": "bibou@git.an",
"date_of_birth": "12/6/1942",
"password1": "plop",
"password2": "plop",
"captcha_0": "dummy-value",
"captcha_1": "PASSED",
},
)
response = c.post(
reverse("core:login"), {"username": "gcarlier", "password": "guy"}
)
2015-11-19 13:44:48 +00:00
self.assertTrue(response.status_code == 200)
2018-10-04 19:29:19 +00:00
self.assertTrue(
"""<p>Votre nom d\\'utilisateur et votre mot de passe ne correspondent pas. Merci de r\\xc3\\xa9essayer.</p>"""
in str(response.content)
)
2015-12-02 16:14:47 +00:00
class MarkdownTest(TestCase):
def test_full_markdown_syntax(self):
root_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
2018-10-04 19:29:19 +00:00
with open(os.path.join(root_path) + "/doc/SYNTAX.md", "r") as md_file:
md = md_file.read()
2018-10-04 19:29:19 +00:00
with open(os.path.join(root_path) + "/doc/SYNTAX.html", "r") as html_file:
html = html_file.read()
result = markdown(md)
self.assertTrue(result == html)
2017-06-12 07:42:03 +00:00
2018-10-04 19:29:19 +00:00
class PageHandlingTest(TestCase):
2015-12-02 16:14:47 +00:00
def setUp(self):
try:
Group.objects.create(name="root")
2018-10-04 19:29:19 +00:00
u = User(
username="root",
last_name="",
first_name="Bibou",
email="ae.info@utbm.fr",
date_of_birth="1942-06-12",
is_superuser=True,
is_staff=True,
)
u.set_password("plop")
u.save()
2018-10-04 19:29:19 +00:00
self.client.login(username="root", password="plop")
2016-01-28 09:13:45 +00:00
except Exception as e:
print(e)
2015-11-24 09:53:16 +00:00
def test_create_page_ok(self):
"""
Should create a page correctly
"""
2018-10-04 19:29:19 +00:00
self.client.post(
reverse("core:page_new"), {"parent": "", "name": "guy", "owner_group": 1}
)
response = self.client.get(reverse("core:page", kwargs={"page_name": "guy"}))
2015-11-24 09:53:16 +00:00
self.assertTrue(response.status_code == 200)
2016-08-02 20:20:06 +00:00
self.assertTrue('<a href="/page/guy/hist">' in str(response.content))
2015-11-24 09:53:16 +00:00
def test_create_child_page_ok(self):
"""
Should create a page correctly
"""
2018-10-04 19:29:19 +00:00
self.client.post(
reverse("core:page_new"), {"parent": "", "name": "guy", "owner_group": "1"}
)
response = self.client.post(
reverse("core:page_new"),
{"parent": "1", "name": "bibou", "owner_group": "1"},
)
response = self.client.get(
reverse("core:page", kwargs={"page_name": "guy/bibou"})
)
2015-11-24 09:53:16 +00:00
self.assertTrue(response.status_code == 200)
2016-08-02 20:20:06 +00:00
self.assertTrue('<a href="/page/guy/bibou/">' in str(response.content))
2015-11-24 09:53:16 +00:00
def test_access_child_page_ok(self):
"""
Should display a page correctly
"""
2016-12-14 17:05:19 +00:00
parent = Page(name="guy", owner_group=Group.objects.filter(id=1).first())
parent.save(force_lock=True)
2018-10-04 19:29:19 +00:00
page = Page(
name="bibou", owner_group=Group.objects.filter(id=1).first(), parent=parent
)
2016-12-14 17:05:19 +00:00
page.save(force_lock=True)
2018-10-04 19:29:19 +00:00
response = self.client.get(
reverse("core:page", kwargs={"page_name": "guy/bibou"})
)
2015-11-24 09:53:16 +00:00
self.assertTrue(response.status_code == 200)
2018-10-04 19:29:19 +00:00
self.assertTrue(
'<a href="/page/guy/bibou/edit">\\xc3\\x89diter</a>'
in str(response.content)
)
2015-11-24 09:53:16 +00:00
def test_access_page_not_found(self):
"""
Should not display a page correctly
"""
2018-10-04 19:29:19 +00:00
response = self.client.get(reverse("core:page", kwargs={"page_name": "swagg"}))
2016-12-14 17:05:19 +00:00
response = self.client.get("/page/swagg/")
2015-11-24 09:53:16 +00:00
self.assertTrue(response.status_code == 200)
2016-08-02 20:20:06 +00:00
self.assertTrue('<a href="/page/create?page=swagg">' in str(response.content))
def test_create_page_markdown_safe(self):
"""
Should format the markdown and escape html correctly
"""
2018-10-04 19:29:19 +00:00
self.client.post(
reverse("core:page_new"), {"parent": "", "name": "guy", "owner_group": "1"}
)
self.client.post(
reverse("core:page_edit", kwargs={"page_name": "guy"}),
{
"title": "Bibou",
"content": """Guy *bibou*
http://git.an
# Swag
<guy>Bibou</guy>
<script>alert('Guy');</script>
2018-10-04 19:29:19 +00:00
""",
},
)
response = self.client.get(reverse("core:page", kwargs={"page_name": "guy"}))
self.assertTrue(response.status_code == 200)
2018-10-04 19:29:19 +00:00
self.assertTrue(
'<p>Guy <em>bibou</em></p>\\n<p><a href="http://git.an">http://git.an</a></p>\\n'
+ "<h1>Swag</h1>\\n&lt;guy&gt;Bibou&lt;/guy&gt;"
+ "&lt;script&gt;alert(\\'Guy\\');&lt;/script&gt;"
in str(response.content)
)
2019-07-15 10:36:05 +00:00
class UserToolsTest(TestCase):
def setUp(self):
call_command("populate")
def test_anonymous_user_unauthorized(self):
response = self.client.get(reverse("core:user_tools"))
2022-08-03 19:48:37 +00:00
self.assertEqual(response.status_code, 403)
2019-07-15 10:36:05 +00:00
def test_page_is_working(self):
# Test for simple user
2019-07-15 10:36:05 +00:00
self.client.login(username="guy", password="plop")
response = self.client.get(reverse("core:user_tools"))
2022-08-03 19:48:37 +00:00
self.assertNotEqual(response.status_code, 500)
self.assertEqual(response.status_code, 200)
2019-07-15 10:36:05 +00:00
# Test for root
self.client.login(username="root", password="plop")
response = self.client.get(reverse("core:user_tools"))
2022-08-03 19:48:37 +00:00
self.assertNotEqual(response.status_code, 500)
self.assertEqual(response.status_code, 200)
# Test for skia
self.client.login(username="skia", password="plop")
response = self.client.get(reverse("core:user_tools"))
2022-08-03 19:48:37 +00:00
self.assertNotEqual(response.status_code, 500)
self.assertEqual(response.status_code, 200)
# Test for comunity
self.client.login(username="comunity", password="plop")
response = self.client.get(reverse("core:user_tools"))
2022-08-03 19:48:37 +00:00
self.assertNotEqual(response.status_code, 500)
self.assertEqual(response.status_code, 200)
2019-07-15 10:36:05 +00:00
2017-06-12 07:42:03 +00:00
# TODO: many tests on the pages:
2015-11-24 13:01:10 +00:00
# - renaming a page
# - changing a page's parent --> check that page's children's full_name
# - changing the different groups of the page
2016-12-09 14:48:09 +00:00
2017-06-12 07:42:03 +00:00
2016-12-09 14:48:09 +00:00
class FileHandlingTest(TestCase):
def setUp(self):
try:
call_command("populate")
self.subscriber = User.objects.filter(username="subscriber").first()
2018-10-04 19:29:19 +00:00
self.client.login(username="subscriber", password="plop")
2016-12-09 14:48:09 +00:00
except Exception as e:
print(e)
def test_create_folder_home(self):
2018-10-04 19:29:19 +00:00
response = self.client.post(
reverse("core:file_detail", kwargs={"file_id": self.subscriber.home.id}),
{"folder_name": "GUY_folder_test"},
)
2016-12-09 14:48:09 +00:00
self.assertTrue(response.status_code == 302)
2018-10-04 19:29:19 +00:00
response = self.client.get(
reverse("core:file_detail", kwargs={"file_id": self.subscriber.home.id})
)
2016-12-09 14:48:09 +00:00
self.assertTrue(response.status_code == 200)
self.assertTrue("GUY_folder_test</a>" in str(response.content))
def test_upload_file_home(self):
with open("/bin/ls", "rb") as f:
2018-10-04 19:29:19 +00:00
response = self.client.post(
reverse(
"core:file_detail", kwargs={"file_id": self.subscriber.home.id}
),
{"file_field": f},
)
2016-12-09 14:48:09 +00:00
self.assertTrue(response.status_code == 302)
2018-10-04 19:29:19 +00:00
response = self.client.get(
reverse("core:file_detail", kwargs={"file_id": self.subscriber.home.id})
)
2016-12-09 14:48:09 +00:00
self.assertTrue(response.status_code == 200)
self.assertTrue("ls</a>" in str(response.content))