Skip to content

Commit

Permalink
Server: Add tests for server API
Browse files Browse the repository at this point in the history
During testing some bugs were found in the server handlers, so those have
been fixed as well.
  • Loading branch information
carlosperate committed May 6, 2017
1 parent 4acb7ab commit 68af39f
Show file tree
Hide file tree
Showing 7 changed files with 755 additions and 32 deletions.
4 changes: 4 additions & 0 deletions .appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,11 @@ install:
# Install Python packages
- cmd: "%PIP3% install pyinstaller"
- cmd: "%PIP3% install mkdocs"
#- cmd: "%PIP3% install pydocstyle"
- cmd: "%PIP3% install coverage"
- cmd: "%PIP3% install requests"
- cmd: "%PIP% install coverage"
- cmd: "%PIP% install requests"
- cmd: "%PIP% install mock"
- cmd: "%PIP% freeze"
- cmd: "%PIP3% freeze"
Expand All @@ -82,6 +85,7 @@ test_script:
- cmd: "%PYTHON%\\python -m coverage report"
- cmd: "%PYTHON3%\\python -m coverage run ardublocklyserver\\tests\\run_all.py"
- cmd: "%PYTHON3%\\python -m coverage report"
#- cmd: pydocstyle ardublocklyserver --match-dir='ardublocklyserver'

# Push artefact to S3 bucket and list all
before_deploy:
Expand Down
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@ before_install:
# Install Python packages (built with Python 3, tests for 2 and 3)
- sudo python -m pip install mock
- sudo python -m pip install coverage
- sudo python -m pip install requests
- pip3 install coverage
- pip3 install requests
- pip3 install coveralls
#- pip3 install pydocstyle
- pip3 install mkdocs
- pip3 install pyinstaller

Expand Down Expand Up @@ -77,6 +80,7 @@ script:
- python -m coverage report
- python3 -m coverage run ardublocklyserver/tests/run_all.py
- python3 -m coverage report
#- pydocstyle ardublocklyserver --match-dir='ardublocklyserver'

after_success:
- coveralls
Expand Down
27 changes: 15 additions & 12 deletions ardublocklyserver/__init__.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
# -*- coding: utf-8 -*-
#
# ardublocklyserver package.
#
# Copyright (c) 2017 carlosperate https://github.com/carlosperate/
# Licensed under the Apache License, Version 2.0 (the "License"):
# http://www.apache.org/licenses/LICENSE-2.0
#
# There is a specific requirements for this Python project to not need
# external module dependencies, so all third-party modules have been carefully
# chosen with this purpose in mind and included in a folder named
# 'local-packages'. The sys.path has to be expanded to be able to import these.
#
"""ardublocklyserver package.
Copyright (c) 2017 carlosperate https://github.com/carlosperate/
Licensed under the Apache License, Version 2.0 (the "License"):
http://www.apache.org/licenses/LICENSE-2.0
There is a specific requirements for this Python project to not need external
module dependencies, so all third-party modules have been carefully chosen with
this purpose in mind and included in a folder named 'local-packages'.
The sys.path has to be expanded to be able to import these.
"""
import os
import sys

Expand All @@ -21,3 +20,7 @@

# Follows Semantic Versioning 2.0.0 http://semver.org/spec/v2.0.0.html
__version__ = '0.1.3-a'

__author__ = 'carlosperate'
__copyright__ = 'Copyright 2017, carlosperate https://github.com/carlosperate/'
__license__ = 'Apache License, Version 2.0'
38 changes: 27 additions & 11 deletions ardublocklyserver/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"""
from __future__ import unicode_literals, absolute_import, print_function
import os
import sys
# local-packages imports
from bottle import request, response
from bottle import static_file, run, default_app, redirect, abort
Expand All @@ -23,7 +24,7 @@
document_root = ''


def launch_server(ip='127.0.0.1', port=8000, document_root_=''):
def launch_server(ip='localhost', port=8000, document_root_=''):
"""Launch the Waitress server and Bottle framework with given settings.
:param ip: IP address to serve. Default to localhost, set to '0.0.0.0' to
Expand All @@ -45,6 +46,18 @@ def strip_path():
request.environ['PATH_INFO'] = request.environ['PATH_INFO'].rstrip('/')


def set_header_no_cache():
"""Set the HTTP response to no cache the data.
Implementation depends on Python version.
"""
if sys.version_info[0] < 3:
response.headers[
'Cache-Control'.encode('ascii', 'ignore')] = 'no-cache'
else:
response.headers['Cache-Control'] = 'no-cache'


#
# Serving static files.
#
Expand Down Expand Up @@ -155,7 +168,7 @@ def handler_settings_get_all():
'selected': actions.get_load_ide_selected()
}]
}
response.headers['Cache-Control'] = 'no-cache'
set_header_no_cache()
return response_dict


Expand Down Expand Up @@ -197,20 +210,23 @@ def handler_settings_get_individual(name):
else:
success = False
response_dict.update({
'settings_type': 'invalid',
'errors': [{
'id': 61,
'description': 'Unexpected setting type requested.'
}]})
response_dict.update({'success': success})

response.headers['Cache-Control'] = 'no-cache'
set_header_no_cache()
return response_dict


@app.put('/settings')
def handler_settings_update_all():
"""Handle the invalid PUT all settings requests.
There is no specific reason for this, is just not used by the client, and
so there is no need to implement it at the moment.
Error codes:
62 - Settings have to be individually updated.
Expand Down Expand Up @@ -292,12 +308,14 @@ def handler_settings_update_individual(name):
options = [{'value': k, 'display_text': v} for k, v in
iteritems(actions.get_load_ide_options())]
else:
response_dict.update({'success': False})
response_dict.update({'success': False,
'settings_type': 'invalid'})
response_dict.setdefault('errors', []).append({
'id': 63,
'description': 'Unexpected setting type to update.'
})
if set_value and set_value == new_value:
if set_value and ((set_value == new_value)
or name == 'compiler' or name == 'sketch'):
response_dict.update({
'success': True,
'selected': set_value
Expand All @@ -310,6 +328,7 @@ def handler_settings_update_individual(name):
'id': 67,
'description': 'New value could not be set.'
})
set_header_no_cache()
return response_dict


Expand Down Expand Up @@ -364,7 +383,7 @@ def handler_code_post():
actions.arduino_ide_send_code(sketch_code)
except Exception as e:
exit_code = 52
err_out = 'Unexpected server error.'
err_out += 'Unexpected server error.'
print('Error: Exception in arduino_ide_send_code:\n%s' % str(e))

response_dict.update({'success': success,
Expand All @@ -380,8 +399,5 @@ def handler_code_post():
'description': 'More info available in the \'ide_data\' value.'
}]
})
set_header_no_cache()
return response_dict


if __name__ == '__main__':
launch_server('127.0.0.1', 8000, 'C:\\workspace\\git\\ardublockly')
19 changes: 10 additions & 9 deletions ardublocklyserver/tests/run_all.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Runs all the unit tests from this directory.
#
# This file manually adds the tests to be run.
#
# Copyright (c) 2017 carlosperate https://github.com/carlosperate/
# Licensed under the Apache License, Version 2.0 (the "License"):
# http://www.apache.org/licenses/LICENSE-2.0
#
"""Runs all the unit tests from this directory.
This file manually adds the tests to be run.
Copyright (c) 2017 carlosperate https://github.com/carlosperate/
Licensed under the Apache License, Version 2.0 (the "License"):
http://www.apache.org/licenses/LICENSE-2.0
"""
import os
import sys
import unittest
Expand All @@ -25,10 +24,12 @@
from sketchcreator_test import SketchCreatorTestCase
from compilersettings_test import ServerCompilerSettingsTestCase
from actions_test import ActionsTestCase
from server_test import ServerTestCase


def run_tests():
unittest.main()


if __name__ == '__main__':
run_tests()
Loading

0 comments on commit 68af39f

Please sign in to comment.