def test_minidome_session_vars(self):
        """Test different stories are delivered in different sessions"""
        self.login_testuser()
        response = self.client.get(reverse("minidome"))
        request = response.wsgi_request

        self.story_ids1 = get_expirable_var(request.session, "minidome_stories", None)
        # del request.session['minidome_stories']
        request.session.flush()
        response = self.client.get(reverse("minidome"))

        request = response.wsgi_request
        self.story_ids2 = get_expirable_var(request.session, "minidome_stories", None)
        self.assertNotEqual(self.story_ids1, self.story_ids2)
 def test_minidome_shows_appropriate_stories_public(self):
     """Test all stories are public in public match"""
     self.response = self.client.get(reverse("minidome"))
     self.request = self.response.wsgi_request
     self.story_ids = get_expirable_var(
         self.request.session, "minidome_stories", None
     )
     story_instance0 = Story.objects.get(pk=self.story_ids[0])
     story_instance1 = Story.objects.get(pk=self.story_ids[1])
     self.assertEqual(story_instance0.access, Story.PUBLIC)
     self.assertEqual(story_instance1.access, Story.PUBLIC)
 def test_minidome_shows_appropriate_stories_logged_in(self):
     """Test all stories are at least logged in access level in public match"""
     self.login_testuser()
     self.response = self.client.get(reverse("minidome"))
     self.request = self.response.wsgi_request
     self.story_ids = get_expirable_var(
         self.request.session, "minidome_stories", None
     )
     story_instance0 = Story.objects.get(pk=self.story_ids[0])
     story_instance1 = Story.objects.get(pk=self.story_ids[1])
     self.assertTrue(story_instance0.access >= Story.LOGGED_IN)
     self.assertTrue(story_instance1.access >= Story.LOGGED_IN)
    def test_minidome_judge_limits(self):
        """test suer blocked for period after submission"""
        self.login_testuser()
        response = self.client.get(reverse("minidome"))
        request = response.wsgi_request
        self.story_ids = get_expirable_var(request.session, "minidome_stories", None)
        response = self.client.post(
            reverse("minidome"),
            {
                "winner": self.story_ids[0],
                "category": Story.LOGGED_IN,
            },
        )
        request = response.wsgi_request
        # follow = True becuase we reddirect but still want to read the messages
        response = self.client.get(reverse("minidome"), follow=True)
        request = response.wsgi_request

        self.judged_recently = get_expirable_var(
            request.session, "minidome_judged", None
        )
        self.assertTrue(self.judged_recently)
Exemplo n.º 5
0
 def test_expired_variables(self):
     """tests session vars can be added via functions"""
     self.client.get('/')
     sess = self.client.session
     set_expirable_var(sess, "test_var", "here",
                       timezone.now() - timezone.timedelta(days=3))
     var = get_expirable_var(
         sess,
         "test_var",
     )
     self.assertFalse(var)
     var = get_expirable_var_time_to_del(sess, "test_var")
     #self.assertFalse(var - timezone.now())
     self.assertTrue(
         timezone.now() -
         timezone.timedelta(minutes=1) <= var <= timezone.now() +
         timezone.timedelta(minutes=1))
    def test_minidome_bad_form(self):
        """Checks to see if invalid form handled"""
        self.response = self.client.get(reverse("minidome"))
        self.request = self.response.wsgi_request
        self.story_ids = get_expirable_var(
            self.request.session, "minidome_stories", None
        )

        self.client.post(
            reverse("minidome"),
            {
                "winner": "a",
                "category": Story.PRIVATE,
            },
        )
        story_instance0 = Story.objects.get(pk=self.story_ids[0])
        story_instance1 = Story.objects.get(pk=self.story_ids[1])
        self.assertFalse(story_instance0.stats.minidome_public_wins > 0)
        self.assertFalse(story_instance1.stats.minidome_public_losses > 0)
    def test_minidome_judge_process(self):
        """test judment creates scoress"""
        self.response = self.client.get(reverse("minidome"))
        self.request = self.response.wsgi_request
        self.story_ids = get_expirable_var(
            self.request.session, "minidome_stories", None
        )
        response = self.client.post(
            reverse("minidome"),
            {
                "winner": self.story_ids[0],
                "category": Story.PUBLIC,
            },
        )

        story_instance0 = Story.objects.get(pk=self.story_ids[0])
        story_instance1 = Story.objects.get(pk=self.story_ids[1])
        self.assertTrue(story_instance0.stats.minidome_public_wins)
        self.assertTrue(story_instance1.stats.minidome_public_losses)
        self.assertRedirects(response, "/")
 def test_minidome_uneven_stats_logged_in(self):
     """checks to make sure process works if all scores are not 0"""
     # self.set_up_minidome_public()
     self.create_minidome_logged_in()
     self.login_testuser()
     self.response = self.client.get(reverse("minidome"))
     self.request = self.response.wsgi_request
     self.story_ids = get_expirable_var(
         self.request.session, "minidome_stories", None
     )
     response = self.client.post(
         reverse("minidome"),
         {
             "winner": self.story_ids[0],
             "category": Story.LOGGED_IN,
         },
     )
     story_instance0 = Story.objects.get(pk=self.story_ids[0])
     story_instance1 = Story.objects.get(pk=self.story_ids[1])
     self.assertTrue(story_instance0.stats.minidome_logged_in_wins > 0)
     self.assertTrue(story_instance1.stats.minidome_logged_in_losses > 0)
     self.assertRedirects(response, "/")
Exemplo n.º 9
0
def minidome(request):
    """
    publicly decided ranking between two stories
    First story is random from all stories
    second story is random from those with fewer test than first story
    Process:
        test if already judged (redirect if have)
        Use existing stories if session vars set (prevents gaming system)
        If no stories in session vars, set stories
        choose and deliver form
    """

    # First test to make sure they haven't judged recently (prevent spam)

    logged_in = request.user.is_authenticated

    # next check the values in the session variable to get current stories
    story_ids = get_expirable_var(request.session, "minidome_stories", None)
    if story_ids is not None:
        story1 = Story.objects.get(pk=story_ids[0])
        story2 = Story.objects.get(pk=story_ids[1])
        stories_options = [story1, story2]

        #choose the appropriate form (logged_in aloows comments)
        if logged_in:
            form = MiniDomeLoggedInForm([story1.pk, story2.pk], request.POST
                                        or None)

        else:
            form = MiniDomePublicForm([story1.pk, story2.pk], request.POST
                                      or None)

        if request.method == "POST" and story_ids and form.is_valid():
            return handle_form_submission(form, stories_options, logged_in,
                                          request)

    else:
        #get the upper and lower ends of stories' num minidome tests
        tests_bounds = get_tests_bounds(logged_in, request.user.id)

        if not isinstance(tests_bounds["min_tests"], int):
            messages.error(request, "Not enough stories")
            return redirect("home")
        # get stories, passing the minimum in case our first story shares it
        stories = set_minidome_stories(request, logged_in,
                                       tests_bounds["min_tests"])
        if isinstance(stories, str):
            # Stories has returned an error essage

            messages.error(request, stories)
            return redirect("home")
        story1 = stories[0]
        story2 = stories[1]
        form = stories[2]

    #we don't want people cycling through stories until they get to one they like
    #so we set a session variable to keep them constant
    #This variable is deleted upon judgement (with 5 minute delay)
    expire_at = datetime.today() + timedelta(
        minutes=MINUTES_BEFORE_MINIDOME_STORY_REFRESH)
    set_expirable_var(
        request.session,
        "minidome_stories",
        [story1.pk, story2.pk],
        expire_at,
    )

    minidome_judged = get_expirable_var(request.session, "minidome_judged",
                                        None)
    ttl = get_expirable_var_time_to_del(request.session, "minidome_judged")
    return render(
        request,
        "minidome/minidome.html",
        {
            "story2_context": story2,
            "story1_context": story1,
            "form": form,
            "minidome_judged": minidome_judged,
            "ttl": ttl,
            "wait": MINUTES_BEFORE_MINIDOME_NEXT_JUDGEMENT,
        },
    )