| 1 | """ |
| 2 | |
| 3 | >>> from django.conf import settings |
| 4 | >>> from django.http import HttpRequest |
| 5 | >>> from django.contrib.csrf.forms import SafeForm |
| 6 | |
| 7 | >>> settings.SECRET_KEY='secret' |
| 8 | >>> settings.SESSION_COOKIE_NAME='session_id' |
| 9 | |
| 10 | #If SafeForm is not passed a HttpRequest object, a ValueError is raised |
| 11 | >>> form=SafeForm() |
| 12 | Traceback (most recent call last): |
| 13 | ... |
| 14 | ValueError: SafeForm must be given a HttpRequest object |
| 15 | |
| 16 | #if the user who made the request does not have the session cookie set, |
| 17 | #a csrf token cannot be generated, so the value for the token should be empty |
| 18 | >>> request=HttpRequest() |
| 19 | >>> form=SafeForm(request=request) |
| 20 | >>> form.csrf_token_field() |
| 21 | u'<input type=\"hidden\" name=\"_csrf_token\" id=\"id__csrf_token\" />' |
| 22 | |
| 23 | #if a user that does not send any cookies tries to submit a form, |
| 24 | #SafeForm should automatically invalidate it |
| 25 | >>> request=HttpRequest() |
| 26 | >>> request.POST['_csrf_token']='2376e3ffd767c170fab368189b7e4799' |
| 27 | >>> form=SafeForm(request.POST, request=request) |
| 28 | >>> form.is_valid() |
| 29 | False |
| 30 | >>> form.non_field_errors() |
| 31 | [u'Your session has expired. Please refresh the page and submit the form again.'] |
| 32 | |
| 33 | #if the request has a valid session ID cookie, generate a token for it |
| 34 | >>> request=HttpRequest() |
| 35 | >>> request.COOKIES['session_id']='abcde' |
| 36 | >>> form=SafeForm(request=request) |
| 37 | >>> form.csrf_token_field() |
| 38 | u'<input type=\"hidden\" name=\"_csrf_token\" value=\"2376e3ffd767c170fab368189b7e4799\" id=\"id__csrf_token\" />' |
| 39 | |
| 40 | #if a user submits a form that doesn't have a csrf token at all, the form is not valid |
| 41 | >>> form=SafeForm(request.POST,request=request) |
| 42 | >>> form.is_valid() |
| 43 | False |
| 44 | >>> form.non_field_errors() |
| 45 | [u'Your session has expired. Please refresh the page and submit the form again.'] |
| 46 | |
| 47 | #if a user submits a form with an incorrect csrf token, the form is not valid. |
| 48 | >>> request=HttpRequest() |
| 49 | >>> request.POST['_csrf_token']='hello' |
| 50 | >>> request.COOKIES['session_id']='abcde' |
| 51 | >>> form=SafeForm(request.POST,request=request) |
| 52 | >>> form.is_valid() |
| 53 | False |
| 54 | >>> form.non_field_errors() |
| 55 | [u'Your session has expired. Please refresh the page and submit the form again.'] |
| 56 | |
| 57 | #if a user submits a form with the correct token, only then should is_valid() be True |
| 58 | >>> request=HttpRequest() |
| 59 | >>> request.POST['_csrf_token']='2376e3ffd767c170fab368189b7e4799' |
| 60 | >>> request.COOKIES['session_id']='abcde' |
| 61 | >>> form=SafeForm(request.POST,request=request) |
| 62 | >>> form.is_valid() |
| 63 | True |
| 64 | |
| 65 | """ |
| 66 | |
| 67 | if __name__ == '__main__': |
| 68 | import doctest |
| 69 | doctest.testmod() |