Пример #1
0
class TestEntreeUserFetcher(DBUserTestCase):
    def setUp(self):
        super(TestEntreeUserFetcher, self).setUp()
        cache.clear()
        self.fetcher = EntreeUserFetcherMixin()

    def test_bad_checksum_raises(self):
        cookie_val = "w/ checksum value"
        invalid_cookie = COOKIE_CHECKSUM_SEPARATOR.join([cookie_val, calc_checksum(cookie_val, length=10)])[:-1]
        assert_raises(InvalidAuth, self.fetcher.fetch, invalid_cookie)

    def test_no_checksum_pass_and_perform_fetch(self):
        cookie_val = "no_checksum_value"

        self.fetcher.perform_fetch = Mock()
        self.fetcher.perform_fetch.return_value = AnonymousUser()

        assert_equals(AnonymousUser(), self.fetcher.fetch(cookie_val))
        assert self.fetcher.perform_fetch.called

    @patch("entree.client.managers.urlopen")
    def test_perform_fetch_403_raises_invalid(self, mocked_urlopen):

        mocked_urlopen.side_effect = HTTPError(url="foo", code=403, msg="you shall not pass", hdrs=None, fp=None)
        cookie_val = "no_checksum_value"

        assert_raises(InvalidAuth, self.fetcher.perform_fetch, cookie_val)

    @patch("entree.client.managers.urlopen")
    def test_perform_fetch_404_raises_anonymous(self, mocked_urlopen):

        mocked_urlopen.side_effect = HTTPError(url="foo", code=404, msg="you shall not pass", hdrs=None, fp=None)
        cookie_val = "no_checksum_value"

        assert_equals(AnonymousUser, self.fetcher.perform_fetch(cookie_val).__class__)

    @patch("entree.client.managers.urlopen")
    def test_perform_fetch_returns_entreeuser(self, mocked_urlopen):

        if self.__class__.__name__ == "TestEntreeUserFetcherCache":
            from nose.plugins.skip import SkipTest

            raise SkipTest("entree.client.db in installed_apps means bad import")

        mocked_urlopen.return_value = StringIO("""{"email": "*****@*****.**"}""")
        cookie_val = "no_checksum_value"

        ret = self.fetcher.perform_fetch(cookie_val)
        assert_equals(self.UserClass, ret.__class__)
        assert_equals("*****@*****.**", ret.data["email"])

    @patch("entree.client.managers.urlopen")
    def test_perform_fetch_invalid_response_ret_anonymous(self, mocked_urlopen):

        mocked_urlopen.return_value = StringIO("""{invalid_json "foo"}""")
        cookie_val = "no_checksum_value"

        ret = self.fetcher.perform_fetch(cookie_val)
        assert_equals(AnonymousUser, ret.__class__)
Пример #2
0
class TestEntreeUserFetcher(DBUserTestCase):
    def setUp(self):
        super(TestEntreeUserFetcher, self).setUp()
        cache.clear()
        self.fetcher = EntreeUserFetcherMixin()

    def test_bad_checksum_raises(self):
        cookie_val = 'w/ checksum value'
        invalid_cookie = COOKIE_CHECKSUM_SEPARATOR.join(
            [cookie_val, calc_checksum(cookie_val, length=10)])[:-1]
        assert_raises(InvalidAuth, self.fetcher.fetch, invalid_cookie)

    def test_no_checksum_pass_and_perform_fetch(self):
        cookie_val = 'no_checksum_value'

        self.fetcher.perform_fetch = Mock()
        self.fetcher.perform_fetch.return_value = AnonymousUser()

        assert_equals(AnonymousUser(), self.fetcher.fetch(cookie_val))
        assert self.fetcher.perform_fetch.called

    @patch('entree.client.managers.urlopen')
    def test_perform_fetch_403_raises_invalid(self, mocked_urlopen):

        mocked_urlopen.side_effect = HTTPError(url='foo',
                                               code=403,
                                               msg='you shall not pass',
                                               hdrs=None,
                                               fp=None)
        cookie_val = 'no_checksum_value'

        assert_raises(InvalidAuth, self.fetcher.perform_fetch, cookie_val)

    @patch('entree.client.managers.urlopen')
    def test_perform_fetch_404_raises_anonymous(self, mocked_urlopen):

        mocked_urlopen.side_effect = HTTPError(url='foo',
                                               code=404,
                                               msg='you shall not pass',
                                               hdrs=None,
                                               fp=None)
        cookie_val = 'no_checksum_value'

        assert_equals(AnonymousUser,
                      self.fetcher.perform_fetch(cookie_val).__class__)

    @patch('entree.client.managers.urlopen')
    def test_perform_fetch_returns_entreeuser(self, mocked_urlopen):

        if self.__class__.__name__ == 'TestEntreeUserFetcherCache':
            from nose.plugins.skip import SkipTest
            raise SkipTest(
                "entree.client.db in installed_apps means bad import")

        mocked_urlopen.return_value = StringIO("""{"email": "*****@*****.**"}""")
        cookie_val = 'no_checksum_value'

        ret = self.fetcher.perform_fetch(cookie_val)
        assert_equals(self.UserClass, ret.__class__)
        assert_equals('*****@*****.**', ret.data['email'])

    @patch('entree.client.managers.urlopen')
    def test_perform_fetch_invalid_response_ret_anonymous(
            self, mocked_urlopen):

        mocked_urlopen.return_value = StringIO("""{invalid_json "foo"}""")
        cookie_val = 'no_checksum_value'

        ret = self.fetcher.perform_fetch(cookie_val)
        assert_equals(AnonymousUser, ret.__class__)