]> ToastFreeware Gitweb - philipp/winterrodeln/wradmin.git/blob - tests/test_wradmin.py
Insert testdb.sql as raw SQL.
[philipp/winterrodeln/wradmin.git] / tests / test_wradmin.py
1 import unittest
2 import bs4  # beautiful soup
3 import wradmin.model
4 import wradmin
5
6
7 class WradminTestBase(unittest.TestCase):
8     def setUp(self):
9         wradmin.app.config['TESTING'] = True
10         self.app = wradmin.app.test_client()
11
12
13 class TestNoDbWradmin(WradminTestBase):
14     def test_root(self):
15         result = self.app.get('/')
16         self.assertEqual(result.status_code, 200)
17         self.assertTrue(result.data.startswith(b'<!doctype html'))
18         soup = bs4.BeautifulSoup(result.data, 'html.parser')
19         self.assertEqual(soup.title.text, 'Hauptmenü')
20
21     def test_coordtool(self):
22         result = self.app.get('/coordtool/index')
23         self.assertEqual(result.status_code, 200)
24         self.assertTrue(result.data.startswith(b'<!doctype html'))
25         soup = bs4.BeautifulSoup(result.data, 'html.parser')
26         self.assertEqual(soup.title.text, 'Koordinaten-Rechner')
27
28     def test_coordtool_convert_ok(self):
29         result = self.app.post('/coordtool/convert', data={'input': '47.987, 11.123'}, follow_redirects=True)
30         self.assertEqual(result.status_code, 200)
31         self.assertTrue(result.data.startswith(b'<!doctype html'))
32         soup = bs4.BeautifulSoup(result.data, 'html.parser')
33         self.assertEqual(soup.title.text, 'Koordinaten-Rechner')
34         self.assertIn('GPX Format', str(soup))
35
36     def test_coordtool_convert_error(self):
37         result = self.app.post('/coordtool/convert', data={'input': 'abc'}, follow_redirects=True)
38         self.assertEqual(result.status_code, 200)
39         self.assertTrue(result.data.startswith(b'<!doctype html'))
40         soup = bs4.BeautifulSoup(result.data, 'html.parser')
41         self.assertEqual(soup.title.text, 'Koordinaten-Rechner')
42         self.assertIn('no known format', str(soup))
43
44
45 class TestDbWradmin(WradminTestBase):
46     def setUp(self):
47         super().setUp()
48         with wradmin.app.app_context():
49             # fill database
50             with open('tests/testdb.sql', 'r') as f:
51                 sql = f.read()
52             with wradmin.model.meta.engine.begin() as con:
53                 con.execution_options(no_parameters=True).execute(sql)
54
55                 # update dates
56                 seconds_diff, = con.execute("SELECT TIMESTAMPDIFF(SECOND, '2017-03-31 07:00', now())").fetchone()
57                 seconds_diff = int(seconds_diff)
58                 con.execute("UPDATE wrreport SET date_report = DATE(date_report + INTERVAL %s SECOND) WHERE time_report IS NULL", (seconds_diff,))
59                 con.execute("UPDATE wrreport SET date_report = DATE(TIMESTAMP(date_report, time_report) + INTERVAL %s SECOND) WHERE time_report IS NOT NULL", (seconds_diff,))
60                 con.execute("UPDATE wrreport SET time_report = TIME(TIMESTAMP(date_report, time_report) + INTERVAL %s SECOND) WHERE time_report IS NOT NULL", (seconds_diff,))
61                 con.execute("UPDATE wrreport SET date_entry = date_entry + INTERVAL %s SECOND", (seconds_diff,))
62                 con.execute("UPDATE wrreport SET date_invalid = date_invalid + INTERVAL %s SECOND", (seconds_diff,))
63                 con.execute("UPDATE wrreport SET delete_date = delete_date + INTERVAL %s SECOND", (seconds_diff,))
64
65     def login(self):
66         return self.app.post('/login', data={'user_name': 'Johndoe', 'password': 'doejohn'}, follow_redirects=True)
67
68     def login_and_get(self, url: str):
69         result = self.app.get(url)
70         self.assertEqual(result.status_code, 302)
71         result = self.login()
72         self.assertEqual(result.status_code, 200)
73         result = self.app.get(url)
74         self.assertEqual(result.status_code, 200)
75         return result
76
77     def test_bericht_list(self):
78         result = self.login_and_get('/bericht/list')
79         self.assertTrue(result.data.startswith(b'<!doctype html'))
80         soup = bs4.BeautifulSoup(result.data, 'html.parser')
81         self.assertEqual(soup.title.text, 'Rodelbahnberichte')
82
83     def test_bericht_view(self):
84         result = self.login_and_get('/bericht/view/19591')
85         self.assertTrue(result.data.startswith(b'<!doctype html'))
86         soup = bs4.BeautifulSoup(result.data, 'html.parser')
87         self.assertEqual(soup.title.text, 'Rodelbahnbericht #19591')
88         self.assertIn('Brandstatt Alm', str(soup.table))
89
90     def test_bericht_change_date_invalid_twoweeks(self):
91         url = '/bericht/change_date_invalid/19591'
92         post_data = {'date_invalid': 'two_weeks', 'date_userdefined': '2018-01-30 18:26'}
93         result = self.app.post(url, data=post_data)
94         self.assertEqual(result.status_code, 302)
95         result = self.app.post(url, data=post_data, follow_redirects=True)
96         self.assertEqual(result.status_code, 200)
97         result = self.login()
98         self.assertEqual(result.status_code, 200)
99         result = self.app.post(url, data=post_data, follow_redirects=True)
100         self.assertEqual(result.status_code, 200)
101         self.assertTrue(result.data.startswith(b'<!doctype html'))
102         soup = bs4.BeautifulSoup(result.data, 'html.parser')
103         self.assertEqual(soup.title.text, 'Rodelbahnbericht #19591')
104         self.assertIn('Datum wurde erfolgreich geändert', str(soup))
105
106     def test_bericht_change_date_invalid_userdefined(self):
107         self.login()
108         post_data = {'date_invalid': 'userdefined', 'date_userdefined': '2018-01-30 15:09'}
109         result = self.app.post('/bericht/change_date_invalid/19591', data=post_data, follow_redirects=True)
110         self.assertEqual(result.status_code, 200)
111         self.assertTrue(result.data.startswith(b'<!doctype html'))
112         soup = bs4.BeautifulSoup(result.data, 'html.parser')
113         self.assertEqual(soup.title.text, 'Rodelbahnbericht #19591')
114         self.assertIn('Datum wurde erfolgreich geändert', str(soup))
115         self.assertIn('2018-01-30 15:09:00', str(soup))
116
117     def test_gasthaus_list(self):
118         result = self.app.get('/gasthaus/list')
119         self.assertEqual(result.status_code, 200)
120         self.assertTrue(result.data.startswith(b'<!doctype html'))
121         soup = bs4.BeautifulSoup(result.data, 'html.parser')
122         self.assertEqual(soup.title.text, 'Gasthäuser')
123         self.assertIn('Meißner', str(soup.table))
124
125     def test_gasthaus_view(self):
126         result = self.app.get('/gasthaus/view/362')
127         self.assertEqual(result.status_code, 200)
128         self.assertTrue(result.data.startswith(b'<!doctype html'))
129         soup = bs4.BeautifulSoup(result.data, 'html.parser')
130         self.assertIn('Gasthaus', soup.title.text)
131         self.assertIn('Meißner', str(soup))
132
133     def test_gasthaus_update_success(self):
134         result = self.app.get('/gasthaus/update', follow_redirects=True)
135         self.assertEqual(result.status_code, 200)
136         self.assertTrue(result.data.startswith(b'<!doctype html'))
137         soup = bs4.BeautifulSoup(result.data, 'html.parser')
138         self.assertIn('Die Gasthausliste wurde erfolgreich aktualisiert.', str(soup))
139
140     def test_gasthaus_update_fail(self):
141         session = wradmin.model.meta.Session
142         text = session.query(wradmin.model.MwText).get(8719)
143         text.old_text = text.old_text.replace('maria-waldrast@aon.at', 'abc@def@example.com')
144         session.commit()
145         result = self.app.get('/gasthaus/update', follow_redirects=True)
146         self.assertEqual(result.status_code, 200)
147         self.assertTrue(result.data.startswith(b'<!doctype html'))
148         soup = bs4.BeautifulSoup(result.data, 'html.parser')
149         self.assertIn('Fehler bei Gasthaus \'Maria Waldrast (Klostergasthaus)\'', str(soup))
150
151     def test_rodelbahn_list(self):
152         result = self.app.get('/rodelbahn/list')
153         self.assertEqual(result.status_code, 200)
154         self.assertTrue(result.data.startswith(b'<!doctype html'))
155         soup = bs4.BeautifulSoup(result.data, 'html.parser')
156         self.assertEqual(soup.title.text, 'Rodelbahnen')
157         table = soup.find('table')
158         self.assertIsNotNone(table)
159         rows = table.find_all('tr')
160         self.assertEqual(len(rows), 9+1)
161
162     def test_rodelbahn_view(self):
163         result = self.app.get('/rodelbahn/view/926')
164         self.assertTrue(result.data.startswith(b'<!doctype html'))
165         soup = bs4.BeautifulSoup(result.data, 'html.parser')
166         self.assertIn('Rodelbahn', soup.title.text)
167         self.assertIn('Hühnerspiel', str(soup))
168
169     def test_rodelbahn_update_success(self):
170         result = self.app.get('/rodelbahn/update', follow_redirects=True)
171         self.assertEqual(result.status_code, 200)
172         self.assertTrue(result.data.startswith(b'<!doctype html'))
173         soup = bs4.BeautifulSoup(result.data, 'html.parser')
174         self.assertIn('Die Rodelbahnliste wurde erfolgreich aktualisiert.', str(soup))
175
176     def test_rodelbahn_update_fail(self):
177         session = wradmin.model.meta.Session
178         text = session.query(wradmin.model.MwText).get(12106)
179         text.old_text = text.old_text.replace('Schwierigkeit        = mittel', 'Schwierigkeit        = geht so')
180         session.commit()
181         result = self.app.get('/rodelbahn/update', follow_redirects=True)
182         self.assertEqual(result.status_code, 200)
183         self.assertTrue(result.data.startswith(b'<!doctype html'))
184         soup = bs4.BeautifulSoup(result.data, 'html.parser')
185         self.assertIn('Fehler bei Rodelbahn \'Juifenalm\'', str(soup))
186
187     def test_rodelbahn_update_regioncache(self):
188         result = self.app.get('/rodelbahn/update_regioncache', follow_redirects=True)
189         self.assertEqual(result.status_code, 200)
190         self.assertTrue(result.data.startswith(b'<!doctype html'))
191         soup = bs4.BeautifulSoup(result.data, 'html.parser')
192         self.assertIn('Die Rodelbahneinträge in den Regionslisten wurden erfolgreich aktualisiert.', str(soup))