You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
2.8 KiB

  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # Author: Florian da Costa
  5. # Copyright 2015 Akretion
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as
  9. # published by the Free Software Foundation, either version 3 of the
  10. # License, or (at your option) any later version.
  11. #
  12. # This program is distnaributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. ##############################################################################
  21. import base64
  22. from openerp.tests.common import TransactionCase
  23. from openerp.exceptions import Warning as UserError
  24. class TestExportSqlQuery(TransactionCase):
  25. def setUp(self):
  26. super(TestExportSqlQuery, self).setUp()
  27. self.sql_export_obj = self.env['sql.export']
  28. self.wizard_obj = self.env['sql.file.wizard']
  29. self.sql_report_demo = self.env.ref('sql_export.sql_export_partner')
  30. def test_sql_query(self):
  31. wizard = self.wizard_obj.create({
  32. 'sql_export_id': self.sql_report_demo.id,
  33. })
  34. wizard.export_sql()
  35. export = base64.b64decode(wizard.binary_file)
  36. self.assertEqual(export.split(';')[0], 'name')
  37. self.assertTrue(len(export.split(';')) > 6)
  38. def test_prohibited_queries(self):
  39. prohibited_queries = [
  40. "upDaTe res_partner SET name = 'test' WHERE id = 1",
  41. "DELETE FROM sql_export WHERE name = 'test';",
  42. " DELETE FROM sql_export WHERE name = 'test' ;",
  43. """DELETE
  44. FROM
  45. sql_export
  46. WHERE name = 'test'
  47. """,
  48. ]
  49. for query in prohibited_queries:
  50. with self.assertRaises(UserError):
  51. sql_export = self.sql_export_obj.create({
  52. 'name': 'test_prohibited',
  53. 'query': query})
  54. sql_export.button_clean_check_request()
  55. def test_authorized_queries(self):
  56. authorized_queries = [
  57. "SELECT create_date FROM res_partner",
  58. ]
  59. for query in authorized_queries:
  60. sql_export = self.sql_export_obj.create({
  61. 'name': 'test_authorized',
  62. 'query': query})
  63. sql_export.button_clean_check_request()
  64. self.assertEqual(
  65. sql_export.state, 'sql_valid',
  66. "%s is a valid request" % (query))