def test_get_first_term_as_candidate(self): # No Candidate objects created yet: self.assertFalse(self.profile.is_candidate()) self.assertIsNone(self.profile.get_first_term_as_candidate()) # Create Candidate for user in a past term: candidate = Candidate(user=self.user, term=self.term_old) candidate.save() self.assertEqual(self.profile.get_first_term_as_candidate(), self.term_old) # Create Candidate for user in current term, and past term should # still be marked as first term as candidate: candidate = Candidate(user=self.user, term=self.term) candidate.save() self.assertEqual(self.profile.get_first_term_as_candidate(), self.term_old) # Create user who only has initiation term data and no Candidate # objects: temp_user = get_user_model().objects.create_user( 'tester', '*****@*****.**', 'testpw') temp_user.first_name = 'Bentley' temp_user.last_name = 'Bent' temp_user.save() profile = self.model(user=temp_user) profile.save() self.assertIsNone(profile.get_first_term_as_candidate()) profile.initiation_term = self.term profile.save() self.assertEqual(profile.get_first_term_as_candidate(), self.term)
def test_is_candidate(self): # No Candidate objects created yet: self.assertFalse(self.profile.is_candidate()) # Create Candidate for user in a past term: candidate = Candidate(user=self.user, term=self.term_old) candidate.save() # Should now be considered a candidate only if current=False: self.assertTrue(self.profile.is_candidate(current=False)) self.assertFalse(self.profile.is_candidate(current=True)) # Mark that candidate as initiated: candidate.initiated = True candidate.save() # Re-fetch profile since candidate save affects StudentOrgUserProfile # initiation_term field: self.profile = get_object_or_none(self.model, user=self.user) self.assertFalse(self.profile.is_candidate(current=False)) self.assertFalse(self.profile.is_candidate(current=True)) # Mark initiated as False, and create new Candidate for the current # Term, as is the case for a candidate who doesn't initiate one term # and returns as a candidate a later term: candidate.initiated = False candidate.save() candidate = Candidate(user=self.user, term=self.term) candidate.save() self.profile = get_object_or_none(self.model, user=self.user) # Should be now considered a candidate (initiated not True): self.assertTrue(self.profile.is_candidate(current=False)) self.assertTrue(self.profile.is_candidate(current=True)) # Mark that candidate as initiated: candidate.initiated = True candidate.save() self.profile = get_object_or_none(self.model, user=self.user) self.assertFalse(self.profile.is_candidate(current=False)) self.assertFalse(self.profile.is_candidate(current=True)) # Change the 'current' semester to an old semester: self.term_old.current = True self.term_old.save() # Now the candidate should be considered a candidate since they have # not _yet_ initiated based on what the current semester is, but # they are in the database as a candidate for the current semester: self.assertTrue(self.profile.is_candidate(current=False)) self.assertTrue(self.profile.is_candidate(current=True)) # Ensure that they are also marked as a candidate if initiated = False candidate.initiated = False candidate.save() self.profile = get_object_or_none(self.model, user=self.user) self.assertTrue(self.profile.is_candidate(current=False)) self.assertTrue(self.profile.is_candidate(current=True))
def save(self, *args, **kwargs): """Save a Candidate object along with the user new account.""" cand_user = super(CandidateCreationForm, self).save(*args, **kwargs) candidate = Candidate(user=cand_user, term=Term.objects.get_current_term()) candidate.save() return candidate
def test_is_candidate(self): """Ensure that basic is_candidate usage works as expected. See tests for StudentOrgUserProfilesTest for more extensive testing. """ # No Candidate objects created yet: self.assertFalse(self.profile.is_candidate()) # Create Candidate for user in the current term: Candidate(user=self.user, term=self.term).save() # Should now be considered a candidate: self.assertTrue(self.profile.is_candidate())
def test_get_user_viewable(self): # self.user is just an ordinary user with no groups or special # permissions, so they should be able to view public, open, and # candidate events, but not member and officer events start_time = timezone.now() end_time = start_time + datetime.timedelta(hours=2) event_public = self.create_event(start_time, end_time, restriction=Event.PUBLIC) event_open = self.create_event(start_time, end_time, restriction=Event.OPEN) event_candidate = self.create_event(start_time, end_time, restriction=Event.CANDIDATE) event_member = self.create_event(start_time, end_time, restriction=Event.MEMBER) event_officer = self.create_event(start_time, end_time, restriction=Event.OFFICER) visible_events = Event.objects.get_user_viewable(self.user) expected_events = [event_public, event_open, event_candidate] self.assertQuerysetEqual(visible_events, [repr(event) for event in expected_events], ordered=False) # Make this user a candidate, and view the permissions should stay # the same Candidate(user=self.user, term=self.term).save() visible_events = Event.objects.get_user_viewable(self.user) self.assertQuerysetEqual(visible_events, [repr(event) for event in expected_events], ordered=False) # Make this user an officer, and they should be able to see all # events Officer(user=self.user, position=self.committee, term=self.term).save() visible_events = Event.objects.get_user_viewable(self.user) expected_events.extend([event_member, event_officer]) self.assertQuerysetEqual(visible_events, [repr(event) for event in expected_events], ordered=False)
def test_can_user_sign_up(self): # self.user is just an ordinary user with no groups or special # permissions, so the user should only be able to sign up for public # events restrictions = [ Event.PUBLIC, Event.OPEN, Event.CANDIDATE, Event.MEMBER, Event.OFFICER ] start_time = timezone.now() end_time = start_time + datetime.timedelta(hours=2) event = self.create_event(start_time, end_time) for restriction in restrictions: event.restriction = restriction event.save() if restriction == Event.PUBLIC: self.assert_can_sign_up(event, self.user) else: self.assert_cannot_sign_up(event, self.user) # Make this user a candidate, so the user should be able to sign up # for public and candidate events Candidate(user=self.user, term=self.term).save() for restriction in restrictions: event.restriction = restriction event.save() if restriction == Event.PUBLIC or restriction == Event.CANDIDATE: self.assert_can_sign_up(event, self.user) else: self.assert_cannot_sign_up(event, self.user) # Make this user an officer, so the user should be able to sign up # for all events except open events (which don't allow signups) Officer(user=self.user, position=self.committee, term=self.term).save() for restriction in restrictions: event.restriction = restriction event.save() if restriction == Event.OPEN: self.assert_cannot_sign_up(event, self.user) else: self.assert_can_sign_up(event, self.user)
def test_can_user_view(self): # self.user is just an ordinary user with no groups or special # permissions, so they should be able to view public, open, and # candidate events, but not member and officer events restrictions = [ Event.PUBLIC, Event.OPEN, Event.CANDIDATE, Event.MEMBER, Event.OFFICER ] start_time = timezone.now() end_time = start_time + datetime.timedelta(hours=2) event = self.create_event(start_time, end_time) for restriction in restrictions: event.restriction = restriction event.save() if restriction in Event.VISIBLE_TO_EVERYONE: self.assert_can_view(event, self.user) else: self.assert_cannot_view(event, self.user) # Make this user a candidate, and view the permissions should stay # the same Candidate(user=self.user, term=self.term).save() for restriction in restrictions: event.restriction = restriction event.save() if restriction in Event.VISIBLE_TO_EVERYONE: self.assert_can_view(event, self.user) else: self.assert_cannot_view(event, self.user) # Make this user an officer, and they should be able to see all # events Officer(user=self.user, position=self.committee, term=self.term).save() for restriction in restrictions: event.restriction = restriction event.save() self.assert_can_view(event, self.user)
def setUp(self): self.candidate_group = Group.objects.create(name='Current Candidate') self.member_group = Group.objects.create(name='Member') user_model = get_user_model() # Create candidate self.user = user_model.objects.create_user( username='******', email='*****@*****.**', password='******', first_name='Random', last_name='Candidate') self.term = Term(term=Term.SPRING, year=2012, current=True) self.term.save() self.candidate = Candidate(user=self.user, term=self.term) self.candidate.save() # Create officer officer_user = user_model.objects.create_user( username='******', email='*****@*****.**', password='******', first_name='Joe', last_name='Officer') committee = OfficerPosition( short_name='IT_test', long_name='Information Technology (test)', rank=2, mailing_list='IT') committee.save() self.officer = Officer(user=officer_user, position=committee, term=self.term, is_chair=False) self.officer.save() # Create some manual candidate requirements self.manual_req1 = ManualCandidateRequirement( name='Manual 1', requirement_type=CandidateRequirement.MANUAL, credits_needed=2, term=self.term) self.manual_req1.save() self.manual_req2 = ManualCandidateRequirement( name='Manual 2', requirement_type=CandidateRequirement.MANUAL, credits_needed=5, term=self.term) self.manual_req2.save() # Create a challenge type self.individual_challenge_type = ChallengeType(name='Individual') self.individual_challenge_type.save() # Create a challenge requirement self.challenge_req = ChallengeCandidateRequirement( challenge_type=self.individual_challenge_type, credits_needed=3, term=self.term) self.challenge_req.save() # Create some events and event requirement self.event_type1 = EventType(name='Fun') self.event_type1.save() self.event_type2 = EventType(name='Not Fun') self.event_type2.save() self.fun_event1 = Event(name='Fun Event', event_type=self.event_type1, start_datetime=timezone.now(), end_datetime=timezone.now(), term=self.term, location='A test location', contact=officer_user, committee=committee) self.fun_event1.save() self.fun_event2 = Event(name='Big Fun Event', event_type=self.event_type1, start_datetime=timezone.now(), end_datetime=timezone.now(), requirements_credit=2, term=self.term, location='A test location', contact=officer_user, committee=committee) self.fun_event2.save() self.notfun_event = Event(name='Not Fun Event', event_type=self.event_type2, start_datetime=timezone.now(), end_datetime=timezone.now(), term=self.term, location='A test location', contact=officer_user, committee=committee) self.notfun_event.save() self.event_req = EventCandidateRequirement( event_type=self.event_type1, credits_needed=4, term=self.term) self.event_req.save() # Create some exam files and exam files requirement test_file = open('test.txt', 'w+') test_file.write('This is a test file.') self.test_exam1 = Exam( course_instance=CourseInstance.objects.get(pk=10000), exam_number=Exam.MT1, exam_type=Exam.EXAM, verified=True, exam_file=File(test_file)) self.test_exam1.save() self.test_exam1.course_instance.course.department.save() self.test_exam2 = Exam( course_instance=CourseInstance.objects.get(pk=20000), exam_number=Exam.MT1, exam_type=Exam.EXAM, verified=True, exam_file=File(test_file)) self.test_exam2.save() self.test_exam2.course_instance.course.department.save() self.exam_req = ExamFileCandidateRequirement( credits_needed=2, term=self.term) self.exam_req.save()
def test_multiple_project_report_attendances(self): start_time = timezone.now() end_time = start_time + datetime.timedelta(hours=2) event = self.create_event(start_time, end_time) project_report = ProjectReport.objects.create( term=self.term, date=datetime.date.today(), title='Test project report', author=self.user, committee=self.committee) event.project_report = project_report event.save() candidate1 = get_user_model().objects.create_user( username='******', email='*****@*****.**', password='******', first_name='Fake', last_name='Candidate1') Candidate(user=candidate1, term=self.term).save() candidate2 = get_user_model().objects.create_user( username='******', email='*****@*****.**', password='******', first_name='Fake', last_name='Candidate2') Candidate(user=candidate2, term=self.term).save() officer = get_user_model().objects.create_user( username='******', email='*****@*****.**', password='******', first_name='Fake', last_name='Officer') Officer(user=officer, position=self.committee, term=self.term).save() member = self.user StudentOrgUserProfile(user=member, initiation_term=self.term).save() member.save() officer_attendance = EventAttendance.objects.create(user=officer, event=event) self.assertQuerysetEqual(project_report.officer_list.all(), [repr(officer)]) self.assertQuerysetEqual(project_report.candidate_list.all(), []) self.assertQuerysetEqual(project_report.member_list.all(), []) candidate1_attendance = EventAttendance.objects.create(user=candidate1, event=event) self.assertQuerysetEqual(project_report.officer_list.all(), [repr(officer)]) self.assertQuerysetEqual(project_report.candidate_list.all(), [repr(candidate1)]) self.assertQuerysetEqual(project_report.member_list.all(), []) member_attendance = EventAttendance.objects.create(user=member, event=event) self.assertQuerysetEqual(project_report.officer_list.all(), [repr(officer)]) self.assertQuerysetEqual(project_report.candidate_list.all(), [repr(candidate1)]) self.assertQuerysetEqual(project_report.member_list.all(), [repr(member)]) candidate2_attendance = EventAttendance.objects.create(user=candidate2, event=event) self.assertQuerysetEqual(project_report.officer_list.all(), [repr(officer)]) self.assertQuerysetEqual( project_report.candidate_list.all(), [repr(candidate1), repr(candidate2)], ordered=False) self.assertQuerysetEqual(project_report.member_list.all(), [repr(member)]) candidate1_attendance.delete() self.assertQuerysetEqual(project_report.officer_list.all(), [repr(officer)]) self.assertQuerysetEqual(project_report.candidate_list.all(), [repr(candidate2)]) self.assertQuerysetEqual(project_report.member_list.all(), [repr(member)]) officer_attendance.delete() self.assertQuerysetEqual(project_report.officer_list.all(), []) self.assertQuerysetEqual(project_report.candidate_list.all(), [repr(candidate2)]) self.assertQuerysetEqual(project_report.member_list.all(), [repr(member)]) candidate2_attendance.delete() self.assertQuerysetEqual(project_report.officer_list.all(), []) self.assertQuerysetEqual(project_report.candidate_list.all(), []) self.assertQuerysetEqual(project_report.member_list.all(), [repr(member)]) member_attendance.delete() self.assertQuerysetEqual(project_report.officer_list.all(), []) self.assertQuerysetEqual(project_report.candidate_list.all(), []) self.assertQuerysetEqual(project_report.member_list.all(), [])
def test_project_report_attendance(self): start_time = timezone.now() end_time = start_time + datetime.timedelta(hours=2) event = self.create_event(start_time, end_time) project_report = ProjectReport.objects.create( term=self.term, date=datetime.date.today(), title='Test project report', author=self.user, committee=self.committee) event.project_report = project_report event.save() # self.user is neither an officer, a candidate, nor a member, so # recording attendance should not affect any attendance list attendance = EventAttendance.objects.create(user=self.user, event=event) self.assertQuerysetEqual(project_report.officer_list.all(), []) self.assertQuerysetEqual(project_report.candidate_list.all(), []) self.assertQuerysetEqual(project_report.member_list.all(), []) attendance.delete() self.assertQuerysetEqual(project_report.officer_list.all(), []) self.assertQuerysetEqual(project_report.candidate_list.all(), []) self.assertQuerysetEqual(project_report.member_list.all(), []) # Make this user a candidate, so recording attendance should affect # the candidate list Candidate(user=self.user, term=self.term).save() attendance = EventAttendance.objects.create(user=self.user, event=event) self.assertQuerysetEqual(project_report.officer_list.all(), []) self.assertQuerysetEqual(project_report.candidate_list.all(), [repr(self.user)]) self.assertQuerysetEqual(project_report.member_list.all(), []) attendance.delete() self.assertQuerysetEqual(project_report.officer_list.all(), []) self.assertQuerysetEqual(project_report.candidate_list.all(), []) self.assertQuerysetEqual(project_report.member_list.all(), []) # Make this user a member, so recording attendance should affect # the member list self.user.studentorguserprofile.initiation_term = self.term self.user.save() attendance = EventAttendance.objects.create(user=self.user, event=event) self.assertQuerysetEqual(project_report.officer_list.all(), []) self.assertQuerysetEqual(project_report.candidate_list.all(), []) self.assertQuerysetEqual(project_report.member_list.all(), [repr(self.user)]) attendance.delete() self.assertQuerysetEqual(project_report.officer_list.all(), []) self.assertQuerysetEqual(project_report.candidate_list.all(), []) self.assertQuerysetEqual(project_report.member_list.all(), []) # Make this user an officer, so recording attendance should affect the # officer list Officer(user=self.user, position=self.committee, term=self.term).save() attendance = EventAttendance.objects.create(user=self.user, event=event) self.assertQuerysetEqual(project_report.officer_list.all(), [repr(self.user)]) self.assertQuerysetEqual(project_report.candidate_list.all(), []) self.assertQuerysetEqual(project_report.member_list.all(), []) attendance.delete() self.assertQuerysetEqual(project_report.officer_list.all(), []) self.assertQuerysetEqual(project_report.candidate_list.all(), []) self.assertQuerysetEqual(project_report.member_list.all(), [])