tests.py 14.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
# -*- 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.
#
#

Skia's avatar
Skia committed
25 26
import os

Skia's avatar
Skia committed
27
from django.test import Client, TestCase
Skia's avatar
Skia committed
28
from django.core.urlresolvers import reverse
Skia's avatar
Skia committed
29
from django.core.management import call_command
Skia's avatar
Skia committed
30

Skia's avatar
Skia committed
31
from core.models import User, Group, Page
Skia's avatar
Skia committed
32
from core.markdown import markdown
Skia's avatar
Skia committed
33

34 35 36 37 38
"""
to run these tests :
    python3 manage.py test
"""

Krophil's avatar
Krophil committed
39

40
class UserRegistrationTest(TestCase):
Skia's avatar
Skia committed
41 42 43
    def setUp(self):
        try:
            Group.objects.create(name="root")
Skia's avatar
Skia committed
44 45
        except Exception as e:
            print(e)
Skia's avatar
Skia committed
46

Skia's avatar
Skia committed
47 48 49 50 51
    def test_register_user_form_ok(self):
        """
        Should register a user correctly
        """
        c = Client()
Sli's avatar
Sli committed
52 53 54 55 56 57 58 59 60 61 62 63 64
        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",
            },
        )
Skia's avatar
Skia committed
65
        self.assertTrue(response.status_code == 200)
Sli's avatar
Sli committed
66
        self.assertTrue("TEST_REGISTER_USER_FORM_OK" in str(response.content))
Skia's avatar
Skia committed
67 68 69 70 71 72

    def test_register_user_form_fail_password(self):
        """
        Should not register a user correctly
        """
        c = Client()
Sli's avatar
Sli committed
73 74 75 76 77 78 79 80 81 82 83 84 85
        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",
            },
        )
Skia's avatar
Skia committed
86
        self.assertTrue(response.status_code == 200)
Sli's avatar
Sli committed
87
        self.assertTrue("TEST_REGISTER_USER_FORM_FAIL" in str(response.content))
Skia's avatar
Skia committed
88 89 90 91 92 93

    def test_register_user_form_fail_email(self):
        """
        Should not register a user correctly
        """
        c = Client()
Sli's avatar
Sli committed
94 95 96 97 98 99 100 101 102 103 104 105 106
        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",
            },
        )
Skia's avatar
Skia committed
107
        self.assertTrue(response.status_code == 200)
Sli's avatar
Sli committed
108
        self.assertTrue("TEST_REGISTER_USER_FORM_FAIL" in str(response.content))
Skia's avatar
Skia committed
109 110 111 112 113 114

    def test_register_user_form_fail_missing_name(self):
        """
        Should not register a user correctly
        """
        c = Client()
Sli's avatar
Sli committed
115 116 117 118 119 120 121 122 123 124 125 126 127
        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",
            },
        )
Skia's avatar
Skia committed
128
        self.assertTrue(response.status_code == 200)
Sli's avatar
Sli committed
129
        self.assertTrue("TEST_REGISTER_USER_FORM_FAIL" in str(response.content))
Skia's avatar
Skia committed
130 131 132 133 134 135

    def test_register_user_form_fail_missing_date_of_birth(self):
        """
        Should not register a user correctly
        """
        c = Client()
Sli's avatar
Sli committed
136 137 138 139 140 141 142 143 144 145 146 147 148
        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",
            },
        )
Skia's avatar
Skia committed
149
        self.assertTrue(response.status_code == 200)
Sli's avatar
Sli committed
150
        self.assertTrue("TEST_REGISTER_USER_FORM_FAIL" in str(response.content))
Skia's avatar
Skia committed
151 152 153 154 155 156

    def test_register_user_form_fail_missing_first_name(self):
        """
        Should not register a user correctly
        """
        c = Client()
Sli's avatar
Sli committed
157 158 159 160 161 162 163 164 165 166 167 168 169
        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",
            },
        )
170
        self.assertTrue(response.status_code == 200)
Sli's avatar
Sli committed
171
        self.assertTrue("TEST_REGISTER_USER_FORM_FAIL" in str(response.content))
172 173 174 175 176 177

    def test_register_user_form_fail_wrong_captcha(self):
        """
        Should not register a user correctly
        """
        c = Client()
Sli's avatar
Sli committed
178 179 180 181 182 183 184 185 186 187 188 189 190
        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",
            },
        )
Skia's avatar
Skia committed
191
        self.assertTrue(response.status_code == 200)
Sli's avatar
Sli committed
192
        self.assertTrue("TEST_REGISTER_USER_FORM_FAIL" in str(response.content))
Skia's avatar
Skia committed
193 194 195 196 197 198

    def test_register_user_form_fail_already_exists(self):
        """
        Should not register a user correctly
        """
        c = Client()
Sli's avatar
Sli committed
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
        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",
            },
        )
Skia's avatar
Skia committed
225
        self.assertTrue(response.status_code == 200)
Sli's avatar
Sli committed
226
        self.assertTrue("TEST_REGISTER_USER_FORM_FAIL" in str(response.content))
Skia's avatar
Skia committed
227

Skia's avatar
Skia committed
228 229 230 231 232
    def test_login_success(self):
        """
        Should login a user correctly
        """
        c = Client()
Sli's avatar
Sli committed
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
        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"}
        )
Skia's avatar
Skia committed
249
        self.assertTrue(response.status_code == 302)
Sli's avatar
Sli committed
250
        # self.assertTrue('Hello, world' in str(response.content))
Skia's avatar
Skia committed
251 252 253 254 255 256

    def test_login_fail(self):
        """
        Should not login a user correctly
        """
        c = Client()
Sli's avatar
Sli committed
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
        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"}
        )
Skia's avatar
Skia committed
273
        self.assertTrue(response.status_code == 200)
Sli's avatar
Sli committed
274 275 276 277 278
        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)
        )

Skia's avatar
Skia committed
279

Skia's avatar
Skia committed
280 281 282
class MarkdownTest(TestCase):
    def test_full_markdown_syntax(self):
        root_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
Sli's avatar
Sli committed
283
        with open(os.path.join(root_path) + "/doc/SYNTAX.md", "r") as md_file:
Skia's avatar
Skia committed
284
            md = md_file.read()
Sli's avatar
Sli committed
285
        with open(os.path.join(root_path) + "/doc/SYNTAX.html", "r") as html_file:
Skia's avatar
Skia committed
286 287 288
            html = html_file.read()
        result = markdown(md)
        self.assertTrue(result == html)
Krophil's avatar
Krophil committed
289

Sli's avatar
Sli committed
290

291
class PageHandlingTest(TestCase):
Skia's avatar
Skia committed
292 293 294
    def setUp(self):
        try:
            Group.objects.create(name="root")
Sli's avatar
Sli committed
295 296 297 298 299 300 301 302 303
            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,
            )
304 305
            u.set_password("plop")
            u.save()
Sli's avatar
Sli committed
306
            self.client.login(username="root", password="plop")
Skia's avatar
Skia committed
307 308
        except Exception as e:
            print(e)
Skia's avatar
Skia committed
309 310 311 312 313

    def test_create_page_ok(self):
        """
        Should create a page correctly
        """
Sli's avatar
Sli committed
314 315 316 317
        self.client.post(
            reverse("core:page_new"), {"parent": "", "name": "guy", "owner_group": 1}
        )
        response = self.client.get(reverse("core:page", kwargs={"page_name": "guy"}))
Skia's avatar
Skia committed
318
        self.assertTrue(response.status_code == 200)
Skia's avatar
Skia committed
319
        self.assertTrue('<a href="/page/guy/hist">' in str(response.content))
Skia's avatar
Skia committed
320 321 322 323 324

    def test_create_child_page_ok(self):
        """
        Should create a page correctly
        """
Sli's avatar
Sli committed
325 326 327 328 329 330 331 332 333 334
        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"})
        )
Skia's avatar
Skia committed
335
        self.assertTrue(response.status_code == 200)
Skia's avatar
Skia committed
336
        self.assertTrue('<a href="/page/guy/bibou/">' in str(response.content))
Skia's avatar
Skia committed
337 338 339 340 341

    def test_access_child_page_ok(self):
        """
        Should display a page correctly
        """
Skia's avatar
Skia committed
342 343
        parent = Page(name="guy", owner_group=Group.objects.filter(id=1).first())
        parent.save(force_lock=True)
Sli's avatar
Sli committed
344 345 346
        page = Page(
            name="bibou", owner_group=Group.objects.filter(id=1).first(), parent=parent
        )
Skia's avatar
Skia committed
347
        page.save(force_lock=True)
Sli's avatar
Sli committed
348 349 350
        response = self.client.get(
            reverse("core:page", kwargs={"page_name": "guy/bibou"})
        )
Skia's avatar
Skia committed
351
        self.assertTrue(response.status_code == 200)
Sli's avatar
Sli committed
352 353 354 355
        self.assertTrue(
            '<a href="/page/guy/bibou/edit">\\xc3\\x89diter</a>'
            in str(response.content)
        )
Skia's avatar
Skia committed
356 357 358 359 360

    def test_access_page_not_found(self):
        """
        Should not display a page correctly
        """
Sli's avatar
Sli committed
361
        response = self.client.get(reverse("core:page", kwargs={"page_name": "swagg"}))
Skia's avatar
Skia committed
362
        response = self.client.get("/page/swagg/")
Skia's avatar
Skia committed
363
        self.assertTrue(response.status_code == 200)
Skia's avatar
Skia committed
364
        self.assertTrue('<a href="/page/create?page=swagg">' in str(response.content))
365 366 367 368 369

    def test_create_page_markdown_safe(self):
        """
        Should format the markdown and escape html correctly
        """
Sli's avatar
Sli committed
370 371 372 373 374 375 376 377
        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*
378 379 380 381 382 383 384 385

http://git.an

# Swag

<guy>Bibou</guy>

<script>alert('Guy');</script>
Sli's avatar
Sli committed
386 387 388 389
""",
            },
        )
        response = self.client.get(reverse("core:page", kwargs={"page_name": "guy"}))
390
        self.assertTrue(response.status_code == 200)
Sli's avatar
Sli committed
391 392 393 394 395 396 397
        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)
        )

398

Krophil's avatar
Krophil committed
399
# TODO: many tests on the pages:
Skia's avatar
Skia committed
400 401 402
#   - renaming a page
#   - changing a page's parent --> check that page's children's full_name
#   - changing the different groups of the page
Skia's avatar
Skia committed
403

Krophil's avatar
Krophil committed
404

Skia's avatar
Skia committed
405 406 407 408 409
class FileHandlingTest(TestCase):
    def setUp(self):
        try:
            call_command("populate")
            self.subscriber = User.objects.filter(username="subscriber").first()
Sli's avatar
Sli committed
410
            self.client.login(username="subscriber", password="plop")
Skia's avatar
Skia committed
411 412 413 414
        except Exception as e:
            print(e)

    def test_create_folder_home(self):
Sli's avatar
Sli committed
415 416 417 418
        response = self.client.post(
            reverse("core:file_detail", kwargs={"file_id": self.subscriber.home.id}),
            {"folder_name": "GUY_folder_test"},
        )
Skia's avatar
Skia committed
419
        self.assertTrue(response.status_code == 302)
Sli's avatar
Sli committed
420 421 422
        response = self.client.get(
            reverse("core:file_detail", kwargs={"file_id": self.subscriber.home.id})
        )
Skia's avatar
Skia committed
423 424 425 426 427
        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:
Sli's avatar
Sli committed
428 429 430 431 432 433
            response = self.client.post(
                reverse(
                    "core:file_detail", kwargs={"file_id": self.subscriber.home.id}
                ),
                {"file_field": f},
            )
Skia's avatar
Skia committed
434
        self.assertTrue(response.status_code == 302)
Sli's avatar
Sli committed
435 436 437
        response = self.client.get(
            reverse("core:file_detail", kwargs={"file_id": self.subscriber.home.id})
        )
Skia's avatar
Skia committed
438 439
        self.assertTrue(response.status_code == 200)
        self.assertTrue("ls</a>" in str(response.content))