async def add_user(self, user, approve=False): """Stores user in the shelf """ using_id_only = False name = None user_id = None avatar_url = None try: int(user) user_id = str(user) using_id_only = True except ValueError: # Not an ID user_id = user.id name = str(user) avatar_url = get_avatar_url(user) str(user) except TypeError: user_id = user.id name = str(user) avatar_url = get_avatar_url(user) str(user) # Check if the ID already exists, if true, don't rebuild anything, # just assign $approve try: entry = self.get_user(user) # NOTE: User can also be an ID entry['approved'] = approve print(f'An entry for {user} already exists, ' + f'switching approved to {approve}') # Somethimes DB entry is invalidated, regenerate its key if not entry['key']: print(f'User does not have a key, assigning it now') entry['key'] = make_key() if not using_id_only: try: print(f'Also forcing an update for {name}…') entry = self.update_user_entry(user, entry) except AttributeError: pass # Update later self.shelve[user_id] = entry self.sync() return self.shelve[user_id] except KeyError: pass self.shelve[user_id] = make_entry(user_id, name, avatar_url, make_key(), approve) print(f'Manually approved user {name}, ID - {user_id}') self.shelve.sync() return self.shelve[user_id]
def __init__(self, server, agent): self.server = server self.agent = agent self.nick = None self.room = None self.idx = None self.key = make_key()
def web_shorten(url): url = url.strip() if len(url) < 2 or utils.check_url(url) == False: return no_url() conn = utils.create_connection("test.db") check = utils.check_entry(url, conn) db_url = check[1] if check else False if db_url and db_url == url: conn.close() return already_used() shortcode = utils.make_key(6) _date = utils.get_date() utils.new_entry(url, shortcode, _date, _date, conn) conn.close() return shortcode
def shorten(): shortcode = "" if request.method == 'POST': received = request.get_json(force=True) url = received["url"] if received["url"] else "" if len(url) < 2 or utils.check_url(url) == False: return no_url() conn = utils.create_connection("test.db") check = utils.check_entry(url, conn) db_url = check[1] if check else False if db_url and db_url == url: conn.close() return already_used() try: shortcode = received["shortcode"] except KeyError: logging.warn("No shortcode provided, generating one...") shortcode = utils.make_key(6) if utils.check_shortcode(shortcode) == False: conn.close() return invalid_code() _date = utils.get_date() utils.new_entry(url, shortcode, _date, _date, conn) conn.close() return flask.make_response(shortcode, 201)
def __init__(self): self.key = make_key() self.thread = None
def make_keys(self): return (make_key(), make_key())
def test_04_shorten(self): """Shorten with invalid key""" with service.test_client() as client: sent = {'url': "test.it", "shortcode": make_key(9)} posting = client.post('/shorten', json=sent) self.assertEqual(posting.status_code, 412)
import unittest from service import service from utils import make_key from random import randrange service.testing = True ### OPEN URLS FILE AND CREATE SHORTCODES FOR TESTING ### with open("urls.txt", "r") as urls: urls = urls.readlines()[:10] urls = [x.rstrip() for x in urls] shortcodes = [make_key(6) for x in range(len(urls))] class TestApi(unittest.TestCase): def test_01_test(self): with service.test_client() as client: response = client.get('/') self.assertEqual(response.status_code, 200) def test_02_shorten(self): """Shorten urls from url file""" with service.test_client() as client: for x in range(len(urls)): sent = {'url': urls[x], "shortcode": shortcodes[x]} posting = client.post('/shorten', json=sent) self.assertEqual(posting.status_code, 201) def test_03_shorten(self):