Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # ---------------- api_client.py ----------------
- # pip install requests
- import time
- from requests import session
- class ApiClient:
- user_name = None
- user_pass = None
- user_id = None
- def __init__(self):
- self.client = session()
- self.host = "https://demoqa.com"
- def request(self, method, url, **kwargs) -> dict | None:
- time.sleep(0.5) # slow down not to ddos 3rd party server
- res = self.client.request(method, f"{self.host}/{url}", **kwargs)
- res.raise_for_status()
- if res.text:
- return res.json()
- def get_books(self):
- return self.request("GET", "BookStore/v1/Books")
- def create_user(self):
- payload = {"userName": self.user_name,
- "password": self.user_pass}
- return self.request("POST", "Account/v1/User", json=payload)
- def create_token(self):
- payload = {"userName": self.user_name,
- "password": self.user_pass}
- return self.request("POST", "Account/v1/GenerateToken", json=payload)
- def user_login(self):
- payload = {"userName": self.user_name,
- "password": self.user_pass}
- return self.request("POST", "Account/v1/Login", json=payload)
- def get_user(self):
- return self.request("GET", f"Account/v1/User/{self.user_id}")
- def add_books(self, *isbns: str):
- payload = {"userId": self.user_id,
- "collectionOfIsbns": [{"isbn": i} for i in isbns]}
- return self.request("POST", "BookStore/v1/Books", json=payload)
- def delete_book(self, isbn):
- payload = {"isbn": isbn,
- "userId": self.user_id}
- return self.request("DELETE", "BookStore/v1/Book", json=payload)
- def swap_books(self, isbn1, isbn2):
- payload = {"userId": self.user_id,
- "isbn": isbn2}
- return self.request("PUT", f"BookStore/v1/Books/{isbn1}", json=payload)
- def delete_books(self):
- params = {"UserId": self.user_id}
- return self.request("DELETE", f"BookStore/v1/Books", params=params)
- def delete_user(self):
- return self.request("DELETE", f"Account/v1/User/{self.user_id}")
- def setup(self, user_name, user_pass):
- self.user_name = user_name
- self.user_pass = user_pass
- self.create_user()
- self.create_token()
- res = self.user_login()
- self.user_id = res["userId"]
- self.client.headers["Authorization"] = f"Bearer {res['token']}"
- # ------------- test_sample.py -----------------------
- import os
- import pytest
- from api_client import ApiClient
- USER_NAME = os.environ.get("API_TEST_USER_NAME")
- PASSWORD = os.environ.get("API_TEST_USER_PASSWORD")
- @pytest.fixture
- def client():
- client = ApiClient()
- client.setup(USER_NAME, PASSWORD)
- yield client
- client.delete_user()
- @pytest.fixture(scope="session")
- def books():
- return ApiClient().get_books()["books"]
- def test_full(client, books):
- client.get_user()
- client.add_books(books[0]["isbn"], books[1]["isbn"])
- client.delete_book(books[0]["isbn"])
- client.replace_book(books[1]["isbn"], books[2]["isbn"])
- client.delete_books()
- def test_get_books():
- res = ApiClient().get_books()
- assert len(res["books"]) == 8
- def test_get_profile(client):
- res = client.get_user()
- assert res["userId"] == client.user_id
- assert res["username"] == client.user_name
- assert len(res["books"]) == 0
- def test_add_books(client, books):
- isbn1 = books[0]["isbn"]
- isbn2 = books[1]["isbn"]
- res = client.add_books(isbn1, isbn2)
- assert res["books"] == [{'isbn': isbn1}, {'isbn': isbn2}]
- res = client.get_user()
- assert len(res["books"]) == 2
- assert res["books"][0]["isbn"] == isbn1
- assert res["books"][1]["isbn"] == isbn2
- def test_delete_books(client, books):
- isbn1 = books[0]["isbn"]
- isbn2 = books[1]["isbn"]
- client.add_books(isbn1, isbn2)
- client.delete_books()
- res = client.get_user()
- assert len(res["books"]) == 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement