test_post_frontend_receivestats.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # -*- coding: utf-8 -*-
  2. import os
  3. import shutil
  4. import tempfile
  5. import mock
  6. import six
  7. from q3stats.web_app.blueprints.frontend import views as views_module
  8. from q3stats.testing import BaseQ3StatsWebAppTestCase
  9. class Test_PostFrontendReceivestats(BaseQ3StatsWebAppTestCase):
  10. def setUp(self):
  11. self._tmp_path = tempfile.mkdtemp(dir=os.path.abspath(
  12. os.path.join(os.path.dirname(__file__), 'tmp')
  13. ))
  14. self._fixtures_path = os.path.join(
  15. os.path.dirname(__file__), 'fixtures'
  16. )
  17. def tearDown(self):
  18. shutil.rmtree(self._tmp_path)
  19. def test_get(self):
  20. with self.app.test_request_context():
  21. rsp = self.client.get('/receivestats')
  22. assert rsp.status_code == 405
  23. def test_missing_stats_file(self):
  24. with self.app.test_request_context():
  25. with mock.patch.object(views_module, 'importer'):
  26. rsp = self.client.post('/receivestats')
  27. assert rsp.status_code == 400
  28. assert not views_module.importer.called
  29. def test_bad_stats_file(self):
  30. self.app.config['PROPAGATE_EXCEPTIONS'] = False
  31. self.app.config['LOGGER_HANDLER_POLICY'] = 'never'
  32. broken_tarball_path = os.path.join(
  33. self._fixtures_path, 'broken_tarball.tar'
  34. )
  35. broken_tarball_f = open(broken_tarball_path, 'rb')
  36. with mock.patch.object(views_module.tempfile, 'mkdtemp',
  37. return_value=self._tmp_path):
  38. with mock.patch.object(views_module, 'importer'):
  39. with mock.patch.object(views_module.shutil, 'rmtree'):
  40. with self.app.test_request_context():
  41. rsp = self.client.post('/receivestats', data={
  42. 'stats': (broken_tarball_f, 'stats.tar')
  43. })
  44. assert rsp.status_code == 500
  45. assert not views_module.importer.called
  46. views_module.shutil.rmtree.assert_called_with(
  47. self._tmp_path
  48. )
  49. broken_tarball_f.close()
  50. self.app.config.pop('PROPAGATE_EXCEPTIONS')
  51. self.app.config.pop('LOGGER_HANDLER_POLICY')
  52. def test_ok(self):
  53. stats_path = os.path.join(self._fixtures_path, 'stats.tar')
  54. stats_f = open(stats_path, 'rb')
  55. with mock.patch.object(views_module.tempfile, 'mkdtemp',
  56. return_value=self._tmp_path):
  57. with mock.patch.object(views_module, 'importer'):
  58. with mock.patch.object(views_module.shutil, 'rmtree'):
  59. with self.app.test_request_context():
  60. rsp = self.client.post('/receivestats', data={
  61. 'stats': (stats_f, 'stats.tar')
  62. })
  63. assert rsp.status_code == 200
  64. assert views_module.tempfile.mkdtemp.called
  65. assert os.path.isfile(
  66. os.path.join(self._tmp_path, 'game.xml')
  67. )
  68. views_module.importer.assert_called_with(
  69. [self._tmp_path], self.app.config
  70. )
  71. views_module.shutil.rmtree.assert_called_with(
  72. self._tmp_path
  73. )
  74. stats_f.close()