def test_pull_renames(self):
        upstream_workspace = self.create_upstream_for(self.workspace)
        self.add_schema(upstream_workspace, TestPerson)
        self.add_mapping(upstream_workspace, TestPerson)
        upstream_workspace.sm.store_data('README.rst', 'the readme',
                                         'Writing the readme.')
        self.workspace.pull()
        # do the rename
        upstream_workspace.repo.index.move(['README.rst', 'README.txt'])
        upstream_workspace.repo.index.commit('Renaming rst to txt.')

        repo_name = os.path.basename(self.workspace.working_dir)
        request = testing.DummyRequest({})
        request.route_url = lambda route, name: 'foo'
        request.matchdict = {
            'name': repo_name,
        }

        resource = RepositoryResource(request)
        [diff] = resource.post()
        self.assertEqual(diff, {
            'type': 'R',
            'rename_from': 'README.rst',
            'rename_to': 'README.txt',
        })
    def test_post(self, mock_pull):
        request = testing.DummyRequest({})
        request.route_url = lambda route, name: 'foo'
        repo_name = os.path.basename(self.workspace.working_dir)
        request.matchdict = {
            'name': repo_name,
        }
        request.params = {
            'branch': 'foo',
            'remote': 'bar',
        }
        resource = RepositoryResource(request)

        with patch.object(request.registry, 'notify') as mocked_notify:
            resource.post()
            mock_pull.assert_called_with(branch_name='foo', remote_name='bar')
            (call1, call2) = mocked_notify.call_args_list
            (event, ) = call1[0]
            self.assertIsInstance(event, RepositoryUpdated)
            self.assertEqual(event.repo, self.workspace.repo)
            self.assertEqual(event.branch, 'foo')
            self.assertTrue(event.changes)
            (event, ) = call2[0]
            self.assertEqual(event.event_type, 'repo.push')
            self.assertEqual(event.payload, {
                'repo': repo_name,
                'url': 'foo',
            })
 def test_get(self):
     request = testing.DummyRequest({})
     repo_name = os.path.basename(self.workspace.working_dir)
     request.matchdict = {
         'name': repo_name,
     }
     resource = RepositoryResource(request)
     repo_json = resource.get()
     self.assertEqual(repo_json, format_repo(self.workspace.repo))
 def test_collection_get_name_only(self):
     request = testing.DummyRequest({'name_only': True})
     resource = RepositoryResource(request)
     [repo_json] = resource.collection_get()
     self.assertEqual(
         repo_json, {
             'name':
             'unicore.distribute.api.tests.test_repos.'
             'TestRepositoryResource.test_collection_get_name_only'
         })
    def test_delete(self):
        repo_name = os.path.basename(self.workspace.working_dir)
        request = testing.DummyRequest({})
        request.matchdict = {
            'name': repo_name,
        }

        resource = RepositoryResource(request)
        resource.delete()
        self.assertEqual(resource.request.response.status_int, 204)
        self.assertFalse(self.workspace.sm.storage_exists())
        self.assertFalse(self.workspace.im.index_exists('master'))
 def test_get_404(self):
     request = testing.DummyRequest({})
     request.matchdict = {
         'name': 'does-not-exist',
     }
     resource = RepositoryResource(request)
     self.assertRaises(NotFound, resource.get)
 def test_collection_post_error(self, mock_method):
     mock_method.side_effect = GitCommandError('git clone',
                                               'Boom!',
                                               stderr='mocked response')
     request = testing.DummyRequest({})
     request.validated = {
         'repo_url': 'git://example.org/bar.git',
         'repo_name': None
     }
     request.errors = Errors()
     resource = RepositoryResource(request)
     resource.collection_post()
     [error] = request.errors
     self.assertEqual(error['location'], 'body')
     self.assertEqual(error['name'], 'repo_url')
     self.assertEqual(error['description'], 'mocked response')
    def test_pull_additions(self):
        upstream_workspace = self.create_upstream_for(self.workspace)
        self.add_schema(upstream_workspace, TestPerson)
        self.add_mapping(upstream_workspace, TestPerson)
        person1 = TestPerson({'age': 1, 'name': 'person1'})
        upstream_workspace.save(person1, 'Adding person1.')

        repo_name = os.path.basename(self.workspace.working_dir)
        request = testing.DummyRequest({})
        request.route_url = lambda route, name: 'foo'
        request.matchdict = {
            'name': repo_name,
        }

        resource = RepositoryResource(request)
        [diff] = resource.post()
        self.assertEqual(diff, {
            'type': 'A',
            'path': upstream_workspace.sm.git_name(person1),
        })
    def test_collection_post_success(self):
        # NOTE: cloning to a different directory called `remote` because
        #       the API is trying to clone into the same folder as the
        #       tests: self.WORKING_DIR.
        #
        # FIXME: This is too error prone & tricky to reason about
        api_repo_name = '%s_remote' % (self.id(), )
        self.remote_workspace = self.mk_workspace(working_dir=os.path.join(
            self.WORKING_DIR, 'remote'),
                                                  name=api_repo_name)
        request = testing.DummyRequest({})
        request.validated = {
            'repo_url': self.remote_workspace.working_dir,
            'repo_name': None
        }
        # Cleanup the repo created by the API on tear down
        self.addCleanup(lambda: EG.workspace(
            os.path.join(self.WORKING_DIR, api_repo_name)).destroy())
        self.addCleanup(lambda: EG.workspace(
            os.path.join(self.WORKING_DIR, 'foo-bar')).destroy())
        request.route_url = lambda route, name: ('/repos/%s.json' %
                                                 (api_repo_name, ))
        request.errors = Errors()
        resource = RepositoryResource(request)

        with patch.object(request.registry, 'notify') as mocked_notify:
            resource.collection_post()
            self.assertEqual(request.response.headers['Location'],
                             '/repos/%s.json' % (api_repo_name, ))
            self.assertEqual(request.response.status_code, 301)
            mocked_notify.assert_called()
            (event, ) = mocked_notify.call_args[0]
            self.assertIsInstance(event, RepositoryCloned)
            self.assertIs(event.config, self.config.registry.settings)
            self.assertEqual(
                event.repo.working_dir,
                os.path.abspath(os.path.join(self.WORKING_DIR, api_repo_name)))
            # check that the repo can be cloned with a different name
            request.validated['repo_name'] = 'foo-bar'
            resource.collection_post()
            self.assertEqual(request.response.status_code, 301)
            self.assertTrue(
                os.path.exists(os.path.join(self.WORKING_DIR, 'foo-bar')))
예제 #10
0
 def test_collection_get(self):
     request = testing.DummyRequest({})
     resource = RepositoryResource(request)
     [repo_json] = resource.collection_get()
     self.assertEqual(repo_json, format_repo(self.workspace.repo))