annotate MoinMoin/security/_tests/test_textcha.py @ 1879:b7241c73c394

use more new-style classes
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sat, 22 Dec 2012 00:42:15 +0100
parents 4ac437141bbe
children 87272c032485
rev   line source
718
1c292b6a76b7 added test for textcha.TextChaValid, TextCha.test_amend_form. created setup and teardown for test clean up.
pkumar <contactprashantat@gmail.com>
parents: 717
diff changeset
1 # Copyright: 2011 Prashant Kumar <contactprashantat AT gmail DOT com>
1c292b6a76b7 added test for textcha.TextChaValid, TextCha.test_amend_form. created setup and teardown for test clean up.
pkumar <contactprashantat@gmail.com>
parents: 717
diff changeset
2 # License: GNU GPL v2 (or any later version), see LICENSE.txt for details.
1c292b6a76b7 added test for textcha.TextChaValid, TextCha.test_amend_form. created setup and teardown for test clean up.
pkumar <contactprashantat@gmail.com>
parents: 717
diff changeset
3
1c292b6a76b7 added test for textcha.TextChaValid, TextCha.test_amend_form. created setup and teardown for test clean up.
pkumar <contactprashantat@gmail.com>
parents: 717
diff changeset
4 """
1c292b6a76b7 added test for textcha.TextChaValid, TextCha.test_amend_form. created setup and teardown for test clean up.
pkumar <contactprashantat@gmail.com>
parents: 717
diff changeset
5 Test for security.textcha
1c292b6a76b7 added test for textcha.TextChaValid, TextCha.test_amend_form. created setup and teardown for test clean up.
pkumar <contactprashantat@gmail.com>
parents: 717
diff changeset
6 """
1c292b6a76b7 added test for textcha.TextChaValid, TextCha.test_amend_form. created setup and teardown for test clean up.
pkumar <contactprashantat@gmail.com>
parents: 717
diff changeset
7
717
93ace300d396 added test for security.textcha as test_textcha in security module
pkumar <contactprashantat@gmail.com>
parents:
diff changeset
8 from flask import current_app as app
93ace300d396 added test for security.textcha as test_textcha in security module
pkumar <contactprashantat@gmail.com>
parents:
diff changeset
9 from flask import g as flaskg
93ace300d396 added test for security.textcha as test_textcha in security module
pkumar <contactprashantat@gmail.com>
parents:
diff changeset
10
1274
8c275efc6e8c refactored and cleaned up user module code and tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 719
diff changeset
11 import pytest
8c275efc6e8c refactored and cleaned up user module code and tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 719
diff changeset
12
718
1c292b6a76b7 added test for textcha.TextChaValid, TextCha.test_amend_form. created setup and teardown for test clean up.
pkumar <contactprashantat@gmail.com>
parents: 717
diff changeset
13 from MoinMoin.security.textcha import TextCha, TextChaValid, TextChaizedForm
1274
8c275efc6e8c refactored and cleaned up user module code and tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 719
diff changeset
14 from MoinMoin.constants.keys import LOCALE
8c275efc6e8c refactored and cleaned up user module code and tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 719
diff changeset
15
717
93ace300d396 added test for security.textcha as test_textcha in security module
pkumar <contactprashantat@gmail.com>
parents:
diff changeset
16
93ace300d396 added test for security.textcha as test_textcha in security module
pkumar <contactprashantat@gmail.com>
parents:
diff changeset
17 class TestTextCha(object):
719
05b5d8284031 removed trailing white spcaes from test_textcha in securit module and test_test_environ in MoinMoin._tests
pkumar <contactprashantat@gmail.com>
parents: 718
diff changeset
18 """ Test: class TextCha """
718
1c292b6a76b7 added test for textcha.TextChaValid, TextCha.test_amend_form. created setup and teardown for test clean up.
pkumar <contactprashantat@gmail.com>
parents: 717
diff changeset
19 def setup_method(self, method):
717
93ace300d396 added test for security.textcha as test_textcha in security module
pkumar <contactprashantat@gmail.com>
parents:
diff changeset
20 cfg = app.cfg
719
05b5d8284031 removed trailing white spcaes from test_textcha in securit module and test_test_environ in MoinMoin._tests
pkumar <contactprashantat@gmail.com>
parents: 718
diff changeset
21 cfg.textchas = {'test_user_locale':
717
93ace300d396 added test for security.textcha as test_textcha in security module
pkumar <contactprashantat@gmail.com>
parents:
diff changeset
22 {'Good Question': 'Good Answer',
93ace300d396 added test for security.textcha as test_textcha in security module
pkumar <contactprashantat@gmail.com>
parents:
diff changeset
23 'What is the question?': 'Test_Answer'}
93ace300d396 added test for security.textcha as test_textcha in security module
pkumar <contactprashantat@gmail.com>
parents:
diff changeset
24 }
93ace300d396 added test for security.textcha as test_textcha in security module
pkumar <contactprashantat@gmail.com>
parents:
diff changeset
25 cfg.secrets['security/textcha'] = "test_secret"
1274
8c275efc6e8c refactored and cleaned up user module code and tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 719
diff changeset
26 flaskg.user.profile[LOCALE] = 'test_user_locale'
717
93ace300d396 added test for security.textcha as test_textcha in security module
pkumar <contactprashantat@gmail.com>
parents:
diff changeset
27
718
1c292b6a76b7 added test for textcha.TextChaValid, TextCha.test_amend_form. created setup and teardown for test clean up.
pkumar <contactprashantat@gmail.com>
parents: 717
diff changeset
28 def teardown_method(self, method):
1c292b6a76b7 added test for textcha.TextChaValid, TextCha.test_amend_form. created setup and teardown for test clean up.
pkumar <contactprashantat@gmail.com>
parents: 717
diff changeset
29 cfg = app.cfg
1c292b6a76b7 added test for textcha.TextChaValid, TextCha.test_amend_form. created setup and teardown for test clean up.
pkumar <contactprashantat@gmail.com>
parents: 717
diff changeset
30 cfg.textchas = None
719
05b5d8284031 removed trailing white spcaes from test_textcha in securit module and test_test_environ in MoinMoin._tests
pkumar <contactprashantat@gmail.com>
parents: 718
diff changeset
31 cfg.secrets.pop('security/textcha')
1274
8c275efc6e8c refactored and cleaned up user module code and tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 719
diff changeset
32 flaskg.user.profile[LOCALE] = None
718
1c292b6a76b7 added test for textcha.TextChaValid, TextCha.test_amend_form. created setup and teardown for test clean up.
pkumar <contactprashantat@gmail.com>
parents: 717
diff changeset
33
1c292b6a76b7 added test for textcha.TextChaValid, TextCha.test_amend_form. created setup and teardown for test clean up.
pkumar <contactprashantat@gmail.com>
parents: 717
diff changeset
34 def test_textcha(self):
1c292b6a76b7 added test for textcha.TextChaValid, TextCha.test_amend_form. created setup and teardown for test clean up.
pkumar <contactprashantat@gmail.com>
parents: 717
diff changeset
35 """ test for textchas and its attributes """
1c292b6a76b7 added test for textcha.TextChaValid, TextCha.test_amend_form. created setup and teardown for test clean up.
pkumar <contactprashantat@gmail.com>
parents: 717
diff changeset
36 test_form = TextChaizedForm()
1c292b6a76b7 added test for textcha.TextChaValid, TextCha.test_amend_form. created setup and teardown for test clean up.
pkumar <contactprashantat@gmail.com>
parents: 717
diff changeset
37 test_form['textcha_question'].value = None
1c292b6a76b7 added test for textcha.TextChaValid, TextCha.test_amend_form. created setup and teardown for test clean up.
pkumar <contactprashantat@gmail.com>
parents: 717
diff changeset
38
717
93ace300d396 added test for security.textcha as test_textcha in security module
pkumar <contactprashantat@gmail.com>
parents:
diff changeset
39 textcha_obj = TextCha(test_form)
93ace300d396 added test for security.textcha as test_textcha in security module
pkumar <contactprashantat@gmail.com>
parents:
diff changeset
40
93ace300d396 added test for security.textcha as test_textcha in security module
pkumar <contactprashantat@gmail.com>
parents:
diff changeset
41 # test for textcha
93ace300d396 added test for security.textcha as test_textcha in security module
pkumar <contactprashantat@gmail.com>
parents:
diff changeset
42 test_textchas = textcha_obj.textchas
1381
e3ea3f1901ba pep8: fix some E221 / E222
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1274
diff changeset
43 expected_textchas = {'Good Question': 'Good Answer',
e3ea3f1901ba pep8: fix some E221 / E222
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1274
diff changeset
44 'What is the question?': 'Test_Answer'}
717
93ace300d396 added test for security.textcha as test_textcha in security module
pkumar <contactprashantat@gmail.com>
parents:
diff changeset
45 assert test_textchas == expected_textchas
93ace300d396 added test for security.textcha as test_textcha in security module
pkumar <contactprashantat@gmail.com>
parents:
diff changeset
46 # test for the question
93ace300d396 added test for security.textcha as test_textcha in security module
pkumar <contactprashantat@gmail.com>
parents:
diff changeset
47 test_question = textcha_obj.question
93ace300d396 added test for security.textcha as test_textcha in security module
pkumar <contactprashantat@gmail.com>
parents:
diff changeset
48 possible_questions = ['Good Question', 'What is the question?']
93ace300d396 added test for security.textcha as test_textcha in security module
pkumar <contactprashantat@gmail.com>
parents:
diff changeset
49 assert test_question in possible_questions
93ace300d396 added test for security.textcha as test_textcha in security module
pkumar <contactprashantat@gmail.com>
parents:
diff changeset
50 # test for answer_re
93ace300d396 added test for security.textcha as test_textcha in security module
pkumar <contactprashantat@gmail.com>
parents:
diff changeset
51 possible_answers = ['Good Answer', 'Test_Answer']
93ace300d396 added test for security.textcha as test_textcha in security module
pkumar <contactprashantat@gmail.com>
parents:
diff changeset
52 result_answer1 = textcha_obj.answer_re.match(expected_textchas[test_question])
93ace300d396 added test for security.textcha as test_textcha in security module
pkumar <contactprashantat@gmail.com>
parents:
diff changeset
53 test_answer = result_answer1.group()
93ace300d396 added test for security.textcha as test_textcha in security module
pkumar <contactprashantat@gmail.com>
parents:
diff changeset
54 assert test_answer in possible_answers
93ace300d396 added test for security.textcha as test_textcha in security module
pkumar <contactprashantat@gmail.com>
parents:
diff changeset
55 # invalid value
93ace300d396 added test for security.textcha as test_textcha in security module
pkumar <contactprashantat@gmail.com>
parents:
diff changeset
56 result_answer2 = textcha_obj.answer_re.match('Bad Answer')
93ace300d396 added test for security.textcha as test_textcha in security module
pkumar <contactprashantat@gmail.com>
parents:
diff changeset
57 assert not result_answer2
93ace300d396 added test for security.textcha as test_textcha in security module
pkumar <contactprashantat@gmail.com>
parents:
diff changeset
58 # test for answer_regex
93ace300d396 added test for security.textcha as test_textcha in security module
pkumar <contactprashantat@gmail.com>
parents:
diff changeset
59 result_answer = textcha_obj.answer_regex
93ace300d396 added test for security.textcha as test_textcha in security module
pkumar <contactprashantat@gmail.com>
parents:
diff changeset
60 assert result_answer in possible_answers
93ace300d396 added test for security.textcha as test_textcha in security module
pkumar <contactprashantat@gmail.com>
parents:
diff changeset
61
93ace300d396 added test for security.textcha as test_textcha in security module
pkumar <contactprashantat@gmail.com>
parents:
diff changeset
62 # when question is specified earlier
93ace300d396 added test for security.textcha as test_textcha in security module
pkumar <contactprashantat@gmail.com>
parents:
diff changeset
63 test_signature = 'fb5a8cc203b07b66637aafa7b0647da17e249e9c'
718
1c292b6a76b7 added test for textcha.TextChaValid, TextCha.test_amend_form. created setup and teardown for test clean up.
pkumar <contactprashantat@gmail.com>
parents: 717
diff changeset
64 test_form['textcha_question'].value = 'What is the question? 9876543210' + test_signature
717
93ace300d396 added test for security.textcha as test_textcha in security module
pkumar <contactprashantat@gmail.com>
parents:
diff changeset
65 textcha_obj = TextCha(test_form)
93ace300d396 added test for security.textcha as test_textcha in security module
pkumar <contactprashantat@gmail.com>
parents:
diff changeset
66 # test for the question
93ace300d396 added test for security.textcha as test_textcha in security module
pkumar <contactprashantat@gmail.com>
parents:
diff changeset
67 test_question = textcha_obj.question
93ace300d396 added test for security.textcha as test_textcha in security module
pkumar <contactprashantat@gmail.com>
parents:
diff changeset
68 expected_question = 'What is the question?'
93ace300d396 added test for security.textcha as test_textcha in security module
pkumar <contactprashantat@gmail.com>
parents:
diff changeset
69 assert test_question == expected_question
93ace300d396 added test for security.textcha as test_textcha in security module
pkumar <contactprashantat@gmail.com>
parents:
diff changeset
70 # test the answer
93ace300d396 added test for security.textcha as test_textcha in security module
pkumar <contactprashantat@gmail.com>
parents:
diff changeset
71 test_answer = textcha_obj.answer_regex
93ace300d396 added test for security.textcha as test_textcha in security module
pkumar <contactprashantat@gmail.com>
parents:
diff changeset
72 assert test_answer == 'Test_Answer'
93ace300d396 added test for security.textcha as test_textcha in security module
pkumar <contactprashantat@gmail.com>
parents:
diff changeset
73 assert test_signature == textcha_obj.signature
93ace300d396 added test for security.textcha as test_textcha in security module
pkumar <contactprashantat@gmail.com>
parents:
diff changeset
74 assert textcha_obj.timestamp == 9876543210
93ace300d396 added test for security.textcha as test_textcha in security module
pkumar <contactprashantat@gmail.com>
parents:
diff changeset
75
718
1c292b6a76b7 added test for textcha.TextChaValid, TextCha.test_amend_form. created setup and teardown for test clean up.
pkumar <contactprashantat@gmail.com>
parents: 717
diff changeset
76 def test_amend_form(self):
1c292b6a76b7 added test for textcha.TextChaValid, TextCha.test_amend_form. created setup and teardown for test clean up.
pkumar <contactprashantat@gmail.com>
parents: 717
diff changeset
77 # textchas are disabled for 'some_locale'
1274
8c275efc6e8c refactored and cleaned up user module code and tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 719
diff changeset
78 flaskg.user.profile[LOCALE] = 'some_locale'
718
1c292b6a76b7 added test for textcha.TextChaValid, TextCha.test_amend_form. created setup and teardown for test clean up.
pkumar <contactprashantat@gmail.com>
parents: 717
diff changeset
79 test_form = TextChaizedForm()
1c292b6a76b7 added test for textcha.TextChaValid, TextCha.test_amend_form. created setup and teardown for test clean up.
pkumar <contactprashantat@gmail.com>
parents: 717
diff changeset
80 test_form['textcha_question'].value = None
1c292b6a76b7 added test for textcha.TextChaValid, TextCha.test_amend_form. created setup and teardown for test clean up.
pkumar <contactprashantat@gmail.com>
parents: 717
diff changeset
81 textcha_obj = TextCha(test_form)
1c292b6a76b7 added test for textcha.TextChaValid, TextCha.test_amend_form. created setup and teardown for test clean up.
pkumar <contactprashantat@gmail.com>
parents: 717
diff changeset
82 # before calling amend_form
1c292b6a76b7 added test for textcha.TextChaValid, TextCha.test_amend_form. created setup and teardown for test clean up.
pkumar <contactprashantat@gmail.com>
parents: 717
diff changeset
83 assert not textcha_obj.form['textcha_question'].optional
1c292b6a76b7 added test for textcha.TextChaValid, TextCha.test_amend_form. created setup and teardown for test clean up.
pkumar <contactprashantat@gmail.com>
parents: 717
diff changeset
84 assert not textcha_obj.form['textcha'].optional
1c292b6a76b7 added test for textcha.TextChaValid, TextCha.test_amend_form. created setup and teardown for test clean up.
pkumar <contactprashantat@gmail.com>
parents: 717
diff changeset
85 # on calling amend_form
1c292b6a76b7 added test for textcha.TextChaValid, TextCha.test_amend_form. created setup and teardown for test clean up.
pkumar <contactprashantat@gmail.com>
parents: 717
diff changeset
86 textcha_obj.amend_form()
1c292b6a76b7 added test for textcha.TextChaValid, TextCha.test_amend_form. created setup and teardown for test clean up.
pkumar <contactprashantat@gmail.com>
parents: 717
diff changeset
87 assert textcha_obj.form['textcha_question'].optional
1c292b6a76b7 added test for textcha.TextChaValid, TextCha.test_amend_form. created setup and teardown for test clean up.
pkumar <contactprashantat@gmail.com>
parents: 717
diff changeset
88 assert textcha_obj.form['textcha'].optional
1c292b6a76b7 added test for textcha.TextChaValid, TextCha.test_amend_form. created setup and teardown for test clean up.
pkumar <contactprashantat@gmail.com>
parents: 717
diff changeset
89
1c292b6a76b7 added test for textcha.TextChaValid, TextCha.test_amend_form. created setup and teardown for test clean up.
pkumar <contactprashantat@gmail.com>
parents: 717
diff changeset
90 class TestTextChaValid(object):
1c292b6a76b7 added test for textcha.TextChaValid, TextCha.test_amend_form. created setup and teardown for test clean up.
pkumar <contactprashantat@gmail.com>
parents: 717
diff changeset
91 """ Test: class TextChaValid """
1c292b6a76b7 added test for textcha.TextChaValid, TextCha.test_amend_form. created setup and teardown for test clean up.
pkumar <contactprashantat@gmail.com>
parents: 717
diff changeset
92 def setup_method(self, method):
1c292b6a76b7 added test for textcha.TextChaValid, TextCha.test_amend_form. created setup and teardown for test clean up.
pkumar <contactprashantat@gmail.com>
parents: 717
diff changeset
93 cfg = app.cfg
719
05b5d8284031 removed trailing white spcaes from test_textcha in securit module and test_test_environ in MoinMoin._tests
pkumar <contactprashantat@gmail.com>
parents: 718
diff changeset
94 cfg.textchas = {'test_user_locale':
718
1c292b6a76b7 added test for textcha.TextChaValid, TextCha.test_amend_form. created setup and teardown for test clean up.
pkumar <contactprashantat@gmail.com>
parents: 717
diff changeset
95 {'Good Question': 'Good Answer'}
1c292b6a76b7 added test for textcha.TextChaValid, TextCha.test_amend_form. created setup and teardown for test clean up.
pkumar <contactprashantat@gmail.com>
parents: 717
diff changeset
96 }
1c292b6a76b7 added test for textcha.TextChaValid, TextCha.test_amend_form. created setup and teardown for test clean up.
pkumar <contactprashantat@gmail.com>
parents: 717
diff changeset
97 cfg.secrets['security/textcha'] = "test_secret"
1274
8c275efc6e8c refactored and cleaned up user module code and tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 719
diff changeset
98 flaskg.user.profile[LOCALE] = 'test_user_locale'
718
1c292b6a76b7 added test for textcha.TextChaValid, TextCha.test_amend_form. created setup and teardown for test clean up.
pkumar <contactprashantat@gmail.com>
parents: 717
diff changeset
99
1c292b6a76b7 added test for textcha.TextChaValid, TextCha.test_amend_form. created setup and teardown for test clean up.
pkumar <contactprashantat@gmail.com>
parents: 717
diff changeset
100 def teardown_method(self, method):
1c292b6a76b7 added test for textcha.TextChaValid, TextCha.test_amend_form. created setup and teardown for test clean up.
pkumar <contactprashantat@gmail.com>
parents: 717
diff changeset
101 cfg = app.cfg
1c292b6a76b7 added test for textcha.TextChaValid, TextCha.test_amend_form. created setup and teardown for test clean up.
pkumar <contactprashantat@gmail.com>
parents: 717
diff changeset
102 cfg.textchas = None
719
05b5d8284031 removed trailing white spcaes from test_textcha in securit module and test_test_environ in MoinMoin._tests
pkumar <contactprashantat@gmail.com>
parents: 718
diff changeset
103 cfg.secrets.pop('security/textcha')
1274
8c275efc6e8c refactored and cleaned up user module code and tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 719
diff changeset
104 flaskg.user.profile[LOCALE] = None
718
1c292b6a76b7 added test for textcha.TextChaValid, TextCha.test_amend_form. created setup and teardown for test clean up.
pkumar <contactprashantat@gmail.com>
parents: 717
diff changeset
105
1879
b7241c73c394 use more new-style classes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1397
diff changeset
106 class Element(object):
718
1c292b6a76b7 added test for textcha.TextChaValid, TextCha.test_amend_form. created setup and teardown for test clean up.
pkumar <contactprashantat@gmail.com>
parents: 717
diff changeset
107 def __init__(self):
1c292b6a76b7 added test for textcha.TextChaValid, TextCha.test_amend_form. created setup and teardown for test clean up.
pkumar <contactprashantat@gmail.com>
parents: 717
diff changeset
108 self.parent = None
1c292b6a76b7 added test for textcha.TextChaValid, TextCha.test_amend_form. created setup and teardown for test clean up.
pkumar <contactprashantat@gmail.com>
parents: 717
diff changeset
109 self.value = 'Good Answer'
1c292b6a76b7 added test for textcha.TextChaValid, TextCha.test_amend_form. created setup and teardown for test clean up.
pkumar <contactprashantat@gmail.com>
parents: 717
diff changeset
110
1c292b6a76b7 added test for textcha.TextChaValid, TextCha.test_amend_form. created setup and teardown for test clean up.
pkumar <contactprashantat@gmail.com>
parents: 717
diff changeset
111 def test_validate(self):
1c292b6a76b7 added test for textcha.TextChaValid, TextCha.test_amend_form. created setup and teardown for test clean up.
pkumar <contactprashantat@gmail.com>
parents: 717
diff changeset
112 test_form = TextChaizedForm()
1c292b6a76b7 added test for textcha.TextChaValid, TextCha.test_amend_form. created setup and teardown for test clean up.
pkumar <contactprashantat@gmail.com>
parents: 717
diff changeset
113 textchavalid_obj = TextChaValid()
1c292b6a76b7 added test for textcha.TextChaValid, TextCha.test_amend_form. created setup and teardown for test clean up.
pkumar <contactprashantat@gmail.com>
parents: 717
diff changeset
114 test_element = self.Element()
1c292b6a76b7 added test for textcha.TextChaValid, TextCha.test_amend_form. created setup and teardown for test clean up.
pkumar <contactprashantat@gmail.com>
parents: 717
diff changeset
115 test_element.parent = test_form
1c292b6a76b7 added test for textcha.TextChaValid, TextCha.test_amend_form. created setup and teardown for test clean up.
pkumar <contactprashantat@gmail.com>
parents: 717
diff changeset
116 result = textchavalid_obj.validate(test_element, 'test_state')
1c292b6a76b7 added test for textcha.TextChaValid, TextCha.test_amend_form. created setup and teardown for test clean up.
pkumar <contactprashantat@gmail.com>
parents: 717
diff changeset
117 assert result