예제 #1
0
    def test__route(self):
        router = Router(db, self._request, auth)
        random = '_random_placeholder_'
        self.assertEqual(len(self._book_views(self._book.id)), 0)

        def do_test(request_vars, expect):
            """Run test."""
            self._request.vars = request_vars
            router = Router(db, self._request, auth)
            router.route()
            if 'redirect' in expect:
                self.assertEqual(router.redirect, expect.redirect)
            if 'view_dict' in expect:
                urls = dict(router.view_dict['urls'])
                if 'suggestions' in expect.view_dict:
                    if expect.view_dict['suggestions'] != random:
                        self.assertEqual(
                            urls['suggestions'],
                            expect.view_dict['suggestions']
                        )
                self.assertEqual(
                    urls['invalid'],
                    expect.view_dict['invalid']
                )
            if 'view_dict_keys' in expect:
                self.assertEqual(
                    sorted(router.view_dict.keys()),
                    expect.view_dict_keys
                )
            self.assertEqual(router.view, expect.view)

        # No creator, should route to page_not_found with random creator.
        request_vars = Storage(dict())

        page_not_found_expect = Storage({
            'view_dict': {
                'suggestions': random,
                'invalid': 'http://www.domain.com/path/to/page',
            },
            'view': 'errors/page_not_found.html',
        })

        do_test(request_vars, page_not_found_expect)
        self.assertEqual(len(self._book_views(self._book.id)), 0)

        router.route()
        urls = router.view_dict['urls']
        suggestion_labels = [x['label'] for x in urls['suggestions']]
        suggestion_urls = [x['url'] for x in urls['suggestions']]

        self.assertEqual(sorted(urls.keys()), ['invalid', 'suggestions'])
        self.assertEqual(urls['invalid'], 'http://www.domain.com/path/to/page')
        self.assertEqual(
            suggestion_labels,
            ['Cartoonist page:', 'Book page:', 'Read:']
        )
        # Urls will be random creator/book/read. Look for expected patterns.
        local_domain = 'http://127.0.0.1:8000'
        for count, suggestion_url in enumerate(suggestion_urls):
            self.assertTrue(suggestion_url.startswith(local_domain))
            relative_url = suggestion_url.replace(local_domain, '').lstrip('/')
            parts = relative_url.split('/')
            self.assertTrue(len(parts), count + 1)

        # Creator as integer (creator_id) should redirect.
        self.assertEqual(len(self._book_views(self._book.id)), 0)
        request_vars.creator = str(self._creator.id)
        expect = Storage({
            'redirect': '/FirstLast',
        })
        do_test(request_vars, expect)

        # Creator as name
        self.assertEqual(len(self._book_views(self._book.id)), 0)
        request_vars.creator = 'FirstLast'
        expect = Storage({
            'view_dict_keys': self._keys_for_view['creator'],
            'view': 'creators/creator.html',
        })
        do_test(request_vars, expect)

        # Book as name
        self.assertEqual(len(self._book_views(self._book.id)), 0)
        request_vars.creator = 'FirstLast'
        request_vars.book = 'MyBook'
        expect = Storage({
            'view_dict_keys': self._keys_for_view['book'],
            'view': 'books/book.html',
        })
        do_test(request_vars, expect)

        # Book page: slider
        request_vars.creator = 'FirstLast'
        request_vars.book = 'MyBook'
        request_vars.page = '001'
        expect = Storage({
            'view_dict_keys': self._keys_for_view['reader'],
            'view': 'books/slider.html',
        })

        self.assertEqual(len(self._book_views(self._book.id)), 0)
        do_test(request_vars, expect)
        views = self._book_views(self._book.id)
        self.assertEqual(len(views), 1)
        for obj in views:
            self._objects.append(obj)

        # Book page: scroller
        self._book = Book.from_updated(self._book, dict(reader='scroller'))
        expect = Storage({
            'view_dict_keys': self._keys_for_view['reader'],
            'view': 'books/scroller.html',
        })
        do_test(request_vars, expect)
        views = self._book_views(self._book.id)
        self.assertEqual(len(views), 2)
        for obj in views:
            self._objects.append(obj)

        self._book = Book.from_updated(self._book, dict(reader='slider'))

        # Book page image
        request_vars.page = '001.jpg'
        expect = Storage({
            'view_dict_keys': self._keys_for_view['page_image'],
            'view': 'books/page_image.html'
        })
        do_test(request_vars, expect)

        # Nonexistent creator
        request_vars.creator = str(9999999)
        expect_not_found = Storage({
            'view_dict_keys': self._keys_for_view['page_not_found'],
            'view': 'errors/page_not_found.html',
        })
        do_test(request_vars, expect_not_found)

        request_vars.creator = '_Invalid_Creator_'
        do_test(request_vars, expect_not_found)

        # Nonexistent book
        request_vars.creator = 'FirstLast'
        request_vars.book = 'Some_Invalid_Book'
        do_test(request_vars, expect_not_found)

        # Nonexistent book page
        request_vars.creator = 'FirstLast'
        request_vars.book = 'MyBook'
        request_vars.page = '999.jpg'
        do_test(request_vars, expect_not_found)

        request_vars.page = '999'
        do_test(request_vars, expect_not_found)
예제 #2
0
    def test__page_not_found(self):

        def do_test(request_vars, expect):
            """Run test."""
            self._request.vars = request_vars
            router = Router(db, self._request, auth)
            router.page_not_found()
            self.assertTrue('urls' in router.view_dict)
            self.assertEqual(dict(router.view_dict['urls']), expect.view_dict)
            self.assertEqual(router.view, expect.view)

        def do_test_random(request_vars):
            """Run test."""
            self._request.vars = request_vars
            router = Router(db, self._request, auth)
            router.page_not_found()

            self.assertTrue('urls' in router.view_dict)
            self.assertTrue('suggestions' in router.view_dict['urls'])
            labels = [
                x['label'] for x in router.view_dict['urls']['suggestions']]
            self.assertEqual(
                labels,
                ['Cartoonist page:', 'Book page:', 'Read:']
            )
            self.assertEqual(
                router.view_dict['urls']['invalid'],
                'http://www.domain.com/path/to/page'
            )
            self.assertEqual(router.view, 'errors/page_not_found.html')
            book_url = router.view_dict['urls']['suggestions'][1]['url']
            # http://127.0.0.1:8000/FirstLast/MyBook
            unused_scheme, _, unused_url, creator_for_url, book_for_url = \
                book_url.split('/')

            got = Creator.from_key(dict(
                name_for_url=urllib.unquote(creator_for_url)))
            self.assertTrue(got)
            got = Book.from_key(dict(
                name_for_url=urllib.unquote(book_for_url)))
            self.assertTrue(got)
            self.assertTrue(got.release_date is not None)

        # Test first page, all parameters
        request_vars = Storage(dict(
            creator=self._creator_name,
            book=self._book_name,
            page=self._page_name,
        ))
        expect = Storage({
            'view_dict': {
                'suggestions': [
                    {
                        'label': 'Cartoonist page:',
                        'url': 'http://127.0.0.1:8000/FirstLast',
                    },
                    {
                        'label': 'Book page:',
                        'url': 'http://127.0.0.1:8000/FirstLast/MyBook',
                    },
                    {
                        'label': 'Read:',
                        'url': 'http://127.0.0.1:8000/FirstLast/MyBook/001',
                    },
                ],
                'invalid': 'http://www.domain.com/path/to/page',
            },
            'view': 'errors/page_not_found.html',
        })
        crea_url = 'http://127.0.0.1:8000/JohnHancock'
        book_url = 'http://127.0.0.1:8000/JohnHancock/MySecondBook'
        page_url = 'http://127.0.0.1:8000/JohnHancock/MySecondBook/001'
        expect_2 = Storage({
            'view_dict': {
                'suggestions': [
                    {
                        'label': 'Cartoonist page:',
                        'url': crea_url,
                    },
                    {
                        'label': 'Book page:',
                        'url': book_url,
                    },
                    {
                        'label': 'Read:',
                        'url': page_url,
                    },
                ],
                'invalid': 'http://www.domain.com/path/to/page',
            },
            'view': 'errors/page_not_found.html',
        })

        do_test(request_vars, expect)

        # Second page should be found if indicated.
        request_vars.page = self._page_2_name
        expect.view_dict['suggestions'][2]['url'] = \
            'http://127.0.0.1:8000/FirstLast/MyBook/002'
        do_test(request_vars, expect)

        # If page not indicated, first page of book should be found.
        del request_vars.page
        expect.view_dict['suggestions'][2]['url'] = \
            'http://127.0.0.1:8000/FirstLast/MyBook/001'

        # If page doesn't exist, first page of book should be found.
        request_vars.page = '999'
        do_test(request_vars, expect)

        # If book doesn't match creator, first book of creator should be found
        request_vars = Storage(dict(
            creator=self._creator_name,
            book=self._book_2_name,
            page=self._book_2_page_name,
        ))
        do_test(request_vars, expect)

        request_vars = Storage(dict(
            creator=self._creator_2_name,
            book=self._book_name,
            page=self._page_name,
        ))
        do_test(request_vars, expect_2)

        # If book not indicated, first book of creator should be found.
        del request_vars.page
        del request_vars.book
        request_vars.creator = self._creator_name
        do_test(request_vars, expect)

        request_vars.creator = self._creator_2_name
        do_test(request_vars, expect_2)

        # If book doesn't exist, first book of creator should be found.
        request_vars.book = '_Fake_Book_'
        request_vars.creator = self._creator_name
        do_test(request_vars, expect)

        request_vars.creator = self._creator_2_name
        do_test(request_vars, expect_2)

        # If creator has no books, random released book is used.
        request_vars.page = '001'
        request_vars.book = '_Fake_Book_'
        request_vars.creator = self._creator_no_books.name_for_url
        do_test_random(request_vars)

        # If invalid creator, random released book is used.
        if request_vars.page:
            del request_vars.page
        if request_vars.book:
            del request_vars.book
        request_vars.creator = '_Invalid _Cartoonist'
        do_test_random(request_vars)

        # If no creator, first book of first creator should be found.
        del request_vars.creator
        do_test_random(request_vars)

        # Test missing web2py_original_uri
        self._request.env.web2py_original_uri = None
        request_vars.creator = self._creator_name
        request_vars.book = self._book_name
        request_vars.page = self._page_name
        router = Router(db, self._request, auth)
        router.page_not_found()
        self.assertTrue('urls' in router.view_dict)
        self.assertEqual(
            router.view_dict['urls'].invalid,
            'http://www.domain.com/request/uri/path'
        )
        self.assertEqual(router.view, expect.view)