Browse Source

Add tests to crypter routes.

Cixo Develop 7 months ago
parent
commit
f6db3106c4
3 changed files with 61 additions and 2 deletions
  1. 1 0
      assets/__init__.py
  2. 2 2
      assets/application_crypter.py
  3. 58 0
      tests/009-application_crypter.py

+ 1 - 0
assets/__init__.py

@@ -22,3 +22,4 @@ from .validators import validator_dumper
 from .validators import validator_result
 from .validators import password_validator
 from .validators import nick_validator
+from .application_crypter import application_crypter

+ 2 - 2
assets/application_crypter.py

@@ -5,7 +5,7 @@ from .user import user
 from .user_loader import user_loader
 from .secret_coder import bad_password
 
-class application_secret(application_part):
+class application_crypter(application_part):
     """
     This is endpoints, which is responsible for encrypting  and decrypting
     new secrets, to work it require apikey and password. Apikey is used to
@@ -91,6 +91,6 @@ class application_secret(application_part):
     @property
     def __database(self) -> user_loader:
         """ This return new handler to user loader. """
-        
+
         return user_loader(self._connector)    
     

+ 58 - 0
tests/009-application_crypter.py

@@ -0,0 +1,58 @@
+import pathlib
+
+current = pathlib.Path(__file__).parent
+root = current.parent
+
+import sys
+sys.path.append(str(root))
+
+import assets
+
+def drop_db() -> None:
+    db = pathlib.Path("./009-application_crypter.db")
+
+    if db.is_file():
+        db.unlink()
+
+drop_db()
+
+import sqlmodel
+
+connection = sqlmodel.create_engine("sqlite:///009-application_crypted.db")
+user_app = assets.application_user(connection)
+crypter_app = assets.application_crypter(connection)
+
+sqlmodel.SQLModel.metadata.create_all(connection)
+
+user_app.register("test", "password")
+apikey = user_app.login("test", "password")["apikey"]
+
+print("Create test user, apikey: " + apikey)
+
+code_key = user_app.get(apikey)["code_key"]
+coder = assets.code_key_manager("password", code_key).coder
+outside_sample_plain = "SECRET"
+outside_sample_crypted = coder.encrypt(outside_sample_plain)
+
+print()
+print("Testing encryption works...")
+result_a = crypter_app.encrypt(apikey, "password", outside_sample_plain)
+print("Crypted:")
+print(result_a)
+print("Encrypted with other coder:")
+print(coder.decrypt(result_a["crypted"]))
+
+print()
+print("Testing decryption works...")
+print("Decrypted:")
+print(crypter_app.decrypt(apikey, "password", outside_sample_crypted))
+
+print()
+print("Testing bad password:")
+print(crypter_app.decrypt(apikey, "bad_psk", outside_sample_crypted))
+
+print()
+print("Testing with bad apikey:")
+print(crypter_app.decrypt("NOT_EXISTS", "bad_psk", outside_sample_crypted))
+
+drop_db()