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 tearDown(self): """Will delete all documents after of each tests""" repo = ProfileRepository(config()) coll = repo.build_mongo_coll(repo.COLLNAME) coll.delete_many({}) repo_sec = SecurityRepository(config()) coll_sec = repo_sec.build_mongo_coll(repo_sec.COLLNAME) coll_sec.delete_many({})
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 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 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_delete_success(self): repo = ProfileRepository(config()) reg_adapter = RegisterAdapter(repo) del_adapter = DeleteAdapter(repo) profile = reg_adapter.register('test_name', 'test_password') self.assertIsInstance(profile, Profile) self.assertIsInstance(profile.id, ObjectId) self.assertEqual('test_name', profile.name) self.assertIsNotNone(profile.password) self.assertIsNotNone(profile.id) self.assertTrue(del_adapter.remove(profile.name)) self.assertIsNone(repo.get_detail(profile.name))
def test_logout_no_session(self): repo = ProfileRepository(config()) sec_repo = SecurityRepository(config()) logout_adapter = LogoutAdapter(sec_repo, repo) logout_adapter.logout() self.assertFalse(sec_repo.is_exist())
def test_login_fail_validation(self): repo = ProfileRepository(config()) sec_repo = SecurityRepository(config()) sec_adapter = LoginAdapter(repo, sec_repo, config()) with self.assertRaises(ValidationError): state = sec_adapter.login(None, 'test_password_invalid')
def test_get_current_profile_no_session(self): repo = ProfileRepository(config()) sec_repo = SecurityRepository(config()) current_profile_adapter = CurrentProfileAdapter(repo, sec_repo) name = current_profile_adapter.show() self.assertIsNone(name)
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_register_success(self): repo = ProfileRepository(config()) adapter = RegisterAdapter(repo) profile = adapter.register('test_name', 'test_password') self.assertIsInstance(profile, Profile) self.assertIsInstance(profile.id, ObjectId) self.assertEqual('test_name', profile.name) self.assertIsNotNone(profile.password) self.assertIsNotNone(profile.id)
def test_login_failed_no_profile(self): repo = ProfileRepository(config()) sec_repo = SecurityRepository(config()) sec_adapter = LoginAdapter(repo, sec_repo, config()) state = sec_adapter.login('unknown name', 'test_password') self.assertIsInstance(state, State) self.assertEqual(sec_adapter.usecase.STATE_NAME, state.name) self.assertEqual(sec_adapter.usecase.STATE_FAILED_NO_PROFILE, state.status)
def delete(name: str) -> NoReturn: click.echo('=========================') click.echo(f'Deleting profile...') repo = ProfileRepository(config()) adapter = DeleteAdapter(repo) adapter.remove(name) click.echo(f'Your profile: {name}, has been deleted') click.echo('=========================')
def current() -> NoReturn: click.echo('=========================') repo = ProfileRepository(config()) repo_sec = SecurityRepository(config()) adapter = CurrentProfileAdapter(repo, repo_sec) name = adapter.show() click.echo(f'Current logged in user: {name}') click.echo('=========================')
def logout() -> NoReturn: click.echo('=========================') click.echo('Logging in profile....') repo = ProfileRepository(config()) repo_sec = SecurityRepository(config()) adapter = LogoutAdapter(repo_sec, repo) adapter.logout() click.secho('You have been logged out', fg='green') click.echo('=========================')
def test_login_failed_mismatch_password(self): repo = ProfileRepository(config()) adapter = RegisterAdapter(repo) profile = adapter.register('test_name', 'test_password') sec_repo = SecurityRepository(config()) sec_adapter = LoginAdapter(repo, sec_repo, config()) state = sec_adapter.login(profile.name, 'test_password_invalid') self.assertIsInstance(state, State) self.assertEqual(sec_adapter.usecase.STATE_NAME, state.name) self.assertEqual(sec_adapter.usecase.STATE_FAILED_PASSWORD_MISMTACH, state.status)
def test_should_be_success_after_login(self): repo = ProfileRepository(config()) adapter = RegisterAdapter(repo) profile = adapter.register('test_name', 'test_password') self.assertIsInstance(profile, Profile) sec_repo = SecurityRepository(config()) sec_adapter = LoginAdapter(repo, sec_repo, config()) session = sec_adapter.login(profile.name, 'test_password') self.assertIsInstance(session, Session) adapter = FakeAdapterWithAuth(msg='auth') self.assertEqual('Hello auth', adapter.hello())
def test_logout_success(self): repo = ProfileRepository(config()) adapter = RegisterAdapter(repo) profile = adapter.register('test_name', 'test_password') self.assertIsInstance(profile, Profile) sec_repo = SecurityRepository(config()) sec_adapter = LoginAdapter(repo, sec_repo, config()) session = sec_adapter.login(profile.name, 'test_password') self.assertIsInstance(session, Session) self.assertTrue(sec_repo.is_exist()) logout_adapter = LogoutAdapter(sec_repo, repo) logout_adapter.logout() self.assertFalse(sec_repo.is_exist())
def test_get_current_profile_success(self): repo = ProfileRepository(config()) adapter = RegisterAdapter(repo) profile = adapter.register('test_name', 'test_password') self.assertIsInstance(profile, Profile) sec_repo = SecurityRepository(config()) sec_adapter = LoginAdapter(repo, sec_repo, config()) session = sec_adapter.login(profile.name, 'test_password') self.assertIsInstance(session, Session) current_profile_adapter = CurrentProfileAdapter(repo, sec_repo) name = current_profile_adapter.show() self.assertIsNotNone(name) self.assertEqual(name, profile.name)
def _main(): repo = ProfileRepository(config()) repo_sec = SecurityRepository(config()) # try to run registering process security = LoginAdapter(repo, repo_sec, config()) session = security.login(name, password) if isinstance(session, State): click.secho('Wrong password or profile name', fg='red') else: click.secho( f'Login successfull, your token is: {session.token.build().decode()}', fg='green') click.echo('=========================')
def test_should_be_fail_after_logout(self): repo = ProfileRepository(config()) adapter = RegisterAdapter(repo) profile = adapter.register('test_name', 'test_password') self.assertIsInstance(profile, Profile) sec_repo = SecurityRepository(config()) sec_adapter = LoginAdapter(repo, sec_repo, config()) session = sec_adapter.login(profile.name, 'test_password') self.assertIsInstance(session, Session) logout_adapter = LogoutAdapter(sec_repo, repo) logout_adapter.logout() with self.assertRaises(AuthError): adapter = FakeAdapterWithAuth(msg='auth') adapter.hello()
def test_login_success(self): repo = ProfileRepository(config()) adapter = RegisterAdapter(repo) profile = adapter.register('test_name', 'test_password') self.assertIsInstance(profile, Profile) self.assertIsInstance(profile.id, ObjectId) self.assertEqual('test_name', profile.name) self.assertIsNotNone(profile.password) self.assertIsNotNone(profile.id) sec_repo = SecurityRepository(config()) sec_adapter = LoginAdapter(repo, sec_repo, config()) session = sec_adapter.login(profile.name, 'test_password') self.assertIsInstance(session, Session) self.assertIsNotNone(session.profile_id) self.assertFalse(session.locked) self.assertIsNotNone(session.token)
def test_get_all(self): repo = ProfileRepository(config()) reg_adapter = RegisterAdapter(repo) profile1 = reg_adapter.register('test_name', 'test_password') profile2 = reg_adapter.register('test_name2', 'test_password') self.assertIsInstance(profile1, Profile) self.assertIsInstance(profile2, Profile) adapter = ProfilesAdapter(repo) profiles = adapter.all() self.assertIsNotNone(profiles) self.assertEqual(2, len(profiles)) profile_2 = profiles[1] self.assertEqual(profile2.name, profile_2.name)
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 list_profiles() -> NoReturn: click.echo('=========================') click.echo(f'List profiles...') repo = ProfileRepository(config()) adapter = ProfilesAdapter(repo) profiles = adapter.all() if not profiles: click.secho('You doesnt have any profiles', fg='green') else: table_data = [] table_data.append(['Profile names']) for profile in profiles: table_data.append([profile.name]) table = AsciiTable(table_data) print(table.table) click.echo('=========================')
def test_get_all_empty(self): repo = ProfileRepository(config()) adapter = ProfilesAdapter(repo) profiles = adapter.all() self.assertIsNone(profiles)
def test_delete_failed(self): repo = ProfileRepository(config()) del_adapter = DeleteAdapter(repo) self.assertFalse(del_adapter.remove('not exist name'))
def test_get_list_no_options(self): repo = ProfileRepository(config()) docs = repo.get_list() self.assertIsNone(docs)
def test_register_no_pass(self): repo = ProfileRepository(config()) adapter = RegisterAdapter(repo) with self.assertRaises(ValidationError): adapter.register('test_name', '')