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.

65 lines
2.4 KiB

  1. # Author: Florian da Costa
  2. # Copyright 2015 Akretion
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU Affero General Public License as
  6. # published by the Free Software Foundation, either version 3 of the
  7. # License, or (at your option) any later version.
  8. #
  9. # This program is distnaributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU Affero General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Affero General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. import base64
  17. from openerp.tests.common import TransactionCase
  18. from openerp.osv.orm import except_orm
  19. class TestExportSqlQuery(TransactionCase):
  20. def setUp(self):
  21. super(TestExportSqlQuery, self).setUp()
  22. query_vals = {
  23. 'name': 'test',
  24. 'query': "SELECT name, street FROM res_partner;"
  25. }
  26. self.sql_model = self.registry('sql.export')
  27. self.query_id = self.sql_model.create(
  28. self.cr,
  29. self.uid,
  30. query_vals)
  31. def test_sql_query(self):
  32. test = self.sql_model.export_sql_query(
  33. self.cr, self.uid, [self.query_id])
  34. wizard = self.registry('sql.file.wizard').browse(
  35. self.cr, self.uid, test['res_id'])
  36. export = base64.b64decode(wizard.file)
  37. self.assertEqual(export.split(';')[0], 'name')
  38. self.assertTrue(len(export.split(';')) > 6)
  39. def test_prohibited_queries_creation(self):
  40. prohibited_queries = [
  41. "upDaTe res_partner SET name = 'test' WHERE id = 1",
  42. "DELETE FROM res_partner;",
  43. " DELETE FROM res_partner ;",
  44. """DELETE
  45. FROM
  46. res_partner
  47. """,
  48. ]
  49. for query in prohibited_queries:
  50. with self.assertRaises(except_orm):
  51. self.sql_model.create(
  52. self.cr, self.uid,
  53. {'name': 'test_prohibited',
  54. 'query': query})
  55. ok_query = {
  56. 'name': 'test ok',
  57. 'query': "SELECT create_date FROM res_partner"
  58. }
  59. query_id = self.sql_model.create(self.cr, self.uid, ok_query)
  60. self.assertIsNotNone(query_id)