class UserTests(TestCase): def setUp(self): self._flush_redis() self.user = User(username='******') self.user.save() def tearDown(self): self._flush_redis() def _flush_redis(self): keys = settings.REDIS_CONN.keys('test_*') if keys: settings.REDIS_CONN.delete(*keys) def _add_data_to_redis(self): redis_key = settings.REDIS_KEY_USER_TAGS % self.user.id settings.REDIS_CONN.zincrby(redis_key, 'books', 1) settings.REDIS_CONN.zincrby(redis_key, 'books', 1) settings.REDIS_CONN.zincrby(redis_key, 'tax', 1) def test_01_empty_user_tags_list(self): self.assertEqual(list(self.user.get_user_tags_order()), []) def test_02_get_user_tags_list(self): self._add_data_to_redis() self.assertEqual(list(self.user.get_user_tags_order()), ['books', 'tax'])
def test_was_created_recently_with_hours_before_article(self): user = User(name='user2', password='******') user.save() hours_before_article = ArticlePost( author=user, title='test2', body='test2', created=timezone.now() - datetime.timedelta(hours=3) ) self.assertIs(hours_before_article.was_created_recently(), False)
def test_was_created_recently_with_seconds_before_article(self): user = User(name='user1', password='******') user.save() seconds_before_article = ArticlePost( author = user, title = 'test1', body = 'test1', created=timezone.now() - datetime.timedelta(seconds=45) ) self.assertIs(seconds_before_article.was_created_recently(),True)
def test_was_created_recently_with_days_before_article(self): # 若文章创建时间为几天前,返回 False user = User(name='user3', password='******') user.save() months_before_article = ArticlePost( author=user, title='test3', body='test3', created=timezone.now() - datetime.timedelta(days=5) ) self.assertIs(months_before_article.was_created_recently(), False)
def test_was_created_recently_with_future_article(self): user = User(name='user', password='******') user.save() future_article = ArticlePost( author=user, title = 'test', body='test', created=timezone.now()+datetime.timedelta(days=30) ) self.assertIs(future_article.was_created_recently(), False)
def setUp(self): self.user = User(username="******", first_name="First", last_name="Last") self.user.set_password("testPassword") self.user.save() self.course = Course.objects.create(name="test course", code="123456", url="Course-Url") self.today = timezone.now() self.tomorrow = self.today + timedelta(days=1) self.two_days_from_now = self.today + timedelta(days=2) self.course_instance = CourseInstance.objects.create( instance_name="Fall 2011 day 1", starting_time=self.today, ending_time=self.tomorrow, course=self.course, url="T-00.1000_d1") self.course_module = CourseModule.objects.create( name="test module", url="test-module", points_to_pass=15, course_instance=self.course_instance, opening_time=self.today, closing_time=self.tomorrow) self.learning_object_category = LearningObjectCategory.objects.create( name="test category", course_instance=self.course_instance, points_to_pass=5) self.exercise_with_attachment = ExerciseWithAttachment.objects.create( name="test exercise 3", course_module=self.course_module, category=self.learning_object_category, max_points=50, points_to_pass=50, max_submissions=0, files_to_submit="test1.txt|test2.txt|img.png", content="test_instructions") self.deadline_rule_deviation = DeadlineRuleDeviation.objects.create( exercise=self.exercise_with_attachment, submitter=self.user.userprofile, extra_minutes=1440 # One day )
def setUp(self): self.user = User(username="******", first_name="First", last_name="Last") self.user.set_password("testPassword") self.user.save() self.course = Course.objects.create( name="test course", code="123456", url="Course-Url" ) self.today = timezone.now() self.tomorrow = self.today + timedelta(days=1) self.two_days_from_now = self.today + timedelta(days=2) self.course_instance = CourseInstance.objects.create( instance_name="Fall 2011 day 1", starting_time=self.today, ending_time=self.tomorrow, course=self.course, url="T-00.1000_d1" ) self.course_module = CourseModule.objects.create( name="test module", url="test-module", points_to_pass=15, course_instance=self.course_instance, opening_time=self.today, closing_time=self.tomorrow ) self.learning_object_category = LearningObjectCategory.objects.create( name="test category", course_instance=self.course_instance, points_to_pass=5 ) self.exercise_with_attachment = ExerciseWithAttachment.objects.create( name="test exercise 3", course_module=self.course_module, category=self.learning_object_category, max_points=50, points_to_pass=50, max_submissions=0, files_to_submit="test1.txt|test2.txt|img.png", content="test_instructions" ) self.deadline_rule_deviation = DeadlineRuleDeviation.objects.create( exercise=self.exercise_with_attachment, submitter=self.user.userprofile, extra_minutes=1440 # One day )
def register_view(request): if 'u_id' in request.session: return HttpResponseRedirect('dashboard') else: if request.method == 'POST': if 'rusername' in request.POST and 'rpassword' in request.POST and 'rname' in request.POST and 'rcpassword' in request.POST: username = request.POST['rusername'] password = request.POST['rpassword'] name = request.POST['rname'] cpassword = request.POST['rcpassword'] if cpassword == password: userinstance = User() userinstance.fname = name userinstance.user_name = username userinstance.password = password userinstance.save() messages.success(request, 'You are registered. Now login.') return HttpResponseRedirect('login') else: messages.error(request, "Passwords don't match!") return HttpResponseRedirect('register') else: return render(request, 'userprofile/register.html') else: username = "" loggedin = "1" if 'u_id' in request.session: loggedin = "1" username = request.session['u_id'] else: loggedin = "0" ctx = { 'username': username, 'loggedin': loggedin, } return render(request, 'userprofile/register.html', ctx)
class UserTests(TestCase): def setUp(self): self._flush_redis() self.user = User(username='******') self.user.save() def tearDown(self): self._flush_redis() def _flush_redis(self): keys = settings.REDIS_CONN.keys('test_*') if keys: settings.REDIS_CONN.delete(*keys) def _add_data_to_redis(self): redis_key = settings.REDIS_KEY_USER_TAGS % self.user.id settings.REDIS_CONN.zincrby(redis_key, 1, 'books') settings.REDIS_CONN.zincrby(redis_key, 1, 'books') settings.REDIS_CONN.zincrby(redis_key, 1, 'books') settings.REDIS_CONN.zincrby(redis_key, 1, 'tax') settings.REDIS_CONN.zincrby(redis_key, 1, 'tax') settings.REDIS_CONN.zincrby(redis_key, 1, 'home') def test_01_empty_user_tags_list(self): self.assertEqual(list(self.user.get_tags_order_by_frequency()), []) def test_02_get_user_tags_list(self): self._add_data_to_redis() self.assertEqual(list(self.user.get_tags_order_by_frequency()), ['books', 'tax', 'home']) def test_03_ordered_tags(self): self.user.tags = ['tax', 'new', 'books'] # tag 'home' is old. self.user.save() self._add_data_to_redis() self.assertEqual(list(self.user.get_ordered_tags()), ['books', 'tax', 'new'])
def setUp(self): self.user = User(username="******") self.user.set_password("testPassword") self.user.save() self.assistant = User(username="******") self.assistant.set_password("testPassword") self.assistant.save() self.link_service = LinkService.objects.create( url="http://www.external-service.com", menu_label="External Service") self.disabled_link_service = LinkService.objects.create( url="http://www.disabled-external-service.com", menu_label="Disabled External Service", enabled=False) self.lti_service = LTIService.objects.create( url="http://www.lti-service.com", menu_label="LTI Service", menu_icon_class="star", access_settings=LTIService.LTI_ACCESS.PUBLIC_API_NO, consumer_key="123456789", consumer_secret="987654321") self.course = Course.objects.create(name="test course", code="123456", url="Course-Url") self.today = timezone.now() self.tomorrow = self.today + timedelta(days=1) self.course_instance = CourseInstance.objects.create( instance_name="Fall 2011", starting_time=self.today, ending_time=self.tomorrow, course=self.course, url="T-00.1000_2011") self.course_instance.enroll_student(self.user) self.course_instance.assistants.add(self.assistant.userprofile) self.menu_item1 = MenuItem.objects.create( service=self.link_service, course_instance=self.course_instance, access=MenuItem.ACCESS.STUDENT, menu_label="Overriden Label", menu_icon_class="star") self.menu_item2 = MenuItem.objects.create( service=self.link_service, course_instance=self.course_instance, access=MenuItem.ACCESS.STUDENT, enabled=False) self.menu_item3 = MenuItem.objects.create( service=self.disabled_link_service, course_instance=self.course_instance, access=MenuItem.ACCESS.STUDENT) self.menu_item4 = MenuItem.objects.create( service=self.lti_service, course_instance=self.course_instance, access=MenuItem.ACCESS.STUDENT) self.menu_item5 = MenuItem.objects.create( service=self.lti_service, course_instance=self.course_instance, access=MenuItem.ACCESS.ASSISTANT)
class ExternalServicesTest(TestCase): def setUp(self): self.user = User(username="******") self.user.set_password("testPassword") self.user.save() self.assistant = User(username="******") self.assistant.set_password("testPassword") self.assistant.save() self.link_service = LinkService.objects.create( url="http://www.external-service.com", menu_label="External Service") self.disabled_link_service = LinkService.objects.create( url="http://www.disabled-external-service.com", menu_label="Disabled External Service", enabled=False) self.lti_service = LTIService.objects.create( url="http://www.lti-service.com", menu_label="LTI Service", menu_icon_class="star", access_settings=LTIService.LTI_ACCESS.PUBLIC_API_NO, consumer_key="123456789", consumer_secret="987654321") self.course = Course.objects.create(name="test course", code="123456", url="Course-Url") self.today = timezone.now() self.tomorrow = self.today + timedelta(days=1) self.course_instance = CourseInstance.objects.create( instance_name="Fall 2011", starting_time=self.today, ending_time=self.tomorrow, course=self.course, url="T-00.1000_2011") self.course_instance.enroll_student(self.user) self.course_instance.assistants.add(self.assistant.userprofile) self.menu_item1 = MenuItem.objects.create( service=self.link_service, course_instance=self.course_instance, access=MenuItem.ACCESS.STUDENT, menu_label="Overriden Label", menu_icon_class="star") self.menu_item2 = MenuItem.objects.create( service=self.link_service, course_instance=self.course_instance, access=MenuItem.ACCESS.STUDENT, enabled=False) self.menu_item3 = MenuItem.objects.create( service=self.disabled_link_service, course_instance=self.course_instance, access=MenuItem.ACCESS.STUDENT) self.menu_item4 = MenuItem.objects.create( service=self.lti_service, course_instance=self.course_instance, access=MenuItem.ACCESS.STUDENT) self.menu_item5 = MenuItem.objects.create( service=self.lti_service, course_instance=self.course_instance, access=MenuItem.ACCESS.ASSISTANT) def test_menuitem_label(self): self.assertEqual("Overriden Label", self.menu_item1.label) self.assertEqual("External Service", self.menu_item2.label) self.assertEqual("Disabled External Service", self.menu_item3.label) self.assertEqual("LTI Service", self.menu_item4.label) self.assertEqual("LTI Service", self.menu_item5.label) def test_menuitem_icon_class(self): self.assertEqual("star", self.menu_item1.icon_class) self.assertEqual("globe", self.menu_item2.icon_class) self.assertEqual("globe", self.menu_item3.icon_class) self.assertEqual("star", self.menu_item4.icon_class) self.assertEqual("star", self.menu_item5.icon_class) def test_menuitem_url(self): self.assertEqual("http://www.external-service.com", self.menu_item1.url) self.assertEqual("http://www.external-service.com", self.menu_item2.url) self.assertEqual("http://www.disabled-external-service.com", self.menu_item3.url) self.assertEqual("/Course-Url/T-00.1000_2011/lti-login/4/", self.menu_item4.url) self.assertEqual("/Course-Url/T-00.1000_2011/lti-login/5/", self.menu_item5.url) def test_view(self): url = self.menu_item4.url response = self.client.get(url) self.assertEqual(response.status_code, 302) self.client.login(username="******", password="******") response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertTrue("oauth_signature" in str(response.content)) url = self.menu_item5.url response = self.client.get(url) self.assertEqual(response.status_code, 403) self.client.login(username="******", password="******") response = self.client.get(url) self.assertEqual(response.status_code, 200) self.menu_item5.access = MenuItem.ACCESS.TEACHER self.menu_item5.save() response = self.client.get(url) self.assertEqual(response.status_code, 403) self.course.teachers.add(self.assistant.userprofile) response = self.client.get(url) self.assertEqual(response.status_code, 200) def test_cached(self): menu = CachedCourseMenu(self.course_instance) self.assertEqual(len(menu.student_link_groups()), 1) self.assertEqual(len(menu.student_link_groups()[0]['items']), 4) self.assertEqual(len(menu.staff_link_groups()), 1) self.assertEqual(len(menu.staff_link_groups()[0]['items']), 1)
def setUp(self): self.user = User(username="******") self.user.set_password("testPassword") self.user.save() self.assistant = User(username="******") self.assistant.set_password("testPassword") self.assistant.save() self.link_service = LinkService.objects.create( url="http://www.external-service.com", menu_label="External Service" ) self.disabled_link_service = LinkService.objects.create( url="http://www.disabled-external-service.com", menu_label="Disabled External Service", enabled=False ) self.lti_service = LTIService.objects.create( url="http://www.lti-service.com", menu_label="LTI Service", menu_icon_class="star", consumer_key="123456789", consumer_secret="987654321" ) self.course = Course.objects.create( name="test course", code="123456", url="Course-Url" ) self.today = timezone.now() self.tomorrow = self.today + timedelta(days=1) self.course_instance = CourseInstance.objects.create( instance_name="Fall 2011", starting_time=self.today, ending_time=self.tomorrow, course=self.course, url="T-00.1000_2011" ) self.course_instance.enroll_student(self.user) self.course_instance.assistants.add(self.assistant.userprofile) self.menu_item1 = MenuItem.objects.create( service=self.link_service, course_instance=self.course_instance, access=MenuItem.ACCESS.STUDENT, menu_label="Overriden Label", menu_icon_class="star" ) self.menu_item2 = MenuItem.objects.create( service=self.link_service, course_instance=self.course_instance, access=MenuItem.ACCESS.STUDENT, enabled=False ) self.menu_item3 = MenuItem.objects.create( service=self.disabled_link_service, course_instance=self.course_instance, access=MenuItem.ACCESS.STUDENT ) self.menu_item4 = MenuItem.objects.create( service=self.lti_service, course_instance=self.course_instance, access=MenuItem.ACCESS.STUDENT ) self.menu_item5 = MenuItem.objects.create( service=self.lti_service, course_instance=self.course_instance, access=MenuItem.ACCESS.ASSISTANT )
class ExternalServicesTest(TestCase): def setUp(self): self.user = User(username="******") self.user.set_password("testPassword") self.user.save() self.assistant = User(username="******") self.assistant.set_password("testPassword") self.assistant.save() self.link_service = LinkService.objects.create( url="http://www.external-service.com", menu_label="External Service" ) self.disabled_link_service = LinkService.objects.create( url="http://www.disabled-external-service.com", menu_label="Disabled External Service", enabled=False ) self.lti_service = LTIService.objects.create( url="http://www.lti-service.com", menu_label="LTI Service", menu_icon_class="star", consumer_key="123456789", consumer_secret="987654321" ) self.course = Course.objects.create( name="test course", code="123456", url="Course-Url" ) self.today = timezone.now() self.tomorrow = self.today + timedelta(days=1) self.course_instance = CourseInstance.objects.create( instance_name="Fall 2011", starting_time=self.today, ending_time=self.tomorrow, course=self.course, url="T-00.1000_2011" ) self.course_instance.enroll_student(self.user) self.course_instance.assistants.add(self.assistant.userprofile) self.menu_item1 = MenuItem.objects.create( service=self.link_service, course_instance=self.course_instance, access=MenuItem.ACCESS.STUDENT, menu_label="Overriden Label", menu_icon_class="star" ) self.menu_item2 = MenuItem.objects.create( service=self.link_service, course_instance=self.course_instance, access=MenuItem.ACCESS.STUDENT, enabled=False ) self.menu_item3 = MenuItem.objects.create( service=self.disabled_link_service, course_instance=self.course_instance, access=MenuItem.ACCESS.STUDENT ) self.menu_item4 = MenuItem.objects.create( service=self.lti_service, course_instance=self.course_instance, access=MenuItem.ACCESS.STUDENT ) self.menu_item5 = MenuItem.objects.create( service=self.lti_service, course_instance=self.course_instance, access=MenuItem.ACCESS.ASSISTANT ) def test_menuitem_label(self): self.assertEqual("Overriden Label", self.menu_item1.label) self.assertEqual("External Service", self.menu_item2.label) self.assertEqual("Disabled External Service", self.menu_item3.label) self.assertEqual("LTI Service", self.menu_item4.label) self.assertEqual("LTI Service", self.menu_item5.label) def test_menuitem_icon_class(self): self.assertEqual("star", self.menu_item1.icon_class) self.assertEqual("globe", self.menu_item2.icon_class) self.assertEqual("globe", self.menu_item3.icon_class) self.assertEqual("star", self.menu_item4.icon_class) self.assertEqual("star", self.menu_item5.icon_class) def test_menuitem_url(self): self.assertEqual("http://www.external-service.com", self.menu_item1.url) self.assertEqual("http://www.external-service.com", self.menu_item2.url) self.assertEqual("http://www.disabled-external-service.com", self.menu_item3.url) self.assertEqual("/Course-Url/T-00.1000_2011/lti-login/4/", self.menu_item4.url) self.assertEqual("/Course-Url/T-00.1000_2011/lti-login/5/", self.menu_item5.url) def test_view(self): url = self.menu_item4.url response = self.client.get(url) self.assertEqual(response.status_code, 302) self.client.login(username="******", password="******") response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertTrue("oauth_signature" in str(response.content)) url = self.menu_item5.url response = self.client.get(url) self.assertEqual(response.status_code, 403) self.client.login(username="******", password="******") response = self.client.get(url) self.assertEqual(response.status_code, 200) self.menu_item5.access = MenuItem.ACCESS.TEACHER self.menu_item5.save() response = self.client.get(url) self.assertEqual(response.status_code, 403) self.course.teachers.add(self.assistant.userprofile) response = self.client.get(url) self.assertEqual(response.status_code, 200) def test_cached(self): menu = CachedCourseMenu(self.course_instance) self.assertEqual(len(menu.student_link_groups()), 1) self.assertEqual(len(menu.student_link_groups()[0]['items']), 4) self.assertEqual(len(menu.staff_link_groups()), 1) self.assertEqual(len(menu.staff_link_groups()[0]['items']), 1)
def setUp(self): self._flush_redis() self.user = User(username='******') self.user.save()
class ExternalServicesTest(TestCase): def setUp(self): self.user = User(username="******") self.user.set_password("testPassword") self.user.save() self.link_service = LinkService.objects.create( url="http://www.external-service.com", menu_label="External Service") self.disabled_link_service = LinkService.objects.create( url="http://www.disabled-external-service.com", menu_label="Disabled External Service", enabled=False) self.lti_service = LTIService.objects.create( url="http://www.lti-service.com", menu_label="LTI Service", menu_icon_class="star", consumer_key="123456789", consumer_secret="987654321") self.course = Course.objects.create(name="test course", code="123456", url="Course-Url") self.today = timezone.now() self.tomorrow = self.today + timedelta(days=1) self.course_instance = CourseInstance.objects.create( instance_name="Fall 2011", starting_time=self.today, ending_time=self.tomorrow, course=self.course, url="T-00.1000_2011") self.menu_item1 = MenuItem.objects.create( service=self.link_service, course_instance=self.course_instance, access=MenuItem.ACCESS_STUDENT, menu_label="Overriden Label", menu_icon_class="star") self.menu_item2 = MenuItem.objects.create( service=self.link_service, course_instance=self.course_instance, access=MenuItem.ACCESS_STUDENT, enabled=False) self.menu_item3 = MenuItem.objects.create( service=self.disabled_link_service, course_instance=self.course_instance, access=MenuItem.ACCESS_STUDENT) self.menu_item4 = MenuItem.objects.create( service=self.disabled_link_service, course_instance=self.course_instance, access=MenuItem.ACCESS_STUDENT, enabled=False) self.menu_item5 = MenuItem.objects.create( service=self.lti_service, course_instance=self.course_instance, access=MenuItem.ACCESS_STUDENT) def test_linkservice_string(self): self.assertEqual("External Service: http://www.external-service.com", str(self.link_service)) self.assertEqual( "[Disabled] Disabled External Service: http://www.disabled-external-service.com", str(self.disabled_link_service)) self.assertEqual("LTI Service: http://www.lti-service.com", str(self.lti_service)) def test_menuitem_label(self): self.assertEqual("Overriden Label", self.menu_item1.label) self.assertEqual("External Service", self.menu_item2.label) self.assertEqual("Disabled External Service", self.menu_item3.label) self.assertEqual("Disabled External Service", self.menu_item4.label) self.assertEqual("LTI Service", self.menu_item5.label) def test_menuitem_icon_class(self): self.assertEqual("star", self.menu_item1.icon_class) self.assertEqual("globe", self.menu_item2.icon_class) self.assertEqual("globe", self.menu_item3.icon_class) self.assertEqual("globe", self.menu_item4.icon_class) self.assertEqual("star", self.menu_item5.icon_class) def test_menuitem_url(self): self.assertEqual("http://www.external-service.com", self.menu_item1.url) self.assertEqual("http://www.external-service.com", self.menu_item2.url) self.assertEqual("http://www.disabled-external-service.com", self.menu_item3.url) self.assertEqual("http://www.disabled-external-service.com", self.menu_item4.url) self.assertEqual("/Course-Url/T-00.1000_2011/lti-login/5/", self.menu_item5.url) def test_menuitem_string(self): self.assertEqual("123456 Fall 2011: ", str(self.menu_item1)) self.assertEqual("[Disabled] 123456 Fall 2011: ", str(self.menu_item2)) self.assertEqual("[Disabled] 123456 Fall 2011: ", str(self.menu_item3)) self.assertEqual("[Disabled] 123456 Fall 2011: ", str(self.menu_item4)) self.assertEqual("123456 Fall 2011: ", str(self.menu_item5)) def test_view(self): url = self.menu_item5.url response = self.client.get(url) self.assertEqual(response.status_code, 302) self.client.login(username="******", password="******") response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertTrue("oauth_signature" in str(response.content)) self.assertEqual( tags.external_menu_entries(self.course_instance.id).count(), 2) self.assertEqual( tags.external_staff_menu_entries(self.course_instance.id, True, True).count(), 0) self.menu_item5.access = MenuItem.ACCESS_ASSISTANT self.menu_item5.save() self.assertEqual( tags.external_menu_entries(self.course_instance.id).count(), 1) self.assertEqual( tags.external_staff_menu_entries(self.course_instance.id, True, True).count(), 1) self.assertEqual( tags.external_staff_menu_entries(self.course_instance.id, True, False).count(), 1) self.assertEqual( tags.external_staff_menu_entries(self.course_instance.id, False, False).count(), 0) response = self.client.get(url) self.assertEqual(response.status_code, 403) self.course_instance.assistants.add(self.user.userprofile) response = self.client.get(url) self.assertEqual(response.status_code, 200) self.menu_item5.access = MenuItem.ACCESS_TEACHER self.menu_item5.save() self.assertEqual( tags.external_menu_entries(self.course_instance.id).count(), 1) self.assertEqual( tags.external_staff_menu_entries(self.course_instance.id, True, True).count(), 1) self.assertEqual( tags.external_staff_menu_entries(self.course_instance.id, True, False).count(), 0) self.assertEqual( tags.external_staff_menu_entries(self.course_instance.id, False, False).count(), 0) response = self.client.get(url) self.assertEqual(response.status_code, 403) self.course.teachers.add(self.user.userprofile) response = self.client.get(url) self.assertEqual(response.status_code, 200) self.client.logout() response = self.client.get(url) self.assertEqual(response.status_code, 302)
class DeviationsTest(TestCase): def setUp(self): self.teacher = User(username="******", is_staff=True) self.teacher.set_password("staffPassword") self.teacher.save() self.user = User(username="******", first_name="First", last_name="Last") self.user.set_password("testPassword") self.user.save() self.user_2 = User(username="******", first_name="First2", last_name="Last2") self.user_2.set_password("testPassword2") self.user_2.save() self.course = Course.objects.create( name="test course", code="123456", url="Course-Url", ) self.today = timezone.now() self.tomorrow = self.today + timedelta(days=1) self.two_days_from_now = self.today + timedelta(days=2) self.course_instance = CourseInstance.objects.create( instance_name="Fall 2011 day 1", starting_time=self.today, ending_time=self.tomorrow, course=self.course, url="T-00.1000_d1", ) self.course_module = CourseModule.objects.create( name="test module", url="test-module", points_to_pass=15, course_instance=self.course_instance, opening_time=self.today, closing_time=self.tomorrow, ) self.learning_object_category = LearningObjectCategory.objects.create( name="test category", course_instance=self.course_instance, points_to_pass=5, ) self.exercise_with_attachment = ExerciseWithAttachment.objects.create( name="test exercise 3", course_module=self.course_module, category=self.learning_object_category, max_points=50, points_to_pass=50, max_submissions=1, files_to_submit="test1.txt|test2.txt|img.png", content="test_instructions", ) self.exercise_with_attachment_2 = ExerciseWithAttachment.objects.create( name="test exercise 4", course_module=self.course_module, category=self.learning_object_category, max_points=50, points_to_pass=50, max_submissions=1, files_to_submit="test1.txt|test2.txt|img.png", content="test_instructions", ) self.deadline_rule_deviation_u1_e1 = DeadlineRuleDeviation.objects.create( exercise=self.exercise_with_attachment, submitter=self.user.userprofile, granter=self.teacher.userprofile, extra_minutes=1440, # One day ) self.deadline_rule_deviation_u1_e2 = DeadlineRuleDeviation.objects.create( exercise=self.exercise_with_attachment_2, submitter=self.user.userprofile, granter=self.teacher.userprofile, extra_minutes=2880, # Two days ) self.deadline_rule_deviation_u2_e1 = DeadlineRuleDeviation.objects.create( exercise=self.exercise_with_attachment, submitter=self.user_2.userprofile, granter=self.teacher.userprofile, extra_minutes=4320, # Three days ) def test_deadline_rule_deviation_extra_time(self): self.assertEqual(timedelta(days=1), self.deadline_rule_deviation_u1_e1.get_extra_time()) def test_deadline_rule_deviation_new_deadline(self): self.assertEqual(self.two_days_from_now, self.deadline_rule_deviation_u1_e1.get_new_deadline()) def test_deadline_rule_deviation_new_deadline_with_normal_deadline(self): self.assertEqual(self.tomorrow, self.deadline_rule_deviation_u1_e1.get_new_deadline(self.today)) def test_deadline_rule_deviation_normal_deadline(self): self.assertEqual(self.tomorrow, self.deadline_rule_deviation_u1_e1.get_normal_deadline()) def test_get_max_deviations(self): # Test that the get_max_deviations method returns the correct deviation # when one exercise is passed into the method. deviation = DeadlineRuleDeviation.objects.get_max_deviation( self.user.userprofile, self.exercise_with_attachment, ) self.assertIsNotNone(deviation) self.assertEqual(deviation.exercise.id, self.exercise_with_attachment.id) self.assertEqual(deviation.extra_minutes, 1440) deviation = DeadlineRuleDeviation.objects.get_max_deviation( self.user_2.userprofile, self.exercise_with_attachment, ) self.assertIsNotNone(deviation) self.assertEqual(deviation.exercise.id, self.exercise_with_attachment.id) self.assertEqual(deviation.extra_minutes, 4320) def test_get_max_deviations_multiple(self): # Test that the get_max_deviations method returns the correct deviation # when multiple exercises are passed into the method. deviations = DeadlineRuleDeviation.objects.get_max_deviations( self.user.userprofile, [self.exercise_with_attachment, self.exercise_with_attachment_2], ) counter = 0 for deviation in deviations: counter += 1 if deviation.exercise.id == self.exercise_with_attachment.id: self.assertEqual(deviation.extra_minutes, 1440) elif deviation.exercise.id == self.exercise_with_attachment_2.id: self.assertEqual(deviation.extra_minutes, 2880) else: raise self.failureException('Unexpected exercise returned') self.assertEqual(counter, 2) deviations = DeadlineRuleDeviation.objects.get_max_deviations( self.user_2.userprofile, [self.exercise_with_attachment, self.exercise_with_attachment_2], ) counter = 0 for deviation in deviations: counter += 1 if deviation.exercise.id == self.exercise_with_attachment.id: self.assertEqual(deviation.extra_minutes, 4320) else: raise self.failureException('Unexpected exercise returned') self.assertEqual(counter, 1) def test_get_max_deviations_group(self): # Test that the get_max_deviations method returns the correct deviation # when there is a group submission with both users. In this case, # user 2's deviation should also be returned for user 1. submission = Submission.objects.create( exercise=self.exercise_with_attachment, status=Submission.STATUS.READY, ) submission.submitters.add(self.user.userprofile, self.user_2.userprofile) deviation = DeadlineRuleDeviation.objects.get_max_deviation( self.user.userprofile, self.exercise_with_attachment, ) self.assertIsNotNone(deviation) self.assertEqual(deviation.exercise.id, self.exercise_with_attachment.id) self.assertEqual(deviation.extra_minutes, 4320) deviation = DeadlineRuleDeviation.objects.get_max_deviation( self.user_2.userprofile, self.exercise_with_attachment, ) self.assertIsNotNone(deviation) self.assertEqual(deviation.exercise.id, self.exercise_with_attachment.id) self.assertEqual(deviation.extra_minutes, 4320)
class ExternalServicesTest(TestCase): def setUp(self): self.user = User(username="******") self.user.set_password("testPassword") self.user.save() self.link_service = LinkService.objects.create( url="http://www.external-service.com", menu_label="External Service" ) self.disabled_link_service = LinkService.objects.create( url="http://www.disabled-external-service.com", menu_label="Disabled External Service", enabled=False ) self.lti_service = LTIService.objects.create( url="http://www.lti-service.com", menu_label="LTI Service", menu_icon_class="star", consumer_key="123456789", consumer_secret="987654321" ) self.course = Course.objects.create( name="test course", code="123456", url="Course-Url" ) self.today = timezone.now() self.tomorrow = self.today + timedelta(days=1) self.course_instance = CourseInstance.objects.create( instance_name="Fall 2011", starting_time=self.today, ending_time=self.tomorrow, course=self.course, url="T-00.1000_2011" ) self.menu_item1 = MenuItem.objects.create( service=self.link_service, course_instance=self.course_instance, access=MenuItem.ACCESS_STUDENT, menu_label="Overriden Label", menu_icon_class="star" ) self.menu_item2 = MenuItem.objects.create( service=self.link_service, course_instance=self.course_instance, access=MenuItem.ACCESS_STUDENT, enabled=False ) self.menu_item3 = MenuItem.objects.create( service=self.disabled_link_service, course_instance=self.course_instance, access=MenuItem.ACCESS_STUDENT ) self.menu_item4 = MenuItem.objects.create( service=self.disabled_link_service, course_instance=self.course_instance, access=MenuItem.ACCESS_STUDENT, enabled=False ) self.menu_item5 = MenuItem.objects.create( service=self.lti_service, course_instance=self.course_instance, access=MenuItem.ACCESS_STUDENT ) def test_linkservice_string(self): self.assertEqual("External Service: http://www.external-service.com", str(self.link_service)) self.assertEqual("[Disabled] Disabled External Service: http://www.disabled-external-service.com", str(self.disabled_link_service)) self.assertEqual("LTI Service: http://www.lti-service.com", str(self.lti_service)) def test_menuitem_label(self): self.assertEqual("Overriden Label", self.menu_item1.label) self.assertEqual("External Service", self.menu_item2.label) self.assertEqual("Disabled External Service", self.menu_item3.label) self.assertEqual("Disabled External Service", self.menu_item4.label) self.assertEqual("LTI Service", self.menu_item5.label) def test_menuitem_icon_class(self): self.assertEqual("star", self.menu_item1.icon_class) self.assertEqual("globe", self.menu_item2.icon_class) self.assertEqual("globe", self.menu_item3.icon_class) self.assertEqual("globe", self.menu_item4.icon_class) self.assertEqual("star", self.menu_item5.icon_class) def test_menuitem_url(self): self.assertEqual("http://www.external-service.com", self.menu_item1.url) self.assertEqual("http://www.external-service.com", self.menu_item2.url) self.assertEqual("http://www.disabled-external-service.com", self.menu_item3.url) self.assertEqual("http://www.disabled-external-service.com", self.menu_item4.url) self.assertEqual("/Course-Url/T-00.1000_2011/lti-login/5/", self.menu_item5.url) def test_menuitem_string(self): self.assertEqual("123456 Fall 2011: ", str(self.menu_item1)) self.assertEqual("[Disabled] 123456 Fall 2011: ", str(self.menu_item2)) self.assertEqual("[Disabled] 123456 Fall 2011: ", str(self.menu_item3)) self.assertEqual("[Disabled] 123456 Fall 2011: ", str(self.menu_item4)) self.assertEqual("123456 Fall 2011: ", str(self.menu_item5)) def test_view(self): url = self.menu_item5.url response = self.client.get(url) self.assertEqual(response.status_code, 302) self.client.login(username="******", password="******") response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertTrue("oauth_signature" in str(response.content)) self.assertEqual(tags.external_menu_entries(self.course_instance.id).count(), 2) self.assertEqual(tags.external_staff_menu_entries(self.course_instance.id, True, True).count(), 0) self.menu_item5.access = MenuItem.ACCESS_ASSISTANT self.menu_item5.save() self.assertEqual(tags.external_menu_entries(self.course_instance.id).count(), 1) self.assertEqual(tags.external_staff_menu_entries(self.course_instance.id, True, True).count(), 1) self.assertEqual(tags.external_staff_menu_entries(self.course_instance.id, True, False).count(), 1) self.assertEqual(tags.external_staff_menu_entries(self.course_instance.id, False, False).count(), 0) response = self.client.get(url) self.assertEqual(response.status_code, 403) self.course_instance.assistants.add(self.user.userprofile) response = self.client.get(url) self.assertEqual(response.status_code, 200) self.menu_item5.access = MenuItem.ACCESS_TEACHER self.menu_item5.save() self.assertEqual(tags.external_menu_entries(self.course_instance.id).count(), 1) self.assertEqual(tags.external_staff_menu_entries(self.course_instance.id, True, True).count(), 1) self.assertEqual(tags.external_staff_menu_entries(self.course_instance.id, True, False).count(), 0) self.assertEqual(tags.external_staff_menu_entries(self.course_instance.id, False, False).count(), 0) response = self.client.get(url) self.assertEqual(response.status_code, 403) self.course.teachers.add(self.user.userprofile) response = self.client.get(url) self.assertEqual(response.status_code, 200) self.client.logout() response = self.client.get(url) self.assertEqual(response.status_code, 302)