Exemplo n.º 1
0
    def test_git_ident(self):
        """
        Test valid course with and without user specified.

        Test skipped if git global config override environment variable GIT_CONFIG
        is set.
        """
        git_export_utils.export_to_git(self.course.id,
                                       'file://{0}'.format(self.bare_repo_dir),
                                       'enigma')
        expect_string = '{0}|{1}\n'.format(
            git_export_utils.GIT_EXPORT_DEFAULT_IDENT['name'],
            git_export_utils.GIT_EXPORT_DEFAULT_IDENT['email'])
        cwd = os.path.abspath(git_export_utils.GIT_REPO_EXPORT_DIR /
                              'test_bare')
        git_log = subprocess.check_output(
            ['git', 'log', '-1', '--format=%an|%ae'], cwd=cwd).decode('utf-8')
        self.assertEqual(expect_string, git_log)

        # Make changes to course so there is something to commit
        self.populate_course()
        git_export_utils.export_to_git(self.course.id,
                                       'file://{0}'.format(self.bare_repo_dir),
                                       self.user.username)
        expect_string = '{0}|{1}\n'.format(
            self.user.username,
            self.user.email,
        )
        git_log = subprocess.check_output(
            ['git', 'log', '-1', '--format=%an|%ae'], cwd=cwd).decode('utf-8')
        self.assertEqual(expect_string, git_log)
def export_git(request, course_key_string):
    """
    This method serves up the 'Export to Git' page
    """
    course_key = CourseKey.from_string(course_key_string)
    if not has_course_author_access(request.user, course_key):
        raise PermissionDenied()

    course_module = modulestore().get_course(course_key)
    failed = False

    log.debug('export_git course_module=%s', course_module)

    msg = ""
    if 'action' in request.GET and course_module.giturl:
        if request.GET['action'] == 'push':
            try:
                git_export_utils.export_to_git(
                    course_module.id,
                    course_module.giturl,
                    request.user,
                )
                msg = _('Course successfully exported to git repository')
            except git_export_utils.GitExportError as ex:
                failed = True
                msg = str(ex)

    return render_to_response('export_git.html', {
        'context_course': course_module,
        'msg': msg,
        'failed': failed,
    })
Exemplo n.º 3
0
    def test_dirty_repo(self):
        """
        Add additional items not in the repo and make sure they aren't
        there after the export. This allows old content to removed
        in the repo.
        """
        repo_name = 'dirty_repo1'
        self.make_bare_repo_with_course(repo_name)
        git_export_utils.export_to_git(self.course.id,
                                       self.course_module.giturl, self.user)

        # Make arbitrary change to course to make diff
        self.course_module.matlab_api_key = 'something'
        modulestore().update_item(self.course_module, self.user.id)
        # Touch a file in the directory, export again, and make sure
        # the test file is gone
        repo_dir = os.path.join(
            os.path.abspath(git_export_utils.GIT_REPO_EXPORT_DIR),
            repo_name
        )
        test_file = os.path.join(repo_dir, 'test.txt')
        open(test_file, 'a').close()
        self.assertTrue(os.path.isfile(test_file))
        git_export_utils.export_to_git(self.course.id,
                                       self.course_module.giturl, self.user)
        self.assertFalse(os.path.isfile(test_file))
Exemplo n.º 4
0
    def test_no_change(self):
        """
        Test response if there are no changes
        """
        git_export_utils.export_to_git(self.course.id,
                                       'file://{0}'.format(self.bare_repo_dir))

        with self.assertRaisesRegex(
                GitExportError, six.text_type(GitExportError.CANNOT_COMMIT)):
            git_export_utils.export_to_git(
                self.course.id, 'file://{0}'.format(self.bare_repo_dir))
    def handle(self, *args, **options):
        """
        Checks arguments and runs export function if they are good
        """
        # Rethrow GitExportError as CommandError for SystemExit
        try:
            course_key = CourseKey.from_string(options['course_loc'])
        except InvalidKeyError:
            raise CommandError(str(git_export_utils.GitExportError.BAD_COURSE))  # lint-amnesty, pylint: disable=raise-missing-from

        try:
            git_export_utils.export_to_git(course_key, options['git_url'],
                                           options.get('user', ''),
                                           options.get('rdir', None))
        except git_export_utils.GitExportError as ex:
            raise CommandError(str(ex))  # lint-amnesty, pylint: disable=raise-missing-from
Exemplo n.º 6
0
    def test_bad_git_repos(self):
        """
        Test invalid git repos
        """
        test_repo_path = '{}/test_repo'.format(
            git_export_utils.GIT_REPO_EXPORT_DIR)
        self.assertFalse(os.path.isdir(test_repo_path))
        course_key = CourseLocator('foo', 'blah', '100-')
        # Test bad clones
        with self.assertRaisesRegex(GitExportError,
                                    six.text_type(GitExportError.CANNOT_PULL)):
            git_export_utils.export_to_git(
                course_key, 'https://*****:*****@example.com/test_repo.git')
        self.assertFalse(os.path.isdir(test_repo_path))

        # Setup good repo with bad course to test xml export
        with self.assertRaisesRegex(
                GitExportError, six.text_type(GitExportError.XML_EXPORT_FAIL)):
            git_export_utils.export_to_git(
                course_key, 'file://{0}'.format(self.bare_repo_dir))

        # Test bad git remote after successful clone
        with self.assertRaisesRegex(GitExportError,
                                    six.text_type(GitExportError.CANNOT_PULL)):
            git_export_utils.export_to_git(
                course_key, 'https://*****:*****@example.com/r.git')
    def test_bad_git_url(self):
        """
        Test several bad URLs for validation
        """
        course_key = CourseLocator('org', 'course', 'run')
        with self.assertRaisesRegex(GitExportError, str(GitExportError.URL_BAD)):
            git_export_utils.export_to_git(course_key, 'Sillyness')

        with self.assertRaisesRegex(GitExportError, str(GitExportError.URL_BAD)):
            git_export_utils.export_to_git(course_key, 'example.com:edx/notreal')

        with self.assertRaisesRegex(GitExportError, str(GitExportError.URL_NO_AUTH)):
            git_export_utils.export_to_git(course_key, 'http://blah')