コード例 #1
0
 def test_get_redirect_mobile_fallback(self):
     """Test that we can redirect to the default if not mobile url set."""
     # set up a test record
     snippet = self.model_factory(mobile_redirect=None, )
     snippet.short_url = url_lib.encode_short_url(snippet.id)
     snippet.save(snippet)
     res = self.raw_get(snippet.short_url, headers={'User-Agent': 'iphone'})
     assert res.status_code == 302
     assert res.headers['Location'] == snippet.desktop_redirect
コード例 #2
0
 def test_get_redirect_default(self):
     """Test that we can redirect to the default desktop url."""
     # set up a test record
     snippet = self.model_factory()
     snippet.short_url = url_lib.encode_short_url(snippet.id)
     snippet.save(snippet)
     res = self.raw_get(snippet.short_url)
     assert res.status_code == 302
     assert res.headers['Location'] == snippet.desktop_redirect
コード例 #3
0
 def test_get_redirect_tablet(self):
     """Test that we can redirect to a specified tablet url."""
     # set up a test record
     tablet_redirect = 'https://www.tablet.com/tablet'
     snippet = self.model_factory(tablet_redirect=tablet_redirect)
     snippet.short_url = url_lib.encode_short_url(snippet.id)
     snippet.save(snippet)
     res = self.raw_get(snippet.short_url, headers={'User-Agent': 'ipad'})
     assert res.status_code == 302
     assert res.headers['Location'] == snippet.tablet_redirect
コード例 #4
0
 def test_get_redirect_mobile(self):
     """Test that we can redirect to a specified mobile url."""
     # set up a test record
     mobile_redirect = 'https://mobile.com/mobile'
     snippet = self.model_factory(mobile_redirect=mobile_redirect, )
     snippet.short_url = url_lib.encode_short_url(snippet.id)
     snippet.save(snippet)
     res = self.raw_get(snippet.short_url, headers={'User-Agent': 'iphone'})
     assert res.status_code == 302
     assert res.headers['Location'] == snippet.mobile_redirect
コード例 #5
0
    def post(self):
        """Create a new record."""
        logger.info({
            'msg': 'Creating a new record.',
            'view': self.__class__.__name__,
            'method': 'post',
            'schema_model': self.schema_model.__name__,
            'model': self.model.__name__,
        })
        raw_dict = request.get_json(force=True)

        try:
            self.schema.validate(raw_dict)
            attrs = raw_dict['data'].get('attributes') or {}

            record = self.model(**attrs)
            db.session.add(record)
            # Flush to get a record id
            db.session.flush()

            short_url = url_lib.encode_short_url(record.id)
            record.short_url = short_url
            record.save(record)
            query = self.model.get(record.id)
            result = self.schema.dump(query).data
            return result, 201

        except ValidationError as e:
            logger.error({
                'msg': 'Error validating new record.',
                'view': self.__class__.__name__,
                'method': 'post',
                'schema_model': self.schema_model.__name__,
                'model': self.model.__name__,
                'raw_dict': raw_dict,
                'error': str(e)
            })
            return {'error': e.messages}, 403

        except SQLAlchemyError as e:
            logger.error({
                'msg': 'Error creating new record.',
                'view': self.__class__.__name__,
                'method': 'post',
                'schema_model': self.schema_model.__name__,
                'model': self.model.__name__,
                'raw_dict': raw_dict,
                'error': str(e)
            })
            db.session.rollback()
            return {'error': str(e)}, 403
コード例 #6
0
 def test_get_redirect_desktop(self):
     """Test that we can redirect to a specified desktop url."""
     # set up a test record
     snippet = self.model_factory()
     snippet.short_url = url_lib.encode_short_url(snippet.id)
     snippet.save(snippet)
     res = self.raw_get(
         snippet.short_url,
         headers={
             'User-Agent':
             'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) '
             'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'
         },
     )
     assert res.status_code == 302
     assert res.headers['Location'] == snippet.desktop_redirect
コード例 #7
0
 def test_post(self):
     """Test that we can create a new record."""
     attrs = self.model_factory.build()
     del attrs['id']
     data = {
         'data': {
             'attributes': attrs,
             'type': '{}'.format(self.url_prefix),
         }
     }
     res = self.post(
         '/{}/'.format(self.url_prefix),
         data=data,
     )
     for k, v in attrs.iteritems():
         if k == 'short_url':
             assert (
                 res.data['data']['attributes'][k] ==
                 url_lib.encode_short_url(int(res.data['data']['id']))
             )
         else:
             assert res.data['data']['attributes'][k] == v
     assert res.status_code == 201
コード例 #8
0
 def test_encode_short_url(self):
     """Test encoding a number into a short url."""
     assert url_lib.encode_short_url(123) == 'http://localhost:5000/1Z'
     assert url_lib.encode_short_url(123456) == 'http://localhost:5000/w7e'