test_pagure_flask_api_plugins_view_project.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. """
  22. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  23. def setUp(self):
  24. """ Set up the environnment, ran before every tests. """
  25. super(PagureFlaskApiPluginViewProjecttests, self).setUp()
  26. tests.create_projects(self.session)
  27. def test_view_plugin_on_project(self):
  28. """ Test viewing plugins on a project.
  29. """
  30. # Install plugin
  31. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  32. plugin = plugins.get_plugin("Mail")
  33. plugin.set_up(repo)
  34. dbobj = plugin.db_object()
  35. dbobj.active = True
  36. dbobj.project_id = repo.id
  37. dbobj.mail_to = "serg@wh40k.com"
  38. plugin.install(repo, dbobj)
  39. self.session.add(dbobj)
  40. self.session.commit()
  41. # Retrieve all plugins on project
  42. output = self.app.get("/api/0/test/settings/plugins")
  43. self.assertEqual(output.status_code, 200)
  44. data = json.loads(output.get_data(as_text=True))
  45. self.assertEqual(
  46. data,
  47. {
  48. "plugins": [{"Mail": {"mail_to": "serg@wh40k.com"}}],
  49. "total_plugins": 1,
  50. },
  51. )
  52. def test_viewing_plugin_on_project_no_plugin(self):
  53. """ Test viewing plugins on a project, which doesn't
  54. have any installed.
  55. """
  56. # Retrieve all plugins on project
  57. output = self.app.get("/api/0/test/settings/plugins")
  58. self.assertEqual(output.status_code, 200)
  59. data = json.loads(output.get_data(as_text=True))
  60. self.assertEqual(data, {"plugins": [], "total_plugins": 0})
  61. if __name__ == "__main__":
  62. unittest.main(verbosity=2)