]> WPIA git - motion.git/blob - tests/test_user_api.py
Merge branch 'finish_move' into 'master'
[motion.git] / tests / test_user_api.py
1 from tests.test_basics import BasicTest
2 import click
3 import motion
4 import postgresql
5
6 from click.testing import CliRunner
7 from motion import create_user
8 from motion import motion_masking
9 from motion import app
10
11
12 class GeneralTests(BasicTest):
13
14     def setUp(self):
15         self.init_test()
16
17     def tearDown(self):
18         pass
19
20     def test_create_user(self):
21         user = 'John Doe'
22         host= app.config.get("DEFAULT_HOST")
23         runner = app.test_cli_runner()
24         result = runner.invoke(create_user, (user, host))
25         assert result.exit_code == 0
26         self.assertIn("User 'John Doe' inserted to %s." % host, result.output)
27
28         rv = self.db_select2("SELECT email FROM voter WHERE lower(email)=lower($1) AND host=$2", user, host)
29         self.assertIn(user, rv[0].get("email"))
30
31         result = runner.invoke(create_user, (user, host))
32         assert result.exit_code == 0
33         self.assertIn("User 'John Doe' already exists on %s." % host, result.output)
34
35         # test with second host
36         host= '127.0.0.1:5001'
37         runner = app.test_cli_runner()
38         result = runner.invoke(create_user, (user, host))
39         assert result.exit_code == 0
40         self.assertIn("User 'John Doe' inserted to 127.0.0.1:5001.", result.output)
41
42         rv = self.db_select2("SELECT email FROM voter WHERE lower(email)=lower($1) AND host=$2", user, host)
43         self.assertIn(user, rv[0].get("email"))
44
45         result = runner.invoke(create_user, (user, host))
46         assert result.exit_code == 0
47         self.assertIn("User 'John Doe' already exists on 127.0.0.1:5001.", result.output)
48
49     def test_motion_masking(self):
50         self.db_sampledata()
51         records=0
52         self.assertEqual(records, self.recordCountLog('motionmasking'))
53
54         # test motion not exists
55         motion = 'g1.20200402.999'
56         motionreason='http://motiontest.wpia.club/motion/xxx'
57         host= app.config.get("DEFAULT_HOST")
58         runner = app.test_cli_runner()
59         result = runner.invoke(motion_masking, (motion, motionreason, host))
60         assert result.exit_code == 0
61         self.assertIn("0 record(s) affected by masking of 'g1.20200402.999'.", result.output)
62         self.assertEqual(records, self.recordCountLog('motionmasking'))
63
64         # test motion with wilcards
65         motion = 'g1.20200402.00%'
66         motionreason='http://motiontest.wpia.club/motion/xxx'
67         host= app.config.get("DEFAULT_HOST")
68         runner = app.test_cli_runner()
69         result = runner.invoke(motion_masking, (motion, motionreason, host))
70         assert result.exit_code == 0
71         self.assertIn("No wildcards allowed for motion entry 'g1.20200402.00%'.", result.output)
72         self.assertEqual(records, self.recordCountLog('motionmasking'))
73
74         motion = 'g1.2020040%.001'
75         motionreason='http://motiontest.wpia.club/motion/xxx'
76         host= app.config.get("DEFAULT_HOST")
77         runner = app.test_cli_runner()
78         result = runner.invoke(motion_masking, (motion, motionreason, host))
79         assert result.exit_code == 0
80         self.assertIn("No wildcards allowed for motion entry 'g1.2020040%.001'.", result.output)
81         self.assertEqual(records, self.recordCountLog('motionmasking'))
82
83         motion = 'g1.20200402.00_'
84         motionreason='http://motiontest.wpia.club/motion/xxx'
85         host= app.config.get("DEFAULT_HOST")
86         runner = app.test_cli_runner()
87         result = runner.invoke(motion_masking, (motion, motionreason, host))
88         assert result.exit_code == 0
89         self.assertIn("No wildcards allowed for motion entry 'g1.20200402.00_'.", result.output)
90         self.assertEqual(records, self.recordCountLog('motionmasking'))
91
92         motion = 'g1.2020040_.001'
93         motionreason='http://motiontest.wpia.club/motion/xxx'
94         host= app.config.get("DEFAULT_HOST")
95         runner = app.test_cli_runner()
96         result = runner.invoke(motion_masking, (motion, motionreason, host))
97         assert result.exit_code == 0
98         self.assertIn("No wildcards allowed for motion entry 'g1.2020040_.001'.", result.output)
99         self.assertEqual(records, self.recordCountLog('motionmasking'))
100
101         motion = 'g1.2020040_.%'
102         motionreason='http://motiontest.wpia.club/motion/xxx'
103         host= app.config.get("DEFAULT_HOST")
104         runner = app.test_cli_runner()
105         result = runner.invoke(motion_masking, (motion, motionreason, host))
106         assert result.exit_code == 0
107         self.assertIn("No wildcards allowed for motion entry 'g1.2020040_.%'.", result.output)
108         self.assertEqual(records, self.recordCountLog('motionmasking'))
109
110         motion = 'g1.20200402.0\\1'
111         motionreason='http://motiontest.wpia.club/motion/xxx'
112         host= app.config.get("DEFAULT_HOST")
113         runner = app.test_cli_runner()
114         result = runner.invoke(motion_masking, (motion, motionreason, host))
115         assert result.exit_code == 0
116         self.assertIn("No wildcards allowed for motion entry 'g1.20200402.0\\1'.", result.output)
117         self.assertEqual(records, self.recordCountLog('motionmasking'))
118
119         motion = 'g1.2020040\.001'
120         motionreason='http://motiontest.wpia.club/motion/xxx'
121         host= app.config.get("DEFAULT_HOST")
122         runner = app.test_cli_runner()
123         result = runner.invoke(motion_masking, (motion, motionreason, host))
124         assert result.exit_code == 0
125         self.assertIn("No wildcards allowed for motion entry 'g1.2020040\\.001'.", result.output)
126         self.assertEqual(records, self.recordCountLog('motionmasking'))
127
128         # test masking single motion
129         sql = "SELECT id FROM motion WHERE content LIKE $1"
130         motion = 'g1.20200402.001'
131         motionreason='http://motiontest.wpia.club/motion/xxx'
132         runner = app.test_cli_runner()
133         result = runner.invoke(motion_masking, (motion, motionreason, host))
134         assert result.exit_code == 0
135         self.assertIn("1 record(s) affected by masking of 'g1.20200402.001'.", result.output)
136         self.assertIn("1 record(s) updated by masking of 'g1.20200402.001'.", result.output)
137         records=1
138         self.assertEqual(records, self.recordCountLog('motionmasking'))
139         self.logRecordDetailsTest('motionmasking', records-1, 0, 
140                           "1 motion(s) masked on base of motion http://motiontest.wpia.club/motion/xxx with motion identifier 'g1.20200402.001' on host 127.0.0.1:5000", 0)
141         self.assertEqual(1, self.recordCount(sql, '%' + motionreason +'%'))
142
143         # test masking muliple motions
144         motion = 'g1.20200402'
145         motionreason='http://motiontest.wpia.club/motion/1xxx'
146         runner = app.test_cli_runner()
147         result = runner.invoke(motion_masking, (motion, motionreason, host))
148         assert result.exit_code == 0
149         self.assertIn("4 record(s) affected by masking of 'g1.20200402'.", result.output)
150         self.assertIn("4 record(s) updated by masking of 'g1.20200402'.", result.output)
151         records=2
152         self.assertEqual(records, self.recordCountLog('motionmasking'))
153         self.logRecordDetailsTest('motionmasking', records-1, 0, 
154                           "4 motion(s) masked on base of motion http://motiontest.wpia.club/motion/1xxx with motion identifier 'g1.20200402' on host 127.0.0.1:5000", 0)
155         self.assertEqual(4, self.recordCount(sql, '%' + motionreason +'%'))
156
157         # test different host
158         motion = 'g1.20200402.001'
159         motionreason='http://motiontest.wpia.club/motion/xxx'
160         host= '127.0.0.1:5001'
161         runner = app.test_cli_runner()
162         result = runner.invoke(motion_masking, (motion, motionreason, host))
163         assert result.exit_code == 0
164         self.assertIn("0 record(s) affected by masking of 'g1.20200402.001'.", result.output)
165         self.assertEqual(records, self.recordCountLog('motionmasking'))