Exemplo n.º 1
0
 def test_logged_in_only(self):
     self.assertTrue(
         FakeRequest(AnonymousUser()).experiments.is_in(
             'logged_in_only', 'control'))
     self.assertTrue(
         FakeRequest(create_user()).experiments.is_in(
             'logged_in_only', 'experimental'))
 def test_get_ephemeral_default_value(self):        
     request = FakeRequest(None)
     request.session = {}
     
     key = "key"
     value = "value"
     
     store_ephemeral(request, key, value)
     
     # Test default value
     default = 10
     ret = get_ephemeral(request, key, datetime.timedelta(days=-1), default)
     self.assertEqual(ret, default)
Exemplo n.º 3
0
    def test_get_ephemeral_default_value(self):
        request = FakeRequest(None)
        request.session = {}

        key = "key"
        value = "value"

        store_ephemeral(request, key, value)

        # Test default value
        default = 10
        ret = get_ephemeral(request, key, datetime.timedelta(days=-1), default)
        self.assertEqual(ret, default)
 def test_is_purchasable(self):
     user = create_rich_user()
     # Grab a purchasable sticker
     banana = stickers.get("banana")
     user.kv.stickers.add_limited_sticker(self.sticker)
     self.assertTrue(banana.cost)
     # Purchase it twice
     # @TODO: we should rafactor the api calls to be able to take 
     # arguments that are not wrapped in the request payload.
     request = FakeRequest(user)
     request.POST = dict(item_type="sticker", item_id=banana.type_id)
     request.method = "POST"
     request.body = {}
     for _ in range(0, 2):
         # Buy
         store_buy(request)
         self.assertTrue(banana.is_purchasable(user))
Exemplo n.º 5
0
    def test_force_into_experiment_middleware(self):
        request = FakeRequest(
            GET="force_experiment=forced_into_control:experimental")
        fem = ForceExperimentMiddleware()
        fem.process_request(request)

        self.assertTrue(
            request.experiments.is_in('forced_into_control', 'experimental'))
 def test_is_purchasable(self):
     user = create_rich_user()
     # Grab a purchasable sticker
     banana = stickers.get("banana")
     user.kv.stickers.add_limited_sticker(self.sticker)
     self.assertTrue(banana.cost)
     # Purchase it twice
     # @TODO: we should rafactor the api calls to be able to take
     # arguments that are not wrapped in the request payload.
     request = FakeRequest(user)
     request.POST = dict(item_type="sticker", item_id=banana.type_id)
     request.method = "POST"
     request.body = {}
     for _ in range(0, 2):
         # Buy
         store_buy(request)
         self.assertTrue(banana.is_purchasable(user))
Exemplo n.º 7
0
 def after_setUp(self):
     self.ip = '13.33.33.7'
     IPBlock.objects.create(
         ip=self.ip,
         moderator=create_staff(),
         timestamp=Now(),
     )
     self.request = FakeRequest()
     self.request.META['REMOTE_ADDR'] = self.ip
    def test_store_ephemeral(self):
        request = FakeRequest(None)
        request.session = {}
        
        key = "key"
        value = "value"
        
        ret = store_ephemeral(request, key, value)
        self.assertEqual(ret, value)
        self.assertEqual(request.session.get(key), value)
        
        self.assertEqual(len(request.session.keys()), 2)

        ret = get_ephemeral(request, key, ttl=datetime.timedelta(days=1))
        self.assertEqual(ret, value)
        
        ret = get_ephemeral(request, key, ttl=datetime.timedelta(days=-1))
        self.assertFalse(ret)
        self.assertFalse(request.session.get(key))
    def test_is_purchasable_limited_availability(self):
        user = create_rich_user()

        # Grab a limited availability sticker
        sticker = self.sticker
        user.kv.stickers.add_limited_sticker(sticker)

        self.assertTrue(sticker.is_purchasable(user))

        # Buy it
        request = FakeRequest(user)
        request.POST = dict(item_type="sticker", item_id=sticker)
        request.method = "POST"
        request.body = {}
        assert store_buy(request)

        # is_purchasable should still return True. The logic that checks for
        # whether you bought this sticker twice happens in the api.
        self.assertTrue(sticker.is_purchasable(user))
Exemplo n.º 10
0
    def test_store_ephemeral(self):
        request = FakeRequest(None)
        request.session = {}

        key = "key"
        value = "value"

        ret = store_ephemeral(request, key, value)
        self.assertEqual(ret, value)
        self.assertEqual(request.session.get(key), value)

        self.assertEqual(len(request.session.keys()), 2)

        ret = get_ephemeral(request, key, ttl=datetime.timedelta(days=1))
        self.assertEqual(ret, value)

        ret = get_ephemeral(request, key, ttl=datetime.timedelta(days=-1))
        self.assertFalse(ret)
        self.assertFalse(request.session.get(key))
    def test_is_purchasable_limited_availability(self):
        user = create_rich_user()

        # Grab a limited availability sticker
        sticker = self.sticker
        user.kv.stickers.add_limited_sticker(sticker)
        
        self.assertTrue(sticker.is_purchasable(user))
        
        # Buy it
        request = FakeRequest(user)
        request.POST = dict(item_type="sticker", item_id=sticker)
        request.method = "POST"
        request.body = {}
        assert store_buy(request)
        
        # is_purchasable should still return True. The logic that checks for 
        # whether you bought this sticker twice happens in the api.
        self.assertTrue(sticker.is_purchasable(user))
Exemplo n.º 12
0
    def test_migrate_experiments_from_request_to_user(self):
        request = FakeRequest(AnonymousUser())
        user = create_user()
        request.experiments.get(Experiments.forced_into_control)

        migrate_from_request_to_user(request, user)

        branches = list(user.redis.experiments.get_all_current_branches())
        self.assertEqual(
            [(Experiments.forced_into_control,
              Experiments.forced_into_control.branches["control"])], branches)
 def _ctx(self, user, cmt):
     author = cmt.author
     request = FakeRequest(user)
     return {
         'current_user_info': user.userinfo,
         'admin_infos': {
             cmt.id: {
                 'username': author.username,
             },
         },
         'request': request,
     }
Exemplo n.º 14
0
    def test_update_score(self):
        user = create_user()
        comment = create_comment(author=user)

        for sticker in stickers.primary_types:
            user = create_user()
            user.kv.stickers.currency.increment(100)
            # Sticker the comment a bunch.
            request = FakeRequest(user)
            api._sticker_comment(request, comment, sticker.type_id)

        # Update score
        comment.update_score()
Exemplo n.º 15
0
    def test_granualr_unsubscribe_blanket_ban(self):
        all_actions = EmailChannel.all_handled_actions()
        # ALL has inverted semantics ... make sure it works.
        all_actions.append("ALL")
        # Reuse the same user
        canvas_user = create_user()
        action = "ALL"
        actions_dict = {action: "on"}
        unsubscriptions = self.validate_unsubscript(actions_dict, canvas_user,
                                                    all_actions)
        for action in all_actions:
            # Ensure that we unsubscribed from all of them!
            assert unsubscriptions.get(action)

        action = "ALL"
        # Remove blanket subscription
        actions_dict = {}
        request = FakeRequest()
        views.handle_unsubscribe_post(canvas_user, actions_dict, request)
        unsubscriptions = views.get_unsubscriptions(canvas_user, all_actions)
        for action in all_actions:
            # Ensure that the user is now subscribed for everything, which is the default without the blanket ban.
            assert not unsubscriptions.get(action)
Exemplo n.º 16
0
    def validate_unsubscript(self,
                             actions_dict,
                             canvas_user=None,
                             all_actions=None):
        if not canvas_user:
            canvas_user = create_user()
        if not all_actions:
            all_actions = EmailChannel.all_handled_actions()

        request = FakeRequest()
        views.handle_unsubscribe_post(canvas_user, actions_dict, request)
        unsubscriptions = views.get_unsubscriptions(canvas_user, all_actions)
        for action in all_actions:
            if action == 'newsletter':
                continue
            value = action
            if action == "ALL":
                value = not action
            if actions_dict.get(action) == "on":
                assert not unsubscriptions.get(value)
            else:
                assert unsubscriptions.get(value)
        return unsubscriptions
Exemplo n.º 17
0
 def test_bad_branch_name_raises(self):
     with self.assertRaises(ValueError):
         FakeRequest().experiments.is_in("forced_into_control", "NOPE")
Exemplo n.º 18
0
 def test_api_does_not_record_view_metric(self):
     self.assertViewCount(FakeRequest(create_user(), path="/api/do_stuff"), HttpResponse(status=200), 0)
Exemplo n.º 19
0
 def test_raise_TypeError_if_response_is_not_http_response(self):
     response = []
     mw = ResponseGuard()
     self.assertRaises(TypeError, lambda: mw.process_response(FakeRequest(None), response))
 def _render_replies(self, replies):
     context = {'request': FakeRequest(create_user())}
     return jinja_tags.jinja_thread_comments(context,
                                             [c.details() for c in replies])
Exemplo n.º 21
0
 def test_pageview_records_view_metric(self):
     self.assertViewCount(FakeRequest(create_user(), path="/user/foobar"), HttpResponse(status=200), 1)
Exemplo n.º 22
0
 def test_secure_without_https(self):
     request = FakeRequest()
     response = self.require_secure(request)
     self.assertEqual(response.status_code, 302)
 def _render_post(self, post, fullsize=False, for_user=None):
     for_user = for_user or AnonymousUser()
     context = {'request': FakeRequest(for_user)}
     return jinja_tags.jinja_thread_comment(context, post.details(),
                                            fullsize)
 def test_secure_with_https(self):
     request = FakeRequest()
     request.META['HTTP_X_FORWARDED_PROTO'] = 'https'
     response = self.require_secure(request)
     self.assertEqual(response.content, self.text)
Exemplo n.º 25
0
 def test_bad_experiment_name_raises(self):
     with self.assertRaises(ValueError):
         FakeRequest().experiments.is_in("nope")
Exemplo n.º 26
0
 def test_staff_with_nonstaff_user(self):
     request = FakeRequest(user=create_user())
     self.assertRaises(PermissionDenied,
                       lambda: self.require_staff(request))
Exemplo n.º 27
0
 def test_get_allows_strings(self):
     self.assertEqual(FakeRequest().experiments.get("forced_into_control"),
                      "control")
Exemplo n.º 28
0
 def test_POST_with_POST(self):
     request = FakeRequest()
     request.method = 'POST'
     response = self.require_POST(request)
     self.assertEqual(response.status_code, 200)
Exemplo n.º 29
0
 def test_POST_with_GET(self):
     request = FakeRequest()
     response = self.require_POST(request)
     self.assertEqual(response.status_code, 405)  # Not Allowed.
Exemplo n.º 30
0
 def test_secure_with_https(self):
     request = FakeRequest()
     request.META['HTTP_X_FORWARDED_PROTO'] = 'https'
     response = self.require_secure(request)
     self.assertEqual(response.content, self.text)
Exemplo n.º 31
0
 def test_staff_with_staff(self):
     request = FakeRequest(user=create_staff())
     response = self.require_staff(request)
     self.assertEqual(response.status_code, 200)
Exemplo n.º 32
0
 def test_staff_gets_staff_branch(self):
     staff_request = FakeRequest(create_user(staff=True))
     self.assertTrue(
         staff_request.experiments.is_in('forced_into_control',
                                         'experimental'))
 def test_POST_with_POST(self):
     request = FakeRequest()
     request.method = 'POST'
     response = self.require_POST(request)
     self.assertEqual(response.status_code, 200)
Exemplo n.º 34
0
 def setUp(self):
     CanvasTestCase.setUp(self)
     self.user = create_user(is_qa=False)
     self.request = FakeRequest(self.user)
Exemplo n.º 35
0
 def test_staff_with_anon(self):
     request = FakeRequest()
     self.assertRaises(PermissionDenied,
                       lambda: self.require_staff(request))
Exemplo n.º 36
0
 def test_get_rejects_bad_experiment_name(self):
     with self.assertRaises(ValueError):
         FakeRequest().experiments.get("nope")