Нет описания

test_helper.py 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # IRIS Source Code
  2. # Copyright (C) 2021 - Airbus CyberSecurity (SAS)
  3. # ir@cyberactionlab.net
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU Lesser General Public
  7. # License as published by the Free Software Foundation; either
  8. # version 3 of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. # Lesser General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Lesser General Public License
  16. # along with this program; if not, write to the Free Software Foundation,
  17. # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. from os import environ
  19. from unittest import TestCase
  20. import re
  21. from flask import url_for
  22. from flask.testing import FlaskClient
  23. from random import randrange
  24. from app import app
  25. from app.datamgmt.client.client_db import create_client
  26. from app.models import Client
  27. class TestHelper(TestCase):
  28. @staticmethod
  29. def log_in(test_app: FlaskClient) -> None:
  30. login_page = test_app.get('/login')
  31. csrf_token = re.search(r'id="csrf_token" name="csrf_token" type="hidden" value="(.*?)"', str(login_page.data)).group(1)
  32. test_app.post('/login', data=dict(username='administrator', password=environ.get("IRIS_ADM_PASSWORD", ""), csrf_token=csrf_token), follow_redirects=True)
  33. def verify_path_without_cid_redirects_correctly(self, path: str, assert_string: str):
  34. with app.test_client() as test_app:
  35. self.log_in(test_app)
  36. result = test_app.get(url_for(path))
  37. self.assertEqual(302, result.status_code)
  38. self.assertIn(assert_string, str(result.data))
  39. result2 = test_app.get(url_for(path), follow_redirects=True)
  40. self.assertEqual(200, result2.status_code)
  41. @staticmethod
  42. def create_client(client_name: str = None) -> Client:
  43. client_name = client_name if client_name is not None else f"client_name_{randrange(1,10000)}"
  44. new_client = create_client(client_name)
  45. return new_client