def test_profile_password_mismatch(self): repo = MemorySecurityRepo(MEMORY_SESSION) repo_profile = MemoryProfileRepo(MEMORY) profile = Profile('test', Password('test', Sha256Hasher)) self.assertTrue(repo_profile.create(profile)) profile_invalid = Profile('test', Password('test2', Sha256Hasher)) login_uc = LoginUseCase(repo, repo_profile) session = login_uc.login(SALT, profile_invalid, _on_success_cb) self.assertIsInstance(session, State) self.assertEqual(session.name, login_uc.STATE_NAME) self.assertEqual(session.status, login_uc.STATE_FAILED_PASSWORD_MISMTACH)
def get_list(self, options: Optional[Options] = None) -> Optional[List[Profile]]: if not options: return None if not isinstance(options, dict): return None coll = self.build_mongo_coll(self.COLLNAME) payload = options.get('filter', {}) skip = options.get('skip', 0) limit = options.get('limit', 0) docs = coll.find(filter=payload, skip=skip, limit=limit) if docs.count() >= 1: profiles = [] for doc in docs: profile = Profile(id=doc.get('_id'), name=doc.get('name'), password=doc.get('password')) profiles.append(profile) return profiles return None
def create(self, profile: Profile) -> bool: id = str(uuid.uuid4()) new_password = Password(profile.password.raw, Sha256Hasher, profile.password.to_hash()) new_profile = Profile(profile.name, new_password, id) self.in_memory[profile.name] = new_profile return new_profile
def test_get_list_success(self): repo = MemoryProfileRepo() profile1 = Profile('myname', Password('mypassword', Sha256Hasher)) profile2 = Profile('myname2', Password('mypassword', Sha256Hasher)) register_case = RegisterUseCase(repo) register_case.register(profile1) register_case.register(profile2) profiles_case = ProfilesUseCase(repo) profiles = profiles_case.profiles({'names': ['myname', 'myname2']}) self.assertEqual(2, len(profiles)) profile1_from_db = profiles[0] self.assertEqual('myname', profile1_from_db.name)
def test_unlock_success(self): password = Password(raw='test_password', hasher=Sha256Hasher) profile = Profile(name='test_name', password=password) repo = ProfileRepository(config()) saved = repo.create(profile) self.assertTrue(saved) payload = {'name': profile.name} token = Token(salt='test_salt', payload=payload, builder=JWTToken) session = Session(token=token, locked=False, profile_id=saved.id) repo_sec = SecurityRepository(config()) session_id = repo_sec.register(session) session = repo_sec.get(session_id) self.assertIsInstance(session, Session) self.assertEqual(session.token, token.build().decode()) self.assertEqual(session.profile_id, str(saved.id)) self.assertFalse(session.locked) repo_sec.lock(id=session_id) session = repo_sec.get(session_id) self.assertTrue(session.locked) repo_sec.unlock(id=session_id) session = repo_sec.get(session_id) self.assertFalse(session.locked)
def create(self, profile: Profile) -> Profile: """Create new user profile Raises: core.shared.exceptions.VarTypeError: If profile is not an instance of Profile entity core.shared.exceptions.StorageError: If there is an error relate with storage """ # need to check profile, make sure if it's an instance of Profile typechecker.check(profile, Profile, ('profile', 'Profile')) try: # build coll object instance coll = self.build_mongo_coll(self.COLLNAME) # initialize doc payload payload = {'name': profile.name} if profile.password.hashed: payload['password'] = profile.password.hashed else: payload['password'] = profile.password.to_hash() created = coll.insert_one(payload) saved_profile = Profile(id=created.inserted_id, name=profile.name, password=payload.get('password')) return saved_profile except PyMongoError as exc_pymongo: raise StorageError(exc_pymongo)
def test_register_validation_name_exist(self): with self.assertRaises(ValidationError): repo = MemoryProfileRepo() usecase = RegisterUseCase(repo) profile = Profile('name', Password('mypassword', Sha256Hasher)) usecase.register(profile) # should be success usecase.register(profile) # name should be cannot pass validation
def test_profile_not_valid_no_password(self): repo = MemorySecurityRepo(MEMORY_SESSION) repo_profile = MemoryProfileRepo(MEMORY) profile = Profile('test', Password('', Sha256Hasher)) login_uc = LoginUseCase(repo, repo_profile) with self.assertRaises(ValidationError): session = login_uc.login(SALT, profile, _on_success_cb)
def test_get_list(self): password = Password(raw='test_password', hasher=Sha256Hasher) profile = Profile(name='test_name', password=password) password2 = Password(raw='test_password', hasher=Sha256Hasher) profile2 = Profile(name='test_name', password=password) repo = ProfileRepository(config()) repo.create(profile) repo.create(profile2) docs = repo.get_list({'filter': {'name': 'test_name'}}) self.assertIsInstance(docs, list) self.assertEqual(2, len(docs)) doc1 = docs[0] self.assertEqual(doc1.name, profile.name)
def test_create_success(self): password = Password(raw='test_password', hasher=Sha256Hasher) profile = Profile(name='test_name', password=password) repo = ProfileRepository(config()) saved = repo.create(profile) self.assertTrue(saved) self.assertEqual(saved.name, profile.name) self.assertEqual(saved.password, profile.password.to_hash()) self.assertIsNotNone(saved.id)
def id(self, id: str) -> Optional[Profile]: coll = self.build_mongo_coll(self.COLLNAME) doc = coll.find_one({'_id': ObjectId(id)}) if not doc: return None profile = Profile(id=doc.get('_id'), name=doc.get('name'), password=doc.get('password')) return profile
def test_remove_success(self): password = Password(raw='test_password', hasher=Sha256Hasher) profile = Profile(name='test_name', password=password) repo = ProfileRepository(config()) self.assertTrue(repo.create(profile)) self.assertTrue(repo.remove(profile.name)) doc = repo.get_detail(profile.name) self.assertIsNone(doc)
def test_get_by_name(self): password = Password(raw='test_password', hasher=Sha256Hasher) profile = Profile(name='test_name', password=password) repo = ProfileRepository(config()) self.assertTrue(repo.create(profile)) doc = repo.get_detail(profile.name) self.assertIsNotNone(doc) self.assertEqual(doc.name, profile.name)
def get_detail(self, name: str) -> Optional[Profile]: coll = self.build_mongo_coll(self.COLLNAME) doc = coll.find_one({'name': name}) if not doc: return None profile = Profile(id=doc.get('_id'), name=doc.get('name'), password=doc.get('password')) return profile
def register(self, name: str, password: str) -> Profile: """Register new profile based on given name and password Raises: core.shared.exceptions.ValidationError: if profile not passed validation process core.shared.exceptions.StorageError: if there is an error related with datastorage """ password = Password(raw=password, hasher=Sha256Hasher) profile = Profile(name=name, password=password) return self.usecase.register(profile)
def test_register_name_exist(self): password = Password(raw='test_password', hasher=Sha256Hasher) profile = Profile(name='test_name', password=password) repo = ProfileRepository(config()) saved = repo.create(profile) adapter = RegisterAdapter(repo) with self.assertRaises(ValidationError): adapter.register(saved.name, 'test_pass')
def test_profile_not_exist(self): repo = MemorySecurityRepo(MEMORY_SESSION) repo_profile = MemoryProfileRepo(MEMORY) profile = Profile('test', Password('test', Sha256Hasher)) login_uc = LoginUseCase(repo, repo_profile) session = login_uc.login(SALT, profile, _on_success_cb) self.assertIsInstance(session, State) self.assertEqual(session.name, login_uc.STATE_NAME) self.assertEqual(session.status, login_uc.STATE_FAILED_NO_PROFILE)
def test_delete_success(self): repo = MemoryProfileRepo() profile = Profile('myname', Password('mypassword', Sha256Hasher)) register_case = RegisterUseCase(repo) delete_case = DeleteUseCase(repo) self.assertTrue(register_case.register(profile)) self.assertTrue(delete_case.remove(profile.name)) check = repo.get_detail(profile.name) self.assertIsNone(check)
def test_success(self): repo = MemorySecurityRepo(MEMORY_SESSION) repo_profile = MemoryProfileRepo(MEMORY) profile = Profile('test', Password('test', Sha256Hasher)) self.assertTrue(repo_profile.create(profile)) login_uc = LoginUseCase(repo, repo_profile) session = login_uc.login(SALT, profile, _on_success_cb) self.assertFalse(isinstance(session, State)) self.assertIsNotNone(session.id) self.assertIsNone(session.token.options) self.assertTrue(isinstance(session.token.build(), str))
def test_delete_session_using_none(self): repo = MemorySecurityRepo(MEMORY_SESSION) repo_profile = MemoryProfileRepo(MEMORY) profile = Profile('test', Password('test', Sha256Hasher)) self.assertTrue(repo_profile.create(profile)) login_uc = LoginUseCase(repo, repo_profile) session = login_uc.login(SALT, profile, _on_success_cb) self.assertFalse(isinstance(session, State)) self.assertIsNotNone(session.id) logout_uc = LogoutUseCase(repo, repo_profile) logout_uc.logout() self.assertFalse(repo.is_exist())
def login(self, name: str, password: str) -> Union[State, Session]: """Logging in users Raises: autowp.core.shared.exceptions.VarTypeError: When given profile is not an instance of Profile autowp.core.shared.exceptions.ValidationError: When cannot validate given profile entity """ if self.repo.is_exist(): sess_prev = self.repo.get() if sess_prev: self.repo.remove(sess_prev.id) password = Password(raw=password, hasher=Sha256Hasher) profile = Profile(name=name, password=password) session = self.usecase.login(self.config.salt, profile, self._login_success_callback) return session
def test_remove_success(self): password = Password(raw='test_password', hasher=Sha256Hasher) profile = Profile(name='test_name', password=password) repo = ProfileRepository(config()) saved = repo.create(profile) self.assertTrue(saved) payload = {'name': profile.name} token = Token(salt='test_salt', payload=payload, builder=JWTToken) session = Session(token=token, locked=False, profile_id=saved.id) repo_sec = SecurityRepository(config()) session_id = repo_sec.register(session) self.assertIsInstance(session_id, str) self.assertTrue(repo_sec.remove(session_id)) self.assertFalse(repo_sec.is_exist())
def test_get_profile_success(self): repo = MemorySecurityRepo(MEMORY_SESSION) repo_profile = MemoryProfileRepo(MEMORY) profile = Profile('test', Password('test', Sha256Hasher)) self.assertTrue(repo_profile.create(profile)) login_uc = LoginUseCase(repo, repo_profile) session = login_uc.login(SALT, profile, _on_success_cb) self.assertFalse(isinstance(session, State)) self.assertIsNotNone(session.id) self.assertIsNotNone(session.profile_id) current_profile_uc = CurrentProfileUseCase(repo, repo_profile) current_profile = current_profile_uc.current_profile() self.assertIsNotNone(current_profile) self.assertIsInstance(current_profile, Profile)
def create(self, profile: Profile) -> bool: new_password = Password(profile.password.raw, Sha256Hasher, profile.password.to_hash()) self.in_memory[profile.name] = Profile(profile.name, new_password) return True
def create(self, profile: Profile) -> Profile: new_password = Password(profile.password.raw, Sha256Hasher, profile.password.to_hash()) new_profile = Profile(profile.name, new_password) self.in_memory[profile.name] = new_profile return new_profile
def test_register_validation_no_password(self): with self.assertRaises(ValidationError): repo = MemoryProfileRepo() usecase = RegisterUseCase(repo) profile = Profile('name', Password('', Sha256Hasher)) usecase.register(profile)
def test_register_success(self): repo = MemoryProfileRepo() usecase = RegisterUseCase(repo) profile = Profile('myname', Password('mypassword', Sha256Hasher)) self.assertTrue(usecase.register(profile))