Beispiel #1
0
 def test_get_timestamp(self):
     t1: int = functions.get_timestamp()
     t2: int = functions.get_timestamp()
     assert t2 >= t1
     t3: int = functions.get_timestamp()
     t4: int = functions.get_timestamp(3600)
     assert t4 >= t3 + 3600
Beispiel #2
0
    def test_generate_access_token(self):
        user = TestAuthFunctions.generate_test_user()
        user_id = functions.generate_uuid()
        t0 = functions.get_timestamp()
        token = functions.generate_access_token(user_id, user)
        t1 = functions.get_timestamp()

        decoded = jwt.decode(token, functions.AUTH_CONFIG['access_token']['secret'], algorithms=functions.AUTH_CONFIG['access_token']['sign_alg'])

        assert decoded['sub'] == user_id
        assert decoded['iat'] >= t0 and decoded['iat'] <= t1
        assert decoded['exp'] >= t0 + functions.AUTH_CONFIG['access_token']['lifetime'] and decoded['exp'] <= t1 + functions.AUTH_CONFIG['access_token']['lifetime'] 
Beispiel #3
0
 def generate_test_user() -> UserStored:
     return UserStored(
         username=TEST_USER_NAME,
         password=functions.hash_pw(TEST_USER_PW),
         time_created=functions.get_timestamp(),
         usage_purpose=PURPOSE_PRIVATE_ID
     )
Beispiel #4
0
 def setUp(self):
     """Test setup; is called before every unit test"""
     from api.app import app
     self.client = TestClient(app)
     # Create a document in the database for the test user.
     UsersDb.save_new_user(user_id=generate_uuid(),
                           user=UserStored(
                               username=TEST_USER_NAME,
                               password=hash_pw(TEST_USER_PW),
                               time_created=get_timestamp(),
                               usage_purpose=PURPOSE_PRIVATE_ID))
Beispiel #5
0
    def test_validate_access_token(self):
        user = TestAuthFunctions.generate_test_user()
        user_id = functions.generate_uuid()
        payload = {
            'iss': 'https://coronalog.de/auth',
            'sub': user_id,
            'iat': functions.get_timestamp(),
            'exp': functions.get_timestamp( functions.AUTH_CONFIG['access_token']['lifetime'] ),
            'username': user.username,
            'roles': ['user']
        }
        UsersDb.save_new_user(user_id, user)
        token = jwt.encode(payload, functions.AUTH_CONFIG['access_token']['secret'], algorithm=functions.AUTH_CONFIG['access_token']['sign_alg'])

        dec_user_id, dec_user = functions.validate_access_token(token, 'user')

        assert dec_user.username == user.username
        assert dec_user.password == user.password
        assert dec_user.time_created == user.time_created
        assert dec_user.usage_purpose == user.usage_purpose
        assert dec_user_id == user_id
Beispiel #6
0
def do_signup_logic(response: Response, username: str, password: str,
                    usage_purpose: str) -> UserLoginBody:

    # check if valid usage_purpose was submitted
    all_purps: Sequence = UsagePurposesDb.get_all()
    valid_purpose = False
    for purp_ref in all_purps:
        if purp_ref.value == usage_purpose:
            valid_purpose = True
            break
    if not valid_purpose:
        raise errors.InvalidUsagePurposeException(
            f'Usage purpose id {usage_purpose} is invalid')

    # check if user already exists
    user_exists, _ = UsersDb.username_exists(username)
    if user_exists:
        raise errors.UserAlreadyExistsException(
            f'Bad request: {username} already exists.')

    # create new user
    new_user: UserStored = UserStored(username=username,
                                      password=auth.hash_pw(password),
                                      time_created=auth.get_timestamp(),
                                      usage_purpose=usage_purpose)
    user_id = auth.generate_uuid()

    # create access token
    generate_and_set_access_token(response, user_id, new_user)

    # save newly created user only if all steps up until now were successful
    UsersDb.save_new_user(user_id, new_user)

    return UserLoginBody(
        username=username,
        expires_in=auth.AUTH_CONFIG['access_token']['lifetime'])
Beispiel #7
0
                                              diarrhoea=False))
TEST_NEW_USER_RECORD = Record(date=datetime.strptime('2020-04-27', '%Y-%m-%d'),
                              symptoms=Symptoms(cough_intensity=3,
                                                cough_type='yellow',
                                                cough_color='yellow',
                                                breathlessness=True,
                                                fatigued=False,
                                                limb_pain=1,
                                                sniffles=False,
                                                sore_throat=1,
                                                fever=36,
                                                diarrhoea=True))
TEST_USER_ID = generate_uuid()
TEST_USER_STORED = UserStored(username=TEST_USER_NAME,
                              password=hash_pw(TEST_USER_PW),
                              time_created=get_timestamp(),
                              usage_purpose=PURPOSE_PRIVATE_ID)

os.environ['PORT'] = '8080'


class TestApi(unittest.TestCase):
    def __init__(self, *args, **kwargs):
        self._bin = []
        super().__init__(*args, **kwargs)

    def setUp(self):
        """Test setup; is called before every unit test"""
        from api.app import app
        self.client = TestClient(app)
        # Create a document in the database for the test user.
Beispiel #8
0
from model.db import UsagePurposesDb, UsersDb, AnamnesisDb, RecordsDb, firestore_client, convert_date_to_str, convert_str_to_date
from model.models import UserStored, Symptoms, Record, Anamnesis
from auth import functions
import errors

TEST_USER_NAME = u'UnitTestUser1337'
TEST_USER_PW = u'DJfjdialj'
NON_EXISTING_USER_NAME = u'9012nsdkfl0912k'
PURPOSE_PRIVATE_ID = u'fAAmChFEatGHgUDuORqX'
TEST_USER_ID = functions.generate_uuid()
TEST_USER_STORED = UserStored(
    username=TEST_USER_NAME,
    password=functions.hash_pw(TEST_USER_PW),
    usage_purpose=PURPOSE_PRIVATE_ID,
    time_created=functions.get_timestamp()
)
TEST_USER_RECORD_1 = Record(
    date=datetime.now(),
    symptoms=Symptoms(
        cough_intensity = 2,
        cough_type = 'yellow',
        cough_color = 'yellow',
        breathlessness = True,
        fatigued = True,
        limb_pain = 4,
        sniffles = True,
        sore_throat = 4,
        fever = 39.1,
        diarrhoea = True
    )