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.

74 lines
2.8 KiB

8 years ago
  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. "SELECT id FROM sql_export;DELETE FROM sql_export",
  49. ]
  50. for query in prohibited_queries:
  51. with self.assertRaises(UserError):
  52. sql_export = self.sql_export_obj.create({
  53. 'name': 'test_prohibited',
  54. 'query': query})
  55. sql_export.button_clean_check_request()
  56. def test_authorized_queries(self):
  57. authorized_queries = [
  58. "SELECT create_date FROM res_partner",
  59. ]
  60. for query in authorized_queries:
  61. sql_export = self.sql_export_obj.create({
  62. 'name': 'test_authorized',
  63. 'query': query})
  64. sql_export.button_clean_check_request()
  65. self.assertEqual(
  66. sql_export.state, 'sql_valid',
  67. "%s is a valid request" % (query))