1
0

test_pagure_utils.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. sys.path.insert(
  16. 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
  17. )
  18. import pagure.lib.model
  19. import pagure.lib.query
  20. import pagure.utils
  21. import tests
  22. class PagureUtilsTests(tests.SimplePagureTest):
  23. """ Tests for pagure.utils """
  24. def setUp(self):
  25. """ Set up the environnment, run before every tests. """
  26. super(PagureUtilsTests, self).setUp()
  27. tests.create_projects(self.session)
  28. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  29. tests.add_content_git_repo(
  30. os.path.join(self.path, "repos", "test2.git")
  31. )
  32. project = pagure.lib.query._get_project(self.session, "test")
  33. # Add a deploy key to the project
  34. new_key_obj = pagure.lib.model.SSHKey(
  35. project_id=project.id,
  36. pushaccess=False,
  37. public_ssh_key="\n foo bar",
  38. ssh_short_key="\n foo bar",
  39. ssh_search_key="\n foo bar",
  40. creator_user_id=1, # pingou
  41. )
  42. self.session.add(new_key_obj)
  43. self.session.commit()
  44. def test_lookup_deploykey_non_deploykey(self):
  45. """ Test lookup_deploykey with a non-deploykey username. """
  46. project = pagure.lib.query._get_project(self.session, "test")
  47. res = pagure.utils.lookup_deploykey(project, "pingou")
  48. self.assertEquals(res, None)
  49. def test_lookup_deploykey_different_project(self):
  50. """ Test lookup_deploykey with a username for another project. """
  51. project = pagure.lib.query._get_project(self.session, "test2")
  52. res = pagure.utils.lookup_deploykey(project, "deploykey_test_1")
  53. self.assertEquals(res, None)
  54. def test_lookup_deploykey_non_existent_key(self):
  55. """ Test lookup_deploykey with a non-existing deploykey. """
  56. project = pagure.lib.query._get_project(self.session, "test")
  57. res = pagure.utils.lookup_deploykey(project, "deploykey_test_2")
  58. self.assertEquals(res, None)
  59. def test_lookup_deploykey(self):
  60. """ Test lookup_deploykey with a correct username. """
  61. project = pagure.lib.query._get_project(self.session, "test")
  62. res = pagure.utils.lookup_deploykey(project, "deploykey_test_1")
  63. self.assertNotEquals(res, None)
  64. self.assertFalse(res.pushaccess)