From 4c7caa1a38d3df6d7489789a8f93b602e6bc7396 Mon Sep 17 00:00:00 2001 From: 3wc <3wc.cyberia@doesthisthing.work> Date: Sat, 17 Jul 2021 11:42:44 +0200 Subject: [PATCH] Initial console tests NB capsul create isn't working properly, see #83 --- capsulflask/tests/test_console.py | 95 +++++++++++++++++++++++++++++++ capsulflask/tests_base.py | 3 + 2 files changed, 98 insertions(+) create mode 100644 capsulflask/tests/test_console.py diff --git a/capsulflask/tests/test_console.py b/capsulflask/tests/test_console.py new file mode 100644 index 0000000..1b9f4ca --- /dev/null +++ b/capsulflask/tests/test_console.py @@ -0,0 +1,95 @@ +from flask import url_for + +from capsulflask.db import get_model +from capsulflask.tests_base import BaseTestCase + + +class ConsoleTests(BaseTestCase): + capsul_data = { + "size": "f1-xs", + "os": "debian10", + "ssh_authorized_key_count": 1, + "ssh_key_0": "key" + } + + def test_index(self): + self._login('test@example.com') + with self.client as client: + response = client.get(url_for("console.index")) + self.assert_200(response) + + def test_create_loads(self): + self._login('test@example.com') + with self.client as client: + response = client.get(url_for("console.create")) + self.assert_200(response) + + def test_create_fails_capacity(self): + with self.client as client: + client.get(url_for("console.create")) + csrf_token = self.get_context_variable('csrf_token') + + data = self.capsul_data + data['csrf-token'] = csrf_token + client.post(url_for("console.create"), data=data) + capacity_message = \ + '\n host(s) at capacity. no capsuls can be created at this time. sorry. \n ' + self.assert_message_flashed(capacity_message, category='message') + + self.assertEqual( + len(get_model().list_vms_for_account('test@example.com')), + 0 + ) + + def test_create_fails_invalid(self): + with self.client as client: + client.get(url_for("console.create")) + csrf_token = self.get_context_variable('csrf_token') + + data = self.capsul_data + data['csrf-token'] = csrf_token + data['os'] = '' + client.post(url_for("console.create"), data=data) + + self.assert_message_flashed( + 'OS is required', + category='message' + ) + + self.assertEqual( + len(get_model().list_vms_for_account('test@example.com')), + 0 + ) + + def test_create_succeeds(self): + with self.client as client: + client.get(url_for("console.create")) + csrf_token = self.get_context_variable('csrf_token') + + data = self.capsul_data + data['csrf-token'] = csrf_token + response = client.post(url_for("console.create"), data=data) + + vms = get_model().list_vms_for_account('test@example.com') + + self.assertEqual( + len(vms), + 1 # FIXME: mock create doesn't create, see #83 + ) + + return + + vm_id = vms[0].id + + self.assertRedirects( + response, + url_for("console.index") + f'?{vm_id}' + ) + + + def setUp(self): + self._login('test@example.com') + get_model().create_ssh_public_key('test@example.com', 'key', 'foo') + + def tearDown(self): + get_model().delete_ssh_public_key('test@example.com', 'key') diff --git a/capsulflask/tests_base.py b/capsulflask/tests_base.py index 951f5b0..8067d5e 100644 --- a/capsulflask/tests_base.py +++ b/capsulflask/tests_base.py @@ -4,6 +4,7 @@ from nanoid import generate from flask_testing import TestCase from capsulflask import create_app +from capsulflask.db import get_model class BaseTestCase(TestCase): def create_app(self): @@ -11,6 +12,7 @@ class BaseTestCase(TestCase): os.environ['POSTGRES_CONNECTION_PARAMETERS'] = "host=localhost port=5432 user=postgres password=dev dbname=capsulflask_test" os.environ['TESTING'] = '1' os.environ['SPOKE_MODEL'] = 'mock' + os.environ['HUB_MODEL'] = 'mock' return create_app() def setUp(self): @@ -20,6 +22,7 @@ class BaseTestCase(TestCase): pass def _login(self, user_email): + get_model().login(user_email) with self.client.session_transaction() as session: session['account'] = user_email session['csrf-token'] = generate()