|
1 | 1 | from django.test import TestCase |
| 2 | +from captcha.tools import GenCaptcha |
2 | 3 |
|
3 | 4 | # Create your tests here. |
| 5 | +class GenCaptchaTest(TestCase): |
| 6 | + # set up test |
| 7 | + def setUp(self): |
| 8 | + self.captcha = GenCaptcha() |
| 9 | + |
| 10 | + def testZ_get_random_color(self): |
| 11 | + for i in range(1, 1000): |
| 12 | + color = self.captcha.getRandomColor() |
| 13 | + |
| 14 | + r = color[0] |
| 15 | + g = color[1] |
| 16 | + b = color[2] |
| 17 | + |
| 18 | + r_in_range = r >= 0 and r <= 255 |
| 19 | + g_in_range = g >= 0 and g <= 255 |
| 20 | + b_in_range = b >= 0 and b <= 255 |
| 21 | + |
| 22 | + resault = r_in_range and g_in_range and b_in_range |
| 23 | + |
| 24 | + self.assertTrue(resault) |
| 25 | + |
| 26 | + def testY_get_random_char(self): |
| 27 | + for i in range(1, 1000): |
| 28 | + random_char = self.captcha.getRandomChar() |
| 29 | + |
| 30 | + is_number = random_char >= "0" and random_char <= "9" |
| 31 | + is_upper = random_char >= "A" and random_char <= "Z" |
| 32 | + is_lower = random_char >= "a" and random_char <= "z" |
| 33 | + |
| 34 | + is_char = is_number or is_lower or is_upper |
| 35 | + |
| 36 | + is_o = random_char == 'o' or random_char == 'O' |
| 37 | + |
| 38 | + resault = is_char and not is_o |
| 39 | + |
| 40 | + self.assertTrue(resault) |
| 41 | + |
| 42 | + def testX_check_similarity_false_far(self): |
| 43 | + color1 = (0, 0, 0) |
| 44 | + color2 = (255, 255, 255) |
| 45 | + |
| 46 | + self.assertFalse(self.captcha.checkSimilarity(color1, color2)) |
| 47 | + |
| 48 | + def testW_check_similarity_false_close(self): |
| 49 | + color1 = (249, 244, 217) |
| 50 | + color2 = (69, 157, 245) |
| 51 | + |
| 52 | + self.assertFalse(self.captcha.checkSimilarity(color1, color2)) |
| 53 | + |
| 54 | + def testV_check_similarity_same(self): |
| 55 | + color = (56, 1, 31) |
| 56 | + |
| 57 | + self.assertTrue(self.captcha.checkSimilarity(color, color)) |
| 58 | + |
| 59 | + def testU_check_similarity_true(self): |
| 60 | + color1 = (69, 157, 245) |
| 61 | + color2 = (70, 158, 246) |
| 62 | + |
| 63 | + self.assertTrue(self.captcha.checkSimilarity(color1, color2)) |
0 commit comments