tests.py 13 KB
Newer Older
1
from django.test import SimpleTestCase, 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

5
from core.models import User, Group
Skia's avatar
Skia committed
6
#from core.views.forms import RegisteringForm, LoginForm
Skia's avatar
Skia committed
7

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



class UserRegistrationTest(TestCase):
Skia's avatar
Skia committed
16 17 18 19 20 21
    def setUp(self):
        try:
            Group.objects.create(name="root")
        except:
            pass

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

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

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

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

    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
120
                                          'date_of_birth': '12/6/1942',
Skia's avatar
Skia committed
121 122 123 124 125 126
                                          '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
127
                                                     'date_of_birth': '12/6/1942',
Skia's avatar
Skia committed
128 129 130 131
                                                     'password1': 'plop',
                                                     'password2': 'plop',
                                                    })
        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

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

    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
158
                                          'date_of_birth': '12/6/1942',
Skia's avatar
Skia committed
159 160 161 162 163
                                          '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
164 165
        self.assertTrue('Please try again' in str(response.content))

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

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

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

    def test_access_child_page_ok(self):
        """
        Should display a page correctly
        """
212
        self.client.post(reverse('core:page_prop', kwargs={'page_name': 'guy'}), {'parent': '',
Skia's avatar
Skia committed
213 214 215 216
                                                                        'name': 'guy',
                                                                        'title': 'Guy',
                                                                        'Content': 'Guyéuyuyé',
                                                                       })
217
        self.client.post(reverse('core:page_prop', kwargs={'page_name': 'guy/bibou'}), {'parent': '1',
Skia's avatar
Skia committed
218 219 220 221 222
                                                                              'name': 'bibou',
                                                                              'title': 'Bibou',
                                                                              'Content':
                                                                              'Bibibibiblblblblblbouuuuuuuuu',
                                                                             })
223
        response = self.client.get(reverse('core:page', kwargs={'page_name': 'guy/bibou'}))
Skia's avatar
Skia committed
224
        self.assertTrue(response.status_code == 200)
225
        #self.assertTrue('PAGE_FOUND : Bibou' in str(response.content))
Skia's avatar
Skia committed
226 227 228 229 230

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

235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264

    def test_create_page_markdown_safe(self):
        """
        Should format the markdown and escape html correctly
        """
        self.client.post(reverse('core:page_prop', kwargs={'page_name': 'guy'}), {'parent': '',
                                                                        'name': 'guy',
                                                                        'owner_group': '1',
                                                                       })
        r = 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>
''',
                    })
        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' +
                '<h1>Swag</h1>\\n<p>&lt;guy&gt;Bibou&lt;/guy&gt;</p>\\n' +
                '<p>&lt;script&gt;alert(&#39;Guy&#39;);&lt;/script&gt;</p>' in str(response.content))

Skia's avatar
Skia committed
265 266 267 268
#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