test_pagure_flask_ui_repo_mirrored_from.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2020 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. from __future__ import unicode_literals, absolute_import
  8. import datetime
  9. import os
  10. import shutil
  11. import sys
  12. import tempfile
  13. import time
  14. import unittest
  15. import pygit2
  16. import six
  17. from mock import patch, MagicMock, ANY, call
  18. sys.path.insert(
  19. 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
  20. )
  21. import pagure.lib.git
  22. import tests
  23. from pagure.lib.repo import PagureRepo
  24. class PagureUiRepoMirroredFromTests(tests.Modeltests):
  25. """ Tests for pagure project that are mirrored from a remote location
  26. """
  27. maxDiff = None
  28. def setUp(self):
  29. """ Set up the environnment, ran before every tests. """
  30. super(PagureUiRepoMirroredFromTests, self).setUp()
  31. tests.create_projects(self.session)
  32. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  33. # Make the test project mirrored from elsewhere
  34. self.project = pagure.lib.query.get_authorized_project(
  35. self.session, "test"
  36. )
  37. self.project.mirrored_from = "https://example.com/foo/bar.git"
  38. self.session.add(self.project)
  39. self.session.commit()
  40. def test_custom_projecticon(self):
  41. """ Ensure that the customized project icon is shown the main page of
  42. the project.
  43. """
  44. output = self.app.get("/test")
  45. output_text = output.get_data(as_text=True)
  46. self.assertIn(
  47. '<i class="fa fa-cloud-download text-muted" title="Mirrored from '
  48. 'https://example.com/foo/bar.git"></i>',
  49. output_text,
  50. )
  51. def test_regular_projecticon(self):
  52. """ Ensure that the customized project icon is shown the main page of
  53. the project.
  54. """
  55. output = self.app.get("/test2")
  56. output_text = output.get_data(as_text=True)
  57. self.assertNotIn(
  58. '<i class="fa fa-cloud-download text-muted" title="Mirrored from ',
  59. output_text,
  60. )
  61. def test_settings_shows(self):
  62. """ Ensure that the box to edit the mirrored from value shows up
  63. in the settings.
  64. """
  65. user = tests.FakeUser(username="pingou")
  66. with tests.user_set(self.app.application, user):
  67. output = self.app.get("/test/settings")
  68. self.assertEqual(output.status_code, 200)
  69. output_text = output.get_data(as_text=True)
  70. self.assertIn(
  71. '<input class="form-control" name="mirrored_from" '
  72. 'value="https://example.com/foo/bar.git" />',
  73. output_text,
  74. )
  75. self.assertIn(
  76. "The (public) url from which this repository is mirrored.",
  77. output_text,
  78. )
  79. def test_settings_not_show(self):
  80. """ Ensure that the box to edit the mirrored from value does not
  81. show up in the settings when it shouldn't.
  82. """
  83. user = tests.FakeUser(username="pingou")
  84. with tests.user_set(self.app.application, user):
  85. output = self.app.get("/test2/settings")
  86. self.assertEqual(output.status_code, 200)
  87. output_text = output.get_data(as_text=True)
  88. self.assertNotIn(
  89. '<input class="form-control" name="mirrored_from" ',
  90. output_text,
  91. )
  92. self.assertNotIn(
  93. "The (public) url from which this repository is mirrored.",
  94. output_text,
  95. )
  96. def test_edit_mirrored_from(self):
  97. """ Ensure that we can successfully edit the content of the
  98. mirrored_from field.
  99. """
  100. user = tests.FakeUser(username="pingou")
  101. with tests.user_set(self.app.application, user):
  102. output = self.app.get("/test/settings")
  103. self.assertEqual(output.status_code, 200)
  104. output_text = output.get_data(as_text=True)
  105. self.assertIn(
  106. '<input class="form-control" name="mirrored_from" '
  107. 'value="https://example.com/foo/bar.git" />',
  108. output_text,
  109. )
  110. csrf_token = self.get_csrf(output=output)
  111. data = {
  112. "csrf_token": csrf_token,
  113. "description": "test repo",
  114. "mirrored_from": "https://example2.com/bar.git",
  115. }
  116. output = self.app.post(
  117. "/test/update", data=data, follow_redirects=True
  118. )
  119. self.assertEqual(output.status_code, 200)
  120. output_text = output.get_data(as_text=True)
  121. self.assertIn(
  122. '<input class="form-control" name="mirrored_from" '
  123. 'value="https://example2.com/bar.git" />',
  124. output_text,
  125. )
  126. if __name__ == "__main__":
  127. unittest.main(verbosity=2)