# authentic2 - versatile identity manager
# Copyright (C) 2010-2021 Entr'ouvert
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# authentic2

import threading

from django.db import connection

from authentic2.custom_user.models import User
from authentic2.utils.models import generate_slug, safe_get_or_create


def test_safe_get_or_create(transactional_db, concurrency):
    EMAIL = 'john.doe@example.net'
    barrier = threading.Barrier(concurrency)
    users = []
    exceptions = []
    threads = []

    def thread_run():
        try:
            barrier.wait()
            user, created = safe_get_or_create(User, email=EMAIL, defaults={'email': EMAIL})
        except Exception as e:
            exceptions.append(e)
        else:
            if created:
                users.append(user)
        finally:
            connection.close()

    for _ in range(concurrency):
        threads.append(threading.Thread(target=thread_run))
        threads[-1].start()
    for thread in threads:
        thread.join()
    assert not exceptions
    assert len(users) == 1
    assert User.objects.count() == 1
    users[0].delete()


def test_generate_slug():
    assert generate_slug('a b') == 'a-b'
    assert generate_slug('a' * 257) == 'a' * 256
    assert generate_slug('a' * 10, max_length=9) == 'a' * 9
    assert generate_slug('a' * 10, seen_slugs=['a' * 9], max_length=9) == 'aaa-1-aaa'
    assert generate_slug('a' * 8, seen_slugs=['a' * 8], max_length=9) == 'aaa-1-aaa'
    assert generate_slug('a' * 7, seen_slugs=['a' * 7], max_length=9) == 'aaaaaaa-1'
    seen_slugs = set()
    for _ in range(1000):
        slug = generate_slug('a' * 10, seen_slugs=seen_slugs, max_length=9)
        assert slug not in seen_slugs
        assert len(slug) == 9
        seen_slugs.add(slug)
