def test_officer_post_delete(self): """Test that a user is removed from the appropriate groups on post-delete. """ self.assertFalse(self.user.groups.exists()) # No groups yet # Add a regular officer position (for which the post-save should add # groups): officer_reg = Officer(user=self.user, position=self.position_regular, term=self.term) officer_reg.save() groups = list(self.user.groups.all()) self.assertTrue(len(groups) > 0) # Add an exec officer position for this user, and the user's group # count should increase: officer_exec = Officer(user=self.user, position=self.position_exec, term=self.term) officer_exec.save() self.assertTrue(len(groups) < self.user.groups.count()) # Now delete exec officer, and the user's groups should return to the # same positions as from before the exec position added any: officer_exec.delete() self.assertItemsEqual(groups, list(self.user.groups.all())) # And delete the regular officer, and the user should be part of no # more groups: officer_reg.delete() self.assertFalse(self.user.groups.exists())
def test_get_officer_positions_user_specific(self): """Test that get_officer_positions does not return positions held by other users. """ # No officer positions for either user yet: new_user = self.user_model(username='******', password='******', email='*****@*****.**') new_user.save() new_user_profile = self.model(user=new_user) self.assertEqual(list(self.profile.get_officer_positions()), []) self.assertEqual(list(new_user_profile.get_officer_positions()), []) # Make both users different officer positions: Officer(user=self.user, position=self.committee, term=self.term).save() Officer(user=new_user, position=self.advisor_pos, term=self.term_old).save() # Check the officer positions for self.user: self.assertEqual(list(self.profile.get_officer_positions()), [self.committee]) self.assertEqual( list(self.profile.get_officer_positions(term=self.term)), [self.committee]) self.assertEqual( list(self.profile.get_officer_positions(term=self.term_old)), []) # Check the officer positions for new_user: self.assertEqual(list(new_user_profile.get_officer_positions()), [self.advisor_pos]) self.assertEqual( list(new_user_profile.get_officer_positions(term=self.term)), []) self.assertEqual( list(new_user_profile.get_officer_positions(term=self.term_old)), [self.advisor_pos])
def test_is_officer_position(self): # Note that current=False is the default, which checks whether the # person has ever held the officer position # Not ever an officer: self.assertFalse( self.profile.is_officer_position(self.committee.short_name)) self.assertFalse( self.profile.is_officer_position(self.advisor_pos.short_name)) self.assertFalse( self.profile.is_officer_position( self.committee.short_name, current=True)) # Add an officer position in the current term: Officer(user=self.user, position=self.committee, term=self.term, is_chair=True).save() self.assertTrue( self.profile.is_officer_position(self.committee.short_name)) self.assertTrue( self.profile.is_officer_position( self.committee.short_name, current=True)) self.assertFalse( self.profile.is_officer_position(self.advisor_pos.short_name)) # Add an advisor officer position in an old term: Officer(user=self.user, position=self.advisor_pos, term=self.term_old).save() self.assertTrue(self.profile.is_officer_position( self.committee.short_name)) self.assertTrue(self.profile.is_officer_position( self.advisor_pos.short_name)) self.assertFalse(self.profile.is_officer_position( self.advisor_pos.short_name, current=True))
def test_remove_groups(self): # Test the Officer method for removing the user from groups. Note that # unlike test_add_groups, this method saves the Officer objets, as the # _remove_user_from_officer_groups method depends on database entries # to work properly. Thus, this method relies on post-save functions for # adding groups for a user. (Post-saves are also tested separately.) self.assertFalse(self.user.groups.exists()) # No groups yet # Add a regular officer position (for which the post-save should add # groups): officer_reg = Officer(user=self.user, position=self.position_regular, term=self.term) officer_reg.save() groups = list(self.user.groups.all()) self.assertTrue(len(groups) > 0) # Add the groups for an exec officer position manually so that the # Officer object is not in the database (and does not need to be # deleted here before we can test the removal function), and the user's # group count should increase: officer_exec = Officer(user=self.user, position=self.position_exec, term=self.term) officer_exec._add_user_to_officer_groups() self.assertTrue(len(groups) < self.user.groups.count()) # Now remove groups from the exec position, and the user's groups # should return to the same positions as from before the exec position # added any: officer_exec._remove_user_from_officer_groups() self.assertItemsEqual(groups, list(self.user.groups.all()))
def test_is_officer(self): # Note that is_officer also tests the get_officer_positions() method self.assertFalse(self.profile.is_officer()) # Officer in the current term: officer = Officer(user=self.user, position=self.committee, term=self.term, is_chair=True) officer.save() self.assertTrue(self.profile.is_officer()) self.assertTrue(self.profile.is_officer(current=True)) # Officer in an old term: officer.term = self.term_old officer.save() self.assertTrue(self.profile.is_officer()) self.assertFalse(self.profile.is_officer(current=True)) # Advisor officer in the current term: officer.position = self.advisor_pos officer.term = self.term officer.save() self.assertTrue(self.profile.is_officer()) self.assertTrue(self.profile.is_officer(current=True)) # Exclude auxiliary positions, such as advisors: self.assertFalse(self.profile.is_officer(exclude_aux=True)) self.assertFalse(self.profile.is_officer(current=True, exclude_aux=True))
def test_add_groups(self): # Test the Officer method for adding the user to groups. Note that no # officer objects are saved, as that would activate post-saves, which # are tested seprately. officer = Officer(user=self.user, position=self.position_regular, term=self.term) expected_groups = self.position_regular.get_corresponding_groups(term=self.term) self.assertFalse(self.user.groups.exists()) officer._add_user_to_officer_groups() # Check that all of the expected groups were added for this user: self.assertTrue(self.user.groups.exists()) for group in expected_groups: self.assertTrue(self.user.groups.filter(pk=group.pk).exists())
def test_add_groups(self): # Test the Officer method for adding the user to groups. Note that no # officer objects are saved, as that would activate post-saves, which # are tested seprately. officer = Officer(user=self.user, position=self.position_regular, term=self.term) expected_groups = self.position_regular.get_corresponding_groups( term=self.term) self.assertFalse(self.user.groups.exists()) officer._add_user_to_officer_groups() # Check that all of the expected groups were added for this user: self.assertTrue(self.user.groups.exists()) for group in expected_groups: self.assertTrue(self.user.groups.filter(pk=group.pk).exists())
def test_term_post_save(self): """Test that when terms are saved, the "Current" groups are kept up-to-date. """ self.assertFalse(self.user.groups.exists()) # No groups yet # Add an exec officer position (for which the post-save should add # groups) in the current term: officer_exec = Officer(user=self.user, position=self.position_exec, term=self.term) officer_exec.save() expected_groups = set( self.position_exec.get_corresponding_groups(term=self.term)) groups = list(self.user.groups.all()) self.assertTrue(len(groups) > 0) self.assertItemsEqual(groups, expected_groups) # Make sure saving the current term is a no-op: self.term.save() groups = list(self.user.groups.all()) self.assertItemsEqual(groups, expected_groups) # Add a regular officer position for this user in a new term (not # "current"), and the user's group count should increase: term_new = Term(term=Term.FALL, year=2012) term_new.save() officer_reg = Officer(user=self.user, position=self.position_regular, term=term_new) officer_reg.save() expected_groups.update( self.position_regular.get_corresponding_groups(term=term_new)) groups = list(self.user.groups.all()) self.assertItemsEqual(groups, expected_groups) # Now change the "new" term to be the current term: term_new.current = True term_new.save() # The self.term object is stale now, so re-fetch it from the database: self.term = Term.objects.get(pk=self.term.pk) # We should expect that the user should still be a "Current Officer", # but no longer "Current" for the groups specific to the exec position. groups = list(self.user.groups.all()) # Get "expected_groups" over again, since the current term has changed: expected_groups = set( self.position_exec.get_corresponding_groups(term=self.term)) expected_groups.update( self.position_regular.get_corresponding_groups(term=term_new)) self.assertItemsEqual(groups, expected_groups) # Double-check some of the "Current" groups: self.assertNotIn(self.exec_group_curr, groups) self.assertNotIn(self.pos_exec_group_curr, groups) self.assertIn(self.officer_group_curr, groups) self.assertIn(self.pos_reg_group_curr, groups)
def test_get_preferred_email(self): # When the user is not an officer: self.assertEqual(self.profile.get_preferred_email(), self.user.email) # With an alternate email address specified: test_email = 'test_' + self.user.email self.profile.alt_email = test_email self.profile.save() # user email (if not empty) is still preferred over alt_email: self.assertEqual(self.profile.get_preferred_email(), self.user.email) # Remove user email, keeping alternate email: self.user.email = '' self.user.save() self.assertEqual(self.profile.get_preferred_email(), test_email) # When the user is an officer (using the settings-defined hostname as # the email address domain): Officer(user=self.user, position=self.committee, term=self.term, is_chair=True).save() self.assertEqual(self.profile.get_preferred_email(), '{}@example.com'.format(self.user.get_username()))
def test_is_officer(self): """Ensure that basic is_officer usage works as expected. See tests for StudentOrgUserProfilesTest for more extensive testing. """ # No Officer objects created yet: self.assertFalse(self.profile.is_officer()) # Create Officer for user in the current term: Officer(user=self.user, position=self.committee, term=self.term).save() # Should now be considered an officer: self.assertTrue(self.profile.is_officer())
def test_is_member(self): # User is not a member yet, since not recorded as initiated and not an # officer: self.assertFalse(self.profile.is_member()) # Mark in their StudentOrgUserProfile that they've initiated, which # should qualify them as a member self.profile.initiation_term = self.term_old self.profile.save() self.assertTrue(self.profile.is_member()) # Remove the initiation term, and check that just being an officer # also qualifies the user as a member self.profile.initiation_term = None self.profile.save() self.assertFalse(self.profile.is_member()) Officer(user=self.user, position=self.committee, term=self.term).save() self.assertTrue(self.profile.is_member())
def test_save(self): it_chair = Officer(user=self.user, position=self.position, term=self.term, is_chair=True) it_chair.save() self.assertTrue(Officer.objects.filter(is_chair=True).exists()) new_term = Term(term=Term.FALL, year=2011, current=False) new_term.save() it_officer = Officer(user=self.user, position=self.position, term=new_term, is_chair=False) it_officer.save() officers = Officer.objects.filter(is_chair=True) self.assertEquals(len(officers), 1) self.assertEquals(officers[0].user, self.user)
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_term_post_save(self): """Test that when terms are saved, the "Current" groups are kept up-to-date. """ self.assertFalse(self.user.groups.exists()) # No groups yet # Add an exec officer position (for which the post-save should add # groups) in the current term: officer_exec = Officer(user=self.user, position=self.position_exec, term=self.term) officer_exec.save() expected_groups = set(self.position_exec.get_corresponding_groups(term=self.term)) groups = list(self.user.groups.all()) self.assertTrue(len(groups) > 0) self.assertItemsEqual(groups, expected_groups) # Make sure saving the current term is a no-op: self.term.save() groups = list(self.user.groups.all()) self.assertItemsEqual(groups, expected_groups) # Add a regular officer position for this user in a new term (not # "current"), and the user's group count should increase: term_new = Term(term=Term.FALL, year=2012) term_new.save() officer_reg = Officer(user=self.user, position=self.position_regular, term=term_new) officer_reg.save() expected_groups.update(self.position_regular.get_corresponding_groups(term=term_new)) groups = list(self.user.groups.all()) self.assertItemsEqual(groups, expected_groups) # Now change the "new" term to be the current term: term_new.current = True term_new.save() # The self.term object is stale now, so re-fetch it from the database: self.term = Term.objects.get(pk=self.term.pk) # We should expect that the user should still be a "Current Officer", # but no longer "Current" for the groups specific to the exec position. groups = list(self.user.groups.all()) # Get "expected_groups" over again, since the current term has changed: expected_groups = set(self.position_exec.get_corresponding_groups(term=self.term)) expected_groups.update(self.position_regular.get_corresponding_groups(term=term_new)) self.assertItemsEqual(groups, expected_groups) # Double-check some of the "Current" groups: self.assertNotIn(self.exec_group_curr, groups) self.assertNotIn(self.pos_exec_group_curr, groups) self.assertIn(self.officer_group_curr, groups) self.assertIn(self.pos_reg_group_curr, groups)
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_officer_post_save(self): """Test that a user is added to the appropriate groups on post-save.""" self.assertFalse(self.user.groups.exists()) # No groups yet # Add a regular officer position (for which the post-save should add # groups): officer_reg = Officer(user=self.user, position=self.position_regular, term=self.term) expected_groups = set( self.position_regular.get_corresponding_groups(term=self.term)) self.assertFalse(self.user.groups.exists()) officer_reg.save() # Check that all of the expected groups were added for this user: self.assertItemsEqual(expected_groups, self.user.groups.all()) # Add another position, and check that the correct groups are added: officer_exec = Officer(user=self.user, position=self.position_exec, term=self.term_old) officer_exec.save() expected_groups.update( self.position_exec.get_corresponding_groups(term=self.term_old)) self.assertItemsEqual(expected_groups, self.user.groups.all())
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 test_officer_post_save(self): """Test that a user is added to the appropriate groups on post-save.""" self.assertFalse(self.user.groups.exists()) # No groups yet # Add a regular officer position (for which the post-save should add # groups): officer_reg = Officer(user=self.user, position=self.position_regular, term=self.term) expected_groups = set(self.position_regular.get_corresponding_groups(term=self.term)) self.assertFalse(self.user.groups.exists()) officer_reg.save() # Check that all of the expected groups were added for this user: self.assertItemsEqual(expected_groups, self.user.groups.all()) # Add another position, and check that the correct groups are added: officer_exec = Officer(user=self.user, position=self.position_exec, term=self.term_old) officer_exec.save() expected_groups.update(self.position_exec.get_corresponding_groups(term=self.term_old)) self.assertItemsEqual(expected_groups, self.user.groups.all())
class CandidateTest(TestCase): fixtures = ['test/course_instance.yaml'] 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() @classmethod def tearDownClass(cls): os.remove('test.txt') shutil.rmtree(os.path.join(settings.WORKSPACE_ROOT, 'media', 'tests'), ignore_errors=True) def test_candidate_post_save(self): student_org_profile = get_object_or_none( StudentOrgUserProfile, user=self.user) # Candidate has already been saved for this user created above, so # StudentOrgUserProfile should exist: self.assertIsNotNone(student_org_profile) # Candidate has not been marked as initiated, so initiation_term should # be None in their profile, and the candidate should be in the Current # Candidate group and not in the Member group: self.assertIsNone(student_org_profile.initiation_term) candidate_groups = self.candidate.user.groups.all() self.assertIn(self.candidate_group, candidate_groups) self.assertNotIn(self.member_group, candidate_groups) # Mark candidate as initiated, so profile and groups should update to # match: self.candidate.initiated = True self.candidate.save() student_org_profile = get_object_or_none( StudentOrgUserProfile, user=self.user) self.assertEqual(student_org_profile.initiation_term, self.term) candidate_groups = self.candidate.user.groups.all() self.assertNotIn(self.candidate_group, candidate_groups) self.assertIn(self.member_group, candidate_groups) def test_manual_requirements(self): """Test that credits for manual requirements are counted correctly.""" # Create some candidate progress CandidateRequirementProgress( candidate=self.candidate, requirement=self.manual_req1, manually_recorded_credits=2, alternate_credits_needed=2).save() progress = self.candidate.get_progress(CandidateRequirement.MANUAL) self.assertEqual(progress['completed'], 2) self.assertEqual(progress['required'], 7) # Test the simple summing of credits progress2 = CandidateRequirementProgress( candidate=self.candidate, requirement=self.manual_req2, manually_recorded_credits=3, alternate_credits_needed=5) progress2.save() progress = self.candidate.get_progress(CandidateRequirement.MANUAL) self.assertEqual(progress['completed'], 5) self.assertEqual(progress['required'], 7) # Test alternate credit requirement progress2.alternate_credits_needed = 4 progress2.save() progress = self.candidate.get_progress(CandidateRequirement.MANUAL) self.assertEqual(progress['completed'], 5) self.assertEqual(progress['required'], 6) def test_challenge_requirements(self): """Test that credits for challenges are counted correctly.""" # Make sure unverified challenges don't count challenge = Challenge( candidate=self.candidate, description='Hello kitty', verifying_user=self.officer.user, challenge_type=self.individual_challenge_type) challenge.save() progress = self.candidate.get_progress(CandidateRequirement.CHALLENGE) self.assertEqual(progress['completed'], 0) self.assertEqual(progress['required'], 3) # ...and verified challenges do count challenge.verified = True challenge.save() progress = self.candidate.get_progress(CandidateRequirement.CHALLENGE) self.assertEqual(progress['completed'], 1) self.assertEqual(progress['required'], 3) # Test alternate credit requirement CandidateRequirementProgress( candidate=self.candidate, requirement=self.challenge_req, alternate_credits_needed=2).save() progress = self.candidate.get_progress(CandidateRequirement.CHALLENGE) self.assertEqual(progress['completed'], 1) self.assertEqual(progress['required'], 2) def test_event_requirements(self): """Test that credits for events are counted correctly based on attendance. """ # Attend Fun Event EventAttendance(event=self.fun_event1, user=self.user).save() progress = self.candidate.get_progress(CandidateRequirement.EVENT) self.assertEqual(progress['completed'], 1) # Attend Big Fun Event (worth 2) EventAttendance(event=self.fun_event2, user=self.user).save() progress = self.candidate.get_progress(CandidateRequirement.EVENT) self.assertEqual(progress['completed'], 3) # Attend Not Fun Event (not worth any requirements) EventAttendance(event=self.notfun_event, user=self.user).save() progress = self.candidate.get_progress(CandidateRequirement.EVENT) self.assertEqual(progress['completed'], 3) def test_exams_requirements(self): """Test that credits for exam files are counted correctly.""" # Upload 1 verified exam self.test_exam1.submitter = self.user self.test_exam1.save() self.assertEqual(self.user, self.test_exam1.submitter) progress = self.candidate.get_progress(CandidateRequirement.EXAM_FILE) self.assertEqual(progress['completed'], 1) # Upload 2 verified exams self.test_exam2.submitter = self.user self.test_exam2.save() progress = self.candidate.get_progress(CandidateRequirement.EXAM_FILE) self.assertEqual(progress['completed'], 2) # Unverify an exam (doesn't count anymore) self.test_exam1.verified = False self.test_exam1.save() progress = self.candidate.get_progress(CandidateRequirement.EXAM_FILE) self.assertEqual(progress['completed'], 1) def test_get_total_progress(self): """Test get_progress where requirement_type is not specified and total progress for all requirements is returned. """ # Get all the requirements that were created: requirements = CandidateRequirement.objects.filter(term=self.term) num_required = 0 for requirement in requirements: num_required += requirement.credits_needed progress = self.candidate.get_progress() # Ensure that the method returns the same value as calculated manually. # This value should not change over the course of the tests. self.assertEqual(progress['required'], num_required) # Haven't assigned any completions yet: total_completed = 0 self.assertEqual(progress['completed'], total_completed) # Attend Fun Event for fun event requirement EventAttendance(event=self.fun_event1, user=self.user).save() progress = self.candidate.get_progress() self.assertEqual(progress['required'], num_required) total_completed += 1 self.assertEqual(progress['completed'], total_completed) # Complete a challenge: Challenge(candidate=self.candidate, description='Hello kitty', verifying_user=self.officer.user, verified=True, challenge_type=self.individual_challenge_type).save() progress = self.candidate.get_progress() total_completed += 1 self.assertEqual(progress['required'], num_required) self.assertEqual(progress['completed'], total_completed) # Attend an event not worth any requirements EventAttendance(event=self.notfun_event, user=self.user).save() progress = self.candidate.get_progress() self.assertEqual(progress['required'], num_required) self.assertEqual(progress['completed'], total_completed)
def test_get_officer_positions(self): # Note that when given no 'term' kwarg, the method returns positions # from all terms. The order of the list returned is based on term, then # officer position rank # No officer positions for this user yet: self.assertEqual(list(self.profile.get_officer_positions()), []) # One Officer position in the current term: Officer(user=self.user, position=self.committee, term=self.term, is_chair=True).save() self.assertEqual(list(self.profile.get_officer_positions()), [self.committee]) self.assertEqual( list(self.profile.get_officer_positions(term=self.term)), [self.committee]) self.assertEqual( list(self.profile.get_officer_positions(term=self.term_old)), []) # Advisor officer position in an old term: Officer(user=self.user, position=self.advisor_pos, term=self.term_old).save() self.assertEqual(list(self.profile.get_officer_positions()), [self.advisor_pos, self.committee]) self.assertEqual( list(self.profile.get_officer_positions(term=self.term)), [self.committee]) self.assertEqual( list(self.profile.get_officer_positions(term=self.term_old)), [self.advisor_pos]) # Another advisor officer position in the current term Officer(user=self.user, position=self.advisor_pos, term=self.term).save() self.assertEqual(list(self.profile.get_officer_positions()), [self.advisor_pos, self.committee, self.advisor_pos]) self.assertEqual( list(self.profile.get_officer_positions(term=self.term)), [self.committee, self.advisor_pos]) self.assertEqual( list(self.profile.get_officer_positions(term=self.term_old)), [self.advisor_pos]) # Add a house leader officer position in the current term: # Ensure ordering is correct: Officer(user=self.user, position=self.house_leader, term=self.term).save() self.assertEqual(list(self.profile.get_officer_positions()), [ self.advisor_pos, self.committee, self.house_leader, self.advisor_pos ]) older_term = Term(term=Term.SPRING, year=2008) older_term.save() # Add a house leader officer position in an even older term: Officer(user=self.user, position=self.house_leader, term=older_term).save() self.assertEqual(list(self.profile.get_officer_positions()), [ self.house_leader, self.advisor_pos, self.committee, self.house_leader, self.advisor_pos ])
def test_is_officer(self): # Note that is_officer also tests the get_officer_positions() method self.assertFalse(self.profile.is_officer()) # Officer in the current term: officer = Officer(user=self.user, position=self.committee, term=self.term, is_chair=True) officer.save() self.assertTrue(self.profile.is_officer()) self.assertTrue(self.profile.is_officer(current=True)) # Officer in an old term: officer.term = self.term_old officer.save() self.assertTrue(self.profile.is_officer()) self.assertFalse(self.profile.is_officer(current=True)) # Advisor officer in the current term: officer.position = self.advisor_pos officer.term = self.term officer.save() self.assertTrue(self.profile.is_officer()) self.assertTrue(self.profile.is_officer(current=True)) # Exclude auxiliary positions, such as advisors: self.assertFalse(self.profile.is_officer(exclude_aux=True)) self.assertFalse( self.profile.is_officer(current=True, exclude_aux=True))
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(), [])