tests.py 7.54 KB
Newer Older
Skia's avatar
Skia committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
from django.test import SimpleTestCase, Client
from django.core.urlresolvers import reverse

from .models import User
from .forms import RegisteringForm, LoginForm

class UserRegistrationTest(SimpleTestCase):
    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
16
                                                     'date_of_birth': '12/6/1942',
Skia's avatar
Skia committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30
                                                     '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
31
                                                     'date_of_birth': '12/6/1942',
Skia's avatar
Skia committed
32 33 34 35
                                                     'password1': 'plop',
                                                     'password2': 'plop2',
                                                    })
        self.assertTrue(response.status_code == 200)
Skia's avatar
Skia committed
36
        self.assertTrue('TEST_REGISTER_USER_FORM_FAIL' in str(response.content))
Skia's avatar
Skia committed
37 38 39 40 41 42 43 44 45

    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
46
                                                     'date_of_birth': '12/6/1942',
Skia's avatar
Skia committed
47 48 49 50
                                                     'password1': 'plop',
                                                     'password2': 'plop',
                                                    })
        self.assertTrue(response.status_code == 200)
Skia's avatar
Skia committed
51
        self.assertTrue('TEST_REGISTER_USER_FORM_FAIL' in str(response.content))
Skia's avatar
Skia committed
52 53 54 55 56 57 58 59 60

    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
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
                                                     '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
77 78 79 80
                                                     'password1': 'plop',
                                                     'password2': 'plop',
                                                    })
        self.assertTrue(response.status_code == 200)
Skia's avatar
Skia committed
81
        self.assertTrue('TEST_REGISTER_USER_FORM_FAIL' in str(response.content))
Skia's avatar
Skia committed
82 83 84 85 86 87 88 89 90

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

Skia's avatar
Skia committed
120 121 122 123 124 125 126 127
    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
128
                                          'date_of_birth': '12/6/1942',
Skia's avatar
Skia committed
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
                                          'password1': 'plop',
                                          'password2': 'plop',
                                         })
        response = c.post(reverse('core:login'), {'username': 'gcarlier', 'password': 'plop'})
        self.assertTrue(response.status_code == 200)
        self.assertTrue('LOGIN_OK' in str(response.content))

    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
144
                                          'date_of_birth': '12/6/1942',
Skia's avatar
Skia committed
145 146 147 148 149 150
                                          'password1': 'plop',
                                          'password2': 'plop',
                                         })
        response = c.post(reverse('core:login'), {'username': 'gcarlier', 'password': 'guy'})
        self.assertTrue(response.status_code == 200)
        self.assertTrue('LOGIN_FAIL' in str(response.content))