# Path: amivapi/tests/utils.py
# class WebTestNoAuth(WebTest):
#     """WebTest without authentification."""
# 
#     def setUp(self, **extra_config):
#         """Use auth hook to always authenticate as root for every request."""
#         super().setUp(**extra_config)
# 
#         def authenticate_root(resource):
#             g.resource_admin = True
# 
#         self.app.after_auth += authenticate_root
# 
# Path: amivapi/events/utils.py
# def create_token_secret_on_startup(app):
#     """Create a token secret in the database if it doesn't exist.
# 
#     The secret key is stored in the database to ensure consistency.
#     The database collection holding this key is called `config`.
#     """
#     with app.app_context():  # Context for db connection
#         config = app.data.driver.db['config']
#         result = config.find_one(
#             {'TOKEN_SECRET': {'$exists': True, '$nin': [None, '']}})
# 
#         if result is None:
#             config.insert_one({'TOKEN_SECRET': token_urlsafe()})
# 
# def get_token_secret():
#     db = current_app.data.driver.db['config']
#     result = db.find_one({'TOKEN_SECRET': {'$exists': True}})
#     return result['TOKEN_SECRET']


from unittest.mock import patch
from amivapi.tests.utils import WebTestNoAuth
from amivapi.events.utils import (
    create_token_secret_on_startup,
    get_token_secret
)
        self.assertIsNone(db_item)

    def test_create_secret(self):
        """Test that init_secret creates a secret token & adds it to the db."""
        super().setUp()

        with self.app.app_context():
            db_item = self.db['config'].find_one({
                SECRET_KEY: {'$exists': True, '$nin': [None, '']}
            })

        self.assertIsNotNone(db_item)

    def test_existing_secret(self):
        """Test that a secret from the database is not overwritten."""
        # We need to run the setup to be able to use an app context
        super().setUp()

        old_secret = 'Trololololo'
        # Set the secret in the database
        with self.app.app_context():
            self.db['config'].update_one(
                {SECRET_KEY: {'$exists': True}},
                {'$set': {SECRET_KEY: old_secret}}
            )

        # This should now not change the token
        create_token_secret_on_startup(self.app)

        with self.app.app_context():

# Path: amivapi/tests/utils.py
# class WebTestNoAuth(WebTest):
#     """WebTest without authentification."""
# 
#     def setUp(self, **extra_config):
#         """Use auth hook to always authenticate as root for every request."""
#         super().setUp(**extra_config)
# 
#         def authenticate_root(resource):
#             g.resource_admin = True
# 
#         self.app.after_auth += authenticate_root
# 
# Path: amivapi/events/utils.py
# def create_token_secret_on_startup(app):
#     """Create a token secret in the database if it doesn't exist.
# 
#     The secret key is stored in the database to ensure consistency.
#     The database collection holding this key is called `config`.
#     """
#     with app.app_context():  # Context for db connection
#         config = app.data.driver.db['config']
#         result = config.find_one(
#             {'TOKEN_SECRET': {'$exists': True, '$nin': [None, '']}})
# 
#         if result is None:
#             config.insert_one({'TOKEN_SECRET': token_urlsafe()})
# 
# def get_token_secret():
#     db = current_app.data.driver.db['config']
#     result = db.find_one({'TOKEN_SECRET': {'$exists': True}})
#     return result['TOKEN_SECRET']


# Path: amivapi/tests/events/test_secret.py
from unittest.mock import patch
from amivapi.tests.utils import WebTestNoAuth
from amivapi.events.utils import (
    create_token_secret_on_startup,
    get_token_secret
)

        self.assertIsNone(db_item)

    def test_create_secret(self):
        """Test that init_secret creates a secret token & adds it to the db."""
        super().setUp()

        with self.app.app_context():
            db_item = self.db['config'].find_one({
                SECRET_KEY: {'$exists': True, '$nin': [None, '']}
            })

        self.assertIsNotNone(db_item)

    def test_existing_secret(self):
        """Test that a secret from the database is not overwritten."""
        # We need to run the setup to be able to use an app context
        super().setUp()

        old_secret = 'Trololololo'
        # Set the secret in the database
        with self.app.app_context():
            self.db['config'].update_one(
                {SECRET_KEY: {'$exists': True}},
                {'$set': {SECRET_KEY: old_secret}}
            )

        # This should now not change the token
        create_token_secret_on_startup(self.app)

        with self.app.app_context():

            self.assertEqual(get_token_secret(), old_secret)