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.

71 lines
2.6 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 import exceptions
  24. class TestExportSqlQuery(TransactionCase):
  25. def setUp(self):
  26. super(TestExportSqlQuery, self).setUp()
  27. query_vals = {
  28. 'name': 'test',
  29. 'query': "SELECT name, street FROM res_partner;"
  30. }
  31. self.sql_model = self.registry('sql.export')
  32. self.query_id = self.sql_model.create(
  33. self.cr,
  34. self.uid,
  35. query_vals)
  36. def test_sql_query(self):
  37. test = self.sql_model.export_sql_query(
  38. self.cr, self.uid, [self.query_id])
  39. wizard = self.registry('sql.file.wizard').browse(
  40. self.cr, self.uid, test['res_id'])
  41. export = base64.b64decode(wizard.binary_file)
  42. self.assertEqual(export.split(';')[0], 'name')
  43. self.assertTrue(len(export.split(';')) > 6)
  44. def test_prohibited_queries_creation(self):
  45. prohibited_queries = [
  46. "upDaTe res_partner SET name = 'test' WHERE id = 1",
  47. "DELETE FROM sql_export WHERE name = 'test';",
  48. " DELETE FROM sql_export WHERE name = 'test' ;",
  49. """DELETE
  50. FROM
  51. sql_export
  52. WHERE name = 'test'
  53. """,
  54. ]
  55. for query in prohibited_queries:
  56. with self.assertRaises(exceptions.ValidationError):
  57. self.sql_model.create(
  58. self.cr, self.uid,
  59. {'name': 'test_prohibited',
  60. 'query': query})
  61. ok_query = {
  62. 'name': 'test ok',
  63. 'query': "SELECT create_date FROM res_partner"
  64. }
  65. query_id = self.sql_model.create(self.cr, self.uid, ok_query)
  66. self.assertIsNotNone(query_id)