test_pagure_utils.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2018 - Copyright Red Hat Inc
  4. Authors:
  5. Patrick Uiterwijk <puiterwijk@redhat.com>
  6. """
  7. from __future__ import unicode_literals, absolute_import
  8. import unittest
  9. import shutil
  10. import sys
  11. import os
  12. import mock
  13. import munch
  14. import pygit2
  15. import werkzeug
  16. sys.path.insert(
  17. 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
  18. )
  19. import pagure.lib.model
  20. import pagure.lib.query
  21. import pagure.utils
  22. import tests
  23. class PagureUtilsTests(tests.SimplePagureTest):
  24. """ Tests for pagure.utils """
  25. def setUp(self):
  26. """ Set up the environnment, run before every tests. """
  27. super(PagureUtilsTests, self).setUp()
  28. tests.create_projects(self.session)
  29. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  30. tests.add_content_git_repo(
  31. os.path.join(self.path, "repos", "test2.git")
  32. )
  33. project = pagure.lib.query._get_project(self.session, "test")
  34. # Add a deploy key to the project
  35. new_key_obj = pagure.lib.model.SSHKey(
  36. project_id=project.id,
  37. pushaccess=False,
  38. public_ssh_key="\n foo bar",
  39. ssh_short_key="\n foo bar",
  40. ssh_search_key="\n foo bar",
  41. creator_user_id=1, # pingou
  42. )
  43. self.session.add(new_key_obj)
  44. self.session.commit()
  45. def test_lookup_deploykey_non_deploykey(self):
  46. """ Test lookup_deploykey with a non-deploykey username. """
  47. project = pagure.lib.query._get_project(self.session, "test")
  48. res = pagure.utils.lookup_deploykey(project, "pingou")
  49. self.assertEquals(res, None)
  50. def test_lookup_deploykey_different_project(self):
  51. """ Test lookup_deploykey with a username for another project. """
  52. project = pagure.lib.query._get_project(self.session, "test2")
  53. res = pagure.utils.lookup_deploykey(project, "deploykey_test_1")
  54. self.assertEquals(res, None)
  55. def test_lookup_deploykey_non_existent_key(self):
  56. """ Test lookup_deploykey with a non-existing deploykey. """
  57. project = pagure.lib.query._get_project(self.session, "test")
  58. res = pagure.utils.lookup_deploykey(project, "deploykey_test_2")
  59. self.assertEquals(res, None)
  60. def test_lookup_deploykey(self):
  61. """ Test lookup_deploykey with a correct username. """
  62. project = pagure.lib.query._get_project(self.session, "test")
  63. res = pagure.utils.lookup_deploykey(project, "deploykey_test_1")
  64. self.assertNotEquals(res, None)
  65. self.assertFalse(res.pushaccess)