test_export_git.py 3.26 KB
Newer Older
Carson Gee committed
1 2 3 4 5 6 7 8 9 10 11 12 13
"""
Test the ability to export courses to xml from studio
"""

import copy
import os
import shutil
import subprocess
from uuid import uuid4

from django.conf import settings
from django.core.urlresolvers import reverse
from django.test.utils import override_settings
14
from pymongo import MongoClient
Carson Gee committed
15 16

from .utils import CourseTestCase
17 18
import contentstore.git_export_utils as git_export_utils
from xmodule.contentstore.django import _CONTENTSTORE
Carson Gee committed
19 20 21 22 23 24 25
from xmodule.modulestore.django import modulestore

TEST_DATA_CONTENTSTORE = copy.deepcopy(settings.CONTENTSTORE)
TEST_DATA_CONTENTSTORE['DOC_STORE_CONFIG']['db'] = 'test_xcontent_%s' % uuid4().hex


@override_settings(CONTENTSTORE=TEST_DATA_CONTENTSTORE)
26
class TestExportGit(CourseTestCase):
Carson Gee committed
27 28 29 30 31 32 33 34
    """
    Tests pushing a course to a git repository
    """

    def setUp(self):
        """
        Setup test course, user, and url.
        """
35
        super(TestExportGit, self).setUp()
Carson Gee committed
36
        self.course_module = modulestore().get_item(self.course.location)
37
        self.test_url = reverse('export_git', kwargs={
Carson Gee committed
38 39 40 41 42
            'org': self.course.location.org,
            'course': self.course.location.course,
            'name': self.course.location.name,
        })

43 44 45 46
    def tearDown(self):
        MongoClient().drop_database(TEST_DATA_CONTENTSTORE['DOC_STORE_CONFIG']['db'])
        _CONTENTSTORE.clear()

Carson Gee committed
47 48 49 50 51 52 53 54
    def test_giturl_missing(self):
        """
        Test to make sure an appropriate error is displayed
        if course hasn't set giturl.
        """
        response = self.client.get(self.test_url)
        self.assertEqual(200, response.status_code)
        self.assertIn(
55 56
            ('giturl must be defined in your '
             'course settings before you can export to git.'),
Carson Gee committed
57 58 59 60 61 62
            response.content
        )

        response = self.client.get('{}?action=push'.format(self.test_url))
        self.assertEqual(200, response.status_code)
        self.assertIn(
63 64
            ('giturl must be defined in your '
             'course settings before you can export to git.'),
Carson Gee committed
65 66 67
            response.content
        )

68
    def test_course_export_failures(self):
Carson Gee committed
69 70 71 72 73 74 75
        """
        Test failed course export response.
        """
        self.course_module.giturl = 'foobar'
        modulestore().save_xmodule(self.course_module)

        response = self.client.get('{}?action=push'.format(self.test_url))
76
        self.assertIn('Export Failed:', response.content)
Carson Gee committed
77

78
    def test_course_export_success(self):
Carson Gee committed
79 80 81 82
        """
        Test successful course export response.
        """
        # Build out local bare repo, and set course git url to it
83
        repo_dir = os.path.abspath(git_export_utils.GIT_REPO_EXPORT_DIR)
Carson Gee committed
84 85 86 87
        os.mkdir(repo_dir)
        self.addCleanup(shutil.rmtree, repo_dir)

        bare_repo_dir = '{0}/test_repo.git'.format(
88
            os.path.abspath(git_export_utils.GIT_REPO_EXPORT_DIR))
Carson Gee committed
89 90 91 92 93 94 95 96 97 98
        os.mkdir(bare_repo_dir)
        self.addCleanup(shutil.rmtree, bare_repo_dir)

        subprocess.check_output(['git', '--bare', 'init', ], cwd=bare_repo_dir)

        self.populateCourse()
        self.course_module.giturl = 'file://{}'.format(bare_repo_dir)
        modulestore().save_xmodule(self.course_module)

        response = self.client.get('{}?action=push'.format(self.test_url))
99
        self.assertIn('Export Succeeded', response.content)