core_test.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import os
  2. from time import sleep
  3. from flask import request
  4. os.chdir(os.path.dirname(__file__))
  5. import sys
  6. sys.path.append("..")
  7. sys.path.append("..\\Sources")
  8. from Sources.Modules.Inventory.inventory import * #altered
  9. import Sources.main as main_func
  10. import Sources.Model.model_manager as model_manager
  11. import requests
  12. from werkzeug.security import generate_password_hash
  13. import Sources.View.view_manager as view
  14. item_id_to_delete = 0
  15. import threading
  16. import pytest
  17. import json
  18. from requests.auth import AuthBase
  19. class TokenAuth(AuthBase):
  20. """Implements a custom authentication scheme."""
  21. def __init__(self, token):
  22. self.token = token
  23. def __call__(self, r):
  24. """Attach an API token to a custom auth header."""
  25. r.headers['x-Auth-Token'] = f'{self.token}' # Python 3.6+
  26. return r
  27. @pytest.fixture(autouse=True,scope='session')
  28. def fixture2():
  29. def run() :
  30. main_func.init()
  31. persistence.wipeout_database()
  32. persistence.init()
  33. model_manager.init()
  34. main_func.main()
  35. t1 = threading.Thread(target=run)
  36. t1.start()
  37. while(not main_func.isRunning()) :
  38. sleep(1)
  39. yield
  40. main_func.stop()
  41. t1.join()
  42. # Perform cleanup on the data when the test function exits
  43. def test_login() :
  44. token = None
  45. r = requests.post("http://127.0.0.1:8000/api/login",json={'username' : 'admin','password': 'aseqzdwxc'})
  46. print(r.__dict__)
  47. data = json.loads(r.content)
  48. token = data["token"]
  49. assert token is not None
  50. assert r.status_code == 200
  51. r = requests.get("http://127.0.0.1:8000/api/me",auth=TokenAuth(token))
  52. print(r.__dict__)
  53. assert r.status_code == 200