class OfficerTest(TestCase): fixtures = ["officer_position.yaml"] def setUp(self): self.user = get_user_model().objects.create_user( username="******", email="*****@*****.**", password="******", first_name="Off", last_name="Icer" ) self.term = Term(term=Term.SPRING, year=2012, current=True) self.term.save() self.position = OfficerPosition( short_name="IT_test", long_name="Information Technology (test)", rank=2, mailing_list="IT" ) self.position.save() 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 setUp(self): Group.objects.create(name='Current Candidate') Group.objects.create(name='Member') self.user_model = get_user_model() self.user = self.user_model.objects.create_user( 'test_user', '*****@*****.**', 'testpw') self.first_name = 'Edward' self.last_name = 'Williams' self.user.first_name = self.first_name self.user.last_name = self.last_name self.user.save() # Re-fetch the user from the DB to avoid issues with the current # object having a stale reference to their corresponding userprofile self.user = self.user_model.objects.get(pk=self.user.pk) self.term = Term(term=Term.SPRING, year=2013, current=True) self.term.save() self.term_old = Term(term=Term.SPRING, year=2012) self.term_old.save() self.committee = OfficerPosition( short_name='it', long_name='Information Technology (test)', rank=2, mailing_list='IT') self.committee.save()
class UserInfoTestCase(TestCase): """A TestCase which provides a useful setUp method for creating common user info used in multiple test cases. """ def setUp(self): Group.objects.create(name="Current Candidate") Group.objects.create(name="Member") self.user_model = get_user_model() self.user = self.user_model.objects.create_user("test_user", "*****@*****.**", "testpw") self.first_name = "Edward" self.last_name = "Williams" self.user.first_name = self.first_name self.user.last_name = self.last_name self.user.save() # Re-fetch the user from the DB to avoid issues with the current # object having a stale reference to their corresponding userprofile self.user = self.user_model.objects.get(pk=self.user.pk) self.term = Term(term=Term.SPRING, year=2013, current=True) self.term.save() self.term_old = Term(term=Term.SPRING, year=2012) self.term_old.save() self.committee = OfficerPosition( short_name="it", long_name="Information Technology (test)", rank=2, mailing_list="IT" ) self.committee.save()
def setUp(self): super(StudentOrgUserProfilesTest, self).setUp() self.model = StudentOrgUserProfile self.profile = self.model(user=self.user) self.profile.save() self.house_leader = OfficerPosition( short_name="house-leader", long_name="House Leader (test)", rank=3, mailing_list="IT" ) self.house_leader.save() self.advisor_pos = OfficerPosition( short_name="advisor", long_name="Advisor (test)", rank=4, mailing_list="IT", auxiliary=True ) self.advisor_pos.save()
def setUp(self): self.user = get_user_model().objects.create_user( username='******', email='*****@*****.**', password='******', first_name='Off', last_name='Icer') self.term = Term(term=Term.SPRING, year=2012, current=True) self.term.save() self.position = OfficerPosition( short_name='IT_test', long_name='Information Technology (test)', rank=2, mailing_list='IT') self.position.save()
def setUp(self): super(StudentOrgUserProfilesTest, self).setUp() self.model = StudentOrgUserProfile self.profile = self.model(user=self.user) self.profile.save() self.house_leader = OfficerPosition(short_name='house-leader', long_name='House Leader (test)', rank=3, mailing_list='IT') self.house_leader.save() self.advisor_pos = OfficerPosition(short_name='advisor', long_name='Advisor (test)', rank=4, mailing_list='IT', auxiliary=True) self.advisor_pos.save()
def setUp(self): self.user = get_user_model().objects.create_user( username="******", email="*****@*****.**", password="******", first_name="Off", last_name="Icer" ) self.term = Term(term=Term.SPRING, year=2012, current=True) self.term.save() self.position = OfficerPosition( short_name="IT_test", long_name="Information Technology (test)", rank=2, mailing_list="IT" ) self.position.save()
def setUp(self): Group.objects.create(name='Current Candidate') Group.objects.create(name='Member') self.user = get_user_model().objects.create_user( username='******', email='*****@*****.**', password='******', first_name='Bentley', last_name='Bent') self.committee = OfficerPosition(short_name='IT', long_name='Information Technology', rank=2, mailing_list='IT') self.committee.save() self.term = Term(term=Term.SPRING, year=2012, current=True) self.term.save() self.event_type, _ = EventType.objects.get_or_create( name='Test Event Type')
class OfficerTest(TestCase): fixtures = ['officer_position.yaml'] def setUp(self): self.user = get_user_model().objects.create_user( username='******', email='*****@*****.**', password='******', first_name='Off', last_name='Icer') self.term = Term(term=Term.SPRING, year=2012, current=True) self.term.save() self.position = OfficerPosition( short_name='IT_test', long_name='Information Technology (test)', rank=2, mailing_list='IT') self.position.save() 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 setUp(self): self.user = get_user_model().objects.create_user( username="******", email="*****@*****.**", password="******", first_name="Off", last_name="Icer" ) self.term = Term(term=Term.SPRING, year=2012, current=True) self.term.save() self.term_old = Term(term=Term.FALL, year=2011) self.term_old.save() self.position_exec = OfficerPosition( short_name="vp", long_name="Vice President", rank=1, mailing_list="vp", executive=True ) self.position_exec.save() self.position_regular = OfficerPosition( short_name="it", long_name="Information Technology", rank=2, mailing_list="it" ) self.position_regular.save() self.position_auxiliary = OfficerPosition( short_name="advisor", long_name="Advisor", rank=3, mailing_list="advisor", auxiliary=True ) self.position_auxiliary.save() # Some standard groups: self.officer_group = Group.objects.create(name="Officer") self.officer_group_curr = Group.objects.create(name="Current Officer") self.exec_group = Group.objects.create(name="Executive") self.exec_group_curr = Group.objects.create(name="Current Executive") # Groups from officer positions: self.pos_exec_group = Group.objects.create(name=self.position_exec.long_name) self.pos_exec_group_curr = Group.objects.create(name="Current {}".format(self.position_exec.long_name)) self.pos_reg_group = Group.objects.create(name=self.position_regular.long_name) self.pos_reg_group_curr = Group.objects.create(name="Current {}".format(self.position_regular.long_name)) self.pos_aux_group = Group.objects.create(name=self.position_auxiliary.long_name) self.pos_aux_group_curr = Group.objects.create(name="Current {}".format(self.position_auxiliary.long_name))
def setUp(self): self.user = get_user_model().objects.create_user( username='******', email='*****@*****.**', password='******', first_name='Off', last_name='Icer') self.term = Term(term=Term.SPRING, year=2012, current=True) self.term.save() self.term_old = Term(term=Term.FALL, year=2011) self.term_old.save() self.position_exec = OfficerPosition(short_name='vp', long_name='Vice President', rank=1, mailing_list='vp', executive=True) self.position_exec.save() self.position_regular = OfficerPosition( short_name='it', long_name='Information Technology', rank=2, mailing_list='it') self.position_regular.save() self.position_auxiliary = OfficerPosition(short_name='advisor', long_name='Advisor', rank=3, mailing_list='advisor', auxiliary=True) self.position_auxiliary.save() # Some standard groups: self.officer_group = Group.objects.create(name='Officer') self.officer_group_curr = Group.objects.create(name='Current Officer') self.exec_group = Group.objects.create(name='Executive') self.exec_group_curr = Group.objects.create(name='Current Executive') # Groups from officer positions: self.pos_exec_group = Group.objects.create( name=self.position_exec.long_name) self.pos_exec_group_curr = Group.objects.create( name='Current {}'.format(self.position_exec.long_name)) self.pos_reg_group = Group.objects.create( name=self.position_regular.long_name) self.pos_reg_group_curr = Group.objects.create( name='Current {}'.format(self.position_regular.long_name)) self.pos_aux_group = Group.objects.create( name=self.position_auxiliary.long_name) self.pos_aux_group_curr = Group.objects.create( name='Current {}'.format(self.position_auxiliary.long_name))
def test_save(self): num = OfficerPosition.objects.count() it_officer = OfficerPosition(short_name='IT_test', long_name='Information Technology (test)', rank=2, mailing_list='IT') it_officer.save() self.assertTrue(OfficerPosition.objects.exists()) exec_officer = OfficerPosition(short_name='exec test', long_name='Executive Officer (test)', rank=1, mailing_list='exec') exec_officer.save() positions = OfficerPosition.objects.order_by('pk') self.assertEquals(len(positions) - num, 2) self.assertEquals(positions[num].short_name, 'IT_test') self.assertEquals(positions[num].rank, 2) self.assertEquals(positions[num + 1].short_name, 'exec test') self.assertEquals(positions[num + 1].rank, 1)
def setUp(self): Group.objects.create(name='Current Candidate') Group.objects.create(name='Member') self.user = get_user_model().objects.create_user( username='******', email='*****@*****.**', password='******', first_name='Bentley', last_name='Bent') self.committee = OfficerPosition( short_name='IT', long_name='Information Technology', rank=2, mailing_list='IT') self.committee.save() self.term = Term(term=Term.SPRING, year=2012, current=True) self.term.save() self.event_type, _ = EventType.objects.get_or_create( name='Test Event Type')
def test_save(self): num = OfficerPosition.objects.count() it_officer = OfficerPosition( short_name="IT_test", long_name="Information Technology (test)", rank=2, mailing_list="IT" ) it_officer.save() self.assertTrue(OfficerPosition.objects.exists()) exec_officer = OfficerPosition( short_name="exec test", long_name="Executive Officer (test)", rank=1, mailing_list="exec" ) exec_officer.save() positions = OfficerPosition.objects.order_by("pk") self.assertEquals(len(positions) - num, 2) self.assertEquals(positions[num].short_name, "IT_test") self.assertEquals(positions[num].rank, 2) self.assertEquals(positions[num + 1].short_name, "exec test") self.assertEquals(positions[num + 1].rank, 1)
class StudentOrgUserProfilesTest(UserInfoTestCase): def setUp(self): super(StudentOrgUserProfilesTest, self).setUp() self.model = StudentOrgUserProfile self.profile = self.model(user=self.user) self.profile.save() self.house_leader = OfficerPosition(short_name='house-leader', long_name='House Leader (test)', rank=3, mailing_list='IT') self.house_leader.save() self.advisor_pos = OfficerPosition(short_name='advisor', long_name='Advisor (test)', rank=4, mailing_list='IT', auxiliary=True) self.advisor_pos.save() 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 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_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_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_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_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_student_org_user_profile_post_save(self): """Tests whether creating and saving a StudentOrgUserProfile properly ensures that a CollegeStudentInfo object exists for the user in the post_save callback. """ self.assertIsNotNone( get_object_or_none(CollegeStudentInfo, user=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()
class EventTesting(TestCase): """Define a common setUp and helper method for event testing. Subclassed below for ease of testing various aspects of events. """ def setUp(self): Group.objects.create(name='Current Candidate') Group.objects.create(name='Member') self.user = get_user_model().objects.create_user( username='******', email='*****@*****.**', password='******', first_name='Bentley', last_name='Bent') self.committee = OfficerPosition(short_name='IT', long_name='Information Technology', rank=2, mailing_list='IT') self.committee.save() self.term = Term(term=Term.SPRING, year=2012, current=True) self.term.save() self.event_type, _ = EventType.objects.get_or_create( name='Test Event Type') def create_event(self, start_time, end_time, name='My Test Event', restriction=Event.OFFICER): """Create, save, and return a new event.""" event = Event(name=name, event_type=self.event_type, start_datetime=start_time, end_datetime=end_time, term=self.term, location='A test location', contact=self.user, committee=self.committee, restriction=restriction) event.save() return event def assert_can_view(self, event, user): """Assert that the given event can be viewed by the given user.""" self.assertTrue( event.can_user_view(self.user), 'Should be able to view {} event'.format( event.get_restriction_display())) def assert_cannot_view(self, event, user): """Assert that the given event cannot be viewed by the given user.""" self.assertFalse( event.can_user_view(self.user), 'Should not be able to view {} event'.format( event.get_restriction_display())) def assert_can_sign_up(self, event, user): """Assert that the given event can be viewed by the given user.""" self.assertTrue( event.can_user_sign_up(self.user), 'Should be able to sign up for {} event'.format( event.get_restriction_display())) def assert_cannot_sign_up(self, event, user): """Assert that the given event can be viewed by the given user.""" self.assertFalse( event.can_user_sign_up(self.user), 'Should not be able to sign up for {} event'.format( event.get_restriction_display()))
class OfficerGroupsTest(TestCase): def setUp(self): self.user = get_user_model().objects.create_user( username="******", email="*****@*****.**", password="******", first_name="Off", last_name="Icer" ) self.term = Term(term=Term.SPRING, year=2012, current=True) self.term.save() self.term_old = Term(term=Term.FALL, year=2011) self.term_old.save() self.position_exec = OfficerPosition( short_name="vp", long_name="Vice President", rank=1, mailing_list="vp", executive=True ) self.position_exec.save() self.position_regular = OfficerPosition( short_name="it", long_name="Information Technology", rank=2, mailing_list="it" ) self.position_regular.save() self.position_auxiliary = OfficerPosition( short_name="advisor", long_name="Advisor", rank=3, mailing_list="advisor", auxiliary=True ) self.position_auxiliary.save() # Some standard groups: self.officer_group = Group.objects.create(name="Officer") self.officer_group_curr = Group.objects.create(name="Current Officer") self.exec_group = Group.objects.create(name="Executive") self.exec_group_curr = Group.objects.create(name="Current Executive") # Groups from officer positions: self.pos_exec_group = Group.objects.create(name=self.position_exec.long_name) self.pos_exec_group_curr = Group.objects.create(name="Current {}".format(self.position_exec.long_name)) self.pos_reg_group = Group.objects.create(name=self.position_regular.long_name) self.pos_reg_group_curr = Group.objects.create(name="Current {}".format(self.position_regular.long_name)) self.pos_aux_group = Group.objects.create(name=self.position_auxiliary.long_name) self.pos_aux_group_curr = Group.objects.create(name="Current {}".format(self.position_auxiliary.long_name)) def test_get_corresponding_groups(self): """Test the OfficerPosition.get_corresponding_groups method.""" # If we specify the term that is the current term, then the "Current" # groups should be included; otherwise, "Current" groups should not be # included. # Check the corresponding groups for a "regular" (non-auxiliary, # non-exec) officer position. We should expect the corresponding groups # to be Officer and the group specific to the position: groups = [self.officer_group, self.pos_reg_group] self.assertItemsEqual(groups, self.position_regular.get_corresponding_groups()) self.assertItemsEqual(groups, self.position_regular.get_corresponding_groups(self.term_old)) # For the current term: groups.extend([self.officer_group_curr, self.pos_reg_group_curr]) self.assertItemsEqual(groups, self.position_regular.get_corresponding_groups(term=self.term)) # For the executive position, the corresponding groups will also # include the "Executive" groups: groups = [self.officer_group, self.exec_group, self.pos_exec_group] self.assertItemsEqual(groups, self.position_exec.get_corresponding_groups()) self.assertItemsEqual(groups, self.position_exec.get_corresponding_groups(self.term_old)) # For the current term: groups.extend([self.officer_group_curr, self.exec_group_curr, self.pos_exec_group_curr]) self.assertItemsEqual(groups, self.position_exec.get_corresponding_groups(term=self.term)) # For the auxiliary position, there should be no "Officer" group or # "Executive" group (since the position is non-exec): groups = [self.pos_aux_group] self.assertItemsEqual(groups, self.position_auxiliary.get_corresponding_groups()) self.assertItemsEqual(groups, self.position_auxiliary.get_corresponding_groups(self.term_old)) # For the current term: groups.append(self.pos_aux_group_curr) self.assertItemsEqual(groups, self.position_auxiliary.get_corresponding_groups(term=self.term)) 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_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_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_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_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)
class OfficerGroupsTest(TestCase): def setUp(self): self.user = get_user_model().objects.create_user( username='******', email='*****@*****.**', password='******', first_name='Off', last_name='Icer') self.term = Term(term=Term.SPRING, year=2012, current=True) self.term.save() self.term_old = Term(term=Term.FALL, year=2011) self.term_old.save() self.position_exec = OfficerPosition(short_name='vp', long_name='Vice President', rank=1, mailing_list='vp', executive=True) self.position_exec.save() self.position_regular = OfficerPosition( short_name='it', long_name='Information Technology', rank=2, mailing_list='it') self.position_regular.save() self.position_auxiliary = OfficerPosition(short_name='advisor', long_name='Advisor', rank=3, mailing_list='advisor', auxiliary=True) self.position_auxiliary.save() # Some standard groups: self.officer_group = Group.objects.create(name='Officer') self.officer_group_curr = Group.objects.create(name='Current Officer') self.exec_group = Group.objects.create(name='Executive') self.exec_group_curr = Group.objects.create(name='Current Executive') # Groups from officer positions: self.pos_exec_group = Group.objects.create( name=self.position_exec.long_name) self.pos_exec_group_curr = Group.objects.create( name='Current {}'.format(self.position_exec.long_name)) self.pos_reg_group = Group.objects.create( name=self.position_regular.long_name) self.pos_reg_group_curr = Group.objects.create( name='Current {}'.format(self.position_regular.long_name)) self.pos_aux_group = Group.objects.create( name=self.position_auxiliary.long_name) self.pos_aux_group_curr = Group.objects.create( name='Current {}'.format(self.position_auxiliary.long_name)) def test_get_corresponding_groups(self): """Test the OfficerPosition.get_corresponding_groups method.""" # If we specify the term that is the current term, then the "Current" # groups should be included; otherwise, "Current" groups should not be # included. # Check the corresponding groups for a "regular" (non-auxiliary, # non-exec) officer position. We should expect the corresponding groups # to be Officer and the group specific to the position: groups = [self.officer_group, self.pos_reg_group] self.assertItemsEqual(groups, self.position_regular.get_corresponding_groups()) self.assertItemsEqual( groups, self.position_regular.get_corresponding_groups(self.term_old)) # For the current term: groups.extend([self.officer_group_curr, self.pos_reg_group_curr]) self.assertItemsEqual( groups, self.position_regular.get_corresponding_groups(term=self.term)) # For the executive position, the corresponding groups will also # include the "Executive" groups: groups = [self.officer_group, self.exec_group, self.pos_exec_group] self.assertItemsEqual(groups, self.position_exec.get_corresponding_groups()) self.assertItemsEqual( groups, self.position_exec.get_corresponding_groups(self.term_old)) # For the current term: groups.extend([ self.officer_group_curr, self.exec_group_curr, self.pos_exec_group_curr ]) self.assertItemsEqual( groups, self.position_exec.get_corresponding_groups(term=self.term)) # For the auxiliary position, there should be no "Officer" group or # "Executive" group (since the position is non-exec): groups = [self.pos_aux_group] self.assertItemsEqual( groups, self.position_auxiliary.get_corresponding_groups()) self.assertItemsEqual( groups, self.position_auxiliary.get_corresponding_groups(self.term_old)) # For the current term: groups.append(self.pos_aux_group_curr) self.assertItemsEqual( groups, self.position_auxiliary.get_corresponding_groups(term=self.term)) 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_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_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_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_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)
class EventTesting(TestCase): """Define a common setUp and helper method for event testing. Subclassed below for ease of testing various aspects of events. """ def setUp(self): Group.objects.create(name='Current Candidate') Group.objects.create(name='Member') self.user = get_user_model().objects.create_user( username='******', email='*****@*****.**', password='******', first_name='Bentley', last_name='Bent') self.committee = OfficerPosition( short_name='IT', long_name='Information Technology', rank=2, mailing_list='IT') self.committee.save() self.term = Term(term=Term.SPRING, year=2012, current=True) self.term.save() self.event_type, _ = EventType.objects.get_or_create( name='Test Event Type') def create_event(self, start_time, end_time, name='My Test Event', restriction=Event.OFFICER): """Create, save, and return a new event.""" event = Event(name=name, event_type=self.event_type, start_datetime=start_time, end_datetime=end_time, term=self.term, location='A test location', contact=self.user, committee=self.committee, restriction=restriction) event.save() return event def assert_can_view(self, event, user): """Assert that the given event can be viewed by the given user.""" self.assertTrue( event.can_user_view(self.user), 'Should be able to view {} event'.format( event.get_restriction_display())) def assert_cannot_view(self, event, user): """Assert that the given event cannot be viewed by the given user.""" self.assertFalse( event.can_user_view(self.user), 'Should not be able to view {} event'.format( event.get_restriction_display())) def assert_can_sign_up(self, event, user): """Assert that the given event can be viewed by the given user.""" self.assertTrue( event.can_user_sign_up(self.user), 'Should be able to sign up for {} event'.format( event.get_restriction_display())) def assert_cannot_sign_up(self, event, user): """Assert that the given event can be viewed by the given user.""" self.assertFalse( event.can_user_sign_up(self.user), 'Should not be able to sign up for {} event'.format( event.get_restriction_display()))
class StudentOrgUserProfilesTest(UserInfoTestCase): def setUp(self): super(StudentOrgUserProfilesTest, self).setUp() self.model = StudentOrgUserProfile self.profile = self.model(user=self.user) self.profile.save() self.house_leader = OfficerPosition( short_name="house-leader", long_name="House Leader (test)", rank=3, mailing_list="IT" ) self.house_leader.save() self.advisor_pos = OfficerPosition( short_name="advisor", long_name="Advisor (test)", rank=4, mailing_list="IT", auxiliary=True ) self.advisor_pos.save() 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 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_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_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_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_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_student_org_user_profile_post_save(self): """Tests whether creating and saving a StudentOrgUserProfile properly ensures that a CollegeStudentInfo object exists for the user in the post_save callback. """ self.assertIsNotNone(get_object_or_none(CollegeStudentInfo, user=self.user))