server/server/authentication_test.py

122 lines
4.0 KiB
Python

from authentication import Authetication
import config_init as init
import json
import logging
import os
test_logger = logging.getLogger('TEST ')
test_logger.root.setLevel(logging.INFO)
def testSaveUser(username: str, password: str):
logging.info('STARTING SAVE USER TEST')
auth = Authetication()
auth.initConfig()
result = auth.saveUser(username, password)
check = False
with open(auth.CONFIG_DIRECTORY_LOCATION + '\\config.txt') as json_file:
data = json.load(json_file)
for user in data['user']:
if username == user['username']:
check = True
if result and check:
test_logger.info('TEST 1 ---> Save VALID user :: PASSED')
else:
test_logger.info('TEST 1 ---> Save VALID user :: FAILED')
def testAuth(username: str, password: str):
logging.info('STARTING AUTHENTICATION TEST')
auth = Authetication()
auth.initConfig()
auth.saveUser(username, password)
homeDir = auth.login(username, password)
if homeDir == auth.HOME_DIRECTORY_LOCATION + os.path.sep + '1':
test_logger.info('TEST 1 --> Authentication test with VALID :: PASSED')
else:
test_logger.info('TEST 1 --> Authentication test with VALID :: FAILED')
homeDir = auth.login(username, "")
if homeDir == "":
test_logger.info('TEST 2 --> Authentication test with INVALID :: PASSED')
else:
test_logger.info('TEST 2 --> Authentication test with INVALID ::FAILED')
def testUserExists(username: str, password: str):
logging.info('STARTING USER EXISTS TEST')
auth = Authetication()
auth.initConfig()
auth.saveUser(username, password)
if auth.checkUserExists(username):
logging.info('TEST 1 --> User exists with VALID user :: PASSED')
else:
logging.info('TEST 1 --> User exists with VALID user :: FAILED')
if auth.checkUserExists(""):
logging.info('TEST 2 --> User exists with INVALID user :: FAILED')
else:
logging.info('TEST 2 --> User exists with INVALID user :: PASSED')
def testPersistency():
logging.info('PERSISTENCY TEST')
auth = Authetication()
auth.initConfig()
serverPublicKey = init.generatePrivateKeyForServer(auth, 'admin')
auth.saveUser('alma','alma')
homeDir = auth.login('alma','alma')
init.generatePrivateKeyForUser(auth, 'alma', 'amla', serverPublicKey, init.generateSourceAddress(homeDir))
auth.saveUser('citrom','citrom')
homeDir = auth.login('citrom','citrom')
init.generatePrivateKeyForUser(auth, 'citrom', 'mortic', serverPublicKey, init.generateSourceAddress(homeDir))
auth2 = Authetication()
if auth2.checkUserExists('alma'):
logging.info('TEST 1 --> Persictency test :: PASSED')
else:
logging.info('TEST 1 --> Persictency test :: FAILED')
if auth2.checkUserExists(""):
logging.info('TEST 2 --> Persictency test :: FAILED')
else:
logging.info('TEST 2 --> Persictency test :: PASSED')
if os.path.isdir(auth.HOME_DIRECTORY_LOCATION):
logging.info('TEST 3 --> Persictency test :: PASSED')
else:
logging.info('TEST 3 --> Persictency test :: FAILED')
if os.path.isdir(auth.CONFIG_DIRECTORY_LOCATION):
logging.info('TEST 4 --> Persictency test :: PASSED')
else:
logging.info('TEST 4 --> Persictency test :: FAILED')
if os.path.isfile(auth.CONFIG_FILE_LOCATION):
logging.info('TEST 5 --> Persictency test :: PASSED')
else:
logging.info('TEST 5 --> Persictency test :: FAILED')
if os.stat(auth.CONFIG_FILE_LOCATION).st_size > 0:
logging.info('TEST 6 --> Persictency test :: PASSED')
else:
logging.info('TEST 6 --> Persictency test :: FAILED')
if os.path.isdir(auth.PRIVATE_KEY_DIRECTORY_LOCATION):
logging.info('TEST 7 --> Persictency test :: PASSED')
else:
logging.info('TEST 7 --> Persictency test :: FAILED')
if __name__ == '__main__':
testSaveUser("Diósbejglia", "Diósbejgli")
testAuth("Diósbejglia", "Diósbejgli")
testUserExists("Diósbejglia", "Diósbejgli")
#testPersistency()