|
| 1 | +import json |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from django.contrib.auth.models import User |
| 6 | + |
| 7 | +from pontoon.base.views import get_users |
| 8 | +from pontoon.test.factories import ( |
| 9 | + EntityFactory, |
| 10 | + LocaleFactory, |
| 11 | + ProjectFactory, |
| 12 | + ResourceFactory, |
| 13 | + TranslationFactory, |
| 14 | + UserFactory, |
| 15 | +) |
| 16 | + |
| 17 | + |
| 18 | +@pytest.mark.django_db |
| 19 | +def test_get_users_excludes_system_users(rf, admin): |
| 20 | + request = rf.get("/get-users/", HTTP_X_REQUESTED_WITH="XMLHttpRequest") |
| 21 | + request.user = admin |
| 22 | + response = get_users(request) |
| 23 | + data = json.loads(response.content) |
| 24 | + usernames = [u["username"] for u in data] |
| 25 | + |
| 26 | + system_users = User.objects.filter(profile__system_user=True) |
| 27 | + for u in system_users: |
| 28 | + assert u.username not in usernames |
| 29 | + |
| 30 | + |
| 31 | +@pytest.mark.django_db |
| 32 | +def test_get_users_sorted_by_locale_activity(rf, admin): |
| 33 | + locale = LocaleFactory() |
| 34 | + project = ProjectFactory(locales=[locale]) |
| 35 | + resource = ResourceFactory(project=project) |
| 36 | + entity = EntityFactory(resource=resource) |
| 37 | + |
| 38 | + other_locale = LocaleFactory() |
| 39 | + |
| 40 | + # create a user that is from a different locale, not the current one |
| 41 | + other_locale_user = UserFactory(first_name="Michael", last_name="OtherLocale") |
| 42 | + TranslationFactory(user=other_locale_user, locale=other_locale, entity=entity) |
| 43 | + |
| 44 | + # create a user active in the current locale |
| 45 | + locale_user = UserFactory(first_name="Alice", last_name="ActiveLocale") |
| 46 | + TranslationFactory(user=locale_user, locale=locale, entity=entity) |
| 47 | + |
| 48 | + request = rf.get( |
| 49 | + "/get-users/", |
| 50 | + {"locale": locale.code, "project": project.pk}, |
| 51 | + HTTP_X_REQUESTED_WITH="XMLHttpRequest", |
| 52 | + ) |
| 53 | + |
| 54 | + request.user = admin |
| 55 | + response = get_users(request) |
| 56 | + data = json.loads(response.content) |
| 57 | + |
| 58 | + names = [u["name"] for u in data] |
| 59 | + assert names.index(locale_user.name_or_email) < names.index( |
| 60 | + other_locale_user.name_or_email |
| 61 | + ) |
| 62 | + |
| 63 | + |
| 64 | +# project & locale activity should be prioritized over alphabetic order |
| 65 | +# for users that are both active in the project or locale |
| 66 | + |
| 67 | + |
| 68 | +@pytest.mark.django_db |
| 69 | +def test_get_users_sorted_by_project_and_locale_activity(rf, admin): |
| 70 | + locale = LocaleFactory() |
| 71 | + project = ProjectFactory(locales=[locale]) |
| 72 | + resource = ResourceFactory(project=project) |
| 73 | + entity = EntityFactory(resource=resource) |
| 74 | + |
| 75 | + # create a user that is active in the current locale and project |
| 76 | + active_user = UserFactory(first_name="Zara", last_name="ActiveUser") |
| 77 | + TranslationFactory(user=active_user, locale=locale, entity=entity) |
| 78 | + |
| 79 | + # create a user that is active in the current locale but not the project |
| 80 | + other_project_user = UserFactory(first_name="Bob", last_name="OtherProject") |
| 81 | + TranslationFactory(user=other_project_user, locale=locale) |
| 82 | + |
| 83 | + # create a user that is active in the current project but not the locale |
| 84 | + other_locale_user = UserFactory(first_name="Charlie", last_name="OtherLocale") |
| 85 | + TranslationFactory(user=other_locale_user, entity=entity) |
| 86 | + |
| 87 | + request = rf.get( |
| 88 | + "/get-users/", |
| 89 | + {"locale": locale.code, "project": project.pk}, |
| 90 | + HTTP_X_REQUESTED_WITH="XMLHttpRequest", |
| 91 | + ) |
| 92 | + |
| 93 | + request.user = admin |
| 94 | + response = get_users(request) |
| 95 | + data = json.loads(response.content) |
| 96 | + |
| 97 | + names = [u["name"] for u in data] |
| 98 | + assert ( |
| 99 | + names.index(active_user.name_or_email) |
| 100 | + < names.index(other_project_user.name_or_email) |
| 101 | + < names.index(other_locale_user.name_or_email) |
| 102 | + ) |
0 commit comments