Ejemplo n.º 1
0
    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())
Ejemplo n.º 2
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)
Ejemplo n.º 3
0
    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({})
Ejemplo n.º 4
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
Ejemplo n.º 5
0
    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())
Ejemplo n.º 6
0
    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')
Ejemplo n.º 7
0
    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)
Ejemplo n.º 8
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())
Ejemplo n.º 9
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('=========================')
Ejemplo n.º 10
0
    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)
Ejemplo n.º 11
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('=========================')
Ejemplo n.º 12
0
    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)
Ejemplo n.º 13
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())
Ejemplo n.º 14
0
    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)
Ejemplo n.º 15
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('=========================')
Ejemplo n.º 16
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()
Ejemplo n.º 17
0
    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)
Ejemplo n.º 18
0
	def test_get_session_none(self):
		repo_sec = SecurityRepository(config())
		session = repo_sec.get(str(ObjectId()))
		self.assertIsNone(session)
Ejemplo n.º 19
0
	def test_register_session_invalid_session_var(self):
		repo_sec = SecurityRepository(config())
		with self.assertRaises(VarTypeError):
			session_id = repo_sec.register('test invalid')
Ejemplo n.º 20
0
	def test_is_exist_false_with_invalid_id(self):
		repo_sec = SecurityRepository(config())
		self.assertFalse(repo_sec.is_exist(id=str(ObjectId())))
Ejemplo n.º 21
0
	def test_is_exist_invalid_id(self):
		repo_sec = SecurityRepository(config())
		with self.assertRaises(StorageError):
			repo_sec.is_exist(id='invalid_id')