Skip to content

Commit

Permalink
Merge pull request #3 from milan-tom/add-test-cases
Browse files Browse the repository at this point in the history
Loaded credentials from .ini file for testing
  • Loading branch information
ElijahAhianyo authored Jan 13, 2021
2 parents e8374e5 + 454e6f8 commit 792bcb9
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 40 deletions.
Empty file.
21 changes: 21 additions & 0 deletions apps/auth_zero/config/test_config.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[DEFAULT]
PASSWORD = testPass
EMAIL = [email protected]
otp_verified = False
email_verified = False

[fully_unverified]
USERNAME = fullyUnverifiedUser

[only_otp_verified]
USERNAME = onlyOTPVerifiedUser
otp_verified=True

[only_email_verified]
USERNAME = onlyEmailVerifiedUser
email_verified=True

[fully_verified]
USERNAME = fullyVerifiedUser
otp_verified = True
email_verified = True
56 changes: 16 additions & 40 deletions apps/auth_zero/tests.py
Original file line number Diff line number Diff line change
@@ -1,53 +1,29 @@
from django.test import TestCase
from django.contrib.auth import get_user_model
from common.config import Credentials, ConfigFileLoader, CredentialsFactoryLoader


class UserTestCase(TestCase):
def setUp(self):
self.user_model = get_user_model()
config_file = "apps/auth_zero/config/test_config.ini"
self.creds_obj = CredentialsFactoryLoader(config_file, Credentials, ConfigFileLoader)
self.users = {
verification_status: self.user_model.objects.create_user(**self.creds_obj.read_config(verification_status))
for verification_status in [
"fully_unverified",
"only_otp_verified",
"only_email_verified",
"fully_verified",
]
}

def test_fully_unverified_user(self):
fully_unverified_user = self.user_model.objects.create_user(
username="fullyUnverifiedUser",
email="[email protected]",
password="fullyUnverifiedUserPassword")
fully_unverified_user.save()
self.assertFalse(fully_unverified_user.otp_verified)
self.assertFalse(fully_unverified_user.email_verified)
self.assertFalse(fully_unverified_user.is_verified)
self.assertFalse(self.users["fully_unverified"].is_verified)

def test_partially_verfied_users(self):
only_otp_verified_user = self.user_model.objects.create_user(
username="onlyOTPVerifiedUser",
email="[email protected]",
password="onlyOTPVerifiedUserPassword"
)
only_otp_verified_user.save()
only_otp_verified_user.otp_verified = True
self.assertTrue(only_otp_verified_user.otp_verified)
self.assertFalse(only_otp_verified_user.email_verified)
self.assertFalse(only_otp_verified_user.is_verified)

only_email_verified_user = self.user_model.objects.create_user(
username="onlyEmailVerifiedUser",
email="[email protected]",
password="onlyEmailVerifiedUserPassword"
)
only_email_verified_user.save()
only_email_verified_user.email_verified = True
self.assertFalse(only_email_verified_user.otp_verified)
self.assertTrue(only_email_verified_user.email_verified)
self.assertFalse(only_email_verified_user.is_verified)
self.assertFalse(self.users["only_otp_verified"].is_verified)
self.assertFalse(self.users["only_email_verified"].is_verified)

def test_fully_verified_user(self):
fully_verified_user = self.user_model.objects.create_user(
username="fullyVerifiedUser",
email="[email protected]",
password="fullyVerifiedUserPassword"
)
fully_verified_user.save()
fully_verified_user.otp_verified = fully_verified_user.email_verified = True
self.assertTrue(fully_verified_user.otp_verified)
self.assertTrue(fully_verified_user.email_verified)
self.assertTrue(fully_verified_user.is_verified)

self.assertTrue(self.users["fully_verified"].is_verified)
Empty file added common/__init__.py
Empty file.
52 changes: 52 additions & 0 deletions common/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from django.utils.encoding import force_text
from configparser import ConfigParser
from dataclasses import dataclass
from typing import TypeVar
from io import BytesIO

@dataclass(frozen=True)
class Credentials:
user: str
password: str
email: str


class CredentialsFactoryLoader:
def __init__(self, config_file, credentials_class: TypeVar("Credentials") = None, loader_class=None):
self.credentials_class = credentials_class
self.loader_class = loader_class(config_file)

def read_config(self, verification_status: str) -> dict:
return self.loader_class.read_config(verification_status)


class ConfigByteLoader():
def __init__(self, config_file: str):
with open(config_file, "rb") as f:
self.byte_stream = BytesIO(f.read())

def read_config(self, verification_status: str) -> dict:
# config_settings_for_verification_status = not sure how to do
return dict(
username=force_text(config_settings_for_verification_status["username"]),
password=force_text(config_settings_for_verification_status["password"]),
email=force_text(config_settings_for_verification_status["email"]),
otp_verified=config_settings_for_verification_status.getboolean("otp_verified"),
email_verified=config_settings_for_verification_status.getboolean("email_verified")
)


class ConfigFileLoader:
def __init__(self, config_file: str):
self.config = ConfigParser()
self.config.read(config_file)

def read_config(self, verification_status: str) -> dict:
config_settings_for_verification_status = self.config[verification_status]
return dict(
username=force_text(config_settings_for_verification_status["username"]),
password=force_text(config_settings_for_verification_status["password"]),
email=force_text(config_settings_for_verification_status["email"]),
otp_verified=config_settings_for_verification_status.getboolean("otp_verified"),
email_verified=config_settings_for_verification_status.getboolean("email_verified")
)

0 comments on commit 792bcb9

Please sign in to comment.