Beispiel #1
0
	def delete(self, database_client: DatabaseClient,
			user_identifier: str, authentication_provider: AuthenticationProvider, worker_provider: WorkerProvider) -> None:

		user_record = self.get(database_client, user_identifier)
		if user_record is None:
			raise ValueError("User '%s' does not exist" % user_identifier)
		if user_record["is_enabled"]:
			raise ValueError("User '%s' is enabled" % user_identifier)
		if worker_provider.count(database_client, owner = user_identifier) > 0:
			raise ValueError("User '%s' owns workers" % user_identifier)

		authentication_provider.remove_password(database_client, user_identifier)
		for token in authentication_provider.get_token_list(database_client, user_identifier):
			authentication_provider.delete_token(database_client, user_identifier, token["identifier"])
		database_client.delete_one(self.table, { "identifier": user_identifier })
Beispiel #2
0
def test_token_success():
    """ Test token operations succeed in a normal situation """

    database_client_instance = MemoryDatabaseClient()
    date_time_provider_instance = FakeDateTimeProvider()
    provider = AuthenticationProvider(date_time_provider_instance)

    user = "******"
    wrong_secret = secrets.token_hex(provider.token_size)

    assert provider.count_tokens(database_client_instance, user) == 0
    assert provider.authenticate_with_token(database_client_instance, user,
                                            wrong_secret) is False

    first_token = provider.create_token(database_client_instance, user, None,
                                        None)
    assert provider.count_tokens(database_client_instance, user) == 1
    assert provider.authenticate_with_token(database_client_instance, user,
                                            first_token["secret"]) is True
    assert provider.authenticate_with_token(database_client_instance, user,
                                            wrong_secret) is False

    second_token = provider.create_token(database_client_instance, user, None,
                                         None)
    assert provider.count_tokens(database_client_instance, user) == 2
    assert provider.authenticate_with_token(database_client_instance, user,
                                            first_token["secret"]) is True
    assert provider.authenticate_with_token(database_client_instance, user,
                                            second_token["secret"]) is True
    assert provider.authenticate_with_token(database_client_instance, user,
                                            wrong_secret) is False

    provider.delete_token(database_client_instance, user,
                          first_token["identifier"])
    assert provider.count_tokens(database_client_instance, user) == 1
    assert provider.authenticate_with_token(database_client_instance, user,
                                            first_token["secret"]) is False
    assert provider.authenticate_with_token(database_client_instance, user,
                                            second_token["secret"]) is True
    assert provider.authenticate_with_token(database_client_instance, user,
                                            wrong_secret) is False