Newer
Older
from __future__ import division
__copyright__ = "Copyright (C) 2018 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.
"""
import six
from six.moves.urllib.parse import ParseResult, quote, urlparse
from djangosaml2.urls import urlpatterns as djsaml2_urlpatterns
from django.test import TestCase, override_settings, RequestFactory
from django.contrib import messages
from django.conf import settings
from django.core import mail
from django.contrib.auth import (
REDIRECT_FIELD_NAME, SESSION_KEY,
)
from django.http import QueryDict, HttpResponse
from django.urls import NoReverseMatch, reverse
from unittest import skipIf
from course.auth import get_impersonable_user_qset, get_user_model
from course.models import FlowPageVisit, ParticipationPermission
CoursesTestMixinBase, SingleCoursePageTestMixin, TwoCourseTestMixin,
FallBackStorageMessageTestMixin, TWO_COURSE_SETUP_LIST,
NONE_PARTICIPATION_USER_CREATE_KWARG_LIST)
from tests.utils import (
LocmemBackendTestsMixin, load_url_pattern_names, reload_urlconf, mock)
from tests.factories import UserFactory, ParticipationFactory
# settings names
EDITABLE_INST_ID_BEFORE_VERI = "RELATE_EDITABLE_INST_ID_BEFORE_VERIFICATION"
SHOW_INST_ID_FORM = "RELATE_SHOW_INST_ID_FORM"
SHOW_EDITOR_FORM = "RELATE_SHOW_EDITOR_FORM"
NOT_IMPERSONATING_MESSAGE = "Not currently impersonating anyone."
NO_LONGER_IMPERSONATING_MESSAGE = "No longer impersonating anyone."
ALREADY_IMPERSONATING_SOMEONE_MESSAGE = "Already impersonating someone."
ERROR_WHILE_IMPERSONATING_MESSAGE = "Error while impersonating."
IMPERSONATE_FORM_ERROR_NOT_VALID_USER_MSG = (
"Select a valid choice. That choice is "
"not one of the available choices.")
ADD_MESSAGES_FUNC_PATH = "course.auth.messages.add_message"
class ImpersonateTest(SingleCoursePageTestMixin,
FallBackStorageMessageTestMixin, TestCase):
def test_impersonate_by_not_authenticated(self):
with self.temporarily_switch_to_user(None):
resp = self.get_impersonate()
self.assertEqual(resp.status_code, 403)
resp = self.post_impersonate(
impersonatee=self.student_participation.user)
self.assertEqual(resp.status_code, 403)
resp = self.get_stop_impersonate()
self.assertEqual(resp.status_code, 403)
resp = self.post_stop_impersonate()
self.assertEqual(resp.status_code, 403)
resp = self.get_confirm_stop_impersonate()
self.assertEqual(resp.status_code, 403)
resp = self.post_confirm_stop_impersonate()
self.assertEqual(resp.status_code, 403)
def test_impersonate_by_student(self):
user = self.student_participation.user
impersonatable = get_impersonable_user_qset(user)
self.assertEqual(impersonatable.count(), 0)
with self.temporarily_switch_to_user(user):
resp = self.get_impersonate()
self.assertEqual(resp.status_code, 403)
resp = self.post_impersonate(
impersonatee=self.student_participation.user)
self.assertEqual(resp.status_code, 403)
resp = self.get_stop_impersonate()
self.assertEqual(resp.status_code, 403)
self.assertIsNone(self.c.session.get("impersonate_id"))
resp = self.post_stop_impersonate()
self.assertEqual(resp.status_code, 403)
resp = self.get_confirm_stop_impersonate()
self.assertEqual(resp.status_code, 403)
resp = self.post_confirm_stop_impersonate()
self.assertEqual(resp.status_code, 403)
def test_impersonate_by_ta(self):
user = self.ta_participation.user
impersonatable = get_impersonable_user_qset(user)
self.assertEqual(impersonatable.count(), 1)
# create 2 participations, on is not active,
# impersonatable count should be 2, not 3
ParticipationFactory.create(
course=self.course,
status=constants.participation_status.active)
ParticipationFactory.create(
course=self.course,
status=constants.participation_status.requested)
impersonatable = get_impersonable_user_qset(user)
self.assertEqual(impersonatable.count(), 2)
self.assertNotIn(self.instructor_participation.user, impersonatable)
with self.temporarily_switch_to_user(user):
resp = self.get_impersonate()
self.assertEqual(resp.status_code, 200)
# not impersonating, no need to confirm
resp = self.get_confirm_stop_impersonate()
self.assertEqual(resp.status_code, 200)
self.assertResponseMessageLevelsEqual(resp, [messages.ERROR])
self.assertResponseMessagesEqual(resp, NOT_IMPERSONATING_MESSAGE)
resp = self.post_confirm_stop_impersonate()
self.assertEqual(resp.status_code, 200)
self.assertResponseMessageLevelsEqual(resp, [messages.ERROR])
self.assertResponseMessagesEqual(resp, NOT_IMPERSONATING_MESSAGE)
resp = self.post_impersonate(
impersonatee=self.student_participation.user)
self.assertEqual(resp.status_code, 200)
self.assertEqual(self.c.session["impersonate_id"],
self.student_participation.user.pk)
resp = self.post_impersonate(
impersonatee=self.student_participation.user)
self.assertEqual(resp.status_code, 200)
self.assertResponseMessageLevelsEqual(resp, [messages.ERROR])
self.assertResponseMessagesEqual(
resp, ALREADY_IMPERSONATING_SOMEONE_MESSAGE)
self.assertTemplateUsed(resp, "stop-impersonate-confirmation.html")
self.assertEqual(self.c.session["impersonate_id"],
self.student_participation.user.pk)
# stop_impersonating
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
self.assertIsNone(self.c.session.get("impersonate_id"))
self.assertResponseMessageLevelsEqual(resp, [messages.INFO])
self.assertResponseMessagesEqual(resp, NO_LONGER_IMPERSONATING_MESSAGE)
# fail re-stop_impersonating
resp = self.post_stop_impersonate()
self.assertEqual(resp.status_code, 200)
self.assertResponseMessageLevelsEqual(resp, [messages.ERROR])
self.assertResponseMessagesEqual(resp, NOT_IMPERSONATING_MESSAGE)
# not allowed to impersonate instructor
resp = self.post_impersonate(
impersonatee=self.instructor_participation.user)
self.assertEqual(resp.status_code, 200)
self.assertFormError(resp, 'form', 'user',
IMPERSONATE_FORM_ERROR_NOT_VALID_USER_MSG)
self.assertIsNone(self.c.session.get("impersonate_id"))
# not allowed to impersonate self
resp = self.post_impersonate(
impersonatee=user)
self.assertEqual(resp.status_code, 200)
self.assertFormError(resp, 'form', 'user',
IMPERSONATE_FORM_ERROR_NOT_VALID_USER_MSG)
self.assertIsNone(self.c.session.get("impersonate_id"))
def test_impersonate_by_superuser(self):
user = self.superuser
impersonatable = get_impersonable_user_qset(user)
self.assertEqual(impersonatable.count(), 3)
with self.temporarily_switch_to_user(user):
resp = self.post_impersonate(
Loading
Loading full blame...