Beispiel #1
0
    def update_short_url(self, row_id, short_url):
        account = self.config['account']
        conn = yield self.engine.connect()
        try:
            tables = ShortenerTables(account, conn)

            yield tables.update_short_url(row_id, short_url)
        finally:
            yield conn.close()
Beispiel #2
0
    def update_short_url(self, row_id, short_url):
        account = self.config['account']
        conn = yield self.engine.connect()
        try:
            tables = ShortenerTables(account, conn)

            yield tables.update_short_url(row_id, short_url)
        finally:
            yield conn.close()
Beispiel #3
0
    def get_row_by_short_url(self, short_url):
        account = self.config['account']
        conn = yield self.engine.connect()
        try:
            tables = ShortenerTables(account, conn)

            row = yield tables.get_row_by_short_url(short_url)
            returnValue(row)
        finally:
            yield conn.close()
Beispiel #4
0
    def get_row_by_short_url(self, short_url):
        account = self.config['account']
        conn = yield self.engine.connect()
        try:
            tables = ShortenerTables(account, conn)

            row = yield tables.get_row_by_short_url(short_url)
            returnValue(row)
        finally:
            yield conn.close()
    def test_resolve_url_hits_counter(self):
        tables = ShortenerTables(self.account, self.conn)
        yield tables.create_tables()

        url = 'http://en.wikipedia.org/wiki/Cthulhu'
        yield self.service.shorten_url(url)

        yield self.service.get_row_by_short_url('qr0')
        yield self.service.get_row_by_short_url('qr0')
        result = yield self.service.get_row_by_short_url('qr0')

        audit = yield tables.get_audit_row(result['id'])
        self.assertEqual(audit['hits'], 3)
Beispiel #6
0
    def get_or_create_row(self, url, user_token):
        account = self.config['account']
        domain = urlparse(url).netloc
        conn = yield self.engine.connect()
        try:
            tables = ShortenerTables(account, conn)

            row = yield tables.get_or_create_row(domain, user_token, url)
            returnValue(row)
        except NoShortenerTables:
            raise APIError('Account "%s" does not exist' % account, 200)
        finally:
            yield conn.close()
Beispiel #7
0
    def init_account(self, request):
        '''
        Initializes the account and creates the database tables
        '''
        conn = yield self.engine.connect()
        account = self.config['account']
        tables = ShortenerTables(account, conn)
        try:
            already_exists = yield tables.exists()
            if not already_exists:
                yield tables.create_tables()
        finally:
            yield conn.close()

        returnValue({'created': not already_exists})
Beispiel #8
0
    def init_account(self, request):
        '''
        Initializes the account and creates the database tables
        '''
        conn = yield self.engine.connect()
        account = self.config['account']
        tables = ShortenerTables(account, conn)
        try:
            already_exists = yield tables.exists()
            if not already_exists:
                yield tables.create_tables()
        finally:
            yield conn.close()

        returnValue({'created': not already_exists})
    def test_url_shortening(self):
        yield ShortenerTables(self.account, self.conn).create_tables()

        long_url = 'http://en.wikipedia.org/wiki/Cthulhu'
        short_url = yield self.service.shorten_url(long_url)
        self.assertEqual(short_url, 'http://wtxt.io/qr0')
        self.assertTrue(
            self.tr.value().startswith("test-account.wtxtio.created.count 1"))
Beispiel #10
0
    def get_or_create_row(self, url, user_token):
        account = self.config['account']
        domain = urlparse(url).netloc
        conn = yield self.engine.connect()
        try:
            tables = ShortenerTables(account, conn)

            row = yield tables.get_or_create_row(
                domain,
                user_token,
                url
            )
            returnValue(row)
        except NoShortenerTables:
            raise APIError('Account "%s" does not exist' % account, 200)
        finally:
            yield conn.close()
    def test_get_or_create_row(self):
        tables = ShortenerTables('test-account', self.conn)
        yield tables.create_tables()

        row = yield tables.get_or_create_row(
            'wiki.org', 'test', 'http://wiki.org/test/')
        self.assertEqual(row['domain'], 'wiki.org')
        self.assertEqual(row['short_url'], None)
        self.assertEqual(row['user_token'], 'test')
        self.assertEqual(row['long_url'], 'http://wiki.org/test/')
        self.assertEqual(row['id'], 1)

        row = yield tables.get_or_create_row(
            'wiki.org', 'test', 'http://wiki.org/test/')
        self.assertEqual(row['id'], 1)

        audit = yield tables.get_audit_row(1)
        self.assertEqual(audit['hits'], 0)
Beispiel #12
0
    def render(self, request):
        short_url = request.args.get('url')
        conn = yield self.engine.connect()
        try:
            tables = ShortenerTables(self.config['account'], conn)
            if not short_url:
                request.setResponseCode(http.BAD_REQUEST)
                returnValue({'error': 'expected "?url=<short_url>"'})
            else:
                row = yield tables.get_row_by_short_url(short_url[0], False)

                if row:
                    audit = yield tables.get_audit_row(row['id'])
                    returnValue(self._format(row, audit))
                else:
                    request.setResponseCode(http.NOT_FOUND)
                    returnValue({'error': 'short url not found'})
        finally:
            yield conn.close()
    def test_resolve_url(self):
        yield ShortenerTables(self.account, self.conn).create_tables()

        url = 'http://en.wikipedia.org/wiki/Cthulhu'
        yield self.service.shorten_url(url + '1')
        yield self.service.shorten_url(url + '2')
        yield self.service.shorten_url(url + '3')
        yield self.service.shorten_url(url + '4')

        result = yield self.service.get_row_by_short_url('qH0')
        self.assertEqual(result['long_url'], url + '4')
    def test_repeat_url_generation(self):
        yield ShortenerTables(self.account, self.conn).create_tables()

        url = 'http://en.wikipedia.org/wiki/Cthulhu'
        url1 = yield self.service.shorten_url(url + '1')
        url2 = yield self.service.shorten_url(url + '2')
        url3 = yield self.service.shorten_url(url + '2')
        url4 = yield self.service.shorten_url(url + '1')
        urls = [url1, url2, url3, url4]
        self.assertEqual(len(set(urls)), 2)
        conn_queue = self.tr.value().splitlines()
        self.assertEqual(len(conn_queue), 2)
    def test_create_url_no_user_token(self):
        yield ShortenerTables(self.account, self.conn).create_tables()

        payload = {'long_url': 'foo'}
        resp = yield treq.put(self.make_url('/api/create'),
                              data=json.dumps(payload),
                              allow_redirects=False,
                              pool=self.pool)

        result = yield treq.json_content(resp)
        self.assertEqual(result['short_url'], 'http://wtxt.io/qr0')
        self.assertTrue(
            self.tr.value().startswith("test-account.wtxtio.created.count 1"))
Beispiel #16
0
    def test_api_dump_invalid_querystring(self):
        yield ShortenerTables(self.account, self.conn).create_tables()

        url = 'http://en.wikipedia.org/wiki/Cthulhu'
        yield self.service.shorten_url(url, 'test-user')
        yield treq.get(self.make_url('/qr0'),
                       allow_redirects=False,
                       pool=self.pool)

        resp = yield treq.get(self.make_url('/api/handler/dump'),
                              allow_redirects=False,
                              pool=self.pool)

        self.assertEqual(resp.code, 400)
    def test_short_url_sequencing(self):
        yield ShortenerTables(self.account, self.conn).create_tables()

        url = 'http://en.wikipedia.org/wiki/Cthulhu'
        urls = [''.join([url, str(a)]) for a in range(1, 10)]
        for u in urls:
            yield self.service.shorten_url(u)

        result = yield self.service.get_row_by_short_url('qs0')
        self.assertEqual(result['long_url'], url + '5')

        result = yield self.service.get_row_by_short_url('qp0')
        self.assertEqual(result['long_url'], url + '6')

        conn_queue = self.tr.value().splitlines()
        self.assertEqual(len(conn_queue), 9)
    def test_resolve_url_404(self):
        yield ShortenerTables(self.account, self.conn).create_tables()

        url = 'http://en.wikipedia.org/wiki/Cthulhu'
        yield self.service.shorten_url(url)

        self.assertTrue(
            self.tr.value().startswith("test-account.wtxtio.created.count 1"))

        resp = yield treq.get(self.make_url('/1Tx'),
                              allow_redirects=False,
                              pool=self.pool)

        self.assertEqual(resp.code, 404)
        conn_queue = self.tr.value().splitlines()
        self.assertTrue(
            conn_queue[1].startswith("test-account.wtxtio.invalid.count 1"))
    def test_resolve_url(self):
        tables = ShortenerTables('test-account', self.conn)
        yield tables.create_tables()

        yield tables.get_or_create_row('wiki.org', 'test',
                                       'http://wiki.org/test/')
        yield tables.update_short_url(1, 'aaa')

        row = yield tables.get_row_by_short_url('aaa')

        self.assertEqual(row['domain'], 'wiki.org')
        self.assertEqual(row['short_url'], 'aaa')
        self.assertEqual(row['id'], 1)

        audit = yield tables.get_audit_row(1)
        self.assertEqual(audit['hits'], 1)

        #multiple hits
        for i in range(0, 10):
            yield tables.get_row_by_short_url('aaa')

        audit = yield tables.get_audit_row(1)
        self.assertEqual(audit['hits'], 11)
Beispiel #20
0
    def test_api_dump(self):
        yield ShortenerTables(self.account, self.conn).create_tables()

        url = 'http://en.wikipedia.org/wiki/Cthulhu'
        yield self.service.shorten_url(url, 'test-user')
        yield treq.get(self.make_url('/qr0'),
                       allow_redirects=False,
                       pool=self.pool)

        resp = yield treq.get(self.make_url('/api/handler/dump?url=qr0'),
                              allow_redirects=False,
                              pool=self.pool)

        self.assertEqual(resp.code, 200)
        result = yield treq.json_content(resp)
        self.assertEqual(result['user_token'], 'test-user')
        self.assertEqual(result['short_url'], 'qr0')
        self.assertEqual(result['long_url'], url)
        self.assertEqual(result['hits'], 1)
        self.assertEqual(result['domain'], 'en.wikipedia.org')
    def test_resolve_url_simple(self):
        yield ShortenerTables(self.account, self.conn).create_tables()

        url = 'http://en.wikipedia.org/wiki/Cthulhu'
        yield self.service.shorten_url(url)

        self.assertTrue(
            self.tr.value().startswith("test-account.wtxtio.created.count 1"))

        resp = yield treq.get(self.make_url('/qr0'),
                              allow_redirects=False,
                              pool=self.pool)

        self.assertEqual(resp.code, 301)
        [location] = resp.headers.getRawHeaders('location')
        self.assertEqual(location, url)

        conn_queue = self.tr.value().splitlines()
        self.assertTrue(
            conn_queue[1].startswith("test-account.wtxtio.expanded.count 1"))
    def test_resolve_url(self):
        tables = ShortenerTables('test-account', self.conn)
        yield tables.create_tables()

        yield tables.get_or_create_row(
            'wiki.org', 'test', 'http://wiki.org/test/')
        yield tables.update_short_url(1, 'aaa')

        row = yield tables.get_row_by_short_url('aaa')

        self.assertEqual(row['domain'], 'wiki.org')
        self.assertEqual(row['short_url'], 'aaa')
        self.assertEqual(row['id'], 1)

        audit = yield tables.get_audit_row(1)
        self.assertEqual(audit['hits'], 1)

        #multiple hits
        for i in range(0, 10):
            yield tables.get_row_by_short_url('aaa')

        audit = yield tables.get_audit_row(1)
        self.assertEqual(audit['hits'], 11)
    def test_update_short_url(self):
        tables = ShortenerTables('test-account', self.conn)
        yield tables.create_tables()

        row = yield tables.get_or_create_row('wiki.org', 'test',
                                             'http://wiki.org/test/')
        self.assertEqual(row['domain'], 'wiki.org')
        self.assertEqual(row['short_url'], None)
        self.assertEqual(row['id'], 1)

        yield tables.update_short_url(1, 'aaa')
        row = yield tables.get_or_create_row('wiki.org', 'test',
                                             'http://wiki.org/test/')

        self.assertEqual(row['domain'], 'wiki.org')
        self.assertEqual(row['short_url'], 'aaa')
        self.assertEqual(row['id'], 1)

        audit = yield tables.get_audit_row(1)
        self.assertEqual(audit['hits'], 0)
 def test_tables_create(self):
     tables = ShortenerTables('test-account', self.conn)
     self.successResultOf(tables.create_tables())
 def test_tables_create(self):
     tables = ShortenerTables('test-account', self.conn)
     self.successResultOf(tables.create_tables())