File size: 1,861 Bytes
ff3c435
 
 
 
 
 
 
 
 
 
 
 
 
152ccef
ff3c435
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import unittest
from reports import normalize, human_views, render


class ReportTests(unittest.TestCase):
    def test_normalize(self):
        self.assertEqual(normalize('http://vk.ru/wall-123_456')['url'], 'https://vk.com/wall-123_456')
        self.assertEqual(normalize('https://telegram.me/s/Channel/123')['url'], 'https://t.me/channel/123')
        for link in ('https://t.me/c/123/456', 'https://vk.ru.evil.test/wall-123_456', 'https://vk.com/club123'):
            with self.assertRaises(ValueError):
                normalize(link)

    def test_format_without_zero(self):
        for value, expected in ((2000, '2K'), (10600, '10,6K'), (1000000, '1M'), (845, '0,8K'), (100, '0,1K'), (35, '0K'), (0, '0K')):
            self.assertEqual(human_views(value), expected)

    def test_errors_are_not_zero_views(self):
        first = dict(normalize('https://t.me/channel/1'), title='Имя 🗿', views=2000)
        second = dict(normalize('https://t.me/channel/2'), title='Имя', views=None, error='Нет счётчика')
        r = render([first, second])
        self.assertIn('<b>2K</b>', r['html'])
        self.assertNotIn('🗿', r['html'])
        self.assertNotIn('0K', r['html'])
        self.assertIn('Неполный итог', r['html'])
        self.assertEqual(len(r['errors']), 1)

    def test_grouping_and_html_escaping(self):
        records = [dict(normalize('https://max.ru/channel/AaE-viFyB3E'), title='<script>alert(1)</script> Имя', views=1000),
                   dict(normalize('https://max.ru/channel/AaE-viFyB3F'), title='Имя', views=2000)]
        r = render(records)
        self.assertIn('<h2>Макс</h2>', r['html'])
        self.assertIn('ещё</a>', r['html'])
        self.assertNotIn('<script>', r['html'])
        self.assertIn('<b>3K</b>', r['html'])


if __name__ == '__main__':
    unittest.main()