2021-07-27 18:28:49 +00:00
|
|
|
from io import StringIO
|
|
|
|
import logging
|
|
|
|
import unittest
|
2021-07-16 15:51:06 +00:00
|
|
|
import os
|
|
|
|
from nanoid import generate
|
|
|
|
|
|
|
|
from flask_testing import TestCase
|
2021-07-27 18:28:49 +00:00
|
|
|
from flask import current_app
|
2021-07-16 15:51:06 +00:00
|
|
|
|
|
|
|
from capsulflask import create_app
|
2021-07-17 09:42:44 +00:00
|
|
|
from capsulflask.db import get_model
|
2021-07-16 15:51:06 +00:00
|
|
|
|
|
|
|
class BaseTestCase(TestCase):
|
|
|
|
def create_app(self):
|
|
|
|
# Use default connection paramaters
|
|
|
|
os.environ['POSTGRES_CONNECTION_PARAMETERS'] = "host=localhost port=5432 user=postgres password=dev dbname=capsulflask_test"
|
|
|
|
os.environ['TESTING'] = '1'
|
2021-07-27 17:48:48 +00:00
|
|
|
os.environ['LOG_LEVEL'] = 'DEBUG'
|
2021-07-16 15:51:06 +00:00
|
|
|
os.environ['SPOKE_MODEL'] = 'mock'
|
2021-07-27 17:48:48 +00:00
|
|
|
os.environ['HUB_MODEL'] = 'capsul-flask'
|
2021-07-27 18:28:49 +00:00
|
|
|
self.app = create_app()
|
|
|
|
return self.app
|
2021-07-16 15:51:06 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
2021-07-27 18:28:49 +00:00
|
|
|
self.logs_from_test = StringIO()
|
|
|
|
self.logs_handler = logging.StreamHandler(self.logs_from_test)
|
|
|
|
current_app.logger.addHandler(self.logs_handler)
|
2021-07-16 15:51:06 +00:00
|
|
|
|
|
|
|
def tearDown(self):
|
2021-07-27 18:28:49 +00:00
|
|
|
#self.app.logger.removeHandler(self.logs_handler)
|
|
|
|
#logging.getLogger("asdasd1").warning(f"{self.id()} captured output:\n{self.logs_from_test.getvalue()}\n")
|
|
|
|
|
|
|
|
file_object = open('unittest-output.log', 'a')
|
|
|
|
file_object.write(f"{self.id()} captured output:\n{self.logs_from_test.getvalue()}\n")
|
|
|
|
file_object.close()
|
2021-07-16 15:51:06 +00:00
|
|
|
|
|
|
|
def _login(self, user_email):
|
2021-07-17 09:42:44 +00:00
|
|
|
get_model().login(user_email)
|
2021-07-16 15:51:06 +00:00
|
|
|
with self.client.session_transaction() as session:
|
|
|
|
session['account'] = user_email
|
|
|
|
session['csrf-token'] = generate()
|
2021-07-27 18:28:49 +00:00
|
|
|
|