File: test_set_fake_emails.py

package info (click to toggle)
python-django-extensions 4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,820 kB
  • sloc: python: 18,601; javascript: 7,354; makefile: 108; xml: 17
file content (182 lines) | stat: -rw-r--r-- 6,258 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# -*- coding: utf-8 -*-
from io import StringIO

from django.core.management import call_command, CommandError
from django.contrib.auth.models import User

from django_extensions.management.commands.set_fake_emails import Command

import pytest


@pytest.fixture(scope="module")
def django_db_setup(django_db_setup, django_db_blocker):
    """Load to database a set of users, create for export
    emails command testing"""
    with django_db_blocker.unblock():
        call_command("loaddata", "group.json")
        call_command("loaddata", "user.json")


@pytest.mark.django_db()
def test_without_args(capsys, settings):
    settings.DEBUG = True

    emails = User.objects.values_list("email", flat=True)
    assert all(email.endswith("@gmail.com") for email in emails)

    generate_email = Command()
    call_command(generate_email)
    out, err = capsys.readouterr()
    assert "Changed 3 emails" in out

    emails = User.objects.values_list("email", flat=True)
    assert all(email.endswith("@example.com") for email in emails)


@pytest.mark.django_db()
def test_no_admin(capsys, settings):
    settings.DEBUG = True

    emails = User.objects.values_list("email", flat=True)
    assert all(email.endswith("@gmail.com") for email in emails)

    generate_email = Command()
    call_command(generate_email, "-a")
    out, err = capsys.readouterr()
    assert "Changed 2 emails" in out

    emails = User.objects.filter(is_superuser=False).values_list("email", flat=True)
    assert all(email.endswith("@example.com") for email in emails)

    emails = User.objects.filter(is_superuser=True).values_list("email", flat=True)
    assert all(email.endswith("@gmail.com") for email in emails)


@pytest.mark.django_db()
def test_no_staff(capsys, settings):
    settings.DEBUG = True

    emails = User.objects.values_list("email", flat=True)
    assert all(email.endswith("@gmail.com") for email in emails)

    generate_email = Command()
    call_command(generate_email, "-s")
    out, err = capsys.readouterr()
    assert "Changed 2 emails" in out

    emails = User.objects.filter(is_staff=False).values_list("email", flat=True)
    assert all(email.endswith("@example.com") for email in emails)

    emails = User.objects.filter(is_staff=True).values_list("email", flat=True)
    assert all(email.endswith("@gmail.com") for email in emails)


@pytest.mark.django_db()
def test_include_groups(capsys, settings):
    settings.DEBUG = True

    emails = User.objects.values_list("email", flat=True)
    assert all(email.endswith("@gmail.com") for email in emails)

    generate_email = Command()
    call_command(generate_email, "--include-groups=Attendees")
    out, err = capsys.readouterr()
    assert "Changed 2 emails" in out

    emails = User.objects.filter(is_superuser=False).values_list("email", flat=True)
    assert all(email.endswith("@example.com") for email in emails)

    emails = User.objects.filter(is_superuser=True).values_list("email", flat=True)
    assert all(email.endswith("@gmail.com") for email in emails)


@pytest.mark.django_db()
def test_include_groups_which_does_not_exists(capsys, settings):
    settings.DEBUG = True

    emails = User.objects.values_list("email", flat=True)
    assert all(email.endswith("@gmail.com") for email in emails)

    with pytest.raises(CommandError, match="No groups matches filter: TEST"):
        call_command("set_fake_emails", "--include-groups=TEST")

    assert not User.objects.filter(email__endswith="@example.com").exists()


@pytest.mark.django_db()
def test_exclude_groups(capsys, settings):
    settings.DEBUG = True

    emails = User.objects.values_list("email", flat=True)
    assert all(email.endswith("@gmail.com") for email in emails)

    generate_email = Command()
    call_command(generate_email, "--exclude-groups=Attendees")
    out, err = capsys.readouterr()
    assert "Changed 1 emails" in out

    emails = User.objects.filter(is_superuser=False).values_list("email", flat=True)
    assert all(email.endswith("@gmail.com") for email in emails)

    emails = User.objects.filter(is_superuser=True).values_list("email", flat=True)
    assert all(email.endswith("@example.com") for email in emails)


@pytest.mark.django_db()
def test_exclude_groups_which_does_not_exists(capsys, settings):
    settings.DEBUG = True

    emails = User.objects.values_list("email", flat=True)
    assert all(email.endswith("@gmail.com") for email in emails)

    with pytest.raises(CommandError, match="No groups matches filter: TEST"):
        call_command("set_fake_emails", "--exclude-groups=TEST")

    assert not User.objects.filter(email__endswith="@example.com").exists()


@pytest.mark.django_db()
def test_include_regexp(capsys, settings):
    settings.DEBUG = True

    emails = User.objects.values_list("email", flat=True)
    assert all(email.endswith("@gmail.com") for email in emails)

    generate_email = Command()
    call_command(generate_email, "--include=.*briel")
    out, err = capsys.readouterr()
    assert "Changed 1 emails" in out

    emails = User.objects.exclude(username="Gabriel").values_list("email", flat=True)
    assert all(email.endswith("@gmail.com") for email in emails)

    emails = User.objects.filter(username="Gabriel").values_list("email", flat=True)
    assert all(email.endswith("@example.com") for email in emails)


@pytest.mark.django_db()
def test_exclude_regexp(capsys, settings):
    settings.DEBUG = True

    emails = User.objects.values_list("email", flat=True)
    assert all(email.endswith("@gmail.com") for email in emails)

    generate_email = Command()
    call_command(generate_email, "--exclude=.*briel")
    out, err = capsys.readouterr()
    assert "Changed 2 emails" in out

    emails = User.objects.filter(username="Gabriel").values_list("email", flat=True)
    assert all(email.endswith("@gmail.com") for email in emails)

    emails = User.objects.exclude(username="Gabriel").values_list("email", flat=True)
    assert all(email.endswith("@example.com") for email in emails)


def test_without_debug(settings):
    settings.DEBUG = False

    out = StringIO()
    with pytest.raises(CommandError, match="Only available in debug mode"):
        call_command("set_fake_emails", verbosity=3, stdout=out, stderr=out)