003-validator.py 968 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. import pathlib
  2. import sys
  3. current = pathlib.Path(__file__)
  4. test_dir = current.parent
  5. project_dir = test_dir.parent
  6. source_dir = project_dir / pathlib.Path("source")
  7. sys.path.append(str(source_dir))
  8. import cx_apikey as apikey
  9. test_count = 0
  10. def test(tag: str, result: bool) -> None:
  11. global test_count
  12. test_count += 1
  13. print("Running test " + tag + " (" + str(test_count) + "):", end = " ")
  14. if result:
  15. print("Valid")
  16. return
  17. print("FAIL!!!")
  18. exit(-1)
  19. factory = apikey.apikey_factory("test").set_size(15)
  20. sample = factory.generate()
  21. sample_manual = "test_1234567890"
  22. bad_sample = "no_1234567890"
  23. other_bad = "test_uuuuuuuuuu"
  24. validator = factory.get_validator()
  25. test("self generated", validator.validate(sample))
  26. test("manual writen proof", validator.validate(sample_manual))
  27. test("bad sample, not validated", not validator.validate(bad_sample))
  28. test("second bad sample, not validated", not validator.validate(other_bad))