File: tests_stats.py

package info (click to toggle)
paperwork 2.2.5-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 166,660 kB
  • sloc: python: 44,775; makefile: 992; sh: 625; xml: 135
file content (152 lines) | stat: -rw-r--r-- 5,191 bytes parent folder | download
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import datetime
import http
import http.server
import json
import unittest
import threading
import time
import urllib

import openpaperwork_core


class TestStats(unittest.TestCase):
    def setUp(self):
        self.core = openpaperwork_core.Core(auto_load_dependencies=True)

        class FakeAppModule(object):
            class Plugin(openpaperwork_core.PluginBase):
                def get_interfaces(self):
                    return ['app']

                def app_get_name(self):
                    return "Paperwork"

                def app_get_fs_name(self):
                    return "paperwork2"

                def app_get_version(self):
                    return "2.1"

        self.core._load_module("fake_app", FakeAppModule())
        self.core.load("openpaperwork_core.config.fake")
        self.core.load("openpaperwork_core.beacon.stats")

        self.config = self.core.get_by_name("openpaperwork_core.config.fake")
        self.received = []
        self.stats_sent = False

    def test_send_stats(self):
        self.received = []

        class TestRequestHandler(http.server.BaseHTTPRequestHandler):
            def do_POST(s):
                ctype = s.headers.get_content_type()
                if ctype == 'application/x-www-form-urlencoded':
                    length = int(s.headers['Content-Length'])
                    data = urllib.parse.parse_qs(
                        s.rfile.read(length), keep_blank_values=1
                    )
                else:
                    data = {}

                self.received.append(
                    (s.path, json.loads(data[b'statistics'][0]))
                )

                s.send_response(200)
                s.send_header('Content-type', 'text/html')
                s.end_headers()
                s.wfile.write(b"<html><body><p>OK</p></body></html>")

        with http.server.HTTPServer(('', 0), TestRequestHandler) as h:
            self.config.settings = {
                "send_statistics": True,
                "uuid": 1245,
                "statistics_last_run": datetime.date(1995, 1, 1),
                "statistics_protocol": "http",
                "statistics_server": "127.0.0.1:{}".format(h.server_port),
            }

            threading.Thread(target=h.handle_request).start()
            time.sleep(0.1)

            class FakeModule(object):
                class Plugin(openpaperwork_core.PluginBase):
                    def stats_get(self, stats):
                        stats['nb_documents'] += 122
                        stats['truck'] = 42

                    def on_stats_sent(s):
                        self.stats_sent = True

            self.core._load_module(
                "fake_module", FakeModule()
            )

            self.core.init()
            self.core.call_all("mainloop_quit_graceful")

            self.core.call_one('mainloop')

            self.assertTrue(self.stats_sent)
            self.assertEqual(len(self.received), 1)
            self.assertEqual(self.received[0][0], "/beacon/post_statistics")
            self.assertEqual(self.received[0][1]['uuid'], 1245)
            self.assertEqual(self.received[0][1]['nb_documents'], 122)
            self.assertEqual(self.received[0][1]['truck'], 42)

    def test_server_down(self):
        self.received = []

        class TestRequestHandler(http.server.BaseHTTPRequestHandler):
            def do_POST(s):
                ctype = s.headers.get_content_type()
                if ctype == 'application/x-www-form-urlencoded':
                    length = int(s.headers['Content-Length'])
                    data = urllib.parse.parse_qs(
                        s.rfile.read(length), keep_blank_values=1
                    )
                else:
                    data = {}

                self.received.append(
                    (s.path, json.loads(data[b'statistics'][0]))
                )

                s.send_response(500)
                s.send_header('Content-type', 'text/html')
                s.end_headers()
                s.wfile.write(b"<html><body><p>KO</p></body></html>")

        with http.server.HTTPServer(('', 0), TestRequestHandler) as h:
            self.config.settings = {
                "send_statistics": True,
                "uuid": 1245,
                "statistics_last_run": datetime.date(1995, 1, 1),
                "statistics_protocol": "http",
                "statistics_server": "127.0.0.1:{}".format(h.server_port),
            }

            threading.Thread(target=h.handle_request).start()
            time.sleep(0.1)

            class FakeModule(object):
                class Plugin(openpaperwork_core.PluginBase):
                    def stats_get(self, stats):
                        stats['nb_documents'] += 122
                        stats['truck'] = 42

                    def on_stats_sent(s):
                        self.stats_sent = True

            self.core._load_module(
                "fake_module", FakeModule()
            )

            self.core.init()
            self.core.call_all("mainloop_quit_graceful")

            self.core.call_one('mainloop')

            self.assertFalse(self.stats_sent)