Completed
Push — master ( e187ac...8aa8f5 )
by Peter
07:52
created

CmdLine.test_democreate_call()   A

Complexity

Conditions 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
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', 'configcreate']
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
        self.cfg.set('login', 'LOGIN_GOOGLE_OAUTH_KEY', 'foo')
47
        self.cfg.set('login', 'LOGIN_GOOGLE_OAUTH_SECRET', 'bar')
48
        with open(conf_name, 'w') as cfg_file:
49
            self.cfg.write(cfg_file)
50
        # We got an adjusted INI file, which is not-reconsidered by the
51
        # indirectly triggered Django code, but by the cmdline functionalities.
52
53
    def tearDown(self):
54
        print("Removing temporary installation directory")
55
        shutil.rmtree(self.tmpdir)
56
57
    def test_democreate_call(self):
58
        sys.argv = ['opensubmit-web', 'democreate']
59
        cmdline.console_script(fsroot=self.tmpdir)
60
        self.assertNotEqual(0, Course.objects.all().count())
61
62
    def test_fixperms_call(self):
63
        sys.argv = ['opensubmit-web', 'fixperms']
64
        cmdline.console_script(fsroot=self.tmpdir)
65
66
    def test_fixchecksums_call(self):
67
        sys.argv = ['opensubmit-web', 'fixchecksums']
68
        cmdline.console_script(fsroot=self.tmpdir)
69
70
    def test_makeadmin_call(self):
71
        u = user.create_user(user.get_student_dict(0))
72
        sys.argv = ['opensubmit-web', 'makeadmin', u.email]
73
        cmdline.console_script(fsroot=self.tmpdir)
74
        u.refresh_from_db()
75
        self.assertEqual(True, u.is_superuser)
76
        self.assertEqual(True, u.is_staff)
77
78
    def test_makeowner_call(self):
79
        u = user.create_user(user.get_student_dict(0))
80
        sys.argv = ['opensubmit-web', 'makeowner', u.email]
81
        cmdline.console_script(fsroot=self.tmpdir)
82
        u.refresh_from_db()
83
        self.assertEqual(False, u.is_superuser)
84
        self.assertEqual(True, u.is_staff)
85
86
    def test_maketutor_call(self):
87
        u = user.create_user(user.get_student_dict(0))
88
        sys.argv = ['opensubmit-web', 'maketutor', u.email]
89
        cmdline.console_script(fsroot=self.tmpdir)
90
        u.refresh_from_db()
91
        self.assertEqual(False, u.is_superuser)
92
        self.assertEqual(True, u.is_staff)
93
94
    def test_makestudent_call(self):
95
        u = user.create_user(user.admin_dict)
96
        sys.argv = ['opensubmit-web', 'makestudent', u.email]
97
        cmdline.console_script(fsroot=self.tmpdir)
98
        u.refresh_from_db()
99
        self.assertEqual(False, u.is_superuser)
100
        self.assertEqual(False, u.is_staff)
101
102
103
    def test_configtest_call(self):
104
        '''
105
        Simulate real command-line calls of the 'configure' functionality.
106
107
        Since the config file ist valid,  some Django manage.py
108
        functionality for the collection of static files is triggered here.
109
        Since Django settings are already loaded by the test suite,
110
        the modified INI file is not respected here.
111
        For this reason, we need the settings decorator override.
112
        '''
113
        # simulate command-line argument
114
        with self.settings(STATIC_ROOT=self.tmpdir + 'static'):
115
            sys.argv = ['opensubmit-web', 'configtest']
116
            cmdline.console_script(fsroot=self.tmpdir)
117
            self.assertEqual(True, os.path.isdir(self.tmpdir + 'static/grappelli'))
118