Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
from __future__ import division
__copyright__ = "Copyright (C) 2017 Dong Zhuang"
__license__ = """
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
from copy import deepcopy
from django.test import TestCase, RequestFactory, mock
from course.models import Course, Participation
from course import versioning
from .base_test_mixins import (
CoursesTestMixinBase, SINGLE_COURSE_SETUP_LIST,
FallBackStorageMessageTestMixin)
from .utils import suppress_stdout_decorator
class CourseCreationTest(CoursesTestMixinBase, FallBackStorageMessageTestMixin,
TestCase):
courses_setup_list = []
@classmethod
def setUpTestData(cls): # noqa
super(CourseCreationTest, cls).setUpTestData()
cls.instructor = cls.create_user(
SINGLE_COURSE_SETUP_LIST[0]["participations"][0]["user"])
cls.add_user_permission(cls.instructor, "add_course")
def setUp(self):
super(CourseCreationTest, self).setUp()
self.rf = RequestFactory()
self.addCleanup(self.force_remove_all_course_dir)
def get_set_up_new_course_form_data(self):
return deepcopy(SINGLE_COURSE_SETUP_LIST[0]["course"])
@classmethod
def add_user_permission(cls, user, perm):
from django.contrib.contenttypes.models import ContentType
content_type = ContentType.objects.get_for_model(Course)
from django.contrib.auth.models import Permission
permission = Permission.objects.get(
codename=perm, content_type=content_type)
user.user_permissions.add(permission)
def test_non_auth_set_up_new_course(self):
with self.temporarily_switch_to_user(None):
resp = self.get_set_up_new_course()
self.assertTrue(resp.status_code, 403)
data = SINGLE_COURSE_SETUP_LIST[0]["course"]
resp = self.post_create_course(data, raise_error=False,
login_superuser=False)
self.assertTrue(resp.status_code, 403)
self.assertEqual(Course.objects.count(), 0)
def test_set_up_new_course_no_perm(self):
# create a user which has no perm for creating course
ta = self.create_user(
SINGLE_COURSE_SETUP_LIST[0]["participations"][1]["user"])
self.assertFalse(ta.has_perm("course.add_course"))
self.assertFalse(ta.has_perm("course.change_course"))
self.assertFalse(ta.has_perm("course.delete_course"))
with self.temporarily_switch_to_user(ta):
resp = self.get_set_up_new_course()
self.assertTrue(resp.status_code, 403)
data = self.get_set_up_new_course_form_data()
resp = self.post_create_course(data, raise_error=False,
login_superuser=False)
self.assertTrue(resp.status_code, 403)
self.assertEqual(Course.objects.count(), 0)
def test_set_up_new_course(self):
# In this test, we use client instead of request factory to simplify
# the logic.
with self.temporarily_switch_to_user(self.instructor):
# the permission is cached, need to repopulated from db
resp = self.get_set_up_new_course()
self.assertTrue(resp.status_code, 200)
with mock.patch("dulwich.client.GitClient.fetch",
return_value={b"HEAD": b"some_commit_sha"}),\
mock.patch("course.versioning.transfer_remote_refs",
return_value=None),\
mock.patch('course.versioning.messages') as mock_messages,\
mock.patch("course.validation.validate_course_content",
return_value=None):
data = self.get_set_up_new_course_form_data()
resp = self.post_create_course(data, raise_error=False,
login_superuser=False)
self.assertTrue(resp.status_code, 200)
self.assertEqual(Course.objects.count(), 1)
self.assertEqual(Participation.objects.count(), 1)
self.assertEqual(Participation.objects.first().user.username,
"test_instructor")
self.assertIn("Course content validated, creation succeeded.",
mock_messages.add_message.call_args[0])
from course.enrollment import get_participation_role_identifiers
# the user who setup the course has role instructor
self.assertTrue(
get_participation_role_identifiers(
Course.objects.first(),
Participation.objects.first()),
"instructor")
def test_set_up_new_course_form_invalid(self):
for field_name in ["identifier", "name", "number", "time_period",
"git_source", "from_email", "notify_email"]:
form_data = self.get_set_up_new_course_form_data()
del form_data[field_name]
request = self.rf.post(self.get_set_up_new_course_url(), data=form_data)
request.user = self.instructor
form = versioning.CourseCreationForm(request.POST)
self.assertFalse(form.is_valid())
@suppress_stdout_decorator(suppress_stderr=True)
def test_set_up_new_course_git_source_invalid(self):
data = self.get_set_up_new_course_form_data()
request = self.rf.post(self.get_set_up_new_course_url(), data=data)
request.user = self.instructor
with mock.patch("dulwich.client.GitClient.fetch",
return_value=None),\
mock.patch('course.versioning.messages') as mock_messages,\
mock.patch("course.models.Course.save") as mock_save,\
mock.patch("course.versioning.render"):
resp = versioning.set_up_new_course(request)
self.assertTrue(resp.status_code, 200)
self.assertEqual(mock_save.call_count, 0)
self.assertIn("No refs found in remote repository",
mock_messages.add_message.call_args[0][2])
@suppress_stdout_decorator(suppress_stderr=True)
def test_set_up_new_course_subdir(self):
data = self.get_set_up_new_course_form_data()
data["course_root_path"] = "some_dir"
request = self.rf.post(self.get_set_up_new_course_url(), data=data)
request.user = self.instructor
with mock.patch("dulwich.client.GitClient.fetch",
return_value={b"HEAD": b"some_commit_sha"}), \
mock.patch('course.versioning.messages'), \
mock.patch("course.validation.validate_course_content",
return_value=None) as mock_validate, \
mock.patch("course.models.Course.save"), \
mock.patch("course.models.Participation.save", return_value=True), \
mock.patch("course.versioning.render"):
resp = versioning.set_up_new_course(request)
from course.content import SubdirRepoWrapper
self.assertIsInstance(mock_validate.call_args[0][0], SubdirRepoWrapper)
self.assertTrue(resp.status_code, 200)