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
  8. __requires__ = ['SQLAlchemy >= 0.8']
  9. import pkg_resources
  10. import unittest
  11. import shutil
  12. import sys
  13. import os
  14. import mock
  15. import munch
  16. import pygit2
  17. import werkzeug
  18. sys.path.insert(0, os.path.join(os.path.dirname(
  19. os.path.abspath(__file__)), '..'))
  20. import pagure.lib
  21. import pagure.lib.model
  22. import pagure.utils
  23. import tests
  24. class PagureUtilsTests(tests.SimplePagureTest):
  25. """ Tests for pagure.utils """
  26. def setUp(self):
  27. """ Set up the environnment, run before every tests. """
  28. super(PagureUtilsTests, self).setUp()
  29. tests.create_projects(self.session)
  30. tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
  31. tests.add_content_git_repo(os.path.join(self.path, 'repos', 'test2.git'))
  32. project = pagure.lib._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._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._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._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._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)