def test_identical_base_quote_currencies( self, authorized_client: Client) -> None: rv: Response = authorized_client.post( "/account/quotes", json=dict(action="buy", amount=1_000_000, currency_pair="Coin1_Coin1"), )
def test_login_user(client: Client): resp = client.post( "/auth/login", json=dict(identifier=TEST_USER1_USERNAME, password=TEST_USER1_PASSWORD), ) json = resp.get_json() assert json["status"] == "success" assert len(json["data"]["access_token"]) > 5
def test_user_create_house(client: Client): auth = authenticate_user1(client) resp = client.post( "/house/add", json=dict(name=TEST_HOUSE_NAME, description=TEST_HOUSE_DESCRIPTION), headers={"Authorization": auth}, ) json_resp = resp.get_json() assert json_resp["status"] == "success"
def test_illegal_amount(self, authorized_client: Client) -> None: rv: Response = authorized_client.post( "/account/quotes", json=dict( action="buy", amount="what?!", currency_pair="Coin1_USD", ), ) assert rv.status_code == 400
def test_wrong_action(self, authorized_client: Client) -> None: rv: Response = authorized_client.post( "/account/quotes", json=dict( action="steal", amount=100, currency_pair="Coin1_USD", ), ) assert rv.status_code == 400
def test_login_user_custom_expiry(client: Client): token = client.post( "/auth/login", json={ "identifier": TEST_USER1_EMAIL, "password": TEST_USER1_PASSWORD, "custom_expiry": 1000, }, ).get_json() decoded = jwt.decode(token["data"]["access_token"], verify=False) assert (decoded["exp"] - 1000) == decoded["iat"]
def create_new_job(client: Client, data, auth_header, must_succeed=True): """ Helper function for valid new job creation. """ response = client.post('/api/v1/jobs', headers=auth_header, data=data) if must_succeed: assert response.status_code == 200, f"status code was {response.status_code} with {response.data}" assert response.content_type == 'application/json' return response.json return response
def test_register_user_3(client: Client): resp = client.post( "/auth/register", json=dict( username=TEST_USER3_USERNAME, email=TEST_USER3_EMAIL, password=TEST_USER3_PASSWORD, ), ) json = resp.get_json() assert json["msg"] == "Created a new user." assert json["status"] == "success"
def post_json(client: Client, url: str, json_body: Union[dict, list], **kwargs) -> Response: """ Send a POST request to this URL. :param client: Flask test client. :param url: Relative server URL (starts with /). :param json_body: Python structure corresponding to the JSON to be sent. :return: Received response. """ return client.post(url, data=json.dumps(json_body), content_type="application/json", **kwargs)
def test_non_unique_username(self, admin_client: Client, create_new_user_mock) -> None: rv: Response = admin_client.post( "/admin/users", json=dict( username=NON_UNIQUE_USERNAME, is_admin=True, password="******", first_name="Test", last_name="Test oğlu", ), ) assert rv.status_code == 409
def test_200_usd(self, authorized_client: Client, mock_create_order) -> None: rv: Response = authorized_client.post( "/account/quotes", json=dict( action="buy", amount=10000, currency_pair="Coin1_USD", ), ) assert rv.status_code == 200 data = rv.get_json() assert data["price"] == 12345 assert data["rfq"]["action"] == "buy"
def test_user_add_task(client: Client): auth = authenticate_user2(client) house_1 = get_house1_id(client, auth) add_task_resp = client.post( "/house/{}/task/add".format(house_1), json={ "name": TASK1_NAME, "description": TASK1_DESCRIPTION, "frequency": TASK1_FREQUENCY, }, headers={ "Authorization": auth }, ).get_json() assert add_task_resp["status"] == "success"
def test_create_user(self, admin_client: Client, create_new_user_mock) -> None: user_params = dict( username="******", is_admin=True, password="******", first_name="Test", last_name="Test oğlu", ) rv: Response = admin_client.post( "/admin/users", json=user_params, ) assert rv.status_code == 200 assert rv.data.decode() == "1" # user id assert create_new_user_mock["params"] == user_params
def test_user_reset_password(client: Client): with mail.record_messages() as outbox: client.get("/auth/password_reset/{}".format(TEST_USER1_EMAIL)) assert outbox[0].subject == "Reset your password." body_text: str = outbox[0].body body_text = body_text.replace( "Follow this link to reset your password: /auth/password_reset/reset_form/", "", ) email_verify_resp = client.post( "/auth/password_reset/reset/{}".format(body_text), data={ "password": TEST_USER1_PASSWORD, "password2": TEST_USER1_PASSWORD }, ) assert re.match(b"Your password has been reset.", email_verify_resp.data)
def test_user_update_house(client: Client): auth = authenticate_user1(client) house_1 = get_house1_id(client, auth) update_resp = client.post( "/house/update", json={ "house_id": house_1, "name": TEST_HOUSE_NEW_NAME }, headers={"Authorization": auth}, ) assert update_resp.get_json()["status"] == "success" auth2 = authenticate_user2(client) house_resp = client.get("/house/{}/get".format(house_1), headers={"Authorization": auth2}) json_house = house_resp.get_json() assert json_house["status"] == "success" assert json_house["data"]["name"] == TEST_HOUSE_NEW_NAME
def test_register_user(client: Client): with mail.record_messages() as outbox: resp = client.post( "/auth/register", json=dict( username=TEST_USER1_USERNAME, email=TEST_USER1_EMAIL, password=TEST_USER1_OLD_PASSWORD, ), ) json = resp.get_json() assert outbox[0].subject == "Verify your email." body_text: str = outbox[0].body body_text = body_text.replace( "Follow this link to verify your email: ", "") email_verify_resp = client.get(body_text) assert email_verify_resp.data == b"Successfully verified your email." assert json["msg"] == "Created a new user." assert json["status"] == "success"
def post_file( client: Client, url: str, file_name: str, file_path: str, additional_json: dict = None, **kwargs, ) -> Response: """ Send a POST request to this URL. :param client: Flask test client. :param url: Relative server URL (starts with /). :param file_name: Name of the parameter corresponding to the file to be sent. :param file_path: Path to the file that should be sent. :param additional_json: Additional JSON to be sent in body. :return: Received response. """ with open(file_path, "rb") as file: data = {file_name: (file, file_name)} if additional_json: data.update(additional_json) return client.post(url, data=data, **kwargs)
def test_user_update_task(client: Client): auth = authenticate_user1(client) house_1 = get_house1_id(client, auth) get_tasks_resp = client.get("/house/{}/task/all".format(house_1), headers={ "Authorization": auth }).get_json() task_update_resp = client.post( "/task/{}/update".format(get_tasks_resp["data"][0]["task_id"]), headers={ "Authorization": auth }, json={ "name": TASK1_UPDATED_NAME }, ).get_json() assert task_update_resp["status"] == "success" task_resp = client.get( "/task/{}".format(get_tasks_resp["data"][0]["task_id"]), headers={ "Authorization": auth }, ).get_json() assert task_resp["data"]["name"] == TASK1_UPDATED_NAME
def test_200(self, authorized_client: Client, mock_process_order) -> None: rv: Response = authorized_client.post( "/account/quotes/9d2ed500-981b-11ea-bb37-0242ac130002/actions/execute", json=dict(payment_method="visa1234"), ) assert rv.status_code == 204
def authenticate_user3(client: Client): return client.post( "/auth/login", json=dict(identifier=TEST_USER3_USERNAME, password=TEST_USER3_PASSWORD), ).get_json()["data"]["access_token"]
def test_process_incorrect_file(client: Client): res = client.post("/process/incorrect_data") assert res.status_code == 400 assert "BadFile" in res.json.values()
def test_process_correct_file(client: Client): res = client.post("/process/data") assert res.status_code == 202 assert "Location" in res.headers.keys()
def test_process_not_found(client: Client): res = client.post("/process/data3") assert res.status_code == 404 assert "File not found" in res.json.values()