From 30a9e6400a4b48afdf148188a3a3c2038414a2f0 Mon Sep 17 00:00:00 2001
From: Dmitry <b4tm4n@mail.ru>
Date: Wed, 22 Sep 2021 13:57:03 +0300
Subject: [PATCH] add tests

---
 tests/__init__.py        |  0
 tests/test_pub1c-rest.py | 60 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 60 insertions(+)
 create mode 100644 tests/__init__.py
 create mode 100644 tests/test_pub1c-rest.py

diff --git a/tests/__init__.py b/tests/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/tests/test_pub1c-rest.py b/tests/test_pub1c-rest.py
new file mode 100644
index 0000000..6aa8b4b
--- /dev/null
+++ b/tests/test_pub1c-rest.py
@@ -0,0 +1,60 @@
+import os
+
+from flask import Flask
+import pytest
+import importlib
+import yaml
+
+
+@pytest.fixture
+def temp_config(tmpdir) -> str:
+    configfile = str(tmpdir.join('config.yml'))
+    vrd_path = tmpdir.mkdir('vrds')
+    dir_path = tmpdir.mkdir('pubs')
+    apache_config = tmpdir.join('apache.cfg')
+    apache_config.write('#start\n')
+    flagfile = tmpdir.join('apache_restart')
+
+    with open(configfile, 'w') as f:
+        yaml.dump({
+            'apache_config': str(apache_config),
+            'vrd_path': str(vrd_path),
+            'dir_path': str(dir_path),
+            'url_base': '/1c',
+            'platform_path': '/opt/1cv8/x86_64/current',
+            'ws_module': 'wsap24.so',
+            'vrd_params': {
+                'debug': None,
+                'server_addr': 'localhost'
+            },
+            'infobases': {
+                'server_file': 'test/1CV8Clst.lst',
+            },
+            'apache_restart_flagfile': str(flagfile),
+            'url_prefix': 'http://localhost',
+        }, f)
+    return configfile
+
+
+@pytest.fixture
+def flask_app(temp_config) -> Flask:
+    os.putenv('WEBPUB1C_CONFIG', temp_config)
+    pub1c = importlib.import_module('pub1c-rest')
+    return pub1c.app
+
+
+@pytest.fixture
+def client(flask_app):
+    return flask_app.test_client()
+
+
+def test_index(client):
+    response = client.get('/')
+    assert b'<!DOCTYPE html>' in response.data
+
+
+def test_api_index(client):
+    response = client.get('/api/v1/')
+    data = response.get_json()
+    assert data == ['infobases-available', 'infobases-all', 'publications',
+                    'module', 'config', 'config-test', 'apache-restart']