tests.py 12.8 KB
Newer Older
Skia's avatar
Skia committed
1
from django.test import Client, TestCase
Skia's avatar
Skia committed
2
from django.core.urlresolvers import reverse
Skia's avatar
Skia committed
3
from django.contrib.auth.models import Group
Skia's avatar
Skia committed
4
from django.core.management import call_command
Skia's avatar
Skia committed
5

Skia's avatar
Skia committed
6
from core.models import User, Group, Page
Skia's avatar
Skia committed
7

8 9 10 11 12 13
"""
to run these tests :
    python3 manage.py test
"""

class UserRegistrationTest(TestCase):
Skia's avatar
Skia committed
14 15 16
    def setUp(self):
        try:
            Group.objects.create(name="root")
Skia's avatar
Skia committed
17 18
        except Exception as e:
            print(e)
Skia's avatar
Skia committed
19

Skia's avatar
Skia committed
20 21 22 23 24 25 26 27
    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
28
                                                     'date_of_birth': '12/6/1942',
Skia's avatar
Skia committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42
                                                     'password1': 'plop',
                                                     'password2': 'plop',
                                                    })
        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
43
                                                     'date_of_birth': '12/6/1942',
Skia's avatar
Skia committed
44 45 46 47
                                                     'password1': 'plop',
                                                     'password2': 'plop2',
                                                    })
        self.assertTrue(response.status_code == 200)
Skia's avatar
Skia committed
48
        self.assertTrue('TEST_REGISTER_USER_FORM_FAIL' in str(response.content))
Skia's avatar
Skia committed
49 50 51 52 53 54 55 56 57

    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
58
                                                     'date_of_birth': '12/6/1942',
Skia's avatar
Skia committed
59 60 61 62
                                                     'password1': 'plop',
                                                     'password2': 'plop',
                                                    })
        self.assertTrue(response.status_code == 200)
Skia's avatar
Skia committed
63
        self.assertTrue('TEST_REGISTER_USER_FORM_FAIL' in str(response.content))
Skia's avatar
Skia committed
64 65 66 67 68 69 70 71 72

    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
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
                                                     'date_of_birth': '12/6/1942',
                                                     'password1': 'plop',
                                                     'password2': 'plop',
                                                    })
        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
89 90 91 92
                                                     'password1': 'plop',
                                                     'password2': 'plop',
                                                    })
        self.assertTrue(response.status_code == 200)
Skia's avatar
Skia committed
93
        self.assertTrue('TEST_REGISTER_USER_FORM_FAIL' in str(response.content))
Skia's avatar
Skia committed
94 95 96 97 98 99 100 101 102

    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
103
                                                     'date_of_birth': '12/6/1942',
Skia's avatar
Skia committed
104 105 106 107
                                                     'password1': 'plop',
                                                     'password2': 'plop',
                                                    })
        self.assertTrue(response.status_code == 200)
Skia's avatar
Skia committed
108
        self.assertTrue('TEST_REGISTER_USER_FORM_FAIL' in str(response.content))
Skia's avatar
Skia committed
109 110 111 112 113 114 115 116 117

    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
118
                                          'date_of_birth': '12/6/1942',
Skia's avatar
Skia committed
119 120 121 122 123 124
                                          'password1': 'plop',
                                          'password2': 'plop',
                                         })
        response = c.post(reverse('core:register'), {'first_name': 'Bibou',
                                                     'last_name': 'Carlier',
                                                     'email': 'bibou@git.an',
Skia's avatar
Skia committed
125
                                                     'date_of_birth': '12/6/1942',
Skia's avatar
Skia committed
126 127 128 129
                                                     'password1': 'plop',
                                                     'password2': 'plop',
                                                    })
        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

Skia's avatar
Skia committed
132 133 134 135 136 137 138 139
    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
140
                                          'date_of_birth': '12/6/1942',
Skia's avatar
Skia committed
141 142 143 144
                                          'password1': 'plop',
                                          'password2': 'plop',
                                         })
        response = c.post(reverse('core:login'), {'username': 'gcarlier', 'password': 'plop'})
Skia's avatar
Skia committed
145 146
        self.assertTrue(response.status_code == 302)
        #self.assertTrue('Hello, world' in str(response.content))
Skia's avatar
Skia committed
147 148 149 150 151 152 153 154 155

    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
156
                                          'date_of_birth': '12/6/1942',
Skia's avatar
Skia committed
157 158 159 160 161
                                          'password1': 'plop',
                                          'password2': 'plop',
                                         })
        response = c.post(reverse('core:login'), {'username': 'gcarlier', 'password': 'guy'})
        self.assertTrue(response.status_code == 200)
Skia's avatar
Skia committed
162
        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
163

164
class PageHandlingTest(TestCase):
Skia's avatar
Skia committed
165 166 167
    def setUp(self):
        try:
            Group.objects.create(name="root")
168 169
            u = User(username='root', last_name="", first_name="Bibou",
                     email="ae.info@utbm.fr",
Skia's avatar
Skia committed
170
                     date_of_birth="1942-06-12",
171 172 173 174
                     is_superuser=True, is_staff=True)
            u.set_password("plop")
            u.save()
            self.client.login(username='root', password='plop')
Skia's avatar
Skia committed
175 176
        except Exception as e:
            print(e)
Skia's avatar
Skia committed
177 178 179 180 181

    def test_create_page_ok(self):
        """
        Should create a page correctly
        """
Skia's avatar
Skia committed
182
        self.client.post(reverse('core:page_new'), {
Skia's avatar
Skia committed
183 184 185 186
            'parent': '',
            'name': 'guy',
            'owner_group': 1,
            })
187
        response = self.client.get(reverse('core:page', kwargs={'page_name': 'guy'}))
Skia's avatar
Skia committed
188
        self.assertTrue(response.status_code == 200)
Skia's avatar
Skia committed
189
        self.assertTrue('<a href="/page/guy/hist">' in str(response.content))
Skia's avatar
Skia committed
190 191 192 193 194

    def test_create_child_page_ok(self):
        """
        Should create a page correctly
        """
Skia's avatar
Skia committed
195
        self.client.post(reverse('core:page_new'), {
Skia's avatar
Skia committed
196 197 198 199
            'parent': '',
            'name': 'guy',
            'owner_group': '1',
            })
Skia's avatar
Skia committed
200
        response = self.client.post(reverse('core:page_new'), {
Skia's avatar
Skia committed
201 202 203 204
            'parent': '1',
            'name': 'bibou',
            'owner_group': '1',
            })
205
        response = self.client.get(reverse('core:page', kwargs={'page_name': 'guy/bibou'}))
Skia's avatar
Skia committed
206
        self.assertTrue(response.status_code == 200)
Skia's avatar
Skia committed
207
        self.assertTrue('<a href="/page/guy/bibou/">' in str(response.content))
Skia's avatar
Skia committed
208 209 210 211 212

    def test_access_child_page_ok(self):
        """
        Should display a page correctly
        """
Skia's avatar
Skia committed
213 214 215 216
        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)
217
        response = self.client.get(reverse('core:page', kwargs={'page_name': 'guy/bibou'}))
Skia's avatar
Skia committed
218
        self.assertTrue(response.status_code == 200)
Skia's avatar
Skia committed
219
        self.assertTrue('<a href="/page/guy/bibou/edit">\\xc3\\x89diter</a>' in str(response.content))
Skia's avatar
Skia committed
220 221 222 223 224

    def test_access_page_not_found(self):
        """
        Should not display a page correctly
        """
225
        response = self.client.get(reverse('core:page', kwargs={'page_name': 'swagg'}))
Skia's avatar
Skia committed
226
        response = self.client.get("/page/swagg/")
Skia's avatar
Skia committed
227
        self.assertTrue(response.status_code == 200)
Skia's avatar
Skia committed
228
        self.assertTrue('<a href="/page/create?page=swagg">' in str(response.content))
229 230 231 232 233

    def test_create_page_markdown_safe(self):
        """
        Should format the markdown and escape html correctly
        """
Skia's avatar
Skia committed
234
        self.client.post(reverse('core:page_new'), {
Skia's avatar
Skia committed
235 236 237 238 239
            'parent': '',
            'name': 'guy',
            'owner_group': '1',
            })
        r = self.client.post(reverse('core:page_edit', kwargs={'page_name': 'guy'}), {
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
                    'title': 'Bibou',
                    'content':
                    '''Guy *bibou*

http://git.an

# Swag

<guy>Bibou</guy>

<script>alert('Guy');</script>
''',
                    })
        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' +
256 257
                '<h1>Swag</h1>\\n&lt;guy&gt;Bibou&lt;/guy&gt;' +
                "&lt;script&gt;alert(\\'Guy\\');&lt;/script&gt;" in str(response.content))
258

Skia's avatar
Skia committed
259 260 261 262
#TODO: many tests on the pages:
#   - 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
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289

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):
        response = self.client.post(reverse("core:file_detail", kwargs={"file_id":self.subscriber.home.id}),
                {"folder_name": "GUY_folder_test"})
        self.assertTrue(response.status_code == 302)
        response = self.client.get(reverse("core:file_detail", kwargs={"file_id":self.subscriber.home.id}))
        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:
            response = self.client.post(reverse("core:file_detail", kwargs={"file_id":self.subscriber.home.id}),
                    {"file_field": f})
        self.assertTrue(response.status_code == 302)
        response = self.client.get(reverse("core:file_detail", kwargs={"file_id":self.subscriber.home.id}))
        self.assertTrue(response.status_code == 200)
        self.assertTrue("ls</a>" in str(response.content))