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.

58 lines
2.0 KiB

  1. # Copyright (C) 2015 Akretion (<http://www.akretion.com>)
  2. # @author: Florian da Costa
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. import base64
  5. from odoo.tests.common import TransactionCase, post_install
  6. from odoo.exceptions import Warning as UserError
  7. @post_install(True)
  8. class TestExportSqlQuery(TransactionCase):
  9. def setUp(self):
  10. super(TestExportSqlQuery, self).setUp()
  11. self.sql_export_obj = self.env['sql.export']
  12. self.wizard_obj = self.env['sql.file.wizard']
  13. self.sql_report_demo = self.env.ref('sql_export.sql_export_partner')
  14. def test_sql_query(self):
  15. wizard = self.wizard_obj.create({
  16. 'sql_export_id': self.sql_report_demo.id,
  17. })
  18. wizard.export_sql()
  19. export = base64.b64decode(wizard.binary_file).decode('utf-8')
  20. self.assertEqual(export.split(';')[0], 'name')
  21. self.assertTrue(len(export.split(';')) > 6)
  22. def test_prohibited_queries(self):
  23. prohibited_queries = [
  24. "upDaTe res_partner SET name = 'test' WHERE id = 1",
  25. "DELETE FROM sql_export WHERE name = 'test';",
  26. " DELETE FROM sql_export WHERE name = 'test' ;",
  27. """DELETE
  28. FROM
  29. sql_export
  30. WHERE name = 'test'
  31. """,
  32. ]
  33. for query in prohibited_queries:
  34. with self.assertRaises(UserError):
  35. sql_export = self.sql_export_obj.create({
  36. 'name': 'test_prohibited',
  37. 'query': query})
  38. sql_export.button_validate_sql_expression()
  39. def test_authorized_queries(self):
  40. authorized_queries = [
  41. "SELECT create_date FROM res_partner",
  42. ]
  43. for query in authorized_queries:
  44. sql_export = self.sql_export_obj.create({
  45. 'name': 'test_authorized',
  46. 'query': query})
  47. sql_export.button_validate_sql_expression()
  48. self.assertEqual(
  49. sql_export.state, 'sql_valid',
  50. "%s is a valid request" % (query))