Skip to content

Commit

Permalink
Add base class for integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ogolovatyi authored and ogolovatyi committed Apr 24, 2019
1 parent 55e6a26 commit d184630
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 78 deletions.
5 changes: 3 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
- [Cloning TabPy Repository](#cloning-tabpy-repository)
- [Setting Up Environment](#setting-up-environment)
- [Unit Tests](#unit-tests)
- [Integration Tests](#integration-tests)
- [Code Coverage](#code-coverage)
- [TabPy in Pythong Virtual Environment](#tabpy-in-pythong-virtual-environment)
- [TabPy in Python Virtual Environment](#tabpy-in-python-virtual-environment)
- [Documentation Updates](#documentation-updates)
- [TabPy with Swagger](#tabpy-with-swagger)
- [Code styling](#code-styling)
Expand Down Expand Up @@ -85,7 +86,7 @@ either for server or tools test, or even combined:
pytest tests --cov=tabpy-server/tabpy_server --cov=tabpy-tools/tabpy_tools --cov-append
```

## TabPy in Pythong Virtual Environment
## TabPy in Python Virtual Environment

If you have downloaded Tabpy and would like to manually install Tabpy Server
not using pip then follow the steps below
Expand Down
2 changes: 1 addition & 1 deletion docs/api-v1.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Property | Description
--- | ---
`required` | Authentication is never optional for client to use if it is mentioned in features list.
`methods` | List of supported authentication methods with their properties.
`methods.basic-auth` | TabPy requires to use basic access authenticatio, see [TabPy Server Configuration Instructions](server-config.md#authentication) for how to configure authentication.
`methods.basic-auth` | TabPy requires to use basic access authentication, see [TabPy Server Configuration Instructions](server-config.md#authentication) for how to configure authentication.

<!-- markdownlint-enable MD013 -->

Expand Down
4 changes: 2 additions & 2 deletions docs/server-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
- [Authentication](#authentication)
* [Enabling Authentication](#enabling-authentication)
* [Password File](#password-file)
* [setting Up Environmnet](#setting-up-environmnet)
* [Setting Up Environmnet](#setting-up-environmnet)
* [Adding an Account](#adding-an-account)
* [Updating an Account](#updating-an-account)
* [Deleting an Account](#deleting-an-account)
Expand Down Expand Up @@ -95,7 +95,7 @@ see how to use it.

After making any changes to the password file, TabPy needs to be restarted.

### setting Up Environmnet
### Setting Up Environmnet

Before making any code changes run environment setup script. For
Windows run the next command from the repository root folder:
Expand Down
2 changes: 1 addition & 1 deletion docs/server-rest.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ Property | Description
<!-- markdownlint-enable MD013 -->

For each API version there is set of properties, e.g. for v1 in the example
abovefeatures are:
above features are:

See [TabPy Configuration](#tabpy-configuration) section for more information
on modifying the settings.
Expand Down
85 changes: 85 additions & 0 deletions tests/integration/integ_test_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import http.client
import os
import platform
import shutil
import signal
import subprocess
import tempfile
import time
import unittest


class IntegTestBase(unittest.TestCase):
'''
Base class for integration tests.
'''
def __init__(self, methodName="runTest"):
super(IntegTestBase, self).__init__(methodName)
self.process = None

def setUp(self):
super(IntegTestBase, self).setUp()
prefix = 'TabPyIntegTest'
self.tmp_dir = tempfile.mkdtemp(prefix=prefix)

# create temporary state.ini
self.state_file = open(os.path.join(self.tmp_dir, 'state.ini'), 'w+')
self.state_file.write(
'[Service Info]\n'
'Name = TabPy Serve\n'
'Description = \n'
'Creation Time = 0\n'
'Access-Control-Allow-Origin = \n'
'Access-Control-Allow-Headers = \n'
'Access-Control-Allow-Methods = \n'
'\n'
'[Query Objects Service Versions]\n'
'\n'
'[Query Objects Docstrings]\n'
'\n'
'[Meta]\n'
'Revision Number = 1\n')
self.state_file.close()

# create config file
self.config_file = open(os.path.join(self.tmp_dir, 'auth.conf'), 'w+')
self.config_file.write(
'[TabPy]\n'
'TABPY_PORT=9004\n'
'TABPY_PWD_FILE=./tests/integration/resources/pwdfile.txt\n'
f'TABPY_STATE_PATH = {self.tmp_dir}')
self.config_file.close()

# Platform specific - for integration tests we want to engage
# startup script
if platform.system() == 'Windows':
self.process = subprocess.Popen(
['startup.cmd', self.config_file.name, '&'])
else:
self.process = subprocess.Popen(
['./startup.sh',
'--config=' + self.config_file.name, '&'],
preexec_fn=os.setsid)
# give the app some time to start up...
time.sleep(3)

def tearDown(self):
# stop TabPy
if self.process is not None:
if platform.system() == 'Windows':
subprocess.call(['taskkill', '/F', '/T', '/PID',
str(self.process.pid)])
else:
os.killpg(os.getpgid(self.process.pid), signal.SIGTERM)
self.process.kill()

# remove temporary files
os.remove(self.state_file.name)
os.remove(self.config_file.name)
shutil.rmtree(self.tmp_dir)

super(IntegTestBase, self).tearDown()

def _get_connection(self) -> http.client.HTTPConnection:
connection = http.client.HTTPConnection('localhost:9004')
return connection
75 changes: 3 additions & 72 deletions tests/integration/test_auth.py
Original file line number Diff line number Diff line change
@@ -1,85 +1,16 @@
import base64
import http.client
import os
import platform
import shutil
import signal
import subprocess
import tempfile
import time
import unittest
import integ_test_base


class TestAuth(unittest.TestCase):
class TestAuth(integ_test_base.IntegTestBase):
def setUp(self):
super(TestAuth, self).setUp()
self.payload = (
'''{
"data": { "_arg1": [1, 2] },
"script": "return [x * 2 for x in _arg1]"
}''')

prefix = 'TabPyIntegTest'
self.tmp_dir = tempfile.mkdtemp(prefix=prefix)

# create temporary state.ini
self.state_file = open(os.path.join(self.tmp_dir, 'state.ini'), 'w+')
self.state_file.write(
'[Service Info]\n'
'Name = TabPy Serve\n'
'Description = \n'
'Creation Time = 0\n'
'Access-Control-Allow-Origin = \n'
'Access-Control-Allow-Headers = \n'
'Access-Control-Allow-Methods = \n'
'\n'
'[Query Objects Service Versions]\n'
'\n'
'[Query Objects Docstrings]\n'
'\n'
'[Meta]\n'
'Revision Number = 1\n')
self.state_file.close()

# create config file
self.config_file = open(os.path.join(self.tmp_dir, 'auth.conf'), 'w+')
self.config_file.write(
'[TabPy]\n'
'TABPY_PORT=9004\n'
'TABPY_PWD_FILE=./tests/integration/resources/pwdfile.txt\n'
f'TABPY_STATE_PATH = {self.tmp_dir}')
self.config_file.close()

# Platform specific - for integration tests we want to engage
# startup script
if platform.system() == 'Windows':
self.process = subprocess.Popen(
['startup.cmd', self.config_file.name, '&'])
else:
self.process = subprocess.Popen(
['./startup.sh',
'--config=' + self.config_file.name, '&'],
preexec_fn=os.setsid)
# give the app some time to start up...
time.sleep(3)

def tearDown(self):
# stop TabPy
if platform.system() == 'Windows':
subprocess.call(['taskkill', '/F', '/T', '/PID',
str(self.process.pid)])
else:
os.killpg(os.getpgid(self.process.pid), signal.SIGTERM)
self.process.kill()

# remove temporary files
os.remove(self.state_file.name)
os.remove(self.config_file.name)
shutil.rmtree(self.tmp_dir)

def _get_connection(self) -> http.client.HTTPConnection:
connection = http.client.HTTPConnection('localhost:9004')
return connection

def test_missing_credentials_fails(self):
headers = {
'Content-Type': "application/json",
Expand Down

0 comments on commit d184630

Please sign in to comment.