def testGroupMission(self): """ Test that a user can complete a group mission. """ self.mission.is_group = True self.mission.save() response = self.client.get(reverse("canopy_index")) self.assertContains(response, "Be the first to complete this mission!") self.assertContains(response, "No users are currently participating in this mission.") self.assertNotContains(response, reverse("activity_task", args=(self.activity.type, self.activity.slug))) self.assertContains(response, "Test mission") response = self.client.post(reverse("canopy_mission_accept", args=(self.mission.slug,)), follow=True) self.assertNotContains(response, "No users are currently participating in this mission.") self.assertContains(response, "Be the first to complete this mission!") self.assertContains(response, "The following users are up for this mission:") self.assertContains(response, reverse("activity_task", args=(self.activity.type, self.activity.slug))) self.assertContains(response, 'alt="Photo of Test U."', count=2) activity_member = ActivityMember( user=self.user, activity=self.activity, approval_status="approved" ) activity_member.save() response = self.client.get(reverse("canopy_index")) self.assertNotContains(response, "Be the first to complete this mission!") self.assertContains(response, "No users are currently participating in this mission.") self.assertContains(response, 'alt="Photo of Test U."', count=2)
def testCompletedActivity(self): """Tests that completed_task works when a task is completed.""" activity = Activity( type="activity", name="Test", title="Test activity", slug="test-activity", description="Variable points!", duration=10, point_value=10, pub_date=datetime.datetime.today(), expire_date=datetime.datetime.today() + datetime.timedelta(days=7), confirm_type="text", ) activity.save() # Test within context of a quest self.quest.unlock_conditions = "completed_task(slug='test-activity')" self.quest.save() quests = get_quests(self.user) self.assertTrue(self.quest not in quests, "User should not be able to participate in this quest.") member = ActivityMember(user=self.user, activity=activity, approval_status="approved") member.save() self.assertTrue(completed_task(self.user, slug="test-activity"), "User should have completed 'Test'.") self.assertTrue(completed_task(self.user, task_type="activity"), "User should have completed an activity") quests = get_quests(self.user) self.assertTrue(self.quest in quests["available_quests"], "User should be able to participate in this quest.") self.quest.unlock_conditions = "completed_task(task_type='activity')" self.quest.save() quests = get_quests(self.user) self.assertTrue(self.quest in quests["available_quests"], "User should be able to participate in this quest.") # Test as a completion condition. self.quest.accept(self.user) self.quest.completion_conditions = "not completed_task(slug='test-activity')" self.quest.save() completed_quests = possibly_completed_quests(self.user) self.assertTrue(self.quest not in completed_quests, "User should not be able to complete the quest.") self.quest.completion_conditions = "not completed_task(task_type='activity')" self.quest.save() completed_quests = possibly_completed_quests(self.user) self.assertTrue(self.quest not in completed_quests, "User should not be able to complete the quest.") self.quest.completion_conditions = "completed_task(slug='test-activity')" self.quest.save() completed_quests = possibly_completed_quests(self.user) self.assertTrue(self.quest in completed_quests, "User should have completed the quest.")
def simulate_activities_for_round_2(self): round_start = datetime.datetime.strptime(settings.COMPETITION_ROUNDS["Round 2"]["start"], "%Y-%m-%d").date() round_end = datetime.datetime.strptime(settings.COMPETITION_ROUNDS["Round 2"]["end"], "%Y-%m-%d").date() today_diff = (datetime.date.today() - round_start).days self.stdout.write("Simulating activity participation in the second round.\n") for profile in Profile.objects.all(): # In this case, let's just add a pending activity or two. activities = get_available_activities(profile.user).order_by("?")[0:random.randint(0, 2)] for activity in activities: member = ActivityMember(user=profile.user, activity=activity) member.submission_date = datetime.datetime.today() - datetime.timedelta(days=random.randint(0, today_diff)) member.approval_status = "pending" member.save()
def testGroupMission(self): """ Test that a user can complete a group mission. """ self.mission.is_group = True self.mission.save() response = self.client.get(reverse("canopy_index")) self.assertContains(response, "Be the first to complete this mission!") self.assertContains( response, "No users are currently participating in this mission.") self.assertNotContains( response, reverse("activity_task", args=(self.activity.type, self.activity.slug))) self.assertContains(response, "Test mission") response = self.client.post(reverse("canopy_mission_accept", args=(self.mission.slug, )), follow=True) self.assertNotContains( response, "No users are currently participating in this mission.") self.assertContains(response, "Be the first to complete this mission!") self.assertContains(response, "The following users are up for this mission:") self.assertContains( response, reverse("activity_task", args=(self.activity.type, self.activity.slug))) self.assertContains(response, 'alt="Photo of Test U."', count=2) activity_member = ActivityMember(user=self.user, activity=self.activity, approval_status="approved") activity_member.save() response = self.client.get(reverse("canopy_index")) self.assertNotContains(response, "Be the first to complete this mission!") self.assertContains( response, "No users are currently participating in this mission.") self.assertContains(response, 'alt="Photo of Test U."', count=2)
def testSoloMission(self): """ Test that a user can view and complete a solo mission. """ response = self.client.get(reverse("canopy_index")) self.assertContains(response, "Be the first to complete this mission!") self.assertContains(response, "Test mission") self.assertContains(response, reverse("activity_task", args=(self.activity.type, self.activity.slug))) activity_member = ActivityMember( user=self.user, activity=self.activity, approval_status="approved" ) activity_member.save() response = self.client.get(reverse("canopy_index")) self.assertNotContains(response, "Be the first to complete this mission!") self.assertContains(response, 'alt="Photo of Test U."', count=2)
def simulate_activities_for_round_1(self): round_start = datetime.datetime.strptime(settings.COMPETITION_ROUNDS["Round 1"]["start"], "%Y-%m-%d").date() round_end = datetime.datetime.strptime(settings.COMPETITION_ROUNDS["Round 1"]["end"], "%Y-%m-%d").date() date_delta = (round_end - round_start).days self.stdout.write("Simulating activity participation in the first round.\n") activities = Activity.objects.filter(pub_date__gte=round_start, pub_date__lt=round_end) for profile in Profile.objects.all(): if(profile.user.username == "admin"): # We want to skip admin for now. continue # Assume user will participate between 0 and 3 activities # Random activity from http://stackoverflow.com/questions/962619/how-to-pull-a-random-record-using-djangos-orm user_activities = activities.order_by("?")[0:random.randint(0, 3)] for activity in user_activities: member = ActivityMember(user=profile.user, activity=activity) member.submission_date = datetime.datetime.combine(round_start, datetime.time()) + datetime.timedelta(days=random.randint(0, date_delta)) member.approval_status = "approved" member.save()
def simulate_activities_for_round_1(self): round_start = datetime.datetime.strptime( settings.COMPETITION_ROUNDS["Round 1"]["start"], "%Y-%m-%d").date() round_end = datetime.datetime.strptime( settings.COMPETITION_ROUNDS["Round 1"]["end"], "%Y-%m-%d").date() date_delta = (round_end - round_start).days self.stdout.write( "Simulating activity participation in the first round.\n") activities = Activity.objects.filter(pub_date__gte=round_start, pub_date__lt=round_end) for profile in Profile.objects.all(): if (profile.user.username == "admin"): # We want to skip admin for now. continue # Assume user will participate between 0 and 3 activities # Random activity from http://stackoverflow.com/questions/962619/how-to-pull-a-random-record-using-djangos-orm user_activities = activities.order_by("?")[0:random.randint(0, 3)] for activity in user_activities: member = ActivityMember(user=profile.user, activity=activity) member.submission_date = datetime.datetime.combine( round_start, datetime.time()) + datetime.timedelta( days=random.randint(0, date_delta)) member.approval_status = "approved" member.save()
def testSoloMission(self): """ Test that a user can view and complete a solo mission. """ response = self.client.get(reverse("canopy_index")) self.assertContains(response, "Be the first to complete this mission!") self.assertContains(response, "Test mission") self.assertContains( response, reverse("activity_task", args=(self.activity.type, self.activity.slug))) activity_member = ActivityMember(user=self.user, activity=self.activity, approval_status="approved") activity_member.save() response = self.client.get(reverse("canopy_index")) self.assertNotContains(response, "Be the first to complete this mission!") self.assertContains(response, 'alt="Photo of Test U."', count=2)
def testRoundsUpdate(self): """Test that the score for the round updates when an activity is approved.""" entry, created = ScoreboardEntry.objects.get_or_create( profile=self.user.get_profile(), round_name=self.current_round, ) round_points = entry.points round_submission_date = entry.last_awarded_submission activity_points = self.activity.point_value activity_member = ActivityMember(user=self.user, activity=self.activity) activity_member.approval_status = "approved" activity_member.save() # Verify that the points for the round has been updated. entry, created = ScoreboardEntry.objects.get_or_create( profile=self.user.get_profile(), round_name=self.current_round, ) self.assertEqual(round_points + activity_points, entry.points) self.assertNotEqual(round_submission_date, entry.last_awarded_submission)
def testRejectedActivity(self): """ Test that a rejected activity submission posts a message. """ activity = Activity( title="Test activity", description="Testing!", duration=10, point_value=10, pub_date=datetime.datetime.today(), expire_date=datetime.datetime.today() + datetime.timedelta(days=7), confirm_type="text", type="activity", ) activity.save() member = ActivityMember(activity=activity, user=self.user, approval_status="rejected") member.save() response = self.client.get(reverse("activity_index")) self.assertContains(response, "Your response to <a href='%s'>%s</a> was not approved" % ( reverse("activity_task", args=(activity.id,)), activity.title, )) response = self.client.get(reverse("activity_index")) self.assertNotContains(response, "notification-box")
def testActivityAchievement(self): """Check that the user's activity achievements are loaded.""" activity = Activity( title="Test activity", description="Testing!", duration=10, point_value=10, pub_date=datetime.datetime.today(), expire_date=datetime.datetime.today() + datetime.timedelta(days=7), confirm_type="text", type="activity", is_canopy=True ) activity.save() # Test that profile page has a pending activity. member = ActivityMember(user=self.user, activity=activity, approval_status="approved") member.save() response = self.client.get(reverse("profile_index")) self.assertContains(response, reverse("activity_task", args=(activity.type, activity.slug,))) self.assertContains(response, "Canopy Activity:") self.assertContains(response, "%d (Karma)" % activity.point_value) # Test adding an event to catch a bug. event = Activity( title="Test event", description="Testing!", duration=10, point_value=10, pub_date=datetime.datetime.today(), expire_date=datetime.datetime.today() + datetime.timedelta(days=7), confirm_type="text", type="event", ) event.save() member = ActivityMember(user=self.user, activity=event, approval_status="pending") member.save() response = self.client.get(reverse("profile_index")) self.assertContains(response, reverse("activity_task", args=(activity.type, activity.slug,))) self.assertContains(response, "Pending") self.assertContains(response, "Activity:") self.assertContains(response, "Event:") self.assertNotContains(response, "You have nothing in progress or pending.")
def testSoloMissionCompletion(self): """ Test that a solo mission is completed when its related activity is completed. """ user = User.objects.create_user("user", "*****@*****.**") activity = Activity.objects.create( title="Test activity", slug="test-activity", description="Testing!", duration=10, point_value=10, pub_date=datetime.datetime.today(), expire_date=datetime.datetime.today() + datetime.timedelta(days=7), confirm_type="text", type="activity", ) mission = Mission.objects.create(name="Test mission", slug="test-mission", description="A test mission", is_group=False) mission.activities.add(activity) activity_member = ActivityMember(user=user, activity=activity, approval_status="pending") activity_member.save() try: mission_member = MissionMember.objects.get(user=user, mission=mission, completed=True) self.fail("User should not have completed this mission.") except MissionMember.DoesNotExist: pass activity_member.approval_status = "approved" activity_member.save() try: mission_member = MissionMember.objects.get(user=user, mission=mission, completed=True) except MissionMember.DoesNotExist: self.fail("User should have completed this mission.")
def testSoloMissionCompletion(self): """ Test that a solo mission is completed when its related activity is completed. """ user = User.objects.create_user("user", "*****@*****.**") activity = Activity.objects.create( title="Test activity", slug="test-activity", description="Testing!", duration=10, point_value=10, pub_date=datetime.datetime.today(), expire_date=datetime.datetime.today() + datetime.timedelta(days=7), confirm_type="text", type="activity", ) mission = Mission.objects.create( name="Test mission", slug="test-mission", description="A test mission", is_group=False ) mission.activities.add(activity) activity_member = ActivityMember( user=user, activity=activity, approval_status="pending" ) activity_member.save() try: mission_member = MissionMember.objects.get(user=user, mission=mission, completed=True) self.fail("User should not have completed this mission.") except MissionMember.DoesNotExist: pass activity_member.approval_status = "approved" activity_member.save() try: mission_member = MissionMember.objects.get(user=user, mission=mission, completed=True) except MissionMember.DoesNotExist: self.fail("User should have completed this mission.")
def testNumTasksCompletedWithType(self): activity = Activity( type="activity", name="Test", title="Test activity", description="Variable points!", duration=10, point_value=10, pub_date=datetime.datetime.today(), expire_date=datetime.datetime.today() + datetime.timedelta(days=7), confirm_type="text", ) activity.save() # Test activities member = ActivityMember(user=self.user, activity=activity, approval_status="pending") member.save() self.assertFalse(num_tasks_completed(self.user, 1, task_type="activity"), "User with pending activity should not have completed a task.") # Test within context of a quest self.quest.unlock_conditions = "num_tasks_completed(1, task_type='activity')" self.quest.save() quests = get_quests(self.user) self.assertTrue(self.quest not in quests, "User should not be able to participate in this quest.") # Check that the user can now add the quest. member.approval_status = "approved" member.save() quests = get_quests(self.user) self.assertTrue(self.quest in quests["available_quests"], "User should be able to participate in this quest.") # Test as a completion condition. self.quest.accept(self.user) self.quest.completion_conditions = "num_tasks_completed(2, task_type='activity')" self.quest.save() completed_quests = possibly_completed_quests(self.user) self.assertTrue(self.quest not in completed_quests, "User should not be able to complete the quest.") self.quest.completion_conditions = "num_tasks_completed(1, task_type='activity')" self.quest.save() completed_quests = possibly_completed_quests(self.user) self.assertTrue(self.quest in completed_quests, "User should have completed the quest.")
def testNumTasksCompletedWithType(self): activity = Activity( type="activity", name="Test", title="Test activity", description="Variable points!", duration=10, point_value=10, pub_date=datetime.datetime.today(), expire_date=datetime.datetime.today() + datetime.timedelta(days=7), confirm_type="text", ) activity.save() # Test activities member = ActivityMember(user=self.user, activity=activity, approval_status="pending") member.save() self.assertFalse( num_tasks_completed(self.user, 1, task_type="activity"), "User with pending activity should not have completed a task.", ) # Test within context of a quest self.quest.unlock_conditions = "num_tasks_completed(1, task_type='activity')" self.quest.save() quests = get_quests(self.user) self.assertTrue(self.quest not in quests, "User should not be able to participate in this quest.") # Check that the user can now add the quest. member.approval_status = "approved" member.save() quests = get_quests(self.user) self.assertTrue(self.quest in quests["available_quests"], "User should be able to participate in this quest.") # Test as a completion condition. self.quest.accept(self.user) self.quest.completion_conditions = "num_tasks_completed(2, task_type='activity')" self.quest.save() completed_quests = possibly_completed_quests(self.user) self.assertTrue(self.quest not in completed_quests, "User should not be able to complete the quest.") self.quest.completion_conditions = "num_tasks_completed(1, task_type='activity')" self.quest.save() completed_quests = possibly_completed_quests(self.user) self.assertTrue(self.quest in completed_quests, "User should have completed the quest.")
def simulate_activities_for_round_2(self): round_start = datetime.datetime.strptime( settings.COMPETITION_ROUNDS["Round 2"]["start"], "%Y-%m-%d").date() round_end = datetime.datetime.strptime( settings.COMPETITION_ROUNDS["Round 2"]["end"], "%Y-%m-%d").date() today_diff = (datetime.date.today() - round_start).days self.stdout.write( "Simulating activity participation in the second round.\n") for profile in Profile.objects.all(): # In this case, let's just add a pending activity or two. activities = get_available_activities( profile.user).order_by("?")[0:random.randint(0, 2)] for activity in activities: member = ActivityMember(user=profile.user, activity=activity) member.submission_date = datetime.datetime.today( ) - datetime.timedelta(days=random.randint(0, today_diff)) member.approval_status = "pending" member.save()
def testRoundDoesNotUpdate(self): """Test that the score for the round does not update for an activity submitted outside of the round.""" entry, created = ScoreboardEntry.objects.get_or_create( profile=self.user.get_profile(), round_name=self.current_round, ) round_points = entry.points round_submission_date = entry.last_awarded_submission activity_member = ActivityMember(user=self.user, activity=self.activity) activity_member.approval_status = "approved" activity_member.submission_date = datetime.datetime.today() - datetime.timedelta(days=1) activity_member.save() # Verify that the points for the round has not been updated. entry, created = ScoreboardEntry.objects.get_or_create( profile=self.user.get_profile(), round_name=self.current_round, ) self.assertEqual(round_points, entry.points) self.assertEqual(round_submission_date, entry.last_awarded_submission)
def testAwardRollback(self): """Tests that the last_awarded_submission field rolls back to a previous task.""" user = User(username="******", password="******") user.save() activity1 = Activity( title="Test activity", slug="test-activity", description="Testing!", duration=10, point_value=10, pub_date=datetime.datetime.today(), expire_date=datetime.datetime.today() + datetime.timedelta(days=7), confirm_type="text", type="activity", ) activity1.save() activity2 = Activity( title="Test activity 2", slug="test-activity-2", description="Testing!", duration=10, point_value=15, pub_date=datetime.datetime.today(), expire_date=datetime.datetime.today() + datetime.timedelta(days=7), confirm_type="text", type="activity", ) activity2.save() activities = [activity1, activity2] # Submit the first activity. This is what we're going to rollback to. activity_member = ActivityMember( user=user, activity=activities[0], submission_date=datetime.datetime.today()) activity_member.approval_status = "approved" activity_member.submission_date = datetime.datetime.today( ) - datetime.timedelta(days=1) activity_member.save() points = user.get_profile().points submit_date = user.get_profile().last_awarded_submission # Submit second activity. activity_member = ActivityMember( user=user, activity=activities[1], submission_date=datetime.datetime.today()) activity_member.approval_status = "approved" activity_member.submission_date = datetime.datetime.today() activity_member.save() logs = user.pointstransaction_set.count() activity_member.approval_status = "rejected" activity_member.submission_date = datetime.datetime.today() activity_member.save() # Verify that we rolled back to the previous activity. self.assertEqual(points, user.get_profile().points)
def testAwardRollback(self): """Tests that the last_awarded_submission field rolls back to a previous task.""" user = User(username="******", password="******") user.save() activity1 = Activity( title="Test activity", slug="test-activity", description="Testing!", duration=10, point_value=10, pub_date=datetime.datetime.today(), expire_date=datetime.datetime.today() + datetime.timedelta(days=7), confirm_type="text", type="activity", ) activity1.save() activity2 = Activity( title="Test activity 2", slug="test-activity-2", description="Testing!", duration=10, point_value=15, pub_date=datetime.datetime.today(), expire_date=datetime.datetime.today() + datetime.timedelta(days=7), confirm_type="text", type="activity", ) activity2.save() activities = [activity1, activity2] # Submit the first activity. This is what we're going to rollback to. activity_member = ActivityMember(user=user, activity=activities[0], submission_date=datetime.datetime.today()) activity_member.approval_status = "approved" activity_member.submission_date = datetime.datetime.today() - datetime.timedelta(days=1) activity_member.save() points = user.get_profile().points submit_date = user.get_profile().last_awarded_submission # Submit second activity. activity_member = ActivityMember(user=user, activity=activities[1], submission_date=datetime.datetime.today()) activity_member.approval_status = "approved" activity_member.submission_date = datetime.datetime.today() activity_member.save() logs = user.pointstransaction_set.count() activity_member.approval_status = "rejected" activity_member.submission_date = datetime.datetime.today() activity_member.save() # Verify that we rolled back to the previous activity. self.assertEqual(points, user.get_profile().points)
def testActivityAchievement(self): """Check that the user's activity achievements are loaded.""" activity = Activity( title="Test activity", description="Testing!", duration=10, point_value=10, pub_date=datetime.datetime.today(), expire_date=datetime.datetime.today() + datetime.timedelta(days=7), confirm_type="text", type="activity", ) activity.save() # Test that profile page has a pending activity. member = ActivityMember(user=self.user, activity=activity, approval_status="pending") member.save() response = self.client.get(reverse("profile_index")) self.assertContains(response, reverse("activity_task", args=(activity.id,))) self.assertContains(response, "Pending") self.assertContains(response, "Activity:") self.assertContains(response, "You have not been awarded anything yet!") self.assertNotContains(response, "You have nothing in progress or pending.") # Test that the profile page has a rejected activity member.approval_status = "rejected" member.save() response = self.client.get(reverse("profile_index")) self.assertContains(response, reverse("activity_task", args=(activity.id,))) self.assertContains(response, "Rejected") self.assertContains(response, "You have not been awarded anything yet!") self.assertNotContains(response, "You have nothing in progress or pending.") # Use the argument and check that the context has a rejected member. response = self.client.get(reverse("profile_rejected", args=(member.id,)), follow=True) self.assertEqual(response.context["rejected_member"], member) # Test that the profile page has a completed activity member.approval_status = "approved" member.save() response = self.client.get(reverse("profile_index")) self.assertContains(response, reverse("activity_task", args=(activity.id,))) self.assertNotContains(response, "You have not been awarded anything yet!") self.assertContains(response, "You have nothing in progress or pending.") self.assertContains(response, "Activity:") # Test adding an event to catch a bug. event = Activity( title="Test event", description="Testing!", duration=10, point_value=10, pub_date=datetime.datetime.today(), expire_date=datetime.datetime.today() + datetime.timedelta(days=7), confirm_type="text", type="event", ) event.save() member = ActivityMember(user=self.user, activity=event, approval_status="pending") member.save() response = self.client.get(reverse("profile_index")) self.assertContains(response, reverse("activity_task", args=(activity.id,))) self.assertContains(response, "Pending") self.assertContains(response, "Activity:") self.assertContains(response, "Event:") self.assertNotContains(response, "You have nothing in progress or pending.")