]> ToastFreeware Gitweb - philipp/winterrodeln/wradmin.git/blobdiff - tests/test_wradmin.py
Insert testdb.sql as raw SQL.
[philipp/winterrodeln/wradmin.git] / tests / test_wradmin.py
index 4dce7bdbf0d5b7b3f5ca9ddcfdfd00a64ec277f5..eb8a9ccf5d2e9306f0a120f90672850f70c4e18d 100644 (file)
@@ -1,9 +1,6 @@
-"""How to test wradmin:
-
-WRADMIN_SETTINGS=../wradmin/test.cfg python3 -m unittest tests.test_wradmin
-"""
 import unittest
 import bs4  # beautiful soup
+import wradmin.model
 import wradmin
 
 
@@ -17,21 +14,21 @@ class TestNoDbWradmin(WradminTestBase):
     def test_root(self):
         result = self.app.get('/')
         self.assertEqual(result.status_code, 200)
-        self.assertTrue(result.data.startswith(b'<!DOCTYPE html'))
+        self.assertTrue(result.data.startswith(b'<!doctype html'))
         soup = bs4.BeautifulSoup(result.data, 'html.parser')
         self.assertEqual(soup.title.text, 'Hauptmenü')
 
     def test_coordtool(self):
         result = self.app.get('/coordtool/index')
         self.assertEqual(result.status_code, 200)
-        self.assertTrue(result.data.startswith(b'<!DOCTYPE html'))
+        self.assertTrue(result.data.startswith(b'<!doctype html'))
         soup = bs4.BeautifulSoup(result.data, 'html.parser')
         self.assertEqual(soup.title.text, 'Koordinaten-Rechner')
 
     def test_coordtool_convert_ok(self):
         result = self.app.post('/coordtool/convert', data={'input': '47.987, 11.123'}, follow_redirects=True)
         self.assertEqual(result.status_code, 200)
-        self.assertTrue(result.data.startswith(b'<!DOCTYPE html'))
+        self.assertTrue(result.data.startswith(b'<!doctype html'))
         soup = bs4.BeautifulSoup(result.data, 'html.parser')
         self.assertEqual(soup.title.text, 'Koordinaten-Rechner')
         self.assertIn('GPX Format', str(soup))
@@ -39,7 +36,7 @@ class TestNoDbWradmin(WradminTestBase):
     def test_coordtool_convert_error(self):
         result = self.app.post('/coordtool/convert', data={'input': 'abc'}, follow_redirects=True)
         self.assertEqual(result.status_code, 200)
-        self.assertTrue(result.data.startswith(b'<!DOCTYPE html'))
+        self.assertTrue(result.data.startswith(b'<!doctype html'))
         soup = bs4.BeautifulSoup(result.data, 'html.parser')
         self.assertEqual(soup.title.text, 'Koordinaten-Rechner')
         self.assertIn('no known format', str(soup))
@@ -53,7 +50,7 @@ class TestDbWradmin(WradminTestBase):
             with open('tests/testdb.sql', 'r') as f:
                 sql = f.read()
             with wradmin.model.meta.engine.begin() as con:
-                con.execute(sql)
+                con.execution_options(no_parameters=True).execute(sql)
 
                 # update dates
                 seconds_diff, = con.execute("SELECT TIMESTAMPDIFF(SECOND, '2017-03-31 07:00', now())").fetchone()
@@ -65,34 +62,62 @@ class TestDbWradmin(WradminTestBase):
                 con.execute("UPDATE wrreport SET date_invalid = date_invalid + INTERVAL %s SECOND", (seconds_diff,))
                 con.execute("UPDATE wrreport SET delete_date = delete_date + INTERVAL %s SECOND", (seconds_diff,))
 
-    def test_bericht_list(self):
-        result = self.app.get('/bericht/list')
+    def login(self):
+        return self.app.post('/login', data={'user_name': 'Johndoe', 'password': 'doejohn'}, follow_redirects=True)
+
+    def login_and_get(self, url: str):
+        result = self.app.get(url)
+        self.assertEqual(result.status_code, 302)
+        result = self.login()
         self.assertEqual(result.status_code, 200)
-        self.assertTrue(result.data.startswith(b'<!DOCTYPE html'))
+        result = self.app.get(url)
+        self.assertEqual(result.status_code, 200)
+        return result
+
+    def test_bericht_list(self):
+        result = self.login_and_get('/bericht/list')
+        self.assertTrue(result.data.startswith(b'<!doctype html'))
         soup = bs4.BeautifulSoup(result.data, 'html.parser')
         self.assertEqual(soup.title.text, 'Rodelbahnberichte')
 
     def test_bericht_view(self):
-        result = self.app.get('/bericht/view/19591')
-        self.assertEqual(result.status_code, 200)
-        self.assertTrue(result.data.startswith(b'<!DOCTYPE html'))
+        result = self.login_and_get('/bericht/view/19591')
+        self.assertTrue(result.data.startswith(b'<!doctype html'))
         soup = bs4.BeautifulSoup(result.data, 'html.parser')
-        self.assertEqual(soup.title.text, 'Rodelbahnbericht')
+        self.assertEqual(soup.title.text, 'Rodelbahnbericht #19591')
         self.assertIn('Brandstatt Alm', str(soup.table))
 
     def test_bericht_change_date_invalid_twoweeks(self):
+        url = '/bericht/change_date_invalid/19591'
         post_data = {'date_invalid': 'two_weeks', 'date_userdefined': '2018-01-30 18:26'}
+        result = self.app.post(url, data=post_data)
+        self.assertEqual(result.status_code, 302)
+        result = self.app.post(url, data=post_data, follow_redirects=True)
+        self.assertEqual(result.status_code, 200)
+        result = self.login()
+        self.assertEqual(result.status_code, 200)
+        result = self.app.post(url, data=post_data, follow_redirects=True)
+        self.assertEqual(result.status_code, 200)
+        self.assertTrue(result.data.startswith(b'<!doctype html'))
+        soup = bs4.BeautifulSoup(result.data, 'html.parser')
+        self.assertEqual(soup.title.text, 'Rodelbahnbericht #19591')
+        self.assertIn('Datum wurde erfolgreich geändert', str(soup))
+
+    def test_bericht_change_date_invalid_userdefined(self):
+        self.login()
+        post_data = {'date_invalid': 'userdefined', 'date_userdefined': '2018-01-30 15:09'}
         result = self.app.post('/bericht/change_date_invalid/19591', data=post_data, follow_redirects=True)
         self.assertEqual(result.status_code, 200)
-        self.assertTrue(result.data.startswith(b'<!DOCTYPE html'))
+        self.assertTrue(result.data.startswith(b'<!doctype html'))
         soup = bs4.BeautifulSoup(result.data, 'html.parser')
-        self.assertEqual(soup.title.text, 'Rodelbahnbericht')
+        self.assertEqual(soup.title.text, 'Rodelbahnbericht #19591')
         self.assertIn('Datum wurde erfolgreich geändert', str(soup))
+        self.assertIn('2018-01-30 15:09:00', str(soup))
 
     def test_gasthaus_list(self):
         result = self.app.get('/gasthaus/list')
         self.assertEqual(result.status_code, 200)
-        self.assertTrue(result.data.startswith(b'<!DOCTYPE html'))
+        self.assertTrue(result.data.startswith(b'<!doctype html'))
         soup = bs4.BeautifulSoup(result.data, 'html.parser')
         self.assertEqual(soup.title.text, 'Gasthäuser')
         self.assertIn('Meißner', str(soup.table))
@@ -100,22 +125,33 @@ class TestDbWradmin(WradminTestBase):
     def test_gasthaus_view(self):
         result = self.app.get('/gasthaus/view/362')
         self.assertEqual(result.status_code, 200)
-        self.assertTrue(result.data.startswith(b'<!DOCTYPE html'))
+        self.assertTrue(result.data.startswith(b'<!doctype html'))
         soup = bs4.BeautifulSoup(result.data, 'html.parser')
-        self.assertEqual(soup.title.text, 'Gasthaus')
+        self.assertIn('Gasthaus', soup.title.text)
         self.assertIn('Meißner', str(soup))
 
-    def test_gasthaus_update(self):
+    def test_gasthaus_update_success(self):
         result = self.app.get('/gasthaus/update', follow_redirects=True)
         self.assertEqual(result.status_code, 200)
-        self.assertTrue(result.data.startswith(b'<!DOCTYPE html'))
+        self.assertTrue(result.data.startswith(b'<!doctype html'))
         soup = bs4.BeautifulSoup(result.data, 'html.parser')
         self.assertIn('Die Gasthausliste wurde erfolgreich aktualisiert.', str(soup))
 
+    def test_gasthaus_update_fail(self):
+        session = wradmin.model.meta.Session
+        text = session.query(wradmin.model.MwText).get(8719)
+        text.old_text = text.old_text.replace('maria-waldrast@aon.at', 'abc@def@example.com')
+        session.commit()
+        result = self.app.get('/gasthaus/update', follow_redirects=True)
+        self.assertEqual(result.status_code, 200)
+        self.assertTrue(result.data.startswith(b'<!doctype html'))
+        soup = bs4.BeautifulSoup(result.data, 'html.parser')
+        self.assertIn('Fehler bei Gasthaus \'Maria Waldrast (Klostergasthaus)\'', str(soup))
+
     def test_rodelbahn_list(self):
         result = self.app.get('/rodelbahn/list')
         self.assertEqual(result.status_code, 200)
-        self.assertTrue(result.data.startswith(b'<!DOCTYPE html'))
+        self.assertTrue(result.data.startswith(b'<!doctype html'))
         soup = bs4.BeautifulSoup(result.data, 'html.parser')
         self.assertEqual(soup.title.text, 'Rodelbahnen')
         table = soup.find('table')
@@ -125,22 +161,32 @@ class TestDbWradmin(WradminTestBase):
 
     def test_rodelbahn_view(self):
         result = self.app.get('/rodelbahn/view/926')
-        self.assertEqual(result.status_code, 200)
-        self.assertTrue(result.data.startswith(b'<!DOCTYPE html'))
+        self.assertTrue(result.data.startswith(b'<!doctype html'))
         soup = bs4.BeautifulSoup(result.data, 'html.parser')
-        self.assertEqual(soup.title.text, 'Rodelbahn')
+        self.assertIn('Rodelbahn', soup.title.text)
         self.assertIn('Hühnerspiel', str(soup))
 
-    def test_rodelbahn_update(self):
+    def test_rodelbahn_update_success(self):
         result = self.app.get('/rodelbahn/update', follow_redirects=True)
         self.assertEqual(result.status_code, 200)
-        self.assertTrue(result.data.startswith(b'<!DOCTYPE html'))
+        self.assertTrue(result.data.startswith(b'<!doctype html'))
         soup = bs4.BeautifulSoup(result.data, 'html.parser')
         self.assertIn('Die Rodelbahnliste wurde erfolgreich aktualisiert.', str(soup))
 
+    def test_rodelbahn_update_fail(self):
+        session = wradmin.model.meta.Session
+        text = session.query(wradmin.model.MwText).get(12106)
+        text.old_text = text.old_text.replace('Schwierigkeit        = mittel', 'Schwierigkeit        = geht so')
+        session.commit()
+        result = self.app.get('/rodelbahn/update', follow_redirects=True)
+        self.assertEqual(result.status_code, 200)
+        self.assertTrue(result.data.startswith(b'<!doctype html'))
+        soup = bs4.BeautifulSoup(result.data, 'html.parser')
+        self.assertIn('Fehler bei Rodelbahn \'Juifenalm\'', str(soup))
+
     def test_rodelbahn_update_regioncache(self):
         result = self.app.get('/rodelbahn/update_regioncache', follow_redirects=True)
         self.assertEqual(result.status_code, 200)
-        self.assertTrue(result.data.startswith(b'<!DOCTYPE html'))
+        self.assertTrue(result.data.startswith(b'<!doctype html'))
         soup = bs4.BeautifulSoup(result.data, 'html.parser')
         self.assertIn('Die Rodelbahneinträge in den Regionslisten wurden erfolgreich aktualisiert.', str(soup))