From 5b8a6fcd397255100093c97e8aaf133e70d5e172 Mon Sep 17 00:00:00 2001 From: Amit Tripathi Date: Wed, 11 Jul 2018 00:52:40 +0200 Subject: [PATCH] Secret link stats :sparkles: --- pygmy/app/link.py | 4 ++++ pygmy/rest/shorturl.py | 2 -- pygmy/utilities/urls.py | 5 +---- pygmyui/templates/base.html | 4 ++-- tests/test_integration.py | 43 ++++++++++++++++++++++++++----------- 5 files changed, 38 insertions(+), 20 deletions(-) diff --git a/pygmy/app/link.py b/pygmy/app/link.py index 221a12f..7db6f7a 100644 --- a/pygmy/app/link.py +++ b/pygmy/app/link.py @@ -130,6 +130,10 @@ def formatted_link_stats(link): 'time_series_base': click_meta.get('time_base'), 'time_stats': click_meta.get('timestamp_hits', {}), } + + # Hide original/long_url in case of protected links + if link.is_protected is True: + link_info['long_url'] = '' return {**link_info, **click_info} diff --git a/pygmy/rest/shorturl.py b/pygmy/rest/shorturl.py index 590bbc2..fd616f9 100644 --- a/pygmy/rest/shorturl.py +++ b/pygmy/rest/shorturl.py @@ -98,8 +98,6 @@ def resolve(code): secret_key = request.headers.get('secret_key') try: # check if link is not a secret link - long_url = resolve_short( - code.strip('+'), secret_key=secret_key) if code.startswith('+') or code.endswith('+'): stats = link_stats(code) response = jsonify(stats) diff --git a/pygmy/utilities/urls.py b/pygmy/utilities/urls.py index 18fd769..0d891b5 100644 --- a/pygmy/utilities/urls.py +++ b/pygmy/utilities/urls.py @@ -26,10 +26,7 @@ def validate_url(url): def make_short_url(short_path): - short_url = urljoin( - config.pygmy['short_url_schema'], - config.pygmy['short_url'], - short_path) + short_url = urljoin('{}{}'.format(config.pygmy['short_url_schema'], config.pygmy['short_url']), short_path) return short_url diff --git a/pygmyui/templates/base.html b/pygmyui/templates/base.html index c829112..42867be 100644 --- a/pygmyui/templates/base.html +++ b/pygmyui/templates/base.html @@ -74,7 +74,7 @@

SIGN UP

- +
@@ -105,7 +105,7 @@

LOGIN

- +
diff --git a/tests/test_integration.py b/tests/test_integration.py index fd3be01..6c5b365 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -1,9 +1,7 @@ import pytest -import sqlite3 import unittest import requests -from pygmy.config import config from pygmyui.restclient.base import Client @@ -22,14 +20,14 @@ def teardown_class(cls): pass def teardown_method(self, _): + # self.conn = sqlite3.connect(config.database['url']) + # self.cur = self.conn.cursor() + # tables = ['clickmeta', 'link', 'user'] + # for table in tables: + # self.cur.execute('DELETE FROM {}'.format(table)) + # self.conn.commit() + # self.conn.close() return - self.conn = sqlite3.connect(config.database['url']) - self.cur = self.conn.cursor() - tables = ['clickmeta', 'link', 'user'] - for table in tables: - self.cur.execute('DELETE FROM {}'.format(table)) - self.conn.commit() - self.conn.close() def setup_method(self, _): self._token = None @@ -235,13 +233,34 @@ def test_non_loggedin_dashboard(self): def test_check_link_availability(self): custom_code = 'logo' - requests.get(self.url + '/check?custom_code={}'.format(custom_code), 200) + response = requests.get(self.url + '/check?custom_code={}'.format(custom_code)) + self.assertEqual(response.status_code, 200) + self.assertTrue(response.json().get('ok')) def test_custom_taken_link_availability(self): - pass + custom_code = 'logo' + response = requests.get(self.url + '/check?custom_code={}'.format(custom_code)) + self.assertTrue(response.json().get('ok')) + data = self.data + data['custom_url'] = custom_code + requests.post(self.url + '/shorten', data=data, headers=self.headers) + response = requests.get(self.url + '/check?custom_code={}'.format(custom_code)) + self.assertFalse(response.json().get('ok')) def test_custom_taken_link_shorten(self): - pass + custom_code = 'go' + response = requests.get(self.url + '/check?custom_code={}'.format(custom_code)) + self.assertTrue(response.json().get('ok')) + + data = self.data + data['custom_url'] = custom_code + requests.post(self.url + '/shorten', data=data, headers=self.headers) + + response = requests.get(self.url + '/check?custom_code={}'.format(custom_code)) + self.assertFalse(response.json().get('ok')) + + response = requests.post(self.url + '/shorten', data=data, headers=self.headers) + self.assertEqual(response.status_code, 400) def test_custom_links(self): data = self.data