def test_fetch(self): """ Test fetching a list of documents. """ with fudge.patch('basecamp.base.Base.get') as fake_get: # Get all the documents. mock = self.setup_mock(200, self.documents_list) fake_get.is_callable().returns(mock) self.assertEquals(self.document.fetch(), self.documents_list) # Get documents for a single project mock = self.setup_mock(200, self.documents_list) fake_get.is_callable().returns(mock) self.assertEquals( self.document.fetch(project_id=1), self.documents_list) # Get a specific document from a project mock = self.setup_mock(200, self.documents_list[0]) fake_get.is_callable().returns(mock) self.assertEquals( self.document.fetch(project_id=1, document_id=1), self.documents_list[0])
def test_create_document_no_permission(self): """ Test creating a new document. """ with fudge.patch('basecamp.base.Base.post') as fake_post: fake_post.is_callable().returns(self.setup_mock(403)) self.document.create(66, 'Jenny', 'I got it!<br>I got it!')
def test_remove_document(self): """ Test removing a document. """ with fudge.patch('basecamp.base.Base.delete') as fake_del: fake_del.is_callable().returns(self.setup_mock(204)) self.assertTrue(self.document.remove(1, 1))
def test_patch_long_path(): import fudge.tests.support._for_patch orig = fudge.tests.support._for_patch.some_object.inner long_path = 'fudge.tests.support._for_patch.some_object.inner' with fudge.patch(long_path) as fake: assert isinstance(fake, fudge.Fake) eq_(fudge.tests.support._for_patch.some_object.inner, orig)
def test_updates_package_release_modified(self, db_session, tmpdir): from belt.refresh import refresh_packages modified = Delorean(datetime.datetime(1999, 10, 5), timezone='UTC') package = create_releases_for_package(tmpdir.join('lemon-3.0.tar.gz'), '1.4', '1.2.3', modified=modified.datetime) db_session.add(package) db_session.flush() original_modification_times = [rel.modified for rel in package.releases] with fudge.patch('belt.refresh.package_releases') as package_releases: (package_releases.expects_call() .with_args('lemon', '/na/lemon', arg.passes_test(version_order)) .returns([])) package = list(refresh_packages(db_session, datetime.datetime.utcnow(), '/na'))[0] new_modification_times = [rel.modified for rel in package.releases] results = [original < new for original, new in zip(original_modification_times, new_modification_times)] assert all(results)
def test_list(self): """ Get list of projects """ response = [ { "id": 605816632, "name": "BCX", "description": "The Next Generation", "updated_at": "2012-03-23T13:55:43-05:00", "url": "{0}/projects/605816632-bcx.json".format(self.url), "archived": False, "starred": True }, { "id": 684146117, "name": "Nothing here!", "description": None, "updated_at": "2012-03-22T16:56:51-05:00", "url": "{0}/projects/684146117-nothing.json".format(self.url), "archived": False, "starred": False } ] with fudge.patch('basecamp.base.Base.get') as fake_get: fake_get.is_callable().returns(self.setup_mock(200, response)) projects = basecamp.api.Project( self.url, self.token, self.refresh_token) self.assertEqual(projects.fetch(), response)
def test_list(self): """ Get list of projects """ response = [ { "id": 605816632, "name": "BCX", "description": "The Next Generation", "updated_at": "2012-03-23T13:55:43-05:00", "url": "{0}/projects/605816632-bcx.json".format(self.url), "archived": False, "starred": True }, { "id": 684146117, "name": "Nothing here!", "description": None, "updated_at": "2012-03-22T16:56:51-05:00", "url": "{0}/projects/684146117-nothing.json".format(self.url), "archived": False, "starred": False } ] with fudge.patch('basecamp.base.Base.get') as fake_get: fake_get.is_callable().returns(self.setup_mock(200, response)) projects = basecamp.api.Project( self.url, self.token, self.refresh_token) self.assertEquals(projects.fetch(), response)
def setup(self): # Fake Servers fake_server1 = fudge.Fake('server1') fake_server2 = fudge.Fake('server2') fake_servers = [fake_server1, fake_server2] fake_server1.provides('start_server') fake_server2.provides('start_server') # Fake Options fake_options = fudge.Fake('options') # Patch threading patch_context = fudge.patch('threading.Thread', 'zmq.Context') self.context = ContextUser(patch_context) fake_thread_cls, fake_zmq_context = self.context.enter() # Fake threading.Thread Setup fake_thread = fake_thread_cls.is_callable().returns_fake() fake_thread.expects('start') # Fake zmq.Context Setup fake_socket = fake_zmq_context.is_callable().returns_fake() fake_socket.is_a_stub() self.fake_thread_cls = fake_thread_cls self.fake_thread = fake_thread self.fake_server1 = fake_server1 self.fake_server2 = fake_server2 self.fake_zmq_context = fake_zmq_context self.coordinator = ServerCoordinator.setup(fake_servers, fake_options)
def http_requests_fail(): """ A context manager, during which delivery attempts will fail with an exception from the HTTP layer. """ with fudge.patch('requests.Session.send') as mock_send: mock_send.is_callable().raises(RequestException) yield
def base_setup(self): # Patch all of these objects for all tests context = fudge.patch('subwrap.run', 'overlay4u.overlay.ensure_directories', 'overlay4u.overlay.MountTable', ) self.context_user = ContextUser(context) self.fake_run, self.fake_ensure, self.fake_mount_table = self.context_user.enter()
def test_fetch_people_list(self): """ Test fetching a list of people. """ with fudge.patch('basecamp.base.Base.get') as fake_get: fake_get.is_callable().returns(self.setup_mock(200, self.response)) self.assertEqual(self.people.fetch(), self.response)
def test_get_package_from_pypi(self): from ..utils import get_package_from_pypi, pypi_url pypi_base_url = 'https://pypi.python.org/packages' url = pypi_url(pypi_base_url, 'source', 'flake8', 'flake8-2.0.tar.gz') with fudge.patch('belt.utils.get_url') as get_url: get_url.expects_call().returns(cStringIO.StringIO('Got it!')) response = get_package_from_pypi(url) assert 'Got it!' == response.read()
def test_fetch_single_person(self): """ Test fetching a single person. """ with fudge.patch('basecamp.base.Base.get') as fake_get: fake_get.is_callable().returns( self.setup_mock(200, self.response[0])) self.assertEqual(self.people.fetch(person='me'), self.response[0])
def test_fetch_test_404(self): """ Test what happens when a 404 is returned. """ with fudge.patch('basecamp.base.Base.get') as fake_get: mock = self.setup_mock(404) fake_get.is_callable().returns(mock) self.document.fetch(project_id=1, document_id=1)
def test_fetch_people_list(self): """ Test fetching a list of people. """ with fudge.patch('basecamp.base.Base.get') as fake_get: fake_get.is_callable().returns( self.setup_mock(200, self.response)) self.assertEquals(self.people.fetch(), self.response)
def patch_open(self, *args, **kwargs): fake_file = Fake('file') @contextmanager def _open(*args): yield fake_file with patch('builtins.open') as fake_open: fake_open.expects_call().with_args(*args, **kwargs).calls(_open) yield fake_file
def test_fetch_single_person(self): """ Test fetching a single person. """ with fudge.patch('basecamp.base.Base.get') as fake_get: fake_get.is_callable().returns( self.setup_mock(200, self.response[0])) self.assertEquals( self.people.fetch(person='me'), self.response[0])
def test_fetch_people_list_no_permission(self): """ Ensure an exception is raised when someone does not have permission to view the people list. """ with fudge.patch('basecamp.base.Base.get') as fake_get: fake_get.is_callable().returns(self.setup_mock(403, self.response)) self.assertEqual(self.people.fetch(), self.response)
def run_test(): with fudge.patch('shutil.copy', 'os.remove') as fakes: copy, remove = fakes import shutil import os assert isinstance(copy, fudge.Fake) assert isinstance(remove, fudge.Fake) eq_(copy, shutil.copy) eq_(remove, os.remove) holder.test_called = True
def setUp(self): self.create_redmine_issue() self.assignee_email = self.user.mail self.success_message = 'Deployment successful' self.error_message = 'Conflict while merging' self.patch = fudge.patch("{0}.handlers.send_mail".format( config.PROJECT_NAME)) self.mock_send_mail = self.patch.__enter__() self.mock_send_mail.is_callable()
def test_create_odd_status_code(self): """ Test updating a document but dealing with a 403 response """ response = {'title': 'foobar', 'content': 'baz'} with fudge.patch('basecamp.base.Base.post') as fake_put: fake_put.is_callable().returns(self.setup_mock(501, response)) self.assertEquals(self.document.create(66, 'foobar', 'baz'), response)
def test_update_document(self): """ Test updating a document but dealing with a 403 response """ response = {'title': 'foobar', 'content': 'baz'} with fudge.patch('basecamp.base.Base.put') as fake_put: fake_put.is_callable().returns(self.setup_mock(403, response)) self.assertEquals(self.document.update(1, 2, 'foobar', 'baz'), response)
def processing_results_fail(): """ A context manager, during which processing results of delivery attempts will fail with an unexpected exception. """ path = 'nti.webhooks.delivery_manager.ShipmentInfo._fill_req_resp_from_request' with fudge.patch(path) as mock_proc: mock_proc.is_callable().raises(UnicodeError) yield
def test_delete(self): """ Delete a project """ with fudge.patch('basecamp.base.Base.delete') as fake_delete: fake_delete.is_callable().returns(self.setup_mock(204)) project = basecamp.api.Project( self.url, self.token, self.refresh_token) self.assertTrue(project.remove(9))
def test_get_token_with_bad_code(self): """ Test getting the token, but with a bad response. """ with fudge.patch('basecamp.base.Base.post') as fake_post: mock = self.setup_mock(400, { 'error': 'This verification code was already used. ' 'Verification codes are single-use'}) fake_post.is_callable().returns(mock) self.auth.get_token('foobar')
def test_delete(self): """ Delete a project """ with fudge.patch('basecamp.base.Base.delete') as fake_delete: fake_delete.is_callable().returns(self.setup_mock(204)) project = basecamp.api.Project(self.url, self.token, self.refresh_token) self.assertTrue(project.remove(9))
def test_fetch_people_list_no_permission(self): """ Ensure an exception is raised when someone does not have permission to view the people list. """ with fudge.patch('basecamp.base.Base.get') as fake_get: fake_get.is_callable().returns( self.setup_mock(403, self.response)) self.assertEquals(self.people.fetch(), self.response)
def test_delete_no_perissions(self): """ Ensure an exception is raised when a user does not have permission to delete a project. """ with fudge.patch('basecamp.base.Base.delete') as fake_delete: fake_delete.is_callable().returns(self.setup_mock(403)) project = basecamp.api.Project( self.url, self.token, self.refresh_token) self.assertTrue(project.remove(9))
def test_archive_no_permissions(self): """ Ensure an exception is rasied in when a user who does not have permission to archive a project tries to do so. """ with fudge.patch('basecamp.base.Base.put') as fake_put: fake_put.is_callable().returns(self.setup_mock(403)) project = basecamp.api.Project( self.url, self.token, self.refresh_token) self.assertEquals(project.archive(9, archive=True), response)
def test_archive_no_permissions(self): """ Ensure an exception is rasied in when a user who does not have permission to archive a project tries to do so. """ with fudge.patch('basecamp.base.Base.put') as fake_put: fake_put.is_callable().returns(self.setup_mock(403)) project = basecamp.api.Project(self.url, self.token, self.refresh_token) self.assertEquals(project.archive(9, archive=True), response)
def test_delete_no_perissions(self): """ Ensure an exception is raised when a user does not have permission to delete a project. """ with fudge.patch('basecamp.base.Base.delete') as fake_delete: fake_delete.is_callable().returns(self.setup_mock(403)) project = basecamp.api.Project(self.url, self.token, self.refresh_token) self.assertTrue(project.remove(9))
def test_update_document(self): """ Test updating a document but dealing with a 403 response """ response = { 'title': 'foobar', 'content': 'baz' } with fudge.patch('basecamp.base.Base.put') as fake_put: fake_put.is_callable().returns(self.setup_mock(403, response)) self.assertEquals(self.document.update(1, 2, 'foobar', 'baz'), response)
def test_create_no_permissions(self): """ When a user does not have permission to create a project, an exception will be raised """ with fudge.patch('basecamp.base.Base.post') as fake_post: fake_post.is_callable().returns(self.setup_mock(403)) project = basecamp.api.Project( self.url, self.token, self.refresh_token) project.create('Some Project', 'Project Description')
def test_create_odd_status_code(self): """ Test updating a document but dealing with a 403 response """ response = { 'title': 'foobar', 'content': 'baz' } with fudge.patch('basecamp.base.Base.post') as fake_put: fake_put.is_callable().returns(self.setup_mock(501, response)) self.assertEquals(self.document.create(66, 'foobar', 'baz'), response)
def test_doesnt_overwrite_existing_wheels(tmpdir): from ..axle import copy_wheels_to_pypi built_wheel = tmpdir.join('belt-0.2-py27-none-any.whl') built_wheel.write('') local_pypi = tmpdir.mkdir('local') package_wheel = local_pypi.mkdir('belt').join('belt-0.2-py27-none-any.whl') package_wheel.write('') with fudge.patch('belt.axle.shutil') as shutil: shutil.provides('copyfile').times_called(0) copy_wheels_to_pypi(wheel_dir=str(tmpdir), local_pypi=str(local_pypi))
def test_list_error(self): """ Ensure an exception is thrown on a 404 """ # test a 404 error with fudge.patch('basecamp.base.Base.get') as fake_get: mock = self.setup_mock(404, {'error': 'something went wrong'}) fake_get.is_callable().returns(mock) projects = basecamp.api.Project(self.url, self.token, self.refresh_token) projects.fetch()
def test_create_no_permissions(self): """ When a user does not have permission to create a project, an exception will be raised """ with fudge.patch('basecamp.base.Base.post') as fake_post: fake_post.is_callable().returns(self.setup_mock(403)) project = basecamp.api.Project(self.url, self.token, self.refresh_token) project.create('Some Project', 'Project Description')
def test_successful_token(self): """ Get a good token. """ content = { 'access_token': 'abceasyas123==--1d3c', 'expires_in': 1209600, 'refresh_token': 'yICSwF7ImV4c==--zxvf' } with fudge.patch('basecamp.base.Base.post') as fake_post: fake_post.is_callable().returns(self.setup_mock(200, content)) self.assertEqual(self.auth.get_token('foobar'), content)
def test_list_error(self): """ Ensure an exception is thrown on a 404 """ # test a 404 error with fudge.patch('basecamp.base.Base.get') as fake_get: mock = self.setup_mock(404, {'error': 'something went wrong'}) fake_get.is_callable().returns(mock) projects = basecamp.api.Project( self.url, self.token, self.refresh_token) projects.fetch()
def test_edit_no_permission(self): """ Ensure an exception is raised in the event the currently authenticated user does not have permission to edit the name and description of the project. """ with fudge.patch('basecamp.base.Base.put') as fake_put: fake_put.is_callable().returns(self.setup_mock(403)) project = basecamp.api.Project(self.url, self.token, self.refresh_token) project.update(1, 'foobar', 'something')
def test_edit_no_permission(self): """ Ensure an exception is raised in the event the currently authenticated user does not have permission to edit the name and description of the project. """ with fudge.patch('basecamp.base.Base.put') as fake_put: fake_put.is_callable().returns(self.setup_mock(403)) project = basecamp.api.Project( self.url, self.token, self.refresh_token) project.update(1, 'foobar', 'something')
def test_edit(self): """ Edit a project """ response = {'id': 1, 'name': 'foobar', 'description': 'something'} with fudge.patch('basecamp.base.Base.put') as fake_put: fake_put.is_callable().returns(self.setup_mock(200, response)) project = basecamp.api.Project( self.url, self.token, self.refresh_token) self.assertEquals(project.update(1, 'foobar', 'something'), response)
def test_edit(self): """ Edit a project """ response = {'id': 1, 'name': 'foobar', 'description': 'something'} with fudge.patch('basecamp.base.Base.put') as fake_put: fake_put.is_callable().returns(self.setup_mock(200, response)) project = basecamp.api.Project(self.url, self.token, self.refresh_token) self.assertEquals(project.update(1, 'foobar', 'something'), response)
def test_detail_error(self): """ Ensure an exception is raised in the event the user does not have permissions to view the requested project's details. """ response = {'error': 'no permission'} with fudge.patch('basecamp.base.Base.get') as fake_get: fake_get.is_callable().returns(self.setup_mock(403, response)) projects = basecamp.api.Project(self.url, self.token, self.refresh_token) projects.fetch(project=9)
def test_detail_error(self): """ Ensure an exception is raised in the event the user does not have permissions to view the requested project's details. """ response = {'error': 'no permission'} with fudge.patch('basecamp.base.Base.get') as fake_get: fake_get.is_callable().returns(self.setup_mock(403, response)) projects = basecamp.api.Project( self.url, self.token, self.refresh_token) projects.fetch(project=9)
def test_successful_token(self): """ Get a good token. """ content = { 'access_token': 'abceasyas123==--1d3c', 'expires_in': 1209600, 'refresh_token': 'yICSwF7ImV4c==--zxvf' } with fudge.patch('basecamp.base.Base.post') as fake_post: fake_post.is_callable().returns(self.setup_mock(200, content)) self.assertEquals( self.auth.get_token('foobar'), content)
def test_create_document(self): """ Test creating a new document. """ response = { 'id': 8675309, 'project_id': 66, 'name': 'Jenny', 'content': 'I got it!<br>I got it!' } with fudge.patch('basecamp.base.Base.post') as fake_post: fake_post.is_callable().returns(self.setup_mock(201, response)) self.assertEquals( self.document.create(66, 'Jenny', 'I got it!<br>I got it!'), response)
def test_archive(self): """ Archive a project """ response = { 'id': 9, 'name': 'test', 'description': 'some test', "archived": True, } with fudge.patch('basecamp.base.Base.put') as fake_put: fake_put.is_callable().returns(self.setup_mock(200, response)) project = basecamp.api.Project(self.url, self.token, self.refresh_token) self.assertEquals(project.archive(9, archive=True), response)
def test_can_upload_video(self, HTTPConnection): expected_response = {"video": {"uuid": "shouldBeUUID"}} file_name = 'ShouldBeFileName' dirname = 'ShouldBeDirName' title = 'ShouldBeTitle' description = 'ShouldBeDescription' channel = 'ShouldBeChannel' privacy = 0 metatags = 'ShouldBeMetatags' video_dados = { 'video': { 'titulo': title, 'descricao': description, 'canal': channel, 'privacidade': privacy, 'metatags': metatags } } headers = { 'Token': 'shouldBeToken', 'Autenticacao': 'shouldBeAuth', 'Content-Type': 'application/x-www-form-urlencoded' } self.httpconnection_mock(HTTPConnection, 'POST', '<api_url>', '/video.xml', simplexml.dumps(video_dados), headers, simplexml.dumps(expected_response)) video_api = Video("<api_url>", "shouldBeToken") video_api._auth_hash = 'shouldBeAuth' video_api.post_multipart = fudge.Fake(callable=True) os_fake = fudge.Fake(callable=True) os_fake.path = fudge.Fake(callable=True) os_fake.path.isfile = fudge.Fake(callable=True).returns(True) with fudge.patched_context(video, "os", os_fake): with fudge.patch("__builtin__.open") as open_fake: open_fake.is_callable().with_args('ShouldBeDirName/ShouldBeFileName').returns_fake().expects('read').returns('shouldBeFileData') video_upload = video_api.upload(file_name=file_name, dirname=dirname, title=title, description=description, channel=channel, privacy=privacy, metatags=metatags) self.assertTrue(video_upload)
def test_archive(self): """ Archive a project """ response = { 'id': 9, 'name': 'test', 'description': 'some test', "archived": True, } with fudge.patch('basecamp.base.Base.put') as fake_put: fake_put.is_callable().returns( self.setup_mock(200, response)) project = basecamp.api.Project( self.url, self.token, self.refresh_token) self.assertEquals(project.archive(9, archive=True), response)
def test_archive_but_not_archived(self): """ Test archiving, but what happens if it is not archived. I'm sure sure if this is valid to test, but it might make sense. Regardless, here we go. """ response = { 'id': 9, 'name': 'test', 'description': 'some test', "archived": False, } with fudge.patch('basecamp.base.Base.put') as fake_put: fake_put.is_callable().returns(self.setup_mock(200, response)) project = basecamp.api.Project(self.url, self.token, self.refresh_token) project.archive(9, archive=True)
def test_archive_but_not_archived(self): """ Test archiving, but what happens if it is not archived. I'm sure sure if this is valid to test, but it might make sense. Regardless, here we go. """ response = { 'id': 9, 'name': 'test', 'description': 'some test', "archived": False, } with fudge.patch('basecamp.base.Base.put') as fake_put: fake_put.is_callable().returns(self.setup_mock(200, response)) project = basecamp.api.Project( self.url, self.token, self.refresh_token) project.archive(9, archive=True)
def test_create(self): """ Create a project """ response = { "id": 605816632, "name": "BCX", "description": "The Next Generation", "updated_at": "2012-03-23T13:55:43-05:00", "url": "{0}projects/605816632-bcx.json".format(self.url), "archived": False, "starred": True } with fudge.patch('basecamp.base.Base.post') as fake_post: fake_post.is_callable().returns(self.setup_mock(201, response)) project = basecamp.api.Project(self.url, self.token, self.refresh_token) new_project = project.create('Some Project', 'Project Description') self.assertEquals(new_project, response)