]> ToastFreeware Gitweb - philipp/winterrodeln/wradmin.git/blob - tests/test_wradmin.py
80515c23847b89b9b3296e51207516b185192113
[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.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 test_bericht_list_auth(self):
66         result = self.app.get('/bericht/list')
67         self.assertEqual(result.status_code, 302)
68
69     def test_bericht_list(self):
70         result = self.app.post('/login', data={'user': 'john', 'password': 'doe'})
71         self.assertEqual(result.status_code, 200)
72
73         result = self.app.get('/bericht/list')
74         self.assertEqual(result.status_code, 200)
75         self.assertTrue(result.data.startswith(b'<!doctype html'))
76         soup = bs4.BeautifulSoup(result.data, 'html.parser')
77         self.assertEqual(soup.title.text, 'Rodelbahnberichte')
78
79     def test_bericht_view(self):
80         result = self.app.get('/bericht/view/19591')
81         self.assertEqual(result.status_code, 200)
82         self.assertTrue(result.data.startswith(b'<!doctype html'))
83         soup = bs4.BeautifulSoup(result.data, 'html.parser')
84         self.assertEqual(soup.title.text, 'Rodelbahnbericht #19591')
85         self.assertIn('Brandstatt Alm', str(soup.table))
86
87     def test_bericht_change_date_invalid_twoweeks(self):
88         post_data = {'date_invalid': 'two_weeks', 'date_userdefined': '2018-01-30 18:26'}
89         result = self.app.post('/bericht/change_date_invalid/19591', data=post_data, follow_redirects=True)
90         self.assertEqual(result.status_code, 200)
91         self.assertTrue(result.data.startswith(b'<!doctype html'))
92         soup = bs4.BeautifulSoup(result.data, 'html.parser')
93         self.assertEqual(soup.title.text, 'Rodelbahnbericht #19591')
94         self.assertIn('Datum wurde erfolgreich geändert', str(soup))
95
96     def test_bericht_change_date_invalid_userdefined(self):
97         post_data = {'date_invalid': 'userdefined', 'date_userdefined': '2018-01-30 15:09'}
98         result = self.app.post('/bericht/change_date_invalid/19591', data=post_data, follow_redirects=True)
99         self.assertEqual(result.status_code, 200)
100         self.assertTrue(result.data.startswith(b'<!doctype html'))
101         soup = bs4.BeautifulSoup(result.data, 'html.parser')
102         self.assertEqual(soup.title.text, 'Rodelbahnbericht #19591')
103         self.assertIn('Datum wurde erfolgreich geändert', str(soup))
104         self.assertIn('2018-01-30 15:09:00', str(soup))
105
106     def test_gasthaus_list(self):
107         result = self.app.get('/gasthaus/list')
108         self.assertEqual(result.status_code, 200)
109         self.assertTrue(result.data.startswith(b'<!doctype html'))
110         soup = bs4.BeautifulSoup(result.data, 'html.parser')
111         self.assertEqual(soup.title.text, 'Gasthäuser')
112         self.assertIn('Meißner', str(soup.table))
113
114     def test_gasthaus_view(self):
115         result = self.app.get('/gasthaus/view/362')
116         self.assertEqual(result.status_code, 200)
117         self.assertTrue(result.data.startswith(b'<!doctype html'))
118         soup = bs4.BeautifulSoup(result.data, 'html.parser')
119         self.assertIn('Gasthaus', soup.title.text)
120         self.assertIn('Meißner', str(soup))
121
122     def test_gasthaus_update_success(self):
123         result = self.app.get('/gasthaus/update', follow_redirects=True)
124         self.assertEqual(result.status_code, 200)
125         self.assertTrue(result.data.startswith(b'<!doctype html'))
126         soup = bs4.BeautifulSoup(result.data, 'html.parser')
127         self.assertIn('Die Gasthausliste wurde erfolgreich aktualisiert.', str(soup))
128
129     def test_gasthaus_update_fail(self):
130         session = wradmin.model.meta.Session
131         text = session.query(wradmin.model.MwText).get(8719)
132         text.old_text = text.old_text.replace('maria-waldrast@aon.at', 'abc@def@example.com')
133         session.commit()
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('Fehler bei Gasthaus \'Maria Waldrast (Klostergasthaus)\'', str(soup))
139
140     def test_rodelbahn_list(self):
141         result = self.app.get('/rodelbahn/list')
142         self.assertEqual(result.status_code, 200)
143         self.assertTrue(result.data.startswith(b'<!doctype html'))
144         soup = bs4.BeautifulSoup(result.data, 'html.parser')
145         self.assertEqual(soup.title.text, 'Rodelbahnen')
146         table = soup.find('table')
147         self.assertIsNotNone(table)
148         rows = table.find_all('tr')
149         self.assertEqual(len(rows), 9+1)
150
151     def test_rodelbahn_view(self):
152         result = self.app.get('/rodelbahn/view/926')
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.assertIn('Rodelbahn', soup.title.text)
157         self.assertIn('Hühnerspiel', str(soup))
158
159     def test_rodelbahn_update_success(self):
160         result = self.app.get('/rodelbahn/update', follow_redirects=True)
161         self.assertEqual(result.status_code, 200)
162         self.assertTrue(result.data.startswith(b'<!doctype html'))
163         soup = bs4.BeautifulSoup(result.data, 'html.parser')
164         self.assertIn('Die Rodelbahnliste wurde erfolgreich aktualisiert.', str(soup))
165
166     def test_rodelbahn_update_fail(self):
167         session = wradmin.model.meta.Session
168         text = session.query(wradmin.model.MwText).get(12106)
169         text.old_text = text.old_text.replace('Schwierigkeit        = mittel', 'Schwierigkeit        = geht so')
170         session.commit()
171         result = self.app.get('/rodelbahn/update', follow_redirects=True)
172         self.assertEqual(result.status_code, 200)
173         self.assertTrue(result.data.startswith(b'<!doctype html'))
174         soup = bs4.BeautifulSoup(result.data, 'html.parser')
175         self.assertIn('Fehler bei Rodelbahn \'Juifenalm\'', str(soup))
176
177     def test_rodelbahn_update_regioncache(self):
178         result = self.app.get('/rodelbahn/update_regioncache', follow_redirects=True)
179         self.assertEqual(result.status_code, 200)
180         self.assertTrue(result.data.startswith(b'<!doctype html'))
181         soup = bs4.BeautifulSoup(result.data, 'html.parser')
182         self.assertIn('Die Rodelbahneinträge in den Regionslisten wurden erfolgreich aktualisiert.', str(soup))