Passed
Push — master ( 523113...818d71 )
by Peter
02:04
created

CmdLine.test_makestudent_call()   A

Complexity

Conditions 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
1
'''
2
    Test cases for the command line tools.
3
'''
4
5
import sys
6
import tempfile
7
import os.path
8
import configparser
9
import shutil
10
11
from django.test import TestCase
12
from opensubmit import cmdline, settings
13
from opensubmit.models import Course
14
from .helpers import user
15
16
17
class CmdLine(TestCase):
18
    '''
19
    Test cases for the "configure" functionality of the command-line script.
20
    '''
21
22
    def setUp(self):
23
        # prepare temporary target directory for tests
24
        self.tmpdir = tempfile.mkdtemp() + os.sep
25
        '''
26
        Create config file.
27
        This needs some explanation:
28
        - The 'configure' functionality does not care about Django settings,
29
          so we use the 'fsroot' parameter to force them into the
30
          temporary directory.
31
        - This should lead to the creation of a fresh settings file,
32
          which then must be adjusted by the user. We mock that accordingly.
33
        '''
34
        sys.argv = ['opensubmit-web', 'configure']
35
        cmdline.console_script(fsroot=self.tmpdir)
36
        conf_name = self.tmpdir + 'etc/opensubmit/settings.ini'
37
        self.assertEqual(True, os.path.isfile(conf_name))
38
        # Got a working settings file from the template, now configure it
39
        self.cfg = configparser.ConfigParser()
40
        with open(conf_name) as cfg_file:
41
            self.cfg.readfp(cfg_file)
42
        self.cfg.set('server', 'HOST', 'http://www.troeger.eu')
43
        self.cfg.set('server', 'MEDIA_ROOT', self.tmpdir + settings.MEDIA_ROOT)
44
        self.cfg.set('server', 'LOG_FILE', self.tmpdir + settings.LOG_FILE)
45
        self.cfg.set('database', 'DATABASE_NAME', self.tmpdir + 'database.sqlite')
46
        with open(conf_name, 'w') as cfg_file:
47
            self.cfg.write(cfg_file)
48
        # We got an adjusted INI file, which is not-reconsidered by the
49
        # indirectly triggered Django code, but by the cmdline functionalities.
50
51
    def tearDown(self):
52
        print("Removing temporary installation directory")
53
        shutil.rmtree(self.tmpdir)
54
55
    def test_create_demo_call(self):
56
        sys.argv = ['opensubmit-web', 'createdemo']
57
        cmdline.console_script(fsroot=self.tmpdir)
58
        self.assertNotEqual(0, Course.objects.all().count())
59
60
    def test_fixperms_call(self):
61
        sys.argv = ['opensubmit-web', 'fixperms']
62
        cmdline.console_script(fsroot=self.tmpdir)
63
64
    def test_fixchecksums_call(self):
65
        sys.argv = ['opensubmit-web', 'fixchecksums']
66
        cmdline.console_script(fsroot=self.tmpdir)
67
68
    def test_makeadmin_call(self):
69
        u = user.create_user(user.get_student_dict(0))
70
        sys.argv = ['opensubmit-web', 'makeadmin', u.email]
71
        cmdline.console_script(fsroot=self.tmpdir)
72
        u.refresh_from_db()
73
        self.assertEqual(True, u.is_superuser)
74
        self.assertEqual(True, u.is_staff)
75
76
    def test_makeowner_call(self):
77
        u = user.create_user(user.get_student_dict(0))
78
        sys.argv = ['opensubmit-web', 'makeowner', u.email]
79
        cmdline.console_script(fsroot=self.tmpdir)
80
        u.refresh_from_db()
81
        self.assertEqual(False, u.is_superuser)
82
        self.assertEqual(True, u.is_staff)
83
84
    def test_maketutor_call(self):
85
        u = user.create_user(user.get_student_dict(0))
86
        sys.argv = ['opensubmit-web', 'maketutor', u.email]
87
        cmdline.console_script(fsroot=self.tmpdir)
88
        u.refresh_from_db()
89
        self.assertEqual(False, u.is_superuser)
90
        self.assertEqual(True, u.is_staff)
91
92
    def test_makestudent_call(self):
93
        u = user.create_user(user.admin_dict)
94
        sys.argv = ['opensubmit-web', 'makestudent', u.email]
95
        cmdline.console_script(fsroot=self.tmpdir)
96
        u.refresh_from_db()
97
        self.assertEqual(False, u.is_superuser)
98
        self.assertEqual(False, u.is_staff)
99
100
101
    def test_configure_call(self):
102
        '''
103
        Simulate real command-line calls of the 'configure' functionality.
104
105
        Since the config file ist valid,  some Django manage.py
106
        functionality for the collection of static files is triggered here.
107
        Since Django settings are already loaded by the test suite,
108
        the modified INI file is not respected here.
109
        For this reason, we need the settings decorator override.
110
        '''
111
        # simulate command-line argument
112
        with self.settings(STATIC_ROOT=self.tmpdir + 'static'):
113
            sys.argv = ['opensubmit-web', 'configure']
114
            cmdline.console_script(fsroot=self.tmpdir)
115
            self.assertEqual(True, os.path.isfile(
116
                self.tmpdir + 'etc/opensubmit/apache24.conf'))
117
            self.assertEqual(True, os.path.isdir(self.tmpdir + 'static/grappelli'))
118