102 lines
3.4 KiB
Python
102 lines
3.4 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
import os
|
|
import shutil
|
|
import tempfile
|
|
|
|
import mock
|
|
import six
|
|
|
|
from q3stats.web_app.blueprints.frontend import views as views_module
|
|
from q3stats.testing import BaseQ3StatsWebAppTestCase
|
|
|
|
|
|
class Test_PostFrontendReceivestats(BaseQ3StatsWebAppTestCase):
|
|
def setUp(self):
|
|
self._tmp_path = tempfile.mkdtemp(dir=os.path.abspath(
|
|
os.path.join(os.path.dirname(__file__), 'tmp')
|
|
))
|
|
|
|
self._fixtures_path = os.path.join(
|
|
os.path.dirname(__file__), 'fixtures'
|
|
)
|
|
|
|
def tearDown(self):
|
|
shutil.rmtree(self._tmp_path)
|
|
|
|
def test_get(self):
|
|
with self.app.test_request_context():
|
|
rsp = self.client.get('/receivestats')
|
|
assert rsp.status_code == 405
|
|
|
|
def test_missing_stats_file(self):
|
|
with self.app.test_request_context():
|
|
with mock.patch.object(views_module, 'importer'):
|
|
rsp = self.client.post('/receivestats')
|
|
assert rsp.status_code == 400
|
|
|
|
assert not views_module.importer.called
|
|
|
|
def test_bad_stats_file(self):
|
|
self.app.config['PROPAGATE_EXCEPTIONS'] = False
|
|
self.app.config['LOGGER_HANDLER_POLICY'] = 'never'
|
|
|
|
broken_tarball_path = os.path.join(
|
|
self._fixtures_path, 'broken_tarball.tar'
|
|
)
|
|
|
|
broken_tarball_f = open(broken_tarball_path, 'rb')
|
|
|
|
with mock.patch.object(views_module.tempfile, 'mkdtemp',
|
|
return_value=self._tmp_path):
|
|
with mock.patch.object(views_module, 'importer'):
|
|
with mock.patch.object(views_module.shutil, 'rmtree'):
|
|
with self.app.test_request_context():
|
|
rsp = self.client.post('/receivestats', data={
|
|
'stats': (broken_tarball_f, 'stats.tar')
|
|
})
|
|
|
|
assert rsp.status_code == 500
|
|
|
|
assert not views_module.importer.called
|
|
|
|
views_module.shutil.rmtree.assert_called_with(
|
|
self._tmp_path
|
|
)
|
|
|
|
broken_tarball_f.close()
|
|
|
|
self.app.config.pop('PROPAGATE_EXCEPTIONS')
|
|
self.app.config.pop('LOGGER_HANDLER_POLICY')
|
|
|
|
def test_ok(self):
|
|
stats_path = os.path.join(self._fixtures_path, 'stats.tar')
|
|
stats_f = open(stats_path, 'rb')
|
|
|
|
with mock.patch.object(views_module.tempfile, 'mkdtemp',
|
|
return_value=self._tmp_path):
|
|
with mock.patch.object(views_module, 'importer'):
|
|
with mock.patch.object(views_module.shutil, 'rmtree'):
|
|
with self.app.test_request_context():
|
|
rsp = self.client.post('/receivestats', data={
|
|
'stats': (stats_f, 'stats.tar')
|
|
})
|
|
|
|
assert rsp.status_code == 200
|
|
|
|
assert views_module.tempfile.mkdtemp.called
|
|
|
|
assert os.path.isfile(
|
|
os.path.join(self._tmp_path, 'game.xml')
|
|
)
|
|
|
|
views_module.importer.assert_called_with(
|
|
[self._tmp_path], self.app.config
|
|
)
|
|
|
|
views_module.shutil.rmtree.assert_called_with(
|
|
self._tmp_path
|
|
)
|
|
|
|
stats_f.close()
|