| 1 | import string |
|---|
| 2 | from base import * |
|---|
| 3 | |
|---|
| 4 | from DateTime import DateTime |
|---|
| 5 | from Products.CMFFormController.ControllerState import ControllerState |
|---|
| 6 | |
|---|
| 7 | class TestStatic(ptc.FunctionalTestCase): |
|---|
| 8 | |
|---|
| 9 | def afterSetUp(self): |
|---|
| 10 | self.loginAsPortalOwner() |
|---|
| 11 | self.addProduct(PRODUCT_NAME) |
|---|
| 12 | self.skins = self.portal.portal_skins |
|---|
| 13 | |
|---|
| 14 | self.captcha_key = self.portal.captcha_key |
|---|
| 15 | self.hashkey = self.portal.getCaptcha() |
|---|
| 16 | |
|---|
| 17 | def testStaticByDefault(self): |
|---|
| 18 | # After installation static layer must present in all skin paths |
|---|
| 19 | for skin in self.skins.getSkinSelections(): |
|---|
| 20 | path = self.skins.getSkinPath(skin) |
|---|
| 21 | path = map( string.strip, string.split( path,',' )) |
|---|
| 22 | self.assertTrue(LAYER_STATIC_CAPTCHAS in path) |
|---|
| 23 | |
|---|
| 24 | def testImagesCount(self): |
|---|
| 25 | # All images must present in static skin layer |
|---|
| 26 | static = self.skins.restrictedTraverse('captchas') |
|---|
| 27 | static_ids = static.objectIds() |
|---|
| 28 | for i in range(1, CAPTCHAS_COUNT+1): |
|---|
| 29 | self.assertTrue("%s.jpg" % i in static_ids, |
|---|
| 30 | "No %s.jpg in static, %s" % (i,static_ids)) |
|---|
| 31 | |
|---|
| 32 | def test_GetCaptcha_Date(self): |
|---|
| 33 | # *date* must present after parsing decrypted key |
|---|
| 34 | decrypted_key = decrypt(self.captcha_key, self.hashkey) |
|---|
| 35 | parsed_key = parseKey(decrypted_key) |
|---|
| 36 | self.assertTrue('date' in parsed_key.keys()) |
|---|
| 37 | |
|---|
| 38 | def test_GetCaptcha_Key(self): |
|---|
| 39 | decrypted_key = decrypt(self.captcha_key, self.hashkey) |
|---|
| 40 | parsed_key = parseKey(decrypted_key) |
|---|
| 41 | # *key* must present after parsing decrypted key |
|---|
| 42 | self.assertTrue('key' in parsed_key.keys()) |
|---|
| 43 | # index start from 1 and lower or equals to CAPTCHAS_COUNT |
|---|
| 44 | index = int(parsed_key['key']) |
|---|
| 45 | self.assertTrue(index >= 1 and index <= CAPTCHAS_COUNT) |
|---|
| 46 | # encrypted key must be equals to title of the image |
|---|
| 47 | key = getWord( index-1 ) |
|---|
| 48 | img = getattr(self.portal, '%s.jpg' % index) |
|---|
| 49 | self.assertTrue(encrypt1(key) == img.title) |
|---|
| 50 | |
|---|
| 51 | def test_GetImage(self): |
|---|
| 52 | # getCaptchaImage function must return image coded in hashkey same to |
|---|
| 53 | # image get by 'key' after parsing decrypted key |
|---|
| 54 | req, resp = self.app.REQUEST, self.app.REQUEST.RESPONSE |
|---|
| 55 | decrypted_key = decrypt(self.captcha_key, self.hashkey) |
|---|
| 56 | parsed_key = parseKey(decrypted_key) |
|---|
| 57 | img = self.portal.restrictedTraverse(parsed_key['key']+'.jpg') |
|---|
| 58 | img_html = img.index_html(req, resp) |
|---|
| 59 | |
|---|
| 60 | obj_html = self.publish( |
|---|
| 61 | self.portal.absolute_url(1)+"/getCaptchaImage/%s" % self.hashkey).getBody() |
|---|
| 62 | self.assertTrue(obj_html == img_html, "Image get by getCaptchaImage script " \ |
|---|
| 63 | "is differ from image get by index (after parsing decrypted key)") |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | class TestStaticValidator(ptc.PloneTestCase): |
|---|
| 67 | |
|---|
| 68 | def afterSetUp(self): |
|---|
| 69 | self.loginAsPortalOwner() |
|---|
| 70 | self.addProduct(PRODUCT_NAME) |
|---|
| 71 | self.captcha_key = self.portal.captcha_key |
|---|
| 72 | # Preparation for validator tests |
|---|
| 73 | self.pfc = self.portal.portal_form_controller |
|---|
| 74 | self.req = self.app.REQUEST |
|---|
| 75 | # set up a dummy state object |
|---|
| 76 | self.dummycs = ControllerState(id='prefs_captchas_setup_form', |
|---|
| 77 | context=self.portal, button='submit', status='success', |
|---|
| 78 | errors={}, ext_action=None,) |
|---|
| 79 | |
|---|
| 80 | def testGoodData(self): |
|---|
| 81 | hashkey = self.portal.getCaptcha() |
|---|
| 82 | key = getWord(int(parseKey(decrypt(self.captcha_key, hashkey))['key'])-1 ) |
|---|
| 83 | self.req.form['hashkey'] = hashkey |
|---|
| 84 | self.req.form['key'] = key+'1' |
|---|
| 85 | |
|---|
| 86 | cstate = self.pfc.validate(self.dummycs, self.req, ['captcha_validator',]) |
|---|
| 87 | self.assertTrue(cstate.getErrors()['key'] == 'Please re-enter validation code.', |
|---|
| 88 | "Static captcha validator validate wrong key") |
|---|
| 89 | |
|---|
| 90 | def testBadKey(self): |
|---|
| 91 | hashkey = self.portal.getCaptcha() |
|---|
| 92 | key = getWord(int(parseKey(decrypt(self.captcha_key, hashkey))['key'])-1 ) |
|---|
| 93 | self.req.form['hashkey'] = hashkey |
|---|
| 94 | self.req.form['key'] = 'bad key' |
|---|
| 95 | |
|---|
| 96 | cstate = self.pfc.validate(self.dummycs, self.req, ['captcha_validator',]) |
|---|
| 97 | self.assertTrue(cstate.getErrors()['key'] == 'Please re-enter validation code.', |
|---|
| 98 | "Static captcha validator validate wrong key") |
|---|
| 99 | |
|---|
| 100 | def testBadDate(self): |
|---|
| 101 | # First path DateTime to get old dated hash |
|---|
| 102 | origDTTT = DateTime.timeTime |
|---|
| 103 | now = DateTime().timeTime() |
|---|
| 104 | DateTime.timeTime = lambda s: now - float(3601) |
|---|
| 105 | |
|---|
| 106 | hashkey = self.portal.getCaptcha() |
|---|
| 107 | key = getWord(int(parseKey(decrypt(self.captcha_key, hashkey))['key'])-1 ) |
|---|
| 108 | self.req.form['hashkey'] = hashkey |
|---|
| 109 | self.req.form['key'] = key |
|---|
| 110 | # Return original Date |
|---|
| 111 | DateTime.timeTime = origDTTT |
|---|
| 112 | cstate = self.pfc.validate(self.dummycs, self.req, ['captcha_validator',]) |
|---|
| 113 | self.assertTrue(cstate.getErrors()['key'] == 'Please re-enter validation code.', |
|---|
| 114 | "Static captcha validator validate wrong key") |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | def test_suite(): |
|---|
| 118 | suite = unittest.TestSuite() |
|---|
| 119 | suite.addTest(unittest.makeSuite(TestStatic)) |
|---|
| 120 | suite.addTest(unittest.makeSuite(TestStaticValidator)) |
|---|
| 121 | return suite |
|---|