def test_labgroup_filter_by_course(self): """ Tests that labgroups can be filtered by the courses query parameter. """ # add course to database course = Course(name='course name') course.save() # add labgroups to database labgroup = LabGroup(course=course, instructor=self.instructor, group_name='test name 1', term=get_current_term(), enroll_key='test key 1') labgroup.save() LabGroup(course=self.course, instructor=self.instructor, group_name='test name 2', term=get_current_term(), enroll_key='test key 2').save() # request response = self.client.get(reverse(self.view_name), {'course': course.id}) response_body = json.loads(response.content.decode('utf-8')) # test response self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(len(response_body['labgroups']), 1) self.assertEqual(response_body['labgroups'][0]['pk'], labgroup.id) self.assertEqual(response_body['labgroups'][0]['course'], labgroup.course.id) self.assertEqual(response_body['labgroups'][0]['instructor'], labgroup.instructor.id) self.assertEqual(response_body['labgroups'][0]['group_name'], labgroup.group_name) self.assertEqual(response_body['labgroups'][0]['term'], labgroup.term) self.assertEqual(response_body['labgroups'][0]['enroll_key'], labgroup.enroll_key)
def create(self, request, *args, **kwargs): """ Override create method to create access codes as well. :param request: POST with 'name', 'description', 'students', 'tas, 'instructors'. Last three fields optional. :return: 201 response """ data = request.data course = Course(name=data['name'], description=data['description']) course.save() # convert to dict to deal with issues of managing the lists passed in data = dict(data) students, tas, instructors = [], [], [] # set the students, tas, and instructors if 'students' in data: students = User.objects.filter(id__in=data['students']) if 'tas' in data: tas = User.objects.filter(id__in=data['tas']) if 'instructors' in data: instructors = User.objects.filter(id__in=data['instructors']) course.students.set(students) course.tas.set(tas) course.instructors.set(instructors) generate_access_codes(course) course.save() return Response(status=status.HTTP_201_CREATED)
def test_assignment_update_template_course_incompatible(self): """ Tests that an assignment is not created when the assignment template doesn't belong to a shared course. """ # create different course course = Course(name='other course') course.save() # create different template template = AssignmentTemplate(course=course, name='other template') template.save() # request current_time = datetime.now(timezone(settings.TIME_ZONE)) request_body = { 'assignment_template': template.id, 'labgroup': self.group.id, 'open_date': (current_time - timedelta(days=1)).strftime('%Y-%m-%dT%H:%M:%SZ'), 'close_date': (current_time + timedelta(days=1)).strftime('%Y-%m-%dT%H:%M:%SZ'), } response = self.client.put( reverse(self.view_name, args=[self.assignment.id]), request_body) # test response self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) # test database assignment = Assignment.objects.get(id=self.assignment.id) self.assertNotEqual(assignment.assignment_template, template)
def course(db): phase = Phase(title='Lobby', timer=False) phase.save() phase2 = Phase(title='calculate', timer=True) phase2.save() course = Course(title='Math', default_phase=phase) course.save() course.phases.add(phase) course.phases.add(phase2) course.save() return course
def updateCourseData(request): START = 0 LIMIT = 1000 count = 0 url = 'http://oars.cc.iitk.ac.in:4040/Common/CourseListing.asp' page = urllib2.urlopen(url) soup = BeautifulSoup(page.read()) course_codes = soup.find_all('a') for code in course_codes: if count < START: count += 1 continue if count > LIMIT: break course = code.text.strip() course_url = 'http://oars.cc.iitk.ac.in:4040/Utils/CourseInfoPopup2.asp?Course='+course course_page = urllib2.urlopen(course_url) course_soup = BeautifulSoup(course_page.read()) cells = course_soup.find_all('td') title = cells[3].text.strip() instr = cells[5].text.strip() instr_mail = cells[7].text.strip() prereq = cells[9].text.strip() credits_distrb = cells[11].text.strip() credits = int(credits_distrb.split('-')[-1]) dept = cells[13].text.strip() schedule = cells[15].text.strip() instr_notes = cells[17].text.strip() course_model = Course(code=course,title=title,instructor=instr,instr_mail=instr_mail,prereq=prereq,credits=credits,credits_distrb=credits_distrb,dept=dept,schedule=schedule,instr_notes=instr_notes) course_model.save() print course count += 1 print 'count = ', count print 'Last inserted course : %s' % course
def setUp(self): course_a = Course( name=f'Course A', description='', start_date=datetime.date.today(), end_date=datetime.date.today() + datetime.timedelta(days=1), ) course_b = Course( name=f'Course B', description='', start_date=datetime.date.today(), end_date=datetime.date.today() + datetime.timedelta(days=1), ) course_a.save() course_b.save() student_a = Student(first_name="Student A", last_name="aaa", email="*****@*****.**") student_b = Student(first_name="Student B", last_name="bbb", email="*****@*****.**") student_a.save() student_b.save() participants = [ CourseParticipant(course=course_a, student=student_a, completed=True), CourseParticipant(course=course_b, student=student_b, completed=False), CourseParticipant(course=course_a, student=student_b, completed=True), ] CourseParticipant.objects.bulk_create(participants)
class AssignmentEntryStartTest(APITestCase): """ Test cases for starting assignments on AssignmentEntryStartView. """ def setUp(self): # create test user with permissions self.student_username = '******' self.instructor_username = '******' self.password = '******' self.student_user = User.objects.create_user( username=self.student_username, password=self.password) self.instructor_user = User.objects.create_user( username=self.instructor_username, password=self.password) self.client.login(username=self.student_username, password=self.password) # populate test database self.instructor = Instructor(user=self.instructor_user, wwuid='9994141') self.instructor.save() self.course = Course(name='Bounty Hunting 101') self.course.save() self.group = LabGroup(course=self.course, instructor=self.instructor, term=get_current_term(), enroll_key='4', group_name='Group A') self.group.save() self.student = Student(user=self.student_user, labgroup=self.group, wwuid='1111111') self.student.save() self.template = AssignmentTemplate(course=self.course, name='Royalty Kidnapping Section A') self.template.save() self.assignment = Assignment( assignment_template=self.template, labgroup=self.group, open_date=datetime.now(timezone(settings.TIME_ZONE)), close_date=datetime.now(timezone(settings.TIME_ZONE)) + timedelta(days=1)) self.assignment.save() # retrieve the view self.view_name = 'api:assignment-entry-start' def test_assignment_start(self): """ Tests that an assignment entry is properly created. """ # request response = self.client.post( reverse(self.view_name, args=[self.assignment.id])) response_body = json.loads(response.content.decode('utf-8')) # test return code self.assertEqual(response.status_code, status.HTTP_201_CREATED) # test database assignment_entry = AssignmentEntry.objects.get(id=response_body['pk']) self.assertEqual(assignment_entry.student, self.student) self.assertEqual(assignment_entry.assignment, self.assignment) self.assertNotEqual(assignment_entry.start_date, None) self.assertEqual(assignment_entry.submit_date, None) # test response self.assertEqual(response_body['pk'], assignment_entry.id) self.assertEqual(response_body['student'], assignment_entry.student.id) self.assertEqual( datetime.strptime(response_body['start_date'], '%Y-%m-%dT%H:%M:%S.%fZ'), assignment_entry.start_date.replace(tzinfo=None)) self.assertEqual(response_body['submit_date'], None) def test_assignment_start_duplicate(self): """ Tests that nothing happens when the assignment is started more than once. """ # create extra assignment entry self.client.post(reverse(self.view_name, args=[self.assignment.id])) # request response = self.client.post( reverse(self.view_name, args=[self.assignment.id])) # test database self.assertEqual(len(AssignmentEntry.objects.all()), 1) # test response self.assertEqual(response.status_code, status.HTTP_409_CONFLICT) def test_assignment_does_not_exist(self): """ Tests that nothing happens when the assignment does not exist. """ response = self.client.post(reverse(self.view_name, args=[0])) # test database self.assertEqual(len(AssignmentEntry.objects.all()), 0) # test response self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) def test_student_not_assigned_assignment(self): """ Tests that nothing happens when the user tries to start an assignment not assigned to their labgroup. """ # add user to different labgroup new_labgroup = LabGroup(course=self.course, instructor=self.instructor, term=get_current_term(), enroll_key='ABC', group_name='Group B') new_labgroup.save() self.student.labgroup = new_labgroup self.student.save() # request response = self.client.post( reverse(self.view_name, args=[self.assignment.id])) # test database self.assertEqual(len(AssignmentEntry.objects.all()), 0) # test response self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) def test_assignment_not_open(self): """ Tests that nothing happens when the user tries to start an assignment that is not open. """ # modify open dates self.assignment.open_date = datetime.now(timezone( settings.TIME_ZONE)) + timedelta(days=1) self.assignment.close_date = datetime.now(timezone( settings.TIME_ZONE)) + timedelta(days=2) self.assignment.save() # request response = self.client.post( reverse(self.view_name, args=[self.assignment.id])) # test response self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) # test database self.assertEqual(len(AssignmentEntry.objects.all()), 0) def test_assignment_closed(self): """ Tests that nothing happens when the user tries to start an assignment that has been closed. """ # modify open dates self.assignment.open_date = datetime.now(timezone( settings.TIME_ZONE)) - timedelta(days=2) self.assignment.close_date = datetime.now(timezone( settings.TIME_ZONE)) - timedelta(days=1) self.assignment.save() # request response = self.client.post( reverse(self.view_name, args=[self.assignment.id])) # test response self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) # test database self.assertEqual(len(AssignmentEntry.objects.all()), 0)
class AssignmentEntryEntrySubmitTest(APITestCase): """ Test cases for submitting assignment on AssignmentEntrySubmitView. """ def setUp(self): # create test user with permissions self.student_username = '******' self.instructor_username = '******' self.password = '******' self.student_user = User.objects.create_user( username=self.student_username, password=self.password) self.instructor_user = User.objects.create_user( username=self.instructor_username, password=self.password) self.client.login(username=self.student_username, password=self.password) # populate test database self.instructor = Instructor(user=self.instructor_user, wwuid='9994141') self.instructor.save() self.course = Course(name='Bounty Hunting 101') self.course.save() self.group = LabGroup(course=self.course, instructor=self.instructor, term=get_current_term(), enroll_key='4', group_name='Group A') self.group.save() self.student = Student(user=self.student_user, labgroup=self.group, wwuid='1111111') self.student.save() self.template = AssignmentTemplate(course=self.course, name='Royalty Kidnapping Section A') self.template.save() self.assignment = Assignment( assignment_template=self.template, labgroup=self.group, open_date=datetime.now(timezone(settings.TIME_ZONE)), close_date=datetime.now(timezone(settings.TIME_ZONE)) + timedelta(days=1)) self.assignment.save() self.assignment_entry = AssignmentEntry(student=self.student, assignment=self.assignment) self.assignment_entry.save() # retrieve the view self.view_name = 'api:assignment-entry-submit' def test_assignment_entry_submit(self): """ Tests that an assignment is properly submitted. """ # request response = self.client.post( reverse(self.view_name, args=[self.assignment.id])) response_body = json.loads(response.content.decode('utf-8')) # test database assignment_entry = AssignmentEntry.objects.get( id=self.assignment_entry.id) self.assertEqual(assignment_entry.student, self.student) self.assertEqual(assignment_entry.assignment, self.assignment) self.assertNotEqual(assignment_entry.start_date, None) self.assertNotEqual(assignment_entry.submit_date, None) # test response self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response_body['pk'], assignment_entry.id) self.assertEqual(response_body['student'], self.student.id) self.assertEqual(response_body['assignment'], self.assignment.id) self.assertTrue('start_date' in response_body.keys()) self.assertTrue('submit_date' in response_body.keys()) def test_assignment_entry_does_not_exist(self): """ Tests that nothing happens when the assignment does not exist. """ # request response = self.client.post(reverse(self.view_name, args=[0])) # test database assignment_entry = AssignmentEntry.objects.get( id=self.assignment_entry.id) self.assertEqual(assignment_entry.submit_date, None) # test response self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) def test_assignment_entry_not_started(self): """ Tests that nothing happens when the assignment entry has not been started. """ # delete assignment_entry self.assignment_entry.delete() # request response = self.client.post( reverse(self.view_name, args=[self.assignment.id])) # test database self.assertEqual(len(AssignmentEntry.objects.all()), 0) # test response self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) def test_assignment_entry_already_submitted(self): """ Tests that nothing happens when the assignment entry has already been submitted. """ # submit assignment self.assignment_entry.submit_date = datetime.now( timezone(settings.TIME_ZONE)) self.assignment_entry.save() # request response = self.client.post( reverse(self.view_name, args=[self.assignment.id])) # test response self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) # test database self.assertEqual( AssignmentEntry.objects.get( id=self.assignment_entry.id).submit_date, self.assignment_entry.submit_date) def test_assignment_closed(self): """ Tests that nothing happens when the user tries to start an assignment that has been closed. """ # modify open dates self.assignment.open_date = datetime.now(timezone( settings.TIME_ZONE)) - timedelta(days=2) self.assignment.close_date = datetime.now(timezone( settings.TIME_ZONE)) - timedelta(days=1) self.assignment.save() # request response = self.client.post( reverse(self.view_name, args=[self.assignment.id])) # test response self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) # test database self.assertEqual(self.assignment_entry.submit_date, None)
class AssignmentEntryTest(APITestCase): """ Test cases for viewing the user's assignment entry on AssignmentEntryView. """ def setUp(self): # create test user with permissions self.student_username = '******' self.instructor_username = '******' self.password = '******' self.student_user = User.objects.create_user( username=self.student_username, password=self.password) self.instructor_user = User.objects.create_user( username=self.instructor_username, password=self.password) self.client.login(username=self.student_username, password=self.password) # populate test database self.instructor = Instructor(user=self.instructor_user, wwuid='9994141') self.instructor.save() self.course = Course(name='Bounty Hunting 101') self.course.save() self.group = LabGroup(course=self.course, instructor=self.instructor, term=get_current_term(), enroll_key='4', group_name='Group A') self.group.save() self.student = Student(user=self.student_user, labgroup=self.group, wwuid='1111111') self.student.save() self.template = AssignmentTemplate(course=self.course, name='Royalty Kidnapping Section A') self.template.save() self.assignment = Assignment( assignment_template=self.template, labgroup=self.group, open_date=datetime.now(timezone(settings.TIME_ZONE)), close_date=datetime.now(timezone(settings.TIME_ZONE)) + timedelta(days=1)) self.assignment.save() # retrieve the view self.view_name = 'api:assignment-entry' def test_view_assignment_entry(self): """ Tests that an assignment entry is properly retrieved. """ # create assignment entry assignment_entry = AssignmentEntry(student=self.student, assignment=self.assignment) assignment_entry.save() # request response = self.client.get( reverse(self.view_name, args=[self.assignment.id])) response_body = json.loads(response.content.decode('utf-8')) # test response self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response_body['pk'], assignment_entry.id) self.assertEqual(response_body['student'], self.student.id) self.assertEqual(response_body['assignment'], self.assignment.id) self.assertTrue('start_date' in response_body.keys()) self.assertTrue('submit_date' in response_body.keys()) def test_view_assignment_entry_not_started(self): """ Tests that an assignment entry is not retrieved if it has not been started. """ # request response = self.client.get( reverse(self.view_name, args=[self.assignment.id])) # test response self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
class EnrollTest(APITestCase): """ Test cases for POST requests on EnrollView. """ def setUp(self): # create test users self.student_username = '******' self.instructor_username = '******' self.password = '******' self.student_user = User.objects.create_user( username=self.student_username, password=self.password) self.instructor_user = User.objects.create_user( username=self.instructor_username, password=self.password) group = permissions.get_or_create_instructor_permissions() group.user_set.add(self.instructor_user) group = permissions.get_or_create_student_permissions() group.user_set.add(self.student_user) self.client.login(username=self.student_username, password=self.password) # populate the database self.student = Student(labgroup=None, user=self.student_user, wwuid='1111111') self.student.save() self.instructor = Instructor(user=self.instructor_user, wwuid='2222222') self.instructor.save() self.course = Course(name='test name') self.course.save() self.labgroup = LabGroup(course=self.course, instructor=self.instructor, group_name='A', term='FALL2018', enroll_key='ABC') self.labgroup.save() # retrieve the view self.view_name = 'api:enroll' def test_enroll(self): """ Tests that a labgroup can be properly enrolled in. """ # request request_body = { 'wwuid': self.student.wwuid, 'labgroup': self.labgroup.id, 'enroll_key': self.labgroup.enroll_key } response = self.client.post(reverse(self.view_name), request_body) # test response self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) # test database self.assertEqual(Student.objects.first().user, self.student_user) self.assertEqual(Student.objects.first().labgroup, self.labgroup) self.assertEqual(Student.objects.first().wwuid, self.student.wwuid) def test_enroll_bad_labgroup(self): """ Tests that entering a bad labgroup is properly handled. """ # request request_body = { 'wwuid': self.student.wwuid, 'labgroup': 0, 'enroll_key': self.labgroup.enroll_key } response = self.client.post(reverse(self.view_name), request_body) # test response self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) # test database self.assertEqual(Student.objects.first().labgroup, None) def test_enroll_bad_key(self): """ Tests that a labgroup is not enrolled in with a bad key. """ # request request_body = { 'wwuid': self.student.wwuid, 'labgroup': self.labgroup.id, 'enroll_key': '' } response = self.client.post(reverse(self.view_name), request_body) # test response self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) # test database self.assertEqual(Student.objects.first().labgroup, None) def test_missing_parameters(self): """ Tests that a missing parameter causes the request to do nothing. """ # request request_body = { 'wwuid': self.student.wwuid, 'enroll_key': self.labgroup.enroll_key } response = self.client.post(reverse(self.view_name), request_body) # test response self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) # test database self.assertEqual(Student.objects.first().labgroup, None) def test_invalid_student(self): """ Tests that entering invalid student does nothing. """ # request request_body = { 'wwuid': '123456789', # too long 'labgroup': self.labgroup.id, 'enroll_key': self.labgroup.enroll_key } response = self.client.post(reverse(self.view_name), request_body) # test response self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) # test database self.assertEqual(len(Student.objects.all()), 0) def test_enroll_status(self): """ Tests that the enrollment status of a user can be retrieved. """ # enroll request request_body = { 'user': self.student_user, 'student': self.student, } self.client.post(reverse(self.view_name), request_body) # enroll status request response = self.client.get(reverse(self.view_name)) response_body = json.loads(response.content.decode('utf-8')) # test response self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response_body['user']['username'], self.student_user.username) self.assertEqual(response_body['user']['email'], self.student_user.email) self.assertEqual(response_body['user']['first_name'], self.student_user.first_name) self.assertEqual(response_body['user']['last_name'], self.student_user.last_name) self.assertEqual(response_body['student']['pk'], self.student.id) self.assertEqual(response_body['student']['labgroup'], self.student.labgroup) self.assertEqual(response_body['student']['user'], self.student.user.id) self.assertEqual(response_body['student']['wwuid'], self.student.wwuid) def test_enroll_status_not_enrolled(self): """ Tests that no enrollment status is retrieved for an un-enrolled user. """ # un-enroll user self.student.delete() # enroll status request response = self.client.get(reverse(self.view_name)) response_body = json.loads(response.content.decode('utf-8')) # test response self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response_body['user']['username'], self.student_user.username) self.assertEqual(response_body['user']['email'], self.student_user.email) self.assertEqual(response_body['user']['first_name'], self.student_user.first_name) self.assertEqual(response_body['user']['last_name'], self.student_user.last_name) self.assertEqual(response_body['student'], None)
class StudentLCTest(APITestCase): """ Test cases for list and create requests on StudentLCView. """ def setUp(self): # create test user with permissions self.username = '******' self.password = '******' self.student_username = '******' self.instructor_user = User.objects.create_user(username=self.username, password=self.password) group = permissions.get_or_create_instructor_permissions() group.user_set.add(self.instructor_user) self.student_user = User.objects.create_user( username=self.student_username, password=self.password) self.client.login(username=self.username, password=self.password) # populate test database self.instructor = Instructor(user=self.instructor_user, wwuid='9994141') self.instructor.save() self.course = Course(name='fact') self.course.save() self.group = LabGroup(course=self.course, instructor=self.instructor, term='never', enroll_key='6') self.group.save() # retrieve the view self.view_name = 'api:student-lc' def test_student_create(self): """ Tests that a student is properly created. """ # request request_body = { 'user': self.student_user.id, 'labgroup': self.group.id, 'wwuid': '16', } response = self.client.post(reverse(self.view_name), request_body) response_body = json.loads(response.content.decode('utf-8')) # test database student = Student.objects.first() self.assertEqual(student.user.id, request_body['user']) self.assertEqual(student.labgroup.id, request_body['labgroup']) self.assertEqual(student.wwuid, request_body['wwuid']) # test response self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(response_body['user'], request_body['user']) self.assertEqual(response_body['labgroup'], request_body['labgroup']) self.assertEqual(response_body['wwuid'], request_body['wwuid']) def test_student_permissions(self): """ Tests that a student's user is assigned the appropriate permissions. """ # request request_body = {'wwuid': '1111111', 'user': self.student_user.id} self.client.post(reverse(self.view_name), request_body) # check that the user is in the instructor group self.assertTrue( self.student_user.groups.filter(name='Student').exists()) def test_student_list(self): """ Tests that students are properly listed. """ # add students to database Student(user=self.student_user, labgroup=self.group, wwuid='64').save() Student(user=self.instructor_user, labgroup=self.group, wwuid='12').save() # request response = self.client.get(reverse(self.view_name)) response_body = json.loads(response.content.decode('utf-8')) # test response students = Student.objects.all() self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response_body['students'][0]['user'], students[0].user.id) self.assertEqual(response_body['students'][0]['labgroup'], students[0].labgroup.id) self.assertEqual(response_body['students'][0]['wwuid'], students[0].wwuid) self.assertEqual(response_body['students'][1]['user'], students[1].user.id) self.assertEqual(response_body['students'][1]['labgroup'], students[1].labgroup.id) self.assertEqual(response_body['students'][1]['wwuid'], students[1].wwuid) def test_student_list_not_shown_to_students(self): """ Tests that students are not listed to other students. """ # login the student self.client.logout() self.client.login(username=self.student_username, password=self.password) # add students to database Student(user=self.student_user, labgroup=self.group, wwuid='64').save() Student(user=self.instructor_user, labgroup=self.group, wwuid='12').save() # request response = self.client.get(reverse(self.view_name)) # test response self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def db_saver(dict_of_data): """ Save data to db from dict @params: dict_of_data - Required : dictionary with data (dict) """ #Number of courses counter j = 0 #Number of courses overall courses = int(dict_of_data["pagination"]["count"]) #Ununique courses provided unique_count = 0 #Initiate progress bar printProgressBar(j, courses, prefix='Saving to db: ', suffix='Saved {} of {}'.format(j, courses), length=30) #Saving to db for i in dict_of_data['results']: ''' if j == 494: print(('https://openedu.ru/api/courses/export/{}/{}?format=json'.format(i['org'],i['number']))) j+=1 ''' jlink = 'https://openedu.ru/api/courses/export/{}/{}?format=json'.format( i['org'], i['number']) r = requests.api.get(jlink) try: data = json.loads(json.dumps(r.json())) except JSONDecodeError: print(' Такого нет =( {}'.format(jlink)) j += 1 continue try: data['detail'] except: start_date = get_start_date(data['started_at'], j) end_date = get_end_date(data['finished_at'], j) deadlines = get_deadlines(start_date, end_date) post_entrance = get_post_entrance(deadlines) if data['cert']: certification = 'Да' else: certification = 'Нет' if data['requirements'] == '': prerequesetes = 'Нет' else: prerequesetes = data['requirements'] authors = '' for author in data['teachers']: authors = authors + author['display_name'] + ', ' authors = authors[:-2] if data['hours_per_week']: avg_length = str(int(float( data['hours_per_week']))) + ' часов в неделю' else: avg_length = 'Неизвестно' cour = Course( distributor='openedu', name=data['title'], description=data['description'], long_description=data['content'], organization=i['org'], org_link=data['external_url'], start_date=data['started_at'], end_date=data['finished_at'], course_id=i['id'], mnemocode=i['id'], detail_image=data['image'], anons_image=data['image'], video=data['promo_url'], modules_type='Лекция', modules_number=data['lectures'], avg_length=avg_length, length=str(data['duration']['value']) + ' ' + data['duration']['code'], code=get_code(j), deadlines=deadlines, post_entrance=post_entrance, sync=get_sync(), payment=get_payment(), prerequesetes=prerequesetes, skills=data['results'], language=data['language'], authors=authors, certification=certification, ) try: cour.save() printProgressBar(j, courses, prefix='Progress of parsing: ', suffix='Complete {} of {}'.format(j, courses), length=30) except IntegrityError: unique_count += 1 printProgressBar(j, courses, prefix='Progress of parsing: ', suffix='Complete {} of {}'.format(j, courses), length=30) j += 1 print( 'Didn`t save {} courses out of {} because they already existed'.format( unique_count, courses))
class TaskTemplateRUDTest(APITestCase): """ Test cases for retrieve, update, and destroy requests on TaskTemplateRUDView. """ def setUp(self): # create test user with permissions self.username = '******' self.password = '******' self.user = User.objects.create_user(username=self.username, password=self.password) Instructor(user=self.user, wwuid='9999999').save() group = permissions.get_or_create_instructor_permissions() group.user_set.add(self.user) self.client.login(username=self.username, password=self.password) # add courses and or templates to database self.course = Course(name="Astrophysics 820") self.course.save() self.template = AssignmentTemplate(name='Gravitational Vortices', course=self.course) self.template.save() self.template_2 = AssignmentTemplate(name='Cosmic Drift', course=self.course) self.template_2.save() self.task_template_1 = TaskTemplate(assignment_template=self.template, problem_num=1, summary='This is a summary.', prompt='This is the prompt1', prompt_format='CAF-citation', image_urls='all', attempts_allowed=1, numeric_accuracy=1, numeric_only=False) self.task_template_1.save() self.task_template_2 = TaskTemplate(assignment_template=self.template, problem_num=2, summary='This is a summary.', prompt='This is the prompt2', prompt_format='CAF-citation', image_urls='all', attempts_allowed=2, numeric_accuracy=2, numeric_only=False) self.task_template_2.save() self.task_template_3 = TaskTemplate(assignment_template=self.template, problem_num=3, summary='This is a summary.', prompt='This is the prompt3', prompt_format='SIGFIGS', image_urls='all', attempts_allowed=3, numeric_accuracy=3, numeric_only=True) self.task_template_3.save() # retrieve the view self.view_name = 'api:task-template-rud' def test_task_template_retrieve(self): """ Tests that a task template is properly retrieved. """ # request response = self.client.get(reverse(self.view_name, args=[self.template_2.id, self.task_template_1.id])) response_body = json.loads(response.content.decode('utf-8')) # test response self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response_body['pk'], self.task_template_1.id) self.assertEqual(response_body['assignment_template'], self.task_template_1.assignment_template.id) self.assertEqual(response_body['problem_num'], self.task_template_1.problem_num) self.assertEqual(response_body['summary'], self.task_template_1.summary) self.assertEqual(response_body['prompt'], self.task_template_1.prompt) self.assertEqual(response_body['prompt_format'], self.task_template_1.prompt_format) self.assertEqual(response_body['image_urls'], self.task_template_1.image_urls) self.assertEqual(response_body['attempts_allowed'], self.task_template_1.attempts_allowed) self.assertEqual(response_body['numeric_accuracy'], self.task_template_1.numeric_accuracy) self.assertEqual(response_body['numeric_only'], self.task_template_1.numeric_only) def test_task_template_update(self): """ Tests that a task template is properly updated. """ # modify values request_body = { 'problem_num': 10, 'summary': 'new summary', 'prompt': 'new prompt', 'prompt_format': 'CAC-Citation', 'image_urls': 'none', 'attempts_allowed': 64, 'numeric_accuracy': 64, 'numeric_only': True, } # request response = self.client.put(reverse(self.view_name, args=[self.template_2.id, self.task_template_2.id]), request_body) response_body = json.loads(response.content.decode('utf-8')) # test database task_template = TaskTemplate.objects.filter(problem_num=request_body['problem_num']).first() self.assertEqual(task_template.id, self.task_template_2.id) self.assertEqual(task_template.problem_num, request_body['problem_num']) self.assertEqual(task_template.summary, request_body['summary']) self.assertEqual(task_template.prompt, request_body['prompt']) self.assertEqual(task_template.prompt_format, request_body['prompt_format']) self.assertEqual(task_template.image_urls, request_body['image_urls']) self.assertEqual(task_template.attempts_allowed, request_body['attempts_allowed']) self.assertEqual(task_template.numeric_accuracy, request_body['numeric_accuracy']) self.assertEqual(task_template.numeric_only, request_body['numeric_only']) # test response self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response_body['pk'], self.task_template_2.id) self.assertEqual(response_body['problem_num'], request_body['problem_num']) self.assertEqual(response_body['summary'], request_body['summary']) self.assertEqual(response_body['prompt'], request_body['prompt']) self.assertEqual(response_body['prompt_format'], request_body['prompt_format']) self.assertEqual(response_body['image_urls'], request_body['image_urls']) self.assertEqual(response_body['attempts_allowed'], request_body['attempts_allowed']) self.assertEqual(response_body['numeric_accuracy'], request_body['numeric_accuracy']) self.assertEqual(response_body['numeric_only'], request_body['numeric_only']) def test_task_template_destroy(self): """ Tests that a task template is properly destroyed. """ # request response = self.client.delete(reverse(self.view_name, args=[self.template_2.id, self.task_template_2.id])) # test database task_templates = TaskTemplate.objects.all() self.assertTrue(self.task_template_1 in task_templates) self.assertTrue(self.task_template_2 not in task_templates) self.assertTrue(self.task_template_3 in task_templates) # test response self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
class TaskTemplateLCTest(APITestCase): """ Test cases for list and create requests on TaskTemplateLCView. """ def setUp(self): # create test user with permissions self.username = '******' self.password = '******' self.user = User.objects.create_user(username=self.username, password=self.password) Instructor(user=self.user, wwuid='9999999').save() group = permissions.get_or_create_instructor_permissions() group.user_set.add(self.user) self.client.login(username=self.username, password=self.password) # retrieve the view self.view_name = 'api:task-template-lc' # make a fake course self.course = Course(name="TestCourse") self.course.save() # make a fake assignment_template self.assignment_template = AssignmentTemplate(course=self.course, name="TestAssignmentTemplate") self.assignment_template.save() def test_task_template_create(self): """ Tests that a task template is properly created. """ # request request_body = { 'problem_num': 1, 'summary': 'test summary', 'prompt': 'test prompt', 'prompt_format': 'test prompt format', 'image_urls': 'test urls', 'attempts_allowed': 3, 'numeric_accuracy': 2, 'numeric_only': False } # create task template response = self.client.post(reverse(viewname=self.view_name, args=[self.assignment_template.id]), request_body) response_body = json.loads(response.content.decode('utf-8')) # test database temp = TaskTemplate.objects.first() self.assertEqual(temp.assignment_template.id, self.assignment_template.id) self.assertEqual(temp.problem_num, request_body['problem_num']) self.assertEqual(temp.summary, request_body['summary']) self.assertEqual(temp.prompt, request_body['prompt']) self.assertEqual(temp.prompt_format, request_body['prompt_format']) self.assertEqual(temp.image_urls, request_body['image_urls']) self.assertEqual(temp.attempts_allowed, request_body['attempts_allowed']) self.assertEqual(temp.numeric_accuracy, request_body['numeric_accuracy']) self.assertEqual(temp.numeric_only, request_body['numeric_only']) # test response self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(response_body['assignment_template'], self.assignment_template.id) self.assertEqual(response_body['problem_num'], request_body['problem_num']) self.assertEqual(response_body['summary'], request_body['summary']) self.assertEqual(response_body['prompt'], request_body['prompt']) self.assertEqual(response_body['prompt_format'], request_body['prompt_format']) self.assertEqual(response_body['image_urls'], request_body['image_urls']) self.assertEqual(response_body['attempts_allowed'], request_body['attempts_allowed']) self.assertEqual(response_body['numeric_accuracy'], request_body['numeric_accuracy']) self.assertEqual(response_body['numeric_only'], request_body['numeric_only']) def test_task_template_create_include_template_key(self): """ Tests that a task template is properly created even when 'assignment_template' is included in the request body. """ # request request_body = { 'assignment_template': 0, 'problem_num': 1, 'summary': 'test summary', 'prompt': 'test prompt', 'prompt_format': 'test prompt format', 'image_urls': 'test urls', 'attempts_allowed': 3, 'numeric_accuracy': 2, 'numeric_only': False } # create task template response = self.client.post(reverse(viewname=self.view_name, args=[self.assignment_template.id]), request_body) response_body = json.loads(response.content.decode('utf-8')) # test database temp = TaskTemplate.objects.first() self.assertEqual(temp.assignment_template.id, self.assignment_template.id) self.assertEqual(temp.problem_num, request_body['problem_num']) self.assertEqual(temp.summary, request_body['summary']) self.assertEqual(temp.prompt, request_body['prompt']) self.assertEqual(temp.prompt_format, request_body['prompt_format']) self.assertEqual(temp.image_urls, request_body['image_urls']) self.assertEqual(temp.attempts_allowed, request_body['attempts_allowed']) self.assertEqual(temp.numeric_accuracy, request_body['numeric_accuracy']) self.assertEqual(temp.numeric_only, request_body['numeric_only']) # test response self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(response_body['assignment_template'], self.assignment_template.id) self.assertEqual(response_body['problem_num'], request_body['problem_num']) self.assertEqual(response_body['summary'], request_body['summary']) self.assertEqual(response_body['prompt'], request_body['prompt']) self.assertEqual(response_body['prompt_format'], request_body['prompt_format']) self.assertEqual(response_body['image_urls'], request_body['image_urls']) self.assertEqual(response_body['attempts_allowed'], request_body['attempts_allowed']) self.assertEqual(response_body['numeric_accuracy'], request_body['numeric_accuracy']) self.assertEqual(response_body['numeric_only'], request_body['numeric_only']) def test_task_template_list(self): """ Tests that task templates are properly listed. """ # add task templates to database TaskTemplate(assignment_template=self.assignment_template, problem_num=1, summary='test summary 1', prompt='test prompt 1', prompt_format='test format 1', image_urls='test image 1', attempts_allowed=3, numeric_accuracy=2, numeric_only=False).save() TaskTemplate(assignment_template=self.assignment_template, problem_num=2, summary='test summary 2', prompt='test prompt 2', prompt_format='test format 2', image_urls='test image 2', attempts_allowed=3, numeric_accuracy=2, numeric_only=False).save() # request response = self.client.get(reverse(self.view_name, args=[self.assignment_template.id])) response_body = json.loads(response.content.decode('utf-8')) # test response task_templates = TaskTemplate.objects.all() self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response_body['task_templates'][0]['pk'], task_templates[0].id) self.assertEqual(response_body['task_templates'][0]['assignment_template'], task_templates[0].assignment_template.id) self.assertEqual(response_body['task_templates'][0]['problem_num'], task_templates[0].problem_num) self.assertEqual(response_body['task_templates'][0]['summary'], task_templates[0].summary) self.assertEqual(response_body['task_templates'][0]['prompt'], task_templates[0].prompt) self.assertEqual(response_body['task_templates'][0]['prompt_format'], task_templates[0].prompt_format) self.assertEqual(response_body['task_templates'][0]['image_urls'], task_templates[0].image_urls) self.assertEqual(response_body['task_templates'][0]['attempts_allowed'], task_templates[0].attempts_allowed) self.assertEqual(response_body['task_templates'][0]['numeric_accuracy'], task_templates[0].numeric_accuracy) self.assertEqual(response_body['task_templates'][0]['numeric_only'], task_templates[0].numeric_only) self.assertEqual(response_body['task_templates'][1]['pk'], task_templates[1].id) self.assertEqual(response_body['task_templates'][1]['assignment_template'], task_templates[1].assignment_template.id) self.assertEqual(response_body['task_templates'][1]['problem_num'], task_templates[1].problem_num) self.assertEqual(response_body['task_templates'][1]['summary'], task_templates[1].summary) self.assertEqual(response_body['task_templates'][1]['prompt'], task_templates[1].prompt) self.assertEqual(response_body['task_templates'][1]['prompt_format'], task_templates[1].prompt_format) self.assertEqual(response_body['task_templates'][1]['image_urls'], task_templates[1].image_urls) self.assertEqual(response_body['task_templates'][1]['attempts_allowed'], task_templates[1].attempts_allowed) self.assertEqual(response_body['task_templates'][1]['numeric_accuracy'], task_templates[1].numeric_accuracy) self.assertEqual(response_body['task_templates'][1]['numeric_only'], task_templates[1].numeric_only)
class AssignmentLCTest(APITestCase): """ Test cases for list and create requests on AssignmentLCView. """ def setUp(self): # create test user with permissions self.student_username = '******' self.instructor_username = '******' self.password = '******' self.student_user = User.objects.create_user( username=self.student_username, password=self.password) group = permissions.get_or_create_student_permissions() group.user_set.add(self.student_user) self.instructor_user = User.objects.create_user( username=self.instructor_username, password=self.password) group = permissions.get_or_create_instructor_permissions() group.user_set.add(self.instructor_user) self.client.login(username=self.instructor_username, password=self.password) # populate test database self.course = Course(name='Bounty Hunting 101') self.course.save() self.instructor = Instructor(user=self.instructor_user, wwuid='9994141') self.instructor.save() self.group = LabGroup(course=self.course, instructor=self.instructor, term='before', enroll_key='4') self.group.save() self.student = Student(user=self.student_user, wwuid='9994141', labgroup=self.group) self.student.save() self.template = AssignmentTemplate(course=self.course, name='Royalty Kidnapping Section A') self.template.save() # retrieve the view self.view_name = 'api:assignment-lc' def test_assignment_create(self): """ Tests that an assignment is properly created. """ # request request_body = { 'assignment_template': self.template.id, 'labgroup': self.group.id, 'open_date': '2013-12-12T22:22:22Z', 'close_date': '2014-12-12T22:22:22Z', } response = self.client.post(reverse(self.view_name), request_body) response_body = json.loads(response.content.decode('utf-8')) # test database assignment = Assignment.objects.first() self.assertEqual(assignment.assignment_template.id, request_body['assignment_template']) self.assertEqual(assignment.labgroup.id, request_body['labgroup']) self.assertEqual( assignment.open_date.replace(tzinfo=None), datetime.strptime(request_body['open_date'], '%Y-%m-%dT%H:%M:%SZ')) self.assertEqual( assignment.close_date.replace(tzinfo=None), datetime.strptime(request_body['close_date'], '%Y-%m-%dT%H:%M:%SZ')) # test response self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(response_body['assignment_template'], request_body['assignment_template']) self.assertEqual(response_body['labgroup'], request_body['labgroup']) self.assertEqual(response_body['open_date'], request_body['open_date']) self.assertEqual(response_body['close_date'], request_body['close_date']) def test_assignment_create_check_valid_dates(self): """ Tests that an assignment is not created when the open and close dates are incompatible. """ # get current time current_time = datetime.now(timezone(settings.TIME_ZONE)) # request request_body = { 'assignment_template': self.template.id, 'labgroup': self.group.id, 'open_date': (current_time + timedelta(days=1)).strftime('%Y-%m-%dT%H:%M:%SZ'), 'close_date': current_time.strftime('%Y-%m-%dT%H:%M:%SZ'), } response = self.client.post(reverse(self.view_name), request_body) # test response self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) # test database self.assertFalse(Assignment.objects.exists()) def test_assignment_create_template_course_incompatible(self): """ Tests that an assignment is not created when the assignment template doesn't belong to a shared course. """ # create different course course = Course(name='other course') course.save() # create different template template = AssignmentTemplate(course=course, name='other template') template.save() # request current_time = datetime.now(timezone(settings.TIME_ZONE)) request_body = { 'assignment_template': template.id, 'labgroup': self.group.id, 'open_date': (current_time - timedelta(days=1)).strftime('%Y-%m-%dT%H:%M:%SZ'), 'close_date': (current_time + timedelta(days=1)).strftime('%Y-%m-%dT%H:%M:%SZ'), } response = self.client.post(reverse(self.view_name), request_body) # test response self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) # test database self.assertFalse(Assignment.objects.exists()) def test_assignment_list_instructor(self): """ Tests that assignments are properly listed. """ # add assignments to database current_time = datetime.now(timezone(settings.TIME_ZONE)) Assignment(assignment_template=self.template, labgroup=self.group, open_date=current_time, close_date=current_time + timedelta(days=1)).save() Assignment(assignment_template=self.template, labgroup=self.group, open_date=current_time, close_date=current_time + timedelta(days=1)).save() # request response = self.client.get(reverse(self.view_name)) response_body = json.loads(response.content.decode('utf-8')) # test response assignments = Assignment.objects.all() self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual( response_body['assignments'][0]['assignment_template'], assignments[0].assignment_template.id) self.assertEqual(response_body['assignments'][0]['name'], assignments[0].assignment_template.name) self.assertEqual(response_body['assignments'][0]['labgroup'], assignments[0].labgroup.id) self.assertEqual( datetime.strptime(response_body['assignments'][0]['open_date'], '%Y-%m-%dT%H:%M:%S.%fZ'), assignments[0].open_date.replace(tzinfo=None)) self.assertEqual( datetime.strptime(response_body['assignments'][0]['close_date'], '%Y-%m-%dT%H:%M:%S.%fZ'), assignments[0].close_date.replace(tzinfo=None)) self.assertEqual( response_body['assignments'][1]['assignment_template'], assignments[1].assignment_template.id) self.assertEqual(response_body['assignments'][1]['name'], assignments[1].assignment_template.name) self.assertEqual(response_body['assignments'][1]['labgroup'], assignments[1].labgroup.id) self.assertEqual( datetime.strptime(response_body['assignments'][1]['open_date'], '%Y-%m-%dT%H:%M:%S.%fZ'), assignments[1].open_date.replace(tzinfo=None)) self.assertEqual( datetime.strptime(response_body['assignments'][1]['close_date'], '%Y-%m-%dT%H:%M:%S.%fZ'), assignments[1].close_date.replace(tzinfo=None)) def test_assignment_list_instructor_does_not_own(self): """ Tests that assignments are properly listed. """ # create other instructor and labgroup user = User.objects.create_user(username='******', password='******') instructor = Instructor(wwuid='2027616', user=user) instructor.save() labgroup = LabGroup(course=self.course, instructor=instructor, term=get_current_term(), enroll_key='ABC') labgroup.save() # add assignments to database current_time = datetime.now(timezone(settings.TIME_ZONE)) assignment_1 = Assignment(assignment_template=self.template, labgroup=self.group, open_date=current_time, close_date=current_time + timedelta(days=1)) assignment_1.save() Assignment(assignment_template=self.template, labgroup=labgroup, open_date=current_time, close_date=current_time + timedelta(days=1)).save() # request response = self.client.get(reverse(self.view_name)) response_body = json.loads(response.content.decode('utf-8')) # test response self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(len(response_body['assignments']), 1) self.assertEqual( response_body['assignments'][0]['assignment_template'], assignment_1.assignment_template.id) self.assertEqual(response_body['assignments'][0]['name'], assignment_1.assignment_template.name) self.assertEqual(response_body['assignments'][0]['labgroup'], assignment_1.labgroup.id) self.assertEqual( datetime.strptime(response_body['assignments'][0]['open_date'], '%Y-%m-%dT%H:%M:%S.%fZ'), assignment_1.open_date.replace(tzinfo=None)) self.assertEqual( datetime.strptime(response_body['assignments'][0]['close_date'], '%Y-%m-%dT%H:%M:%S.%fZ'), assignment_1.close_date.replace(tzinfo=None)) def test_assignment_list_student(self): """ Tests that assignments are properly listed. """ # login the student self.client.logout() self.client.login(username=self.student_username, password=self.password) # add assignments to database current_time = datetime.now(timezone(settings.TIME_ZONE)) Assignment(assignment_template=self.template, labgroup=self.group, open_date=current_time, close_date=current_time + timedelta(days=1)).save() Assignment(assignment_template=self.template, labgroup=self.group, open_date=current_time, close_date=current_time + timedelta(days=1)).save() # request response = self.client.get(reverse(self.view_name)) response_body = json.loads(response.content.decode('utf-8')) # test response assignments = Assignment.objects.all() self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual( response_body['assignments'][0]['assignment_template'], assignments[0].assignment_template.id) self.assertEqual(response_body['assignments'][0]['name'], assignments[0].assignment_template.name) self.assertEqual(response_body['assignments'][0]['labgroup'], assignments[0].labgroup.id) self.assertEqual( datetime.strptime(response_body['assignments'][0]['open_date'], '%Y-%m-%dT%H:%M:%S.%fZ'), assignments[0].open_date.replace(tzinfo=None)) self.assertEqual( datetime.strptime(response_body['assignments'][0]['close_date'], '%Y-%m-%dT%H:%M:%S.%fZ'), assignments[0].close_date.replace(tzinfo=None)) self.assertEqual( response_body['assignments'][1]['assignment_template'], assignments[1].assignment_template.id) self.assertEqual(response_body['assignments'][1]['name'], assignments[1].assignment_template.name) self.assertEqual(response_body['assignments'][1]['labgroup'], assignments[1].labgroup.id) self.assertEqual( datetime.strptime(response_body['assignments'][1]['open_date'], '%Y-%m-%dT%H:%M:%S.%fZ'), assignments[1].open_date.replace(tzinfo=None)) self.assertEqual( datetime.strptime(response_body['assignments'][1]['close_date'], '%Y-%m-%dT%H:%M:%S.%fZ'), assignments[1].close_date.replace(tzinfo=None)) def test_assignment_list_student_different_labgroup(self): """ Tests that assignments are not listed if they are for a different labgroup. """ # login the student self.client.logout() self.client.login(username=self.student_username, password=self.password) # create other labgroup labgroup = LabGroup(course=self.course, instructor=self.instructor, term=get_current_term(), enroll_key='ABC') labgroup.save() # add assignments to database current_time = datetime.now(timezone(settings.TIME_ZONE)) assignment_1 = Assignment(assignment_template=self.template, labgroup=self.group, open_date=current_time, close_date=current_time + timedelta(days=1)) assignment_1.save() Assignment(assignment_template=self.template, labgroup=labgroup, open_date=current_time, close_date=current_time + timedelta(days=1)).save() # request response = self.client.get(reverse(self.view_name)) response_body = json.loads(response.content.decode('utf-8')) # test response self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(len(response_body['assignments']), 1) self.assertEqual( response_body['assignments'][0]['assignment_template'], assignment_1.assignment_template.id) self.assertEqual(response_body['assignments'][0]['name'], assignment_1.assignment_template.name) self.assertEqual(response_body['assignments'][0]['labgroup'], assignment_1.labgroup.id) self.assertEqual( datetime.strptime(response_body['assignments'][0]['open_date'], '%Y-%m-%dT%H:%M:%S.%fZ'), assignment_1.open_date.replace(tzinfo=None)) self.assertEqual( datetime.strptime(response_body['assignments'][0]['close_date'], '%Y-%m-%dT%H:%M:%S.%fZ'), assignment_1.close_date.replace(tzinfo=None))
class AssignmentRUDTest(APITestCase): """ Test cases for retrieve, update, and destroy requests on AssignmentRUDView. """ def setUp(self): # create test user with permissions self.username = '******' self.password = '******' self.instructor_user = User.objects.create_user(username=self.username, password=self.password) self.instructor_user.user_permissions.add( Permission.objects.get(codename='add_assignment')) self.instructor_user.user_permissions.add( Permission.objects.get(codename='change_assignment')) self.instructor_user.user_permissions.add( Permission.objects.get(codename='delete_assignment')) self.client.login(username=self.username, password=self.password) # populate test database self.instructor = Instructor(user=self.instructor_user, wwuid='9994141') self.instructor.save() self.course = Course(name='Bounty Hunting 101') self.course.save() self.group = LabGroup(course=self.course, instructor=self.instructor, term='before', enroll_key='4') self.group.save() self.template = AssignmentTemplate(course=self.course, name='Royalty Kidnapping Section A') self.template.save() self.assignment = Assignment(assignment_template=self.template, labgroup=self.group, open_date='2013-12-10T22:22:22Z', close_date='2014-12-10T22:22:22Z') self.assignment.save() self.assignment2 = Assignment(assignment_template=self.template, labgroup=self.group, open_date='2014-12-10T22:22:22Z', close_date='2015-12-10T22:22:22Z') self.assignment2.save() self.assignment3 = Assignment(assignment_template=self.template, labgroup=self.group, open_date='1492-12-10T22:22:22Z', close_date='3012-12-10T22:22:22Z') self.assignment3.save() # retrieve the view self.view_name = 'api:assignment-rud' def test_assignment_retrieve(self): """ Tests that an assignment is properly retrieved. """ # request response = self.client.get( reverse(self.view_name, args=[self.assignment.id])) response_body = json.loads(response.content.decode('utf-8')) # test response self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response_body['assignment_template'], self.assignment.assignment_template.id) self.assertEqual(response_body['name'], self.assignment.assignment_template.name) self.assertEqual(response_body['labgroup'], self.assignment.labgroup.id) self.assertEqual(response_body['open_date'], self.assignment.open_date) self.assertEqual(response_body['close_date'], self.assignment.close_date) def test_assignment_update(self): """ Tests that an assignment is properly updated. """ # modify values request_body = { 'assignment_template': self.template.id, 'labgroup': self.group.id, 'open_date': '1890-10-10T22:22:22Z', 'close_date': '1892-10-10T22:22:22Z', } # request response = self.client.put( reverse(self.view_name, args=[self.assignment.id]), request_body) response_body = json.loads(response.content.decode('utf-8')) # test database assignment_tester = Assignment.objects.filter( pk=self.assignment.id).first() self.assertEqual(assignment_tester.assignment_template.id, request_body['assignment_template']) self.assertEqual(assignment_tester.labgroup.id, request_body['labgroup']) self.assertEqual( assignment_tester.open_date.replace(tzinfo=None), datetime.strptime(request_body['open_date'], '%Y-%m-%dT%H:%M:%SZ')) self.assertEqual( assignment_tester.close_date.replace(tzinfo=None), datetime.strptime(request_body['close_date'], '%Y-%m-%dT%H:%M:%SZ')) # test response self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response_body['assignment_template'], request_body['assignment_template']) self.assertEqual(response_body['labgroup'], request_body['labgroup']) self.assertEqual(response_body['open_date'], request_body['open_date']) self.assertEqual(response_body['close_date'], request_body['close_date']) def test_assignment_update_check_valid_dates(self): """ Tests that an assignment is not updated when the open and close dates are incompatible. """ # get current time current_time = datetime.now(timezone(settings.TIME_ZONE)) # request request_body = { 'assignment_template': self.template.id, 'labgroup': self.group.id, 'open_date': (current_time + timedelta(days=1)).strftime('%Y-%m-%dT%H:%M:%SZ'), 'close_date': current_time.strftime('%Y-%m-%dT%H:%M:%SZ'), } response = self.client.put( reverse(self.view_name, args=[self.assignment.id]), request_body) # test response self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) # test database self.assertEqual(self.assignment.assignment_template.id, request_body['assignment_template']) self.assertEqual(self.assignment.labgroup.id, request_body['labgroup']) self.assertNotEqual(self.assignment.open_date, request_body['open_date']) self.assertNotEqual(self.assignment.close_date, request_body['close_date']) def test_assignment_update_template_course_incompatible(self): """ Tests that an assignment is not created when the assignment template doesn't belong to a shared course. """ # create different course course = Course(name='other course') course.save() # create different template template = AssignmentTemplate(course=course, name='other template') template.save() # request current_time = datetime.now(timezone(settings.TIME_ZONE)) request_body = { 'assignment_template': template.id, 'labgroup': self.group.id, 'open_date': (current_time - timedelta(days=1)).strftime('%Y-%m-%dT%H:%M:%SZ'), 'close_date': (current_time + timedelta(days=1)).strftime('%Y-%m-%dT%H:%M:%SZ'), } response = self.client.put( reverse(self.view_name, args=[self.assignment.id]), request_body) # test response self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) # test database assignment = Assignment.objects.get(id=self.assignment.id) self.assertNotEqual(assignment.assignment_template, template) def test_assignment_destroy(self): """ Tests that an assignment is properly destroyed. """ # request response = self.client.delete( reverse(self.view_name, args=[self.assignment2.id])) # test database assignments = Assignment.objects.all() self.assertTrue(self.assignment in assignments) self.assertTrue(self.assignment2 not in assignments) self.assertTrue(self.assignment3 in assignments) # test response self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
def setup(): """ Function that creates and saves elements to the database for demo purposes. Function assumes that the database starts out empty. For LOCAL DEV ONLY. """ passwords = [] # create example users passwords.append(UserService.generate_password()) user1 = User.objects.create_user( "*****@*****.**", password=passwords[0], email="*****@*****.**", first_name="Josiah", last_name="Carberry", ) user1.save() passwords.append(UserService.generate_password()) user2 = User.objects.create_user( "*****@*****.**", password=passwords[1], email="*****@*****.**", first_name="Blueno", ) user2.save() passwords.append(UserService.generate_password()) user3 = User.objects.create_user( "*****@*****.**", password=passwords[2], email="*****@*****.**", first_name="Random", last_name="Person", ) user3.save() # create course course1 = Course(name="Course 1") course1.save() course1.instructors.add(user1) course1.students.add(user2) course1.students.add(user3) course1.save() # create posts post1 = Post( author=user1, course=course1, title="I have a question", content="idk what im doing", anonymous=True, type=Post.Type.QUESTION, visibility=Post.Visibility.PUBLIC, ) post1.save() post2 = Post( author=user1, course=course1, title="I also have a question", content="i really dont know what im doing", anonymous=False, type=Post.Type.NOTE, visibility=Post.Visibility.PRIVATE, ) post2.save() post3 = Post( author=user3, course=course1, title="My code is broken", content="Hi you can see my name, Student answers welcomed!!", anonymous=False, type=Post.Type.NOTE, visibility=Post.Visibility.PUBLIC, ) post3.save() post4 = Post( author=user2, course=course1, title="Could a TA help", content="Only TAs should be able to see this", anonymous=False, type=Post.Type.NOTE, visibility=Post.Visibility.PRIVATE, ) post4.save() post5 = Post( author=user1, course=course1, title="TA Note about posting", content="Hours Changed from 11 to 2", anonymous=True, type=Post.Type.NOTE, visibility=Post.Visibility.PUBLIC, ) post5.save() post6 = Post( author=user1, course=course1, title="Question to Students", content="TA asking for student replies ", anonymous=True, type=Post.Type.QUESTION, visibility=Post.Visibility.PUBLIC, ) post6.save() # create tags tag1 = Tag(name="Tag 1", course=course1) tag1.save() tag2 = Tag(name="Tag 2", course=course1) tag2.save() post1.tags.add(tag1, tag2) post2.tags.add(tag2) post4.tags.add(tag1) post6.tags.add(tag1, tag2) print("setup successful!") print("user passwords in order: " + str(passwords))
class LabGroupLCTest(APITestCase): """ Test cases for list and create requests on LabGroupLCView. """ def setUp(self): # create test user with permissions self.instructor_username = '******' self.student_username = '******' self.password = '******' self.instructor_user = User.objects.create_user(username=self.instructor_username, password=self.password) self.student_user = User.objects.create_user(username=self.student_username, password=self.password) self.instructor_user.groups.add(permissions.get_or_create_instructor_permissions()) self.student_user.groups.add(permissions.get_or_create_student_permissions()) self.client.login(username=self.instructor_username, password=self.password) # retrieve the view self.view_name = 'api:lab-group-lc' # Create foreign keys self.instructor = Instructor(user=self.instructor_user, wwuid="1234567") self.instructor.save() self.course = Course(name="test_course") self.course.save() def test_labgroup_create(self): """ Tests that a labgroup is properly created. """ # request request_body = { 'course': self.course.id, 'instructor': self.instructor.id, 'group_name': 'test name', 'term': 'FALL2000', 'enroll_key': 'test enroll_key', } response = self.client.post(reverse(self.view_name), request_body) response_body = json.loads(response.content.decode('utf-8')) # test database labgroup = LabGroup.objects.first() self.assertEqual(labgroup.course.id, request_body['course']) self.assertEqual(labgroup.instructor.id, request_body['instructor']) self.assertEqual(labgroup.group_name, request_body['group_name']) self.assertEqual(labgroup.term, request_body['term']) self.assertEqual(labgroup.enroll_key, request_body['enroll_key']) # test response self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(response_body['pk'], labgroup.id) self.assertEqual(response_body['course'], request_body['course']) self.assertEqual(response_body['instructor'], request_body['instructor']) self.assertEqual(response_body['group_name'], request_body['group_name']) self.assertEqual(response_body['term'], request_body['term']) self.assertEqual(response_body['enroll_key'], request_body['enroll_key']) def test_labgroup_create_different_instructor(self): """ Tests that a labgroup is not created if the instructor passed in is not the instructors making the request. """ # create new user and instructor user = User.objects.create_user(username='******', password='******') instructor = Instructor(user=user, wwuid='9999999') instructor.save() # request request_body = { 'course': self.course.id, 'instructor': instructor.id, 'group_name': 'test name', 'term': 'FALL2000', 'enroll_key': 'test enroll_key', } response = self.client.post(reverse(self.view_name), request_body) # test response self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) # test database self.assertFalse(LabGroup.objects.exists()) def test_labgroup_list_instructor(self): """ Tests that labgroups are properly listed for instructors. """ # add labgroups to database LabGroup(course=self.course, instructor=self.instructor, group_name='test name 1', term=get_current_term(), enroll_key='test key 1').save() LabGroup(course=self.course, instructor=self.instructor, group_name='test name 2', term=get_current_term(), enroll_key='test key 2').save() # request response = self.client.get(reverse(self.view_name)) response_body = json.loads(response.content.decode('utf-8')) # test response labgroups = LabGroup.objects.all() self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response_body['labgroups'][0]['pk'], labgroups[0].id) self.assertEqual(response_body['labgroups'][0]['course'], labgroups[0].course.id) self.assertEqual(response_body['labgroups'][0]['instructor'], labgroups[0].instructor.id) self.assertEqual(response_body['labgroups'][0]['group_name'], labgroups[0].group_name) self.assertEqual(response_body['labgroups'][0]['enroll_key'], labgroups[0].enroll_key) self.assertEqual(response_body['labgroups'][0]['term'], labgroups[0].term) self.assertEqual(response_body['labgroups'][1]['pk'], labgroups[1].id) self.assertEqual(response_body['labgroups'][1]['course'], labgroups[1].course.id) self.assertEqual(response_body['labgroups'][1]['instructor'], labgroups[1].instructor.id) self.assertEqual(response_body['labgroups'][1]['group_name'], labgroups[1].group_name) self.assertEqual(response_body['labgroups'][1]['enroll_key'], labgroups[1].enroll_key) self.assertEqual(response_body['labgroups'][1]['term'], labgroups[1].term) def test_labgroup_list_student(self): """ Tests that labgroups are properly listed for students. """ # add labgroups to database LabGroup(course=self.course, instructor=self.instructor, group_name='test name 1', term=get_current_term(), enroll_key='test key 1').save() LabGroup(course=self.course, instructor=self.instructor, group_name='test name 2', term=get_current_term(), enroll_key='test key 2').save() # request self.client.logout() self.client.login(username=self.student_username, password=self.password) response = self.client.get(reverse(self.view_name)) response_body = json.loads(response.content.decode('utf-8')) # test response labgroups = LabGroup.objects.all() self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response_body['labgroups'][0]['pk'], labgroups[0].id) self.assertEqual(response_body['labgroups'][0]['course'], labgroups[0].course.id) self.assertEqual(response_body['labgroups'][0]['instructor'], labgroups[0].instructor.id) self.assertEqual(response_body['labgroups'][0]['group_name'], labgroups[0].group_name) self.assertTrue('enroll_key' not in response_body['labgroups'][0].keys()) self.assertEqual(response_body['labgroups'][0]['term'], labgroups[0].term) self.assertEqual(response_body['labgroups'][1]['pk'], labgroups[1].id) self.assertEqual(response_body['labgroups'][1]['course'], labgroups[1].course.id) self.assertEqual(response_body['labgroups'][1]['instructor'], labgroups[1].instructor.id) self.assertEqual(response_body['labgroups'][1]['group_name'], labgroups[1].group_name) self.assertTrue('enroll_key' not in response_body['labgroups'][0].keys()) self.assertEqual(response_body['labgroups'][1]['term'], labgroups[1].term) def test_labgroup_visibility_instructor(self): """ Tests that labgroups displayed to instructor were created by that instructor """ # setup second instructor users self.instructor_username_2 = 'different_instructor' self.instructor_user_2 = User.objects.create_user(username=self.instructor_username_2, password=self.password) self.instructor_2 = Instructor(user=self.instructor_user_2, wwuid="7654321") self.instructor_2.save() # add labgroups to database LabGroup(course=self.course, instructor=self.instructor, group_name='test name 1', term=get_current_term(), enroll_key='test key 1').save() LabGroup(course=self.course, instructor=self.instructor_2, group_name='test name 2', term=get_current_term(), enroll_key='test key 2').save() # request response = self.client.get(reverse(self.view_name)) response_body = json.loads(response.content.decode('utf-8')) # test response labgroups = LabGroup.objects.all() self.assertEqual(response.status_code, status.HTTP_200_OK) # assert response_body contains exactly 1 labgroup self.assertEqual(len(response_body['labgroups']), 1) def test_labgroup_filter_by_course(self): """ Tests that labgroups can be filtered by the courses query parameter. """ # add course to database course = Course(name='course name') course.save() # add labgroups to database labgroup = LabGroup(course=course, instructor=self.instructor, group_name='test name 1', term=get_current_term(), enroll_key='test key 1') labgroup.save() LabGroup(course=self.course, instructor=self.instructor, group_name='test name 2', term=get_current_term(), enroll_key='test key 2').save() # request response = self.client.get(reverse(self.view_name), {'course': course.id}) response_body = json.loads(response.content.decode('utf-8')) # test response self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(len(response_body['labgroups']), 1) self.assertEqual(response_body['labgroups'][0]['pk'], labgroup.id) self.assertEqual(response_body['labgroups'][0]['course'], labgroup.course.id) self.assertEqual(response_body['labgroups'][0]['instructor'], labgroup.instructor.id) self.assertEqual(response_body['labgroups'][0]['group_name'], labgroup.group_name) self.assertEqual(response_body['labgroups'][0]['term'], labgroup.term) self.assertEqual(response_body['labgroups'][0]['enroll_key'], labgroup.enroll_key)
class TemplateRUDTest(APITestCase): """ Test cases for retrieve, update, and destroy requests on TemplateRUDView. """ def setUp(self): # create test user with permissions self.username = '******' self.password = '******' self.user = User.objects.create_user(username=self.username, password=self.password) Instructor(user=self.user, wwuid='9999999').save() group = permissions.get_or_create_instructor_permissions() group.user_set.add(self.user) self.client.login(username=self.username, password=self.password) # add courses and or templates to database self.course = Course(name="testcourse") self.course.save() self.template_1 = AssignmentTemplate(name='test name 1', course=self.course) self.template_1.save() self.template_2 = AssignmentTemplate(name='test name 2', course=self.course) self.template_2.save() self.template_3 = AssignmentTemplate(name='test name 3', course=self.course) self.template_3.save() # retrieve the view self.view_name = 'api:template-rud' def test_assignment_template_retrieve(self): """ Tests that a assignment template is properly retrieved. """ # request response = self.client.get( reverse(self.view_name, args=[self.template_2.id])) response_body = json.loads(response.content.decode('utf-8')) # test response self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response_body['pk'], self.template_2.id) self.assertEqual(response_body['name'], self.template_2.name) self.assertEqual(response_body['course'], self.course.id) def test_assignment_template_update(self): """ Tests that a template is properly updated. """ # modify values request_body = {'name': 'name changed', 'course': self.course.id} # request response = self.client.put( reverse(self.view_name, args=[self.template_2.id]), request_body) response_body = json.loads(response.content.decode('utf-8')) # test database assignment_template = AssignmentTemplate.objects.filter( name=request_body['name'], course=request_body['course']).first() self.assertEqual(assignment_template.id, self.template_2.id) self.assertEqual(assignment_template.course.id, request_body['course']) self.assertEqual(assignment_template.name, request_body['name']) # test response self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response_body['pk'], self.template_2.id) self.assertEqual(response_body['course'], request_body['course']) self.assertEqual(response_body['name'], request_body['name']) def test_assignment_template_destroy(self): """ Tests that a template is properly destroyed. """ # request response = self.client.delete( reverse(self.view_name, args=[self.template_2.id])) # test database assignment_templates = AssignmentTemplate.objects.all() self.assertTrue(self.template_1 in assignment_templates) self.assertTrue(self.template_2 not in assignment_templates) self.assertTrue(self.template_3 in assignment_templates) # test response self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
class LabGroupRUDTest(APITestCase): """ Test cases for retrieve, update, and destroy requests on LabGroupRUDView. """ def setUp(self): # create test user with permissions self.instructor_username = '******' self.student_username = '******' self.password = '******' self.instructor_user = User.objects.create_user(username=self.instructor_username, password=self.password) self.student_user = User.objects.create_user(username=self.student_username, password=self.password) self.instructor_user.groups.add(permissions.get_or_create_instructor_permissions()) self.student_user.groups.add(permissions.get_or_create_student_permissions()) self.client.login(username=self.instructor_username, password=self.password) # Create foreign keys self.instructor = Instructor(user=self.instructor_user, wwuid="1234567") self.instructor.save() self.course_1 = Course(name="test course 1") self.course_1.save() self.course_2 = Course(name="test course 2") self.course_2.save() # add labgroups to database self.labgroup_1 = LabGroup(course=self.course_1, instructor=self.instructor, term='test1', enroll_key='test key 1') self.labgroup_1.save() self.labgroup_2 = LabGroup(course=self.course_1, instructor=self.instructor, term='test2', enroll_key='test key 2') self.labgroup_2.save() self.labgroup_3 = LabGroup(course=self.course_1, instructor=self.instructor, term='test3', enroll_key='test key 3') self.labgroup_3.save() # retrieve the view self.view_name = 'api:lab-group-rud' def test_labgroup_retrieve_instructor(self): """ Tests that a labgroup is properly retrieved for instructors. """ # request response = self.client.get(reverse(self.view_name, args=[self.labgroup_2.id])) response_body = json.loads(response.content.decode('utf-8')) # test response self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response_body['pk'], self.labgroup_2.id) self.assertEqual(response_body['course'], self.labgroup_2.course.id) self.assertEqual(response_body['term'], self.labgroup_2.term) self.assertEqual(response_body['group_name'], self.labgroup_2.group_name) self.assertEqual(response_body['instructor'], self.labgroup_2.instructor.id) self.assertEqual(response_body['enroll_key'], self.labgroup_2.enroll_key) def test_labgroup_retrieve_student(self): """ Tests that a labgroup is properly retrieved for students. """ # request self.client.logout() self.client.login(username=self.student_username, password=self.password) response = self.client.get(reverse(self.view_name, args=[self.labgroup_2.id])) response_body = json.loads(response.content.decode('utf-8')) # test response self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response_body['pk'], self.labgroup_2.id) self.assertEqual(response_body['course'], self.labgroup_2.course.id) self.assertEqual(response_body['term'], self.labgroup_2.term) self.assertEqual(response_body['group_name'], self.labgroup_2.group_name) self.assertEqual(response_body['instructor'], self.labgroup_2.instructor.id) self.assertTrue('enroll_key' not in response_body.keys()) def test_labgroup_update(self): """ Tests that a labgroup is properly updated. """ # modify values request_body = { 'course': self.course_2.id, 'instructor': self.instructor.id, 'group_name': 'changed', 'term': 'FALL2000', 'enroll_key': 'changed', } # request response = self.client.put(reverse(self.view_name, args=[self.labgroup_2.id]), request_body) response_body = json.loads(response.content.decode('utf-8')) # test database labgroup = LabGroup.objects.get(id=self.labgroup_2.id) self.assertEqual(labgroup.id, self.labgroup_2.id) self.assertEqual(labgroup.course.id, request_body['course']) self.assertEqual(labgroup.group_name, request_body['group_name']) self.assertEqual(labgroup.instructor.id, request_body['instructor']) self.assertEqual(labgroup.term, request_body['term']) self.assertEqual(labgroup.enroll_key, request_body['enroll_key']) # test response self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response_body['pk'], self.labgroup_2.id) self.assertEqual(response_body['course'], request_body['course']) self.assertEqual(response_body['group_name'], request_body['group_name']) self.assertEqual(response_body['instructor'], request_body['instructor']) self.assertEqual(response_body['term'], request_body['term']) self.assertEqual(response_body['enroll_key'], request_body['enroll_key']) def test_labgroup_destroy(self): """ Tests that a labgroup is properly destroyed. """ # request response = self.client.delete(reverse(self.view_name, args=[self.labgroup_2.id])) # test database labgroups = LabGroup.objects.all() self.assertTrue(self.labgroup_1 in labgroups) self.assertTrue(self.labgroup_2 not in labgroups) self.assertTrue(self.labgroup_3 in labgroups) # test response self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
class AssignmentTemplateLCTest(APITestCase): """ Test cases for list and create requests on AssignmentTemplateLCView. """ def setUp(self): # create test user with permissions self.instructor_username = '******' self.password = '******' self.instructor_user = User.objects.create_user( username=self.instructor_username, password=self.password) self.instructor = Instructor(user=self.instructor_user, wwuid='9999999') self.instructor.save() group = permissions.get_or_create_instructor_permissions() group.user_set.add(self.instructor_user) self.client.login(username=self.instructor_username, password=self.password) # retrieve the view self.view_name = 'api:template-lc' # make a fake course self.course = Course(name="TestCourse") self.course.save() def test_assignment_template_create(self): """ Tests that an assignment template is properly created. """ # request request_body = {'name': 'test name', 'course': self.course.id} # create assignment template response = self.client.post(reverse(self.view_name), request_body) response_body = json.loads(response.content.decode('utf-8')) # test database temp = AssignmentTemplate.objects.first() self.assertEqual(temp.name, request_body['name']) self.assertEqual(self.course.id, request_body['course']) # test response self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(response_body['pk'], temp.id) self.assertEqual(response_body['name'], request_body['name']) self.assertEqual(response_body['course'], self.course.id) def test_assignment_template_list_instructor(self): """ Tests that assignment templates are properly listed for instructors. """ # add assignment templates to database AssignmentTemplate(name='test name 1', course=self.course).save() AssignmentTemplate(name='test name 2', course=self.course).save() # request response = self.client.get(reverse(self.view_name)) response_body = json.loads(response.content.decode('utf-8')) # test response assignments = AssignmentTemplate.objects.all() self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response_body['templates'][0]['pk'], assignments[0].id) self.assertEqual(response_body['templates'][0]['course'], assignments[0].course.id) self.assertEqual(response_body['templates'][0]['name'], assignments[0].name) self.assertEqual(response_body['templates'][1]['pk'], assignments[1].id) self.assertEqual(response_body['templates'][1]['course'], assignments[1].course.id) self.assertEqual(response_body['templates'][1]['name'], assignments[1].name) def test_assignment_template_list_student(self): """ Tests that assignment templates are properly listed for students. """ # create a labgroup labgroup_1 = LabGroup(group_name='A', course=self.course, instructor=self.instructor, term=get_current_term(), enroll_key='ABC') labgroup_1.save() # create an unused labgroup labgroup_2 = LabGroup(group_name='B', course=self.course, instructor=self.instructor, term=get_current_term(), enroll_key='ABC') labgroup_2.save() # create student user student_user = User.objects.create_user(username='******', password=self.password) Student(user=student_user, wwuid='1111111', labgroup=labgroup_1).save() group = permissions.get_or_create_student_permissions() group.user_set.add(student_user) self.client.logout() self.client.login(username=student_user.username, password=self.password) # add assignment templates to database at1 = AssignmentTemplate(name='test name 1', course=self.course) at1.save() at2 = AssignmentTemplate(name='test name 2', course=self.course) at2.save() # assign student the first assignment template assignment = Assignment( assignment_template=at1, labgroup=labgroup_1, open_date=datetime.now(timezone(settings.TIME_ZONE)) - timedelta(days=1), close_date=datetime.now(timezone(settings.TIME_ZONE)) + timedelta(days=1)) assignment.save() # create different assignment Assignment(assignment_template=at2, labgroup=labgroup_2, open_date=datetime.now(timezone(settings.TIME_ZONE)) - timedelta(days=1), close_date=datetime.now(timezone(settings.TIME_ZONE)) + timedelta(days=1)) # request response = self.client.get(reverse(self.view_name)) response_body = json.loads(response.content.decode('utf-8')) # test response assignments = AssignmentTemplate.objects.all() self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(len(response_body['templates']), 1) self.assertEqual(response_body['templates'][0]['pk'], assignments[0].id) self.assertEqual(response_body['templates'][0]['course'], assignments[0].course.id) self.assertEqual(response_body['templates'][0]['name'], assignments[0].name)
class StudentRUDTest(APITestCase): """ Test cases for retrieve, update, and destroy requests on StudentRUDView. """ def setUp(self): # create test user with permissions and test student user self.instructor_username = '******' self.student_username_1 = 'test student 1' self.student_username_2 = 'test student 2' self.student_username_3 = 'test student 3' self.password = '******' self.student_user_1 = User.objects.create_user( username=self.student_username_1, password=self.password) self.student_user_2 = User.objects.create_user( username=self.student_username_2, password=self.password) self.student_user_3 = User.objects.create_user( username=self.student_username_3, password=self.password) self.instructor_user = User.objects.create_user( username=self.instructor_username, password=self.password) self.instructor_user.user_permissions.add( Permission.objects.get(codename='change_student')) self.instructor_user.user_permissions.add( Permission.objects.get(codename='delete_student')) self.client.login(username=self.instructor_username, password=self.password) # populate database self.instructor = Instructor(user=self.instructor_user, wwuid='9999999') self.instructor.save() self.course = Course(name='test course') self.course.save() self.group_1 = LabGroup(course=self.course, instructor=self.instructor, term='never', enroll_key='6') self.group_1.save() self.group_2 = LabGroup(course=self.course, instructor=self.instructor, term='ever', enroll_key='8') self.group_2.save() self.student_1 = Student(user=self.student_user_1, labgroup=self.group_1, wwuid='1111111') self.student_1.save() self.student_2 = Student(user=self.student_user_2, labgroup=self.group_1, wwuid='2222222') self.student_2.save() self.student_3 = Student(user=self.student_user_3, labgroup=self.group_1, wwuid='3333333') self.student_3.save() # retrieve the view self.view_name = 'api:student-rud' def test_student_retrieve(self): """ Tests that a student is properly retrieved. """ # request response = self.client.get( reverse(self.view_name, args=[self.student_2.id])) response_body = json.loads(response.content.decode('utf-8')) # test response self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response_body['user'], self.student_2.user.id) self.assertEqual(response_body['labgroup'], self.student_2.labgroup.id) self.assertEqual(response_body['wwuid'], self.student_2.wwuid) def test_student_update(self): """ Tests that a student is properly updated. """ # modify values request_body = { 'user': self.student_user_2.id, 'labgroup': self.group_2.id, 'wwuid': '8888888', } # request response = self.client.put( reverse(self.view_name, args=[self.student_2.id]), request_body) response_body = json.loads(response.content.decode('utf-8')) # test database student = Student.objects.filter(user=self.student_user_2).first() self.assertEqual(student.user.id, request_body['user']) self.assertEqual(student.labgroup.id, request_body['labgroup']) self.assertEqual(student.wwuid, request_body['wwuid']) # test response self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response_body['user'], request_body['user']) self.assertEqual(response_body['labgroup'], request_body['labgroup']) self.assertEqual(response_body['wwuid'], request_body['wwuid']) def test_student_destroy(self): """ Tests that a student is properly destroyed. """ # request response = self.client.delete( reverse(self.view_name, args=[self.student_2.id])) # test database students = Student.objects.all() self.assertTrue(self.student_1 in students) self.assertTrue(self.student_2 not in students) self.assertTrue(self.student_3 in students) # test response self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
class CourseRUDTest(APITestCase): """ Test cases for retrieve, update, and destroy requests on CourseRUDView. """ def setUp(self): # create test user with permissions self.username = '******' self.password = '******' self.user = User.objects.create_user(username=self.username, password=self.password) self.user.user_permissions.add(Permission.objects.get(codename='change_course')) self.user.user_permissions.add(Permission.objects.get(codename='delete_course')) self.client.login(username=self.username, password=self.password) # add courses to database self.course_1 = Course(name='test name 1') self.course_1.save() self.course_2 = Course(name='test name 2') self.course_2.save() self.course_3 = Course(name='test name 3') self.course_3.save() # retrieve the view self.view_name = 'api:course-rud' def test_course_retrieve(self): """ Tests that a course is properly retrieved. """ # request response = self.client.get(reverse(self.view_name, args=[self.course_2.id])) response_body = json.loads(response.content.decode('utf-8')) # test response self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response_body['pk'], self.course_2.id) self.assertEqual(response_body['name'], self.course_2.name) def test_course_update(self): """ Tests that a course is properly updated. """ # modify values request_body = { 'name': 'name changed', } # request response = self.client.put(reverse(self.view_name, args=[self.course_2.id]), request_body) response_body = json.loads(response.content.decode('utf-8')) # test database course = Course.objects.filter(name=request_body['name']).first() self.assertEqual(course.id, self.course_2.id) self.assertEqual(course.name, request_body['name']) # test response self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response_body['pk'], self.course_2.id) self.assertEqual(response_body['name'], request_body['name']) def test_course_destroy(self): """ Tests that a course is properly destroyed. """ # request response = self.client.delete(reverse(self.view_name, args=[self.course_2.id])) # test database courses = Course.objects.all() self.assertTrue(self.course_1 in courses) self.assertTrue(self.course_2 not in courses) self.assertTrue(self.course_3 in courses) # test response self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)