test_pagure_flask_api_plugins_view_project.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2019 - Copyright Red Hat Inc
  4. Authors:
  5. Michal Konecny <mkonecny@redhat.com>
  6. """
  7. from __future__ import unicode_literals, absolute_import
  8. import unittest
  9. import sys
  10. import os
  11. import json
  12. from mock import patch, MagicMock
  13. sys.path.insert(
  14. 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
  15. )
  16. import pagure.lib.plugins as plugins # noqa: E402
  17. import pagure.lib.query # noqa: E402
  18. import tests # noqa: E402
  19. class PagureFlaskApiPluginViewProjecttests(tests.Modeltests):
  20. """Tests for the flask API of pagure for viewing enabled plugins on project"""
  21. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  22. def setUp(self):
  23. """Set up the environnment, ran before every tests."""
  24. super(PagureFlaskApiPluginViewProjecttests, self).setUp()
  25. tests.create_projects(self.session)
  26. def test_view_plugin_on_project(self):
  27. """Test viewing plugins on a project."""
  28. # Install plugin
  29. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  30. plugin = plugins.get_plugin("Mail")
  31. plugin.set_up(repo)
  32. dbobj = plugin.db_object()
  33. dbobj.active = True
  34. dbobj.project_id = repo.id
  35. dbobj.mail_to = "serg@wh40k.com"
  36. plugin.install(repo, dbobj)
  37. self.session.add(dbobj)
  38. self.session.commit()
  39. # Retrieve all plugins on project
  40. output = self.app.get("/api/0/test/settings/plugins")
  41. self.assertEqual(output.status_code, 200)
  42. data = json.loads(output.get_data(as_text=True))
  43. self.assertEqual(
  44. data,
  45. {
  46. "plugins": [{"Mail": {"mail_to": "serg@wh40k.com"}}],
  47. "total_plugins": 1,
  48. },
  49. )
  50. def test_viewing_plugin_on_project_no_plugin(self):
  51. """Test viewing plugins on a project, which doesn't
  52. have any installed.
  53. """
  54. # Retrieve all plugins on project
  55. output = self.app.get("/api/0/test/settings/plugins")
  56. self.assertEqual(output.status_code, 200)
  57. data = json.loads(output.get_data(as_text=True))
  58. self.assertEqual(data, {"plugins": [], "total_plugins": 0})
  59. if __name__ == "__main__":
  60. unittest.main(verbosity=2)