def test_my_account_remove_api_key(self): usr = self.log_user(base.TEST_USER_REGULAR2_LOGIN, base.TEST_USER_REGULAR2_PASS) user = User.get(usr['user_id']) response = self.app.post( base.url('my_account_api_keys'), { 'description': 'desc', 'lifetime': -1, '_session_csrf_secret_token': self.session_csrf_secret_token() }) self.checkSessionFlash(response, 'API key successfully created') response = response.follow() # now delete our key keys = UserApiKeys.query().all() assert 1 == len(keys) response = self.app.post( base.url('my_account_api_keys_delete'), { 'del_api_key': keys[0].api_key, '_session_csrf_secret_token': self.session_csrf_secret_token() }) self.checkSessionFlash(response, 'API key successfully deleted') keys = UserApiKeys.query().all() assert 0 == len(keys)
def test_remove_api_key(self): self.log_user() user = User.get_by_username(base.TEST_USER_REGULAR_LOGIN) user_id = user.user_id response = self.app.post( base.url('edit_user_api_keys_update', id=user_id), { 'description': 'desc', 'lifetime': -1, '_session_csrf_secret_token': self.session_csrf_secret_token() }) self.checkSessionFlash(response, 'API key successfully created') response = response.follow() # now delete our key keys = UserApiKeys.query().filter(UserApiKeys.user_id == user_id).all() assert 1 == len(keys) response = self.app.post( base.url('edit_user_api_keys_delete', id=user_id), { 'del_api_key': keys[0].api_key, '_session_csrf_secret_token': self.session_csrf_secret_token() }) self.checkSessionFlash(response, 'API key successfully deleted') keys = UserApiKeys.query().filter(UserApiKeys.user_id == user_id).all() assert 0 == len(keys)
def _get_api_keys(self): api_keys = [self.api_key] for api_key in UserApiKeys.query() \ .filter_by(user_id=self.user_id, is_expired=False): api_keys.append(api_key.api_key) return api_keys
def get_api_keys(self, user, show_expired=True): user = User.guess_instance(user) user_api_keys = UserApiKeys.query() \ .filter(UserApiKeys.user_id == user.user_id) if not show_expired: user_api_keys = user_api_keys.filter_by(is_expired=False) return user_api_keys
def test_my_account_remove_api_key(self): usr = self.log_user('test_regular2', 'test12') user = User.get(usr['user_id']) response = self.app.post(url('my_account_api_keys'), {'description': 'desc', 'lifetime': -1}) self.checkSessionFlash(response, 'Api key successfully created') response = response.follow() #now delete our key keys = UserApiKeys.query().all() self.assertEqual(1, len(keys)) response = self.app.post(url('my_account_api_keys'), {'_method': 'delete', 'del_api_key': keys[0].api_key}) self.checkSessionFlash(response, 'Api key successfully deleted') keys = UserApiKeys.query().all() self.assertEqual(0, len(keys))
def test_my_account_remove_api_key(self): usr = self.log_user(TEST_USER_REGULAR2_LOGIN, TEST_USER_REGULAR2_PASS) user = User.get(usr['user_id']) response = self.app.post(url('my_account_api_keys'), {'description': 'desc', 'lifetime': -1, '_authentication_token': self.authentication_token()}) self.checkSessionFlash(response, 'API key successfully created') response = response.follow() #now delete our key keys = UserApiKeys.query().all() assert 1 == len(keys) response = self.app.post(url('my_account_api_keys_delete'), {'del_api_key': keys[0].api_key, '_authentication_token': self.authentication_token()}) self.checkSessionFlash(response, 'API key successfully deleted') keys = UserApiKeys.query().all() assert 0 == len(keys)
def get_api_keys(self): api_keys = [self.api_key] for api_key in UserApiKeys.query()\ .filter(UserApiKeys.user_id == self.user_id)\ .filter(or_(UserApiKeys.expires == -1, UserApiKeys.expires >= time.time())).all(): api_keys.append(api_key.api_key) return api_keys
def get_api_keys(self, user, show_expired=True): user = self._get_user(user) user_api_keys = UserApiKeys.query()\ .filter(UserApiKeys.user_id == user.user_id) if not show_expired: user_api_keys = user_api_keys\ .filter(or_(UserApiKeys.expires == -1, UserApiKeys.expires >= time.time())) return user_api_keys
def test_remove_api_key(self): self.log_user() user = User.get_by_username(TEST_USER_REGULAR_LOGIN) user_id = user.user_id response = self.app.post(url('edit_user_api_keys_update', id=user_id), {'description': 'desc', 'lifetime': -1, '_authentication_token': self.authentication_token()}) self.checkSessionFlash(response, 'API key successfully created') response = response.follow() #now delete our key keys = UserApiKeys.query().filter(UserApiKeys.user_id == user_id).all() assert 1 == len(keys) response = self.app.post(url('edit_user_api_keys_delete', id=user_id), {'del_api_key': keys[0].api_key, '_authentication_token': self.authentication_token()}) self.checkSessionFlash(response, 'API key successfully deleted') keys = UserApiKeys.query().filter(UserApiKeys.user_id == user_id).all() assert 0 == len(keys)
def test_remove_api_key(self): self.log_user() user = User.get_by_username(TEST_USER_REGULAR_LOGIN) user_id = user.user_id response = self.app.post(url('edit_user_api_keys', id=user_id), {'_method': 'put', 'description': 'desc', 'lifetime': -1}) self.checkSessionFlash(response, 'Api key successfully created') response = response.follow() #now delete our key keys = UserApiKeys.query().filter(UserApiKeys.user_id == user_id).all() self.assertEqual(1, len(keys)) response = self.app.post(url('edit_user_api_keys', id=user_id), {'_method': 'delete', 'del_api_key': keys[0].api_key}) self.checkSessionFlash(response, 'Api key successfully deleted') keys = UserApiKeys.query().filter(UserApiKeys.user_id == user_id).all() self.assertEqual(0, len(keys))
def test_my_account_remove_api_key(self): usr = self.log_user('test_regular2', 'test12') user = User.get(usr['user_id']) response = self.app.post(url('my_account_api_keys'), { 'description': 'desc', 'lifetime': -1 }) self.checkSessionFlash(response, 'Api key successfully created') response = response.follow() #now delete our key keys = UserApiKeys.query().all() self.assertEqual(1, len(keys)) response = self.app.post(url('my_account_api_keys'), { '_method': 'delete', 'del_api_key': keys[0].api_key }) self.checkSessionFlash(response, 'Api key successfully deleted') keys = UserApiKeys.query().all() self.assertEqual(0, len(keys))
def delete(self, api_key, user=None): """ Deletes given api_key, if user is set it also filters the object for deletion by given user. """ api_key = UserApiKeys.query().filter(UserApiKeys.api_key == api_key) if user is not None: user = User.guess_instance(user) api_key = api_key.filter(UserApiKeys.user_id == user.user_id) api_key = api_key.scalar() Session().delete(api_key)
def delete(self, api_key, user=None): """ Deletes given api_key, if user is set it also filters the object for deletion by given user. """ api_key = UserApiKeys.query().filter(UserApiKeys.api_key == api_key) if user: user = self._get_user(user) api_key = api_key.filter(UserApiKeys.user_id == user.user_id) api_key = api_key.scalar() Session().delete(api_key)
def test_my_account_add_api_keys(self, desc, lifetime): usr = self.log_user('test_regular2', 'test12') user = User.get(usr['user_id']) response = self.app.post(url('my_account_api_keys'), {'description': desc, 'lifetime': lifetime}) self.checkSessionFlash(response, 'Api key successfully created') try: response = response.follow() user = User.get(usr['user_id']) for api_key in user.api_keys: response.mustcontain(api_key) finally: for api_key in UserApiKeys.query().all(): Session().delete(api_key) Session().commit()
def test_my_account_add_api_keys(self, desc, lifetime): usr = self.log_user(TEST_USER_REGULAR2_LOGIN, TEST_USER_REGULAR2_PASS) user = User.get(usr['user_id']) response = self.app.post(url('my_account_api_keys'), {'description': desc, 'lifetime': lifetime, '_authentication_token': self.authentication_token()}) self.checkSessionFlash(response, 'API key successfully created') try: response = response.follow() user = User.get(usr['user_id']) for api_key in user.api_keys: response.mustcontain(api_key) finally: for api_key in UserApiKeys.query().all(): Session().delete(api_key) Session().commit()
def test_my_account_add_api_keys(self, desc, lifetime): usr = self.log_user('test_regular2', 'test12') user = User.get(usr['user_id']) response = self.app.post(url('my_account_api_keys'), { 'description': desc, 'lifetime': lifetime }) self.checkSessionFlash(response, 'Api key successfully created') try: response = response.follow() user = User.get(usr['user_id']) for api_key in user.api_keys: response.mustcontain(api_key) finally: for api_key in UserApiKeys.query().all(): Session().delete(api_key) Session().commit()
def test_add_api_keys(self, desc, lifetime): self.log_user() user = User.get_by_username(TEST_USER_REGULAR_LOGIN) user_id = user.user_id response = self.app.post(url('edit_user_api_keys', id=user_id), {'_method': 'put', 'description': desc, 'lifetime': lifetime}) self.checkSessionFlash(response, 'Api key successfully created') try: response = response.follow() user = User.get(user_id) for api_key in user.api_keys: response.mustcontain(api_key) finally: for api_key in UserApiKeys.query().filter(UserApiKeys.user_id == user_id).all(): Session().delete(api_key) Session().commit()
def test_my_account_add_api_keys(self, desc, lifetime): usr = self.log_user(TEST_USER_REGULAR2_LOGIN, TEST_USER_REGULAR2_PASS) user = User.get(usr['user_id']) response = self.app.post( url('my_account_api_keys'), { 'description': desc, 'lifetime': lifetime, '_authentication_token': self.authentication_token() }) self.checkSessionFlash(response, 'API key successfully created') try: response = response.follow() user = User.get(usr['user_id']) for api_key in user.api_keys: response.mustcontain(api_key) finally: for api_key in UserApiKeys.query().all(): Session().delete(api_key) Session().commit()
def create(self, user, description, lifetime=-1): """ :param user: user or user_id :param description: description of ApiKey :param lifetime: expiration time in seconds """ user = self._get_user(user) new_api_key = UserApiKeys() new_api_key.api_key = generate_api_key(user.username) new_api_key.user_id = user.user_id new_api_key.description = description new_api_key.expires = time.time() + (lifetime * 60) if lifetime != -1 else -1 Session().add(new_api_key) return new_api_key