Add preliminary automated tests #1

Merged
3wordchant merged 16 commits from tests into master 2021-08-16 00:11:16 +00:00
2 changed files with 25 additions and 4 deletions
Showing only changes of commit 4e510ebb01 - Show all commits

View File

@ -1,3 +1,5 @@
import logging
from copy import deepcopy from copy import deepcopy
from unittest.mock import patch from unittest.mock import patch
@ -81,6 +83,10 @@ class ConsoleTests(BaseTestCase):
0 0
) )
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()
def test_create_fails_invalid(self): def test_create_fails_invalid(self):
with self.client as client: with self.client as client:
client.get(url_for("console.create")) client.get(url_for("console.create"))
@ -190,10 +196,12 @@ class ConsoleTests(BaseTestCase):
) )
def setUp(self): def setUp(self):
super().setUp()
self._login('test@example.com') self._login('test@example.com')
get_model().create_ssh_public_key('test@example.com', 'key', 'foo') get_model().create_ssh_public_key('test@example.com', 'key', 'foo')
def tearDown(self): def tearDown(self):
super().tearDown()
get_model().cursor.execute("DELETE FROM ssh_public_keys") get_model().cursor.execute("DELETE FROM ssh_public_keys")
get_model().cursor.execute("DELETE FROM login_tokens") get_model().cursor.execute("DELETE FROM login_tokens")
get_model().cursor.execute("DELETE FROM vms") get_model().cursor.execute("DELETE FROM vms")

View File

@ -1,7 +1,11 @@
from io import StringIO
import logging
import unittest
import os import os
from nanoid import generate from nanoid import generate
from flask_testing import TestCase from flask_testing import TestCase
from flask import current_app
from capsulflask import create_app from capsulflask import create_app
from capsulflask.db import get_model from capsulflask.db import get_model
@ -14,16 +18,25 @@ class BaseTestCase(TestCase):
os.environ['LOG_LEVEL'] = 'DEBUG' os.environ['LOG_LEVEL'] = 'DEBUG'
os.environ['SPOKE_MODEL'] = 'mock' os.environ['SPOKE_MODEL'] = 'mock'
os.environ['HUB_MODEL'] = 'capsul-flask' os.environ['HUB_MODEL'] = 'capsul-flask'
return create_app() self.app = create_app()
return self.app
def setUp(self): def setUp(self):
pass self.logs_from_test = StringIO()
self.logs_handler = logging.StreamHandler(self.logs_from_test)
current_app.logger.addHandler(self.logs_handler)
def tearDown(self): def tearDown(self):
pass #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()
def _login(self, user_email): def _login(self, user_email):
get_model().login(user_email) get_model().login(user_email)
with self.client.session_transaction() as session: with self.client.session_transaction() as session:
session['account'] = user_email session['account'] = user_email
session['csrf-token'] = generate() session['csrf-token'] = generate()