Exemple #1
0
	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)
Exemple #2
0
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('=========================')
Exemple #3
0
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('=========================')
Exemple #4
0
	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())
Exemple #5
0
    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('=========================')
Exemple #6
0
	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()
Exemple #7
0
	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())
Exemple #8
0
    def wrapper_cls(*args, **kwargs):

        # check if session exist or not
        repo = SecurityRepository(config())
        if not repo.is_exist():
            raise AuthError()

        # create original adapter
        obj = cls(*args, **kwargs)
        return obj
Exemple #9
0
    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)
Exemple #10
0
    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)
Exemple #11
0
    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)
Exemple #12
0
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('=========================')
Exemple #13
0
    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)
Exemple #14
0
    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)
Exemple #15
0
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('=========================')
Exemple #16
0
 def test_get_all_empty(self):
     repo = ProfileRepository(config())
     adapter = ProfilesAdapter(repo)
     profiles = adapter.all()
     self.assertIsNone(profiles)