1
0

test_pagure_utils.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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(0, os.path.join(os.path.dirname(
  17. os.path.abspath(__file__)), '..'))
  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(os.path.join(self.path, 'repos', 'test2.git'))
  30. project = pagure.lib.query._get_project(self.session, 'test')
  31. # Add a deploy key to the project
  32. new_key_obj = pagure.lib.model.SSHKey(
  33. project_id=project.id,
  34. pushaccess=False,
  35. public_ssh_key='\n foo bar',
  36. ssh_short_key='\n foo bar',
  37. ssh_search_key='\n foo bar',
  38. creator_user_id=1 # pingou
  39. )
  40. self.session.add(new_key_obj)
  41. self.session.commit()
  42. def test_lookup_deploykey_non_deploykey(self):
  43. """ Test lookup_deploykey with a non-deploykey username. """
  44. project = pagure.lib.query._get_project(self.session, 'test')
  45. res = pagure.utils.lookup_deploykey(project, 'pingou')
  46. self.assertEquals(res, None)
  47. def test_lookup_deploykey_different_project(self):
  48. """ Test lookup_deploykey with a username for another project. """
  49. project = pagure.lib.query._get_project(self.session, 'test2')
  50. res = pagure.utils.lookup_deploykey(project, 'deploykey_test_1')
  51. self.assertEquals(res, None)
  52. def test_lookup_deploykey_non_existent_key(self):
  53. """ Test lookup_deploykey with a non-existing deploykey. """
  54. project = pagure.lib.query._get_project(self.session, 'test')
  55. res = pagure.utils.lookup_deploykey(project, 'deploykey_test_2')
  56. self.assertEquals(res, None)
  57. def test_lookup_deploykey(self):
  58. """ Test lookup_deploykey with a correct username. """
  59. project = pagure.lib.query._get_project(self.session, 'test')
  60. res = pagure.utils.lookup_deploykey(project, 'deploykey_test_1')
  61. self.assertNotEquals(res, None)
  62. self.assertFalse(res.pushaccess)