async def leave(self, ctx): message = ctx.message if Users.select().where((Users.discord_id == message.author.id) & (Users.server_id == message.guild.id)): Users.delete().where((Users.discord_id == message.author.id) & ( Users.server_id == message.guild.id)).execute() await message.channel.send("You have killed your queen ant.") else: await ctx.send("You are not a part of this game.")
class ForgotPasswordTestCase(unittest.TestCase): """ Test user can reset their password using the forgot password endpoint """ def setUp(self): """ Create testing client version of flask app and persist a temporary user """ self.test_app = create_app(DATABASE_NAME='test_analytics', TESTING=True) self.testing_client = self.test_app.test_client() self.testing_client_context = self.test_app.app_context() self.testing_client_context.push() self.dummy_user = Users("Joey", "*****@*****.**", Users.generate_hash("1234".encode( "utf8")).decode("utf8"), True, True) self.dummy_user.save() self.dummy_user.commit() def tearDown(self): """ Remove temporary user from database""" self.testing_client_context.pop() self.dummy_user.delete() self.dummy_user.commit() def test_forgot_password(self): """ Test forgot password sets user's password to a system generated password """ forgotten_password = self.dummy_user.password response = self.testing_client.post('/forgot_password', data=dict( email=self.dummy_user.email)) self.assertIn(b"success", response.data) self.assertEquals(response.status_code, 200) updated_user = Users.find_by_email(self.dummy_user.email) self.assertNotEqual(updated_user.password, forgotten_password) def test_forgot_password_failure(self): """ Test authentication failure and correct status code """ response = self.testing_client.post('/forgot_password', data=dict( email="*****@*****.**")) self.assertIn(b"cannot find user", response.data) self.assertEquals(response.status_code, 403)
def generate_access_token(self) -> {str, str}: """ Generate admin access JWT. :return: a HTTP authorization header containing a admin access token """ admin_user = Users("Admin user", "*****@*****.**", Users.generate_hash(b"1234").decode("utf8"), True, True) admin_user.save() admin_user.commit() response_login = self.testing_client.post('/login', data=dict( email="*****@*****.**", password="******", remember=True)) response_login_json = response_login.get_json() admin_user.delete() admin_user.commit() return {'Authorization': 'Bearer {}'.format( response_login_json["access_token"])}
def test_user_email_duplication() -> NoReturn: """ test that duplication of a user email is not possible """ json_data = { "email": "*****@*****.**", "fullname": "fullname", "admin": True, "password": "******" } new_user = Users("fullname", '*****@*****.**', Users.generate_hash('top_secret'.encode("utf-8")), True, True) new_user.save() new_user.commit() response = dependencies.client.post('/admin/create_new_user', json=json_data, headers=dependencies.auth_header, follow_redirects=True) assert response.status_code == 400 new_user.delete() new_user.commit()
def generate_access_token(self) -> dict: """ Remove the entries that were persisted in the simulate_importer method :return: a HTTP authorization header containing a admin access token """ admin_user = Users("Admin user", "*****@*****.**", Users.generate_hash(b"1234").decode("utf8"), True, True) admin_user.save() admin_user.commit() response_login = self.testing_client.post('/login', data=dict( email="*****@*****.**", password="******", remember=True)) response_login_json = response_login.get_json() admin_user.delete() admin_user.commit() return { 'Authorization': 'Bearer {}'.format(response_login_json["access_token"]) }
class User(Controller): _users = None def __init__(self): super(User, self).__init__() self._users = Users() def get(self, id): self._permissions.signed() data = self._users.get(id) if "password" in data: del data["password"] return data def add(self, data): self._permissions.signed() return self._users.add(data) def edit(self, id, data): self._permissions.signed() return self._users.edit(id, data) def delete(self, id): self._permissions.signed() return self._users.delete(id) def all(self, filter): self._permissions.signed() data = self._users.all(filter) for item in data: if "password" in item: del item["password"] return data def current(self): return self._users.check(session.get("email"), session.get("password")) def signin(self, data): user = self._users.check(data.get("email"), data.get("password")) if "email" in user and "password" in user: session["email"] = user["email"] session["password"] = user["password"] return self.current() def signout(self): session.pop('email', None) session.pop('password', None) return self.current()
class CelryTestCase(unittest.TestCase): """ Test whether user can request and receive a prediction task asynchronously """ def setUp(self): """ Create testing client version of flask app """ self.test_app = create_app(DATABASE_NAME='test_analytics', TESTING=True) self.testing_client = self.test_app.test_client() self.testing_client_context = self.test_app.app_context() self.testing_client_context.push() self.dummy_user = Users( "Joey", "*****@*****.**", Users.generate_hash("1234".encode("utf8")).decode("utf8"), True, True) self.dummy_user.save() self.dummy_user.commit() def tearDown(self): """ Remove testing client context """ self.testing_client_context.pop() self.dummy_user.delete() self.dummy_user.commit() def test_async_prediction(self): """ Test whether the correct response is sent when a user makes an async requests for a prediction """ response = self.testing_client.get( '/data?limit=100&attributedata=NO2&predictions=True' '&n_predictions=200&per_sensor=True') self.assertIn(b"Forecasting engine making predictions", response.data) self.assertIn(b"task_id", response.data) def test_async_polling_and_reception(self): """ Test whether a user can poll the /predict_status endpoint for the state of their async request """ response = self.testing_client.get( '/data?limit=100&attributedata=NO2&predictions=True' '&n_predictions=200&per_sensor=True') self.assertIn(b"Forecasting engine making predictions", response.data) self.assertIn(b"task_id", response.data) response_json = response.get_json() task_id = response_json[1]["task_id"] response_poll = self.testing_client.get( "/pred_status?task_id={}".format(task_id)) response_poll_json = response_poll.get_json() while response_poll_json["state"] in ["PENDING", "PROGRESS"]: response_poll = self.testing_client.get( "/pred_status?task_id={}".format(task_id)) response_poll_json = response_poll.get_json() self.assertEqual(response_poll_json["state"], "SUCCESS") def test_adding_prediction_per_user(self): """ Test whether a prediction result is associated with a user in the userpredictions table """ response = self.testing_client.get( '/data?limit=3&attributedata=NO2&predictions=True' '&n_predictions=3') self.assertIn(b"Forecasting engine making predictions", response.data) self.assertIn(b"task_id", response.data) response_json = response.get_json() task_id = response_json[1]["task_id"] response_poll = self.testing_client.get( "/pred_status?task_id={}".format(task_id)) response_poll_json = response_poll.get_json() while response_poll_json["state"] in ["PENDING", "PROGRESS"]: response_poll = self.testing_client.get( "/pred_status?task_id={}".format(task_id)) response_poll_json = response_poll.get_json() self.assertEqual(response_poll_json["state"], "SUCCESS") self.assertIsNotNone(response_poll_json["result"]["Prediction_id"])