tests.py 13.7 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
from django.test import Client, TestCase
Skia's avatar
Skia committed
26
from django.core.urlresolvers import reverse
Skia's avatar
Skia committed
27
from django.core.management import call_command
Skia's avatar
Skia committed
28

Skia's avatar
Skia committed
29
from core.models import User, Group, Page
Skia's avatar
Skia committed
30

31 32 33 34 35
"""
to run these tests :
    python3 manage.py test
"""

Krophil's avatar
Krophil committed
36

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

Skia's avatar
Skia committed
44 45 46 47 48 49 50 51
    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
52
                                                     'date_of_birth': '12/6/1942',
Skia's avatar
Skia committed
53 54
                                                     'password1': 'plop',
                                                     'password2': 'plop',
Krophil's avatar
Krophil committed
55
                                                     })
Skia's avatar
Skia committed
56 57 58 59 60 61 62 63 64 65 66
        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
67
                                                     'date_of_birth': '12/6/1942',
Skia's avatar
Skia committed
68 69
                                                     'password1': 'plop',
                                                     'password2': 'plop2',
Krophil's avatar
Krophil committed
70
                                                     })
Skia's avatar
Skia committed
71
        self.assertTrue(response.status_code == 200)
Skia's avatar
Skia committed
72
        self.assertTrue('TEST_REGISTER_USER_FORM_FAIL' in str(response.content))
Skia's avatar
Skia committed
73 74 75 76 77 78 79 80 81

    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
82
                                                     'date_of_birth': '12/6/1942',
Skia's avatar
Skia committed
83 84
                                                     'password1': 'plop',
                                                     'password2': 'plop',
Krophil's avatar
Krophil committed
85
                                                     })
Skia's avatar
Skia committed
86
        self.assertTrue(response.status_code == 200)
Skia's avatar
Skia committed
87
        self.assertTrue('TEST_REGISTER_USER_FORM_FAIL' in str(response.content))
Skia's avatar
Skia committed
88 89 90 91 92 93 94 95 96

    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
97 98 99
                                                     'date_of_birth': '12/6/1942',
                                                     'password1': 'plop',
                                                     'password2': 'plop',
Krophil's avatar
Krophil committed
100
                                                     })
Skia's avatar
Skia committed
101 102 103 104 105 106 107 108 109 110 111 112
        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
113 114
                                                     'password1': 'plop',
                                                     'password2': 'plop',
Krophil's avatar
Krophil committed
115
                                                     })
Skia's avatar
Skia committed
116
        self.assertTrue(response.status_code == 200)
Skia's avatar
Skia committed
117
        self.assertTrue('TEST_REGISTER_USER_FORM_FAIL' in str(response.content))
Skia's avatar
Skia committed
118 119 120 121 122 123 124 125 126

    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
127
                                                     'date_of_birth': '12/6/1942',
Skia's avatar
Skia committed
128 129
                                                     'password1': 'plop',
                                                     'password2': 'plop',
Krophil's avatar
Krophil committed
130
                                                     })
Skia's avatar
Skia committed
131
        self.assertTrue(response.status_code == 200)
Skia's avatar
Skia committed
132
        self.assertTrue('TEST_REGISTER_USER_FORM_FAIL' in str(response.content))
Skia's avatar
Skia committed
133 134 135 136 137 138 139 140 141

    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
142
                                          'date_of_birth': '12/6/1942',
Skia's avatar
Skia committed
143 144
                                          'password1': 'plop',
                                          'password2': 'plop',
Krophil's avatar
Krophil committed
145
                                          })
Skia's avatar
Skia committed
146 147 148
        response = c.post(reverse('core:register'), {'first_name': 'Bibou',
                                                     'last_name': 'Carlier',
                                                     'email': 'bibou@git.an',
Skia's avatar
Skia committed
149
                                                     'date_of_birth': '12/6/1942',
Skia's avatar
Skia committed
150 151
                                                     'password1': 'plop',
                                                     'password2': 'plop',
Krophil's avatar
Krophil committed
152
                                                     })
Skia's avatar
Skia committed
153
        self.assertTrue(response.status_code == 200)
Skia's avatar
Skia committed
154
        self.assertTrue('TEST_REGISTER_USER_FORM_FAIL' in str(response.content))
Skia's avatar
Skia committed
155

Skia's avatar
Skia committed
156 157 158 159 160 161 162 163
    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
164
                                          'date_of_birth': '12/6/1942',
Skia's avatar
Skia committed
165 166
                                          'password1': 'plop',
                                          'password2': 'plop',
Krophil's avatar
Krophil committed
167
                                          })
Skia's avatar
Skia committed
168
        response = c.post(reverse('core:login'), {'username': 'gcarlier', 'password': 'plop'})
Skia's avatar
Skia committed
169 170
        self.assertTrue(response.status_code == 302)
        #self.assertTrue('Hello, world' in str(response.content))
Skia's avatar
Skia committed
171 172 173 174 175 176 177 178 179

    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
180
                                          'date_of_birth': '12/6/1942',
Skia's avatar
Skia committed
181 182
                                          'password1': 'plop',
                                          'password2': 'plop',
Krophil's avatar
Krophil committed
183
                                          })
Skia's avatar
Skia committed
184 185
        response = c.post(reverse('core:login'), {'username': 'gcarlier', 'password': 'guy'})
        self.assertTrue(response.status_code == 200)
Skia's avatar
Skia committed
186
        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
187

Krophil's avatar
Krophil committed
188

189
class PageHandlingTest(TestCase):
Skia's avatar
Skia committed
190 191 192
    def setUp(self):
        try:
            Group.objects.create(name="root")
193 194
            u = User(username='root', last_name="", first_name="Bibou",
                     email="ae.info@utbm.fr",
Skia's avatar
Skia committed
195
                     date_of_birth="1942-06-12",
196 197 198 199
                     is_superuser=True, is_staff=True)
            u.set_password("plop")
            u.save()
            self.client.login(username='root', password='plop')
Skia's avatar
Skia committed
200 201
        except Exception as e:
            print(e)
Skia's avatar
Skia committed
202 203 204 205 206

    def test_create_page_ok(self):
        """
        Should create a page correctly
        """
Skia's avatar
Skia committed
207
        self.client.post(reverse('core:page_new'), {
Skia's avatar
Skia committed
208 209 210
            'parent': '',
            'name': 'guy',
            'owner_group': 1,
Krophil's avatar
Krophil committed
211
        })
212
        response = self.client.get(reverse('core:page', kwargs={'page_name': 'guy'}))
Skia's avatar
Skia committed
213
        self.assertTrue(response.status_code == 200)
Skia's avatar
Skia committed
214
        self.assertTrue('<a href="/page/guy/hist">' in str(response.content))
Skia's avatar
Skia committed
215 216 217 218 219

    def test_create_child_page_ok(self):
        """
        Should create a page correctly
        """
Skia's avatar
Skia committed
220
        self.client.post(reverse('core:page_new'), {
Skia's avatar
Skia committed
221 222 223
            'parent': '',
            'name': 'guy',
            'owner_group': '1',
Krophil's avatar
Krophil committed
224
        })
Skia's avatar
Skia committed
225
        response = self.client.post(reverse('core:page_new'), {
Skia's avatar
Skia committed
226 227 228
            'parent': '1',
            'name': 'bibou',
            'owner_group': '1',
Krophil's avatar
Krophil committed
229
        })
230
        response = self.client.get(reverse('core:page', kwargs={'page_name': 'guy/bibou'}))
Skia's avatar
Skia committed
231
        self.assertTrue(response.status_code == 200)
Skia's avatar
Skia committed
232
        self.assertTrue('<a href="/page/guy/bibou/">' in str(response.content))
Skia's avatar
Skia committed
233 234 235 236 237

    def test_access_child_page_ok(self):
        """
        Should display a page correctly
        """
Skia's avatar
Skia committed
238 239 240 241
        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)
242
        response = self.client.get(reverse('core:page', kwargs={'page_name': 'guy/bibou'}))
Skia's avatar
Skia committed
243
        self.assertTrue(response.status_code == 200)
Skia's avatar
Skia committed
244
        self.assertTrue('<a href="/page/guy/bibou/edit">\\xc3\\x89diter</a>' in str(response.content))
Skia's avatar
Skia committed
245 246 247 248 249

    def test_access_page_not_found(self):
        """
        Should not display a page correctly
        """
250
        response = self.client.get(reverse('core:page', kwargs={'page_name': 'swagg'}))
Skia's avatar
Skia committed
251
        response = self.client.get("/page/swagg/")
Skia's avatar
Skia committed
252
        self.assertTrue(response.status_code == 200)
Skia's avatar
Skia committed
253
        self.assertTrue('<a href="/page/create?page=swagg">' in str(response.content))
254 255 256 257 258

    def test_create_page_markdown_safe(self):
        """
        Should format the markdown and escape html correctly
        """
Skia's avatar
Skia committed
259
        self.client.post(reverse('core:page_new'), {
Skia's avatar
Skia committed
260 261 262
            'parent': '',
            'name': 'guy',
            'owner_group': '1',
Krophil's avatar
Krophil committed
263 264 265 266 267
        })
        self.client.post(reverse('core:page_edit', kwargs={'page_name': 'guy'}), {
            'title': 'Bibou',
            'content':
            '''Guy *bibou*
268 269 270 271 272 273 274 275 276

http://git.an

# Swag

<guy>Bibou</guy>

<script>alert('Guy');</script>
''',
Krophil's avatar
Krophil committed
277
        })
278 279 280
        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
281 282
                        '<h1>Swag</h1>\\n&lt;guy&gt;Bibou&lt;/guy&gt;' +
                        "&lt;script&gt;alert(\\'Guy\\');&lt;/script&gt;" in str(response.content))
283

Krophil's avatar
Krophil committed
284
# TODO: many tests on the pages:
Skia's avatar
Skia committed
285 286 287
#   - 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
288

Krophil's avatar
Krophil committed
289

Skia's avatar
Skia committed
290 291 292 293 294 295 296 297 298 299
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
300 301
        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
302
        self.assertTrue(response.status_code == 302)
Krophil's avatar
Krophil committed
303
        response = self.client.get(reverse("core:file_detail", kwargs={"file_id": self.subscriber.home.id}))
Skia's avatar
Skia committed
304 305 306 307 308
        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
309 310
            response = self.client.post(reverse("core:file_detail", kwargs={"file_id": self.subscriber.home.id}),
                                        {"file_field": f})
Skia's avatar
Skia committed
311
        self.assertTrue(response.status_code == 302)
Krophil's avatar
Krophil committed
312
        response = self.client.get(reverse("core:file_detail", kwargs={"file_id": self.subscriber.home.id}))
Skia's avatar
Skia committed
313 314
        self.assertTrue(response.status_code == 200)
        self.assertTrue("ls</a>" in str(response.content))