tests.py 16.6 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 52 53 54
    def test_register_user_form_ok(self):
        """
        Should register a user correctly
        """
        c = Client()
        response = c.post(reverse('core:register'), {'first_name': 'Guy',
                                                     'last_name': 'Carlier',
                                                     'email': 'guy@git.an',
Skia's avatar
Skia committed
55
                                                     'date_of_birth': '12/6/1942',
Skia's avatar
Skia committed
56 57
                                                     'password1': 'plop',
                                                     'password2': 'plop',
58 59
                                                     'captcha_0': 'dummy-value',
                                                     'captcha_1': 'PASSED'
Krophil's avatar
Krophil committed
60
                                                     })
Skia's avatar
Skia committed
61 62 63 64 65 66 67 68 69 70 71
        self.assertTrue(response.status_code == 200)
        self.assertTrue('TEST_REGISTER_USER_FORM_OK' in str(response.content))

    def test_register_user_form_fail_password(self):
        """
        Should not register a user correctly
        """
        c = Client()
        response = c.post(reverse('core:register'), {'first_name': 'Guy',
                                                     'last_name': 'Carlier',
                                                     'email': 'bibou@git.an',
Skia's avatar
Skia committed
72
                                                     'date_of_birth': '12/6/1942',
Skia's avatar
Skia committed
73 74
                                                     'password1': 'plop',
                                                     'password2': 'plop2',
75 76
                                                     'captcha_0': 'dummy-value',
                                                     'captcha_1': 'PASSED'
Krophil's avatar
Krophil committed
77
                                                     })
Skia's avatar
Skia committed
78
        self.assertTrue(response.status_code == 200)
Skia's avatar
Skia committed
79
        self.assertTrue('TEST_REGISTER_USER_FORM_FAIL' in str(response.content))
Skia's avatar
Skia committed
80 81 82 83 84 85 86 87 88

    def test_register_user_form_fail_email(self):
        """
        Should not register a user correctly
        """
        c = Client()
        response = c.post(reverse('core:register'), {'first_name': 'Guy',
                                                     'last_name': 'Carlier',
                                                     'email': 'bibou.git.an',
Skia's avatar
Skia committed
89
                                                     'date_of_birth': '12/6/1942',
Skia's avatar
Skia committed
90 91
                                                     'password1': 'plop',
                                                     'password2': 'plop',
92 93
                                                     'captcha_0': 'dummy-value',
                                                     'captcha_1': 'PASSED'
Krophil's avatar
Krophil committed
94
                                                     })
Skia's avatar
Skia committed
95
        self.assertTrue(response.status_code == 200)
Skia's avatar
Skia committed
96
        self.assertTrue('TEST_REGISTER_USER_FORM_FAIL' in str(response.content))
Skia's avatar
Skia committed
97 98 99 100 101 102 103 104 105

    def test_register_user_form_fail_missing_name(self):
        """
        Should not register a user correctly
        """
        c = Client()
        response = c.post(reverse('core:register'), {'first_name': 'Guy',
                                                     'last_name': '',
                                                     'email': 'bibou@git.an',
Skia's avatar
Skia committed
106 107 108
                                                     'date_of_birth': '12/6/1942',
                                                     'password1': 'plop',
                                                     'password2': 'plop',
109 110
                                                     'captcha_0': 'dummy-value',
                                                     'captcha_1': 'PASSED'
Krophil's avatar
Krophil committed
111
                                                     })
Skia's avatar
Skia committed
112 113 114 115 116 117 118 119 120 121 122 123
        self.assertTrue(response.status_code == 200)
        self.assertTrue('TEST_REGISTER_USER_FORM_FAIL' in str(response.content))

    def test_register_user_form_fail_missing_date_of_birth(self):
        """
        Should not register a user correctly
        """
        c = Client()
        response = c.post(reverse('core:register'), {'first_name': '',
                                                     'last_name': 'Carlier',
                                                     'email': 'bibou@git.an',
                                                     'date_of_birth': '',
Skia's avatar
Skia committed
124 125
                                                     'password1': 'plop',
                                                     'password2': 'plop',
126 127
                                                     'captcha_0': 'dummy-value',
                                                     'captcha_1': 'PASSED'
Krophil's avatar
Krophil committed
128
                                                     })
Skia's avatar
Skia committed
129
        self.assertTrue(response.status_code == 200)
Skia's avatar
Skia committed
130
        self.assertTrue('TEST_REGISTER_USER_FORM_FAIL' in str(response.content))
Skia's avatar
Skia committed
131 132 133 134 135 136 137 138 139

    def test_register_user_form_fail_missing_first_name(self):
        """
        Should not register a user correctly
        """
        c = Client()
        response = c.post(reverse('core:register'), {'first_name': '',
                                                     'last_name': 'Carlier',
                                                     'email': 'bibou@git.an',
Skia's avatar
Skia committed
140
                                                     'date_of_birth': '12/6/1942',
Skia's avatar
Skia committed
141 142
                                                     'password1': 'plop',
                                                     'password2': 'plop',
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
                                                     'captcha_0': 'dummy-value',
                                                     'captcha_1': 'PASSED'
                                                     })
        self.assertTrue(response.status_code == 200)
        self.assertTrue('TEST_REGISTER_USER_FORM_FAIL' in str(response.content))

    def test_register_user_form_fail_wrong_captcha(self):
        """
        Should not register a user correctly
        """
        c = Client()
        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'
Krophil's avatar
Krophil committed
162
                                                     })
Skia's avatar
Skia committed
163
        self.assertTrue(response.status_code == 200)
Skia's avatar
Skia committed
164
        self.assertTrue('TEST_REGISTER_USER_FORM_FAIL' in str(response.content))
Skia's avatar
Skia committed
165 166 167 168 169 170 171 172 173

    def test_register_user_form_fail_already_exists(self):
        """
        Should not register a user correctly
        """
        c = Client()
        c.post(reverse('core:register'), {'first_name': 'Guy',
                                          'last_name': 'Carlier',
                                          'email': 'bibou@git.an',
Skia's avatar
Skia committed
174
                                          'date_of_birth': '12/6/1942',
Skia's avatar
Skia committed
175 176
                                          'password1': 'plop',
                                          'password2': 'plop',
177 178
                                          'captcha_0': 'dummy-value',
                                          'captcha_1': 'PASSED'
Krophil's avatar
Krophil committed
179
                                          })
Skia's avatar
Skia committed
180 181 182
        response = c.post(reverse('core:register'), {'first_name': 'Bibou',
                                                     'last_name': 'Carlier',
                                                     'email': 'bibou@git.an',
Skia's avatar
Skia committed
183
                                                     'date_of_birth': '12/6/1942',
Skia's avatar
Skia committed
184 185
                                                     'password1': 'plop',
                                                     'password2': 'plop',
186 187
                                                     'captcha_0': 'dummy-value',
                                                     'captcha_1': 'PASSED'
Krophil's avatar
Krophil committed
188
                                                     })
Skia's avatar
Skia committed
189
        self.assertTrue(response.status_code == 200)
Skia's avatar
Skia committed
190
        self.assertTrue('TEST_REGISTER_USER_FORM_FAIL' in str(response.content))
Skia's avatar
Skia committed
191

Skia's avatar
Skia committed
192 193 194 195 196 197 198 199
    def test_login_success(self):
        """
        Should login a user correctly
        """
        c = Client()
        c.post(reverse('core:register'), {'first_name': 'Guy',
                                          'last_name': 'Carlier',
                                          'email': 'bibou@git.an',
Skia's avatar
Skia committed
200
                                          'date_of_birth': '12/6/1942',
Skia's avatar
Skia committed
201 202
                                          'password1': 'plop',
                                          'password2': 'plop',
203 204
                                          'captcha_0': 'dummy-value',
                                          'captcha_1': 'PASSED'
Krophil's avatar
Krophil committed
205
                                          })
Skia's avatar
Skia committed
206
        response = c.post(reverse('core:login'), {'username': 'gcarlier', 'password': 'plop'})
Skia's avatar
Skia committed
207 208
        self.assertTrue(response.status_code == 302)
        #self.assertTrue('Hello, world' in str(response.content))
Skia's avatar
Skia committed
209 210 211 212 213 214 215 216 217

    def test_login_fail(self):
        """
        Should not login a user correctly
        """
        c = Client()
        c.post(reverse('core:register'), {'first_name': 'Guy',
                                          'last_name': 'Carlier',
                                          'email': 'bibou@git.an',
Skia's avatar
Skia committed
218
                                          'date_of_birth': '12/6/1942',
Skia's avatar
Skia committed
219 220
                                          'password1': 'plop',
                                          'password2': 'plop',
221 222
                                          'captcha_0': 'dummy-value',
                                          'captcha_1': 'PASSED'
Krophil's avatar
Krophil committed
223
                                          })
Skia's avatar
Skia committed
224 225
        response = c.post(reverse('core:login'), {'username': 'gcarlier', 'password': 'guy'})
        self.assertTrue(response.status_code == 200)
Skia's avatar
Skia committed
226
        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
227

Skia's avatar
Skia committed
228 229 230 231 232 233 234 235 236
class MarkdownTest(TestCase):
    def test_full_markdown_syntax(self):
        root_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
        with open(os.path.join(root_path) + '/doc/SYNTAX.md', 'r') as md_file:
            md = md_file.read()
        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)
Krophil's avatar
Krophil committed
237

238
class PageHandlingTest(TestCase):
Skia's avatar
Skia committed
239 240 241
    def setUp(self):
        try:
            Group.objects.create(name="root")
242 243
            u = User(username='root', last_name="", first_name="Bibou",
                     email="ae.info@utbm.fr",
Skia's avatar
Skia committed
244
                     date_of_birth="1942-06-12",
245 246 247 248
                     is_superuser=True, is_staff=True)
            u.set_password("plop")
            u.save()
            self.client.login(username='root', password='plop')
Skia's avatar
Skia committed
249 250
        except Exception as e:
            print(e)
Skia's avatar
Skia committed
251 252 253 254 255

    def test_create_page_ok(self):
        """
        Should create a page correctly
        """
Skia's avatar
Skia committed
256
        self.client.post(reverse('core:page_new'), {
Skia's avatar
Skia committed
257 258 259
            'parent': '',
            'name': 'guy',
            'owner_group': 1,
Krophil's avatar
Krophil committed
260
        })
261
        response = self.client.get(reverse('core:page', kwargs={'page_name': 'guy'}))
Skia's avatar
Skia committed
262
        self.assertTrue(response.status_code == 200)
Skia's avatar
Skia committed
263
        self.assertTrue('<a href="/page/guy/hist">' in str(response.content))
Skia's avatar
Skia committed
264 265 266 267 268

    def test_create_child_page_ok(self):
        """
        Should create a page correctly
        """
Skia's avatar
Skia committed
269
        self.client.post(reverse('core:page_new'), {
Skia's avatar
Skia committed
270 271 272
            'parent': '',
            'name': 'guy',
            'owner_group': '1',
Krophil's avatar
Krophil committed
273
        })
Skia's avatar
Skia committed
274
        response = self.client.post(reverse('core:page_new'), {
Skia's avatar
Skia committed
275 276 277
            'parent': '1',
            'name': 'bibou',
            'owner_group': '1',
Krophil's avatar
Krophil committed
278
        })
279
        response = self.client.get(reverse('core:page', kwargs={'page_name': 'guy/bibou'}))
Skia's avatar
Skia committed
280
        self.assertTrue(response.status_code == 200)
Skia's avatar
Skia committed
281
        self.assertTrue('<a href="/page/guy/bibou/">' in str(response.content))
Skia's avatar
Skia committed
282 283 284 285 286

    def test_access_child_page_ok(self):
        """
        Should display a page correctly
        """
Skia's avatar
Skia committed
287 288 289 290
        parent = Page(name="guy", owner_group=Group.objects.filter(id=1).first())
        parent.save(force_lock=True)
        page = Page(name="bibou", owner_group=Group.objects.filter(id=1).first(), parent=parent)
        page.save(force_lock=True)
291
        response = self.client.get(reverse('core:page', kwargs={'page_name': 'guy/bibou'}))
Skia's avatar
Skia committed
292
        self.assertTrue(response.status_code == 200)
Skia's avatar
Skia committed
293
        self.assertTrue('<a href="/page/guy/bibou/edit">\\xc3\\x89diter</a>' in str(response.content))
Skia's avatar
Skia committed
294 295 296 297 298

    def test_access_page_not_found(self):
        """
        Should not display a page correctly
        """
299
        response = self.client.get(reverse('core:page', kwargs={'page_name': 'swagg'}))
Skia's avatar
Skia committed
300
        response = self.client.get("/page/swagg/")
Skia's avatar
Skia committed
301
        self.assertTrue(response.status_code == 200)
Skia's avatar
Skia committed
302
        self.assertTrue('<a href="/page/create?page=swagg">' in str(response.content))
303 304 305 306 307

    def test_create_page_markdown_safe(self):
        """
        Should format the markdown and escape html correctly
        """
Skia's avatar
Skia committed
308
        self.client.post(reverse('core:page_new'), {
Skia's avatar
Skia committed
309 310 311
            'parent': '',
            'name': 'guy',
            'owner_group': '1',
Krophil's avatar
Krophil committed
312 313 314 315 316
        })
        self.client.post(reverse('core:page_edit', kwargs={'page_name': 'guy'}), {
            'title': 'Bibou',
            'content':
            '''Guy *bibou*
317 318 319 320 321 322 323 324 325

http://git.an

# Swag

<guy>Bibou</guy>

<script>alert('Guy');</script>
''',
Krophil's avatar
Krophil committed
326
        })
327 328 329
        response = self.client.get(reverse('core:page', kwargs={'page_name': 'guy'}))
        self.assertTrue(response.status_code == 200)
        self.assertTrue('<p>Guy <em>bibou</em></p>\\n<p><a href="http://git.an">http://git.an</a></p>\\n' +
Krophil's avatar
Krophil committed
330 331
                        '<h1>Swag</h1>\\n&lt;guy&gt;Bibou&lt;/guy&gt;' +
                        "&lt;script&gt;alert(\\'Guy\\');&lt;/script&gt;" in str(response.content))
332

Krophil's avatar
Krophil committed
333
# TODO: many tests on the pages:
Skia's avatar
Skia committed
334 335 336
#   - 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
337

Krophil's avatar
Krophil committed
338

Skia's avatar
Skia committed
339 340 341 342 343 344 345 346 347 348
class FileHandlingTest(TestCase):
    def setUp(self):
        try:
            call_command("populate")
            self.subscriber = User.objects.filter(username="subscriber").first()
            self.client.login(username='subscriber', password='plop')
        except Exception as e:
            print(e)

    def test_create_folder_home(self):
Krophil's avatar
Krophil committed
349 350
        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
351
        self.assertTrue(response.status_code == 302)
Krophil's avatar
Krophil committed
352
        response = self.client.get(reverse("core:file_detail", kwargs={"file_id": self.subscriber.home.id}))
Skia's avatar
Skia committed
353 354 355 356 357
        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:
Krophil's avatar
Krophil committed
358 359
            response = self.client.post(reverse("core:file_detail", kwargs={"file_id": self.subscriber.home.id}),
                                        {"file_field": f})
Skia's avatar
Skia committed
360
        self.assertTrue(response.status_code == 302)
Krophil's avatar
Krophil committed
361
        response = self.client.get(reverse("core:file_detail", kwargs={"file_id": self.subscriber.home.id}))
Skia's avatar
Skia committed
362 363
        self.assertTrue(response.status_code == 200)
        self.assertTrue("ls</a>" in str(response.content))