Skip to content

Instantly share code, notes, and snippets.

@silashansen
Created August 18, 2023 08:13
Show Gist options
  • Save silashansen/d0dcf85d9a547cef498b062d2337c034 to your computer and use it in GitHub Desktop.
Save silashansen/d0dcf85d9a547cef498b062d2337c034 to your computer and use it in GitHub Desktop.
Webshop tests
# BEGIN: 8f7d6h3j4k5l
import unittest
import requests
import jwt
import time
class TestAPI(unittest.TestCase):
token = None
api_basepath = "https://api-test.xl-byg.dk"
auth_basepath = "https://api-auth.xl-byg.dk"
def test_oauth_token(self):
token = self.get_jwt()
# check that the token is a JWT by decoding it
decoded_token = jwt.decode(token, options={"verify_signature": False})
self.assertIn("iss", decoded_token)
# check that the token is an access token
self.assertIn("token_use", decoded_token)
self.assertEqual(decoded_token["token_use"], "access")
# check that the scope is present
self.assertIn("scope", decoded_token)
# check that token is not expired
self.assertGreater(decoded_token["exp"], time.time())
def test_get_orders_orderid_api(self):
orderId = 1000
url = self.api_basepath + f"/orders/{orderId}"
response = requests.get(url, headers=self.get_auth_headers())
self.assertEqual(response.status_code, 200)
self.assertIn('"orderId":"1000"', response.text)
def test_get_orders_orderlist_api(self):
url = self.api_basepath + "/orders/orderlist"
response = requests.get(url, headers=self.get_auth_headers())
self.assertEqual(response.status_code, 200)
self.assertIn('{}', response.text)
#@unittest.skip("skip test")
def test_get_pricelists_itemnumber_api(self):
itemNumber = "1263621"
url = self.api_basepath + f"/pricelists/{itemNumber}"
params = {
"groupNumber": 1,
"companyNumber": 1,
"stockNumber": "",
"customerNumber": 71871
}
response = requests.get(url, headers=self.get_auth_headers(), params=params)
#assert that the response code is 200
self.assertEqual(response.status_code, 200)
#assert that the response is a success
response_json = response.json()
self.assertEqual(response_json["pricelistResponse"]["success"], "true")
def test_get_stock_api(self):
url = self.api_basepath + "/stock"
response = requests.get(url, headers=self.get_auth_headers())
self.assertEqual(response.status_code, 200)
#test that response can be pased as valid json
response_json = response.json()
self.assertIn('env', response_json)
def get_auth_headers(self):
if self.token is None:
self.token = self.get_jwt()
return {
"Authorization": f"Bearer {self.token}"
}
def get_jwt(self):
url = f"{self.auth_basepath}/oauth2/token"
data = {
"grant_type": "client_credentials",
"client_id": "",
"client_secret": ""
}
response = requests.post(url, data=data)
if response.status_code == 200:
return response.json()["access_token"]
else:
raise Exception("Could not get token")
if __name__ == "__main__":
try:
unittest.main(verbosity=2)
except Exception as e:
print(e)
# Instructions
```
pip3 install -r requirements.txt
python3 api_tests.py
```
PyJWT==2.8.0
requests==2.31.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment