Beispiel #1
0
    def test_alias_route_password_protected_get(self):
        frontend = views.main.frontend()
        LinkFactory.generate(alias='alias', password='******')

        resp = self.client.get('/alias')

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.data.decode('utf-8'), frontend)
Beispiel #2
0
    def test_alias_route_recaptcha_get(self):
        frontend = views.main.frontend()
        LinkFactory.generate(alias='alias',
                             password=None,
                             require_recaptcha=True)

        resp = self.client.get('/alias')

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.data.decode('utf-8'), frontend)
Beispiel #3
0
    def test_get_links_like_alias(self):
        valid = [
            LinkFactory.generate(alias='test'),
            LinkFactory.generate(alias='es'),
        ]
        LinkFactory.generate('aaa')

        queried_links = database.link.get_links_like_alias('es')

        self.assertEqual(queried_links, valid)
Beispiel #4
0
    def test_api_links_for_user_unauth(self):
        LinkFactory.generate(user_id=5)

        # Trying to view links created by someone else as non-admin
        with self.api_utils.authenticated_user():
            resp = self.api_utils.request(LinksForUserURI,
                                          data={
                                              'user_id': 5,
                                          })

            self.assertEqual(resp.status_code, 403)
            self.assertEqual(resp.json['failure'], 'failure_unauth')
Beispiel #5
0
    def test_alias_route_redirect(self):
        LinkFactory.generate(
            alias='alias',
            outgoing_url='https://google.com',
            password=None,
            require_recaptcha=False,
        )

        resp = self.client.get('/alias')

        self.assertEqual(resp.status_code, 302)
        self.assertEqual(resp.headers['Location'], 'https://google.com')
Beispiel #6
0
    def test_alias_route_password_protected_post(self):
        LinkFactory.generate(alias='alias', password='******')

        resp = self.client.post('/alias')

        self.assertEqual(resp.status_code, 401)
        self.assertEqual(
            resp.json, {
                'success': False,
                'failure': 'failure_incorrect_link_password',
                'message': 'The supplied link password is incorrect.',
            })
Beispiel #7
0
    def test_get_link_hits_by_id(self):
        link = LinkFactory.generate()
        hits = [
            LinkHitFactory.generate(link_id=link.link_id) for _ in xrange(10)
        ]

        irrelevant_link = LinkFactory.generate()
        [
            LinkHitFactory.generate(link_id=irrelevant_link.link_id)
            for _ in xrange(10)
        ]

        queried_hits = database.link.get_link_hits_by_id(link_id=link.link_id)

        self.assertEqual(queried_hits, list(reversed(hits)))
Beispiel #8
0
    def test_api_link_details_lookup_by_alias(self):
        link = LinkFactory.generate()
        resp = self.api_utils.request(LinkDetailsURI,
                                      data={'alias': link.alias})

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.json['details'], link.as_dict())
Beispiel #9
0
    def test_api_link_alias_search_valid(self):
        match_links = [
            LinkFactory.generate(alias='ab'),
            LinkFactory.generate(alias='abcd')
        ]
        LinkFactory.generate(alias='efgh')

        with self.api_utils.authenticated_user(is_admin=True):
            resp = self.api_utils.request(LinkAliasSearchURI,
                                          data={
                                              'alias': 'ab',
                                          })

            self.assertEqual(resp.status_code, 200)
            self.assertEqual(resp.json['links'],
                             [link.as_dict() for link in match_links])
Beispiel #10
0
    def test_api_link_hits_valid(self):
        link = LinkFactory.generate()

        with self.api_utils.authenticated_user(is_admin=True):
            resp = self.api_utils.request(LinkHitsURI,
                                          data={
                                              'link_id': link.link_id,
                                          })

            self.assertEqual(resp.status_code, 200)
            self.assertEqual(resp.json['hits'], [])

            resp = self.api_utils.request(LinkIncrementHitsURI,
                                          data={
                                              'link_id': link.link_id,
                                          })

            self.assertEqual(resp.status_code, 200)

            resp = self.api_utils.request(LinkHitsURI,
                                          data={
                                              'link_id': link.link_id,
                                          })

            self.assertEqual(resp.status_code, 200)
            self.assertEqual(len(resp.json['hits']), 1)
Beispiel #11
0
    def test_api_link_details_empty_password(self):
        link = LinkFactory.generate(password='******')
        resp = self.api_utils.request(LinkDetailsURI,
                                      data={'link_id': link.link_id})

        self.assertEqual(resp.status_code, 401)
        self.assertEqual(resp.json['failure'],
                         'failure_incorrect_link_password')
Beispiel #12
0
    def test_api_links_for_user_current_user_valid(self):
        with self.api_utils.authenticated_user() as user:
            link = LinkFactory.generate(user_id=user.user_id)

            resp = self.api_utils.request(LinksForUserURI)

            self.assertEqual(resp.status_code, 200)
            self.assertEqual(resp.json['links'], [link.as_dict()])
Beispiel #13
0
    def test_alias_route_recaptcha_post(self):
        LinkFactory.generate(alias='alias',
                             password=None,
                             require_recaptcha=True)

        resp = self.client.post('/alias')

        self.assertEqual(resp.status_code, 403)
        self.assertEqual(
            resp.json, {
                'success':
                False,
                'failure':
                'failure_invalid_recaptcha',
                'message':
                'This link requires human verification, and can only be accessed '
                'interactively via a browser.',
            })
Beispiel #14
0
    def test_api_link_details_undefined_error(self):
        link = LinkFactory.generate()

        with mock.patch.object(util.response, 'success') as mock_success:
            mock_success.side_effect = ValueError

            resp = self.api_utils.request(LinkDetailsURI,
                                          data={'link_id': link.link_id})
            self.assertTrue(self.api_utils.is_undefined_error(resp))
Beispiel #15
0
    def test_api_increment_link_hits_correct_password(self):
        link = LinkFactory.generate(password='******')
        resp = self.api_utils.request(LinkIncrementHitsURI,
                                      data={
                                          'link_id': link.link_id,
                                          'password': '******',
                                      })

        self.assertEqual(resp.status_code, 200)
        self.assertIsNotNone(resp.json['hit'])
Beispiel #16
0
    def test_get_recent_links_pagination(self):
        links = [LinkFactory.generate() for _ in xrange(10)]

        queried_links = database.link.get_recent_links(page_num=0,
                                                       num_per_page=5)
        self.assertEqual(queried_links, list(reversed(links))[:5])

        queried_links = database.link.get_recent_links(page_num=1,
                                                       num_per_page=5)
        self.assertEqual(queried_links, list(reversed(links))[5:])
Beispiel #17
0
    def test_api_add_link_unavailable_alias(self):
        link = LinkFactory.generate()
        resp = self.api_utils.request(LinkAddURI,
                                      data={
                                          'alias': link.alias,
                                          'outgoing_url': 'https://google.com',
                                      })

        self.assertEqual(resp.status_code, 409)
        self.assertEqual(resp.json['failure'], 'failure_unavailable_alias')
Beispiel #18
0
    def test_api_increment_link_hits_wrong_password(self):
        link = LinkFactory.generate(password='******')
        resp = self.api_utils.request(LinkIncrementHitsURI,
                                      data={
                                          'link_id': link.link_id,
                                          'password': '******',
                                      })

        self.assertEqual(resp.status_code, 401)
        self.assertEqual(resp.json['failure'],
                         'failure_incorrect_link_password')
Beispiel #19
0
    def test_api_link_details_wrong_recaptcha(self):
        with mock.patch.object(
                util.recaptcha,
                'validate_recaptcha') as mock_validate_recaptcha:
            mock_validate_recaptcha.return_value = False
            link = LinkFactory.generate(require_recaptcha=True)
            resp = self.api_utils.request(LinkDetailsURI,
                                          data={'link_id': link.link_id})

            self.assertEqual(resp.status_code, 401)
            self.assertEqual(resp.json['failure'], 'failure_invalid_recaptcha')
Beispiel #20
0
    def test_api_delete_link_unauthorized(self):
        link = LinkFactory.generate()

        with self.api_utils.authenticated_user():
            resp = self.api_utils.request(LinkDeleteURI,
                                          data={
                                              'link_id': link.link_id,
                                          })

            self.assertEqual(resp.status_code, 403)
            self.assertEqual(resp.json['failure'], 'failure_unauth')
Beispiel #21
0
    def test_api_link_preview_unauth(self):
        link = LinkFactory.generate(user_id=5)

        with self.api_utils.authenticated_user():
            resp = self.api_utils.request(LinkPreviewURI,
                                          data={
                                              'link_id': link.link_id,
                                          })

            self.assertEqual(resp.status_code, 403)
            self.assertEqual(resp.json['failure'], 'failure_unauth')
Beispiel #22
0
    def test_api_link_details_correct_recaptcha(self):
        with mock.patch.object(
                util.recaptcha,
                'validate_recaptcha') as mock_validate_recaptcha:
            mock_validate_recaptcha.return_value = True
            link = LinkFactory.generate(require_recaptcha=True)
            resp = self.api_utils.request(LinkDetailsURI,
                                          data={'link_id': link.link_id})

            self.assertEqual(resp.status_code, 200)
            self.assertEqual(resp.json['details'], link.as_dict())
            self.assertTrue(mock_validate_recaptcha.called)
Beispiel #23
0
    def test_api_edit_link_invalid_url(self):
        with self.api_utils.authenticated_user() as user:
            link = LinkFactory.generate(user_id=user.user_id)

            resp = self.api_utils.request(LinkEditURI,
                                          data={
                                              'link_id': link.link_id,
                                              'alias': 'alias',
                                              'outgoing_url': 'not a url',
                                          })

            self.assertEqual(resp.status_code, 400)
            self.assertEqual(resp.json['failure'], 'failure_invalid_url')
Beispiel #24
0
    def test_api_edit_link_valid(self):
        with self.api_utils.authenticated_user() as user:
            link = LinkFactory.generate(user_id=user.user_id)

            resp = self.api_utils.request(LinkEditURI,
                                          data={
                                              'link_id': link.link_id,
                                              'alias': 'alias',
                                              'outgoing_url':
                                              'https://google.com',
                                          })

            self.assertEqual(resp.status_code, 200)
Beispiel #25
0
    def test_api_recent_links_valid(self):
        links = sorted(
            [LinkFactory.generate() for _ in xrange(5)],
            key=lambda generated: generated.link_id,
            reverse=True,
        )

        with self.api_utils.authenticated_user(is_admin=True):
            resp = self.api_utils.request(RecentLinksURI)

            self.assertEqual(resp.status_code, 200)
            self.assertEqual(resp.json['links'],
                             [link.as_dict() for link in links])
Beispiel #26
0
    def test_api_links_for_user_specified_user_valid(self):
        user = UserFactory.generate()

        with self.api_utils.authenticated_user(is_admin=True):
            link = LinkFactory.generate(user_id=user.user_id)

            resp = self.api_utils.request(LinksForUserURI,
                                          data={
                                              'user_id': user.user_id,
                                          })

            self.assertEqual(resp.status_code, 200)
            self.assertEqual(resp.json['links'], [link.as_dict()])
Beispiel #27
0
    def test_api_delete_link_undefined_error(self):
        with self.api_utils.authenticated_user() as user:
            link = LinkFactory.generate(user_id=user.user_id)

            with mock.patch.object(util.response, 'success') as mock_success:
                mock_success.side_effect = ValueError

                resp = self.api_utils.request(LinkDeleteURI,
                                              data={
                                                  'link_id': link.link_id,
                                              })

                self.assertTrue(self.api_utils.is_undefined_error(resp))
Beispiel #28
0
    def test_api_link_hits_undefined_error(self):
        link = LinkFactory.generate()

        with self.api_utils.authenticated_user(is_admin=True):
            with mock.patch.object(util.response, 'success') as mock_success:
                mock_success.side_effect = ValueError

                resp = self.api_utils.request(LinkHitsURI,
                                              data={
                                                  'link_id': link.link_id,
                                              })

                self.assertTrue(self.api_utils.is_undefined_error(resp))
Beispiel #29
0
    def test_add_link_hit(self):
        associated_link = LinkFactory.generate()
        link_hit = database.link.add_link_hit(
            link_id=associated_link.link_id,
            remote_ip='127.0.0.1',
            referer='referer',
            user_agent='user-agent',
        )

        self.assertIsNotNone(link_hit.hit_id)
        self.assertEqual(link_hit.link_id, 1)
        self.assertEqual(link_hit.remote_ip, '127.0.0.1')
        self.assertEqual(link_hit.referer, 'referer')
        self.assertEqual(link_hit.user_agent, 'user-agent')
Beispiel #30
0
    def test_api_link_details_correct_password(self):
        with mock.patch.object(
                util.recaptcha,
                'validate_recaptcha') as mock_validate_recaptcha:
            link = LinkFactory.generate(password='******')
            resp = self.api_utils.request(LinkDetailsURI,
                                          data={
                                              'link_id': link.link_id,
                                              'password': '******',
                                          })

            self.assertEqual(resp.status_code, 200)
            self.assertEqual(resp.json['details'], link.as_dict())
            self.assertFalse(mock_validate_recaptcha.called)