inventory_api_test.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import os
  2. from time import sleep
  3. os.chdir(os.path.dirname(__file__))
  4. import sys
  5. sys.path.append("..")
  6. sys.path.append("..\\Sources")
  7. from Sources.Modules.Inventory.inventory import * #altered
  8. import Sources.main as main_func
  9. import Sources.Model.model_manager as model_manager
  10. import requests
  11. from werkzeug.security import generate_password_hash
  12. import Sources.View.view_manager as view
  13. item_id_to_delete = 0
  14. import threading
  15. import pytest
  16. from requests.auth import AuthBase
  17. class TokenAuth(AuthBase):
  18. """Implements a custom authentication scheme."""
  19. def __init__(self, token):
  20. self.token = token
  21. def __call__(self, r):
  22. """Attach an API token to a custom auth header."""
  23. r.headers['x-Auth-Token'] = f'{self.token}' # Python 3.6+
  24. return r
  25. @pytest.fixture(autouse=True,scope='session')
  26. def fixture2():
  27. def run() :
  28. main_func.init()
  29. persistence.wipeout_database()
  30. persistence.init()
  31. model_manager.init()
  32. main_func.main()
  33. t1 = threading.Thread(target=run)
  34. t1.start()
  35. while(not main_func.isRunning()) :
  36. sleep(1)
  37. yield
  38. main_func.stop()
  39. t1.join()
  40. # Perform cleanup on the data when the test function exits
  41. import json
  42. def test_api() :
  43. sleep(2)
  44. print("login")
  45. r = requests.post("http://127.0.0.1:8000/api/login",json={'username' : 'admin','password': 'aseqzdwxc'})
  46. print(r.__dict__)
  47. assert r.status_code == 200
  48. data = json.loads(r.content)
  49. token = data["token"]
  50. assert token is not None
  51. print("CREATE")
  52. r = requests.post("http://127.0.0.1:8000/api/inventory/item",json={"assign_date_epoch": 5000, "assigned": True, "brand": "Mikrotik", "name": "RB2011", "note": "saline 2 RT 2, chez ashvin", "owned": True, "serial_number": "12345678", "tags": "#router #mikrotik #saline", "type": "router", "wear_score": 100}
  53. ,auth=TokenAuth(token))
  54. print(r.__dict__)
  55. assert r.status_code == 200
  56. print("CREATE")
  57. r = requests.post("http://127.0.0.1:8000/api/inventory/item",json={"assign_date_epoch": 10000, "assigned": True, "brand": "Mikrotik", "name": "HEX S 1", "note": "saline 2 RT 3, chez ashvin", "owned": True, "serial_number": "12345678", "tags": "#router #mikrotik #saline", "type": "router", "wear_score": 100}
  58. ,auth=TokenAuth(token))
  59. print(r.__dict__)
  60. assert r.status_code == 200
  61. print("CREATE")
  62. r = requests.post("http://127.0.0.1:8000/api/inventory/item",json={"assign_date_epoch": 10000, "assigned": True, "brand": "Mikrotik", "name": "HEX S 1", "note": "saline 2 RT 3, chez ashvin", "owned": True, "serial_number": "12345678", "tags": "#router #mikrotik #saline", "type": "router", "wear_score": 100}
  63. ,auth=TokenAuth(token))
  64. print(r.__dict__)
  65. assert r.status_code == 200
  66. print("CREATE")
  67. r = requests.post("http://127.0.0.1:8000/api/inventory/item",json={"assign_date_epoch": 10000, "assigned": True, "brand": "Mikrotik", "name": "HEX S 1", "note": "saline 2 RT 3, chez ashvin", "owned": True, "serial_number": "12345678", "tags": "#router #mikrotik #saline", "type": "router", "wear_score": 100}
  68. ,auth=TokenAuth(token))
  69. print(r.__dict__)
  70. assert r.status_code == 200
  71. print("GET")
  72. r = requests.get("http://127.0.0.1:8000/api/inventory/items",auth=TokenAuth(token))
  73. print(r.__dict__)
  74. assert r.status_code == 200
  75. print("DELETE")
  76. r = requests.delete("http://127.0.0.1:8000/api/inventory/item/2",auth=TokenAuth(token))
  77. print(r.__dict__)
  78. assert r.status_code == 200
  79. print("CREATE SITE")
  80. r = requests.post("http://127.0.0.1:8000/api/inventory/site",json={"assign_date_epoch": 5000, "assigned": True, "brand": "Mikrotik", "name": "RB2011", "note": "saline 2 RT 2, chez ashvin", "owned": True, "serial_number": "12345678", "tags": "#router #mikrotik #saline", "type": "router", "wear_score": 100},auth=TokenAuth(token))
  81. print(r.__dict__)
  82. assert r.status_code == 200