-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_main.py
151 lines (127 loc) · 4.65 KB
/
test_main.py
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
import os
import re
from unittest.mock import Mock
import pytest
from main import (
create_email_regex,
ImproperlyConfigured,
main,
validate_auth_config_and_get_whitelist,
)
@pytest.fixture
def mock_environ(monkeypatch):
old_environ = os.environ.copy()
yield
os.environ.clear()
os.environ.update(old_environ)
class TestValidateAuthConfigAndGetWhitelist:
"""Tests for validate_auth_config_and_get_whitelist()."""
@pytest.mark.parametrize(
'env',
[
pytest.param(
{},
id='all vars missing',
),
pytest.param(
{
'FLOWER_AUTH_PROVIDER': 'flower.views.auth.GithubLoginHandler',
},
id='missing email whitelist',
),
pytest.param(
{
'EMAIL_WHITELIST': 'test@test.com',
},
id='missing auth provider',
),
],
)
def test_raises_exception_if_incorrectly_configured(self, env, monkeypatch):
for key in {'FLOWER_BASIC_AUTH', 'FLOWER_AUTH_PROVIDER', 'EMAIL_WHITELIST'} - env.keys():
monkeypatch.delenv(key, raising=False)
for key, value in env.items():
monkeypatch.setenv(key, value)
with pytest.raises(ImproperlyConfigured):
validate_auth_config_and_get_whitelist()
@pytest.mark.parametrize(
'env,expected_result',
[
pytest.param(
{
'FLOWER_AUTH_PROVIDER': 'flower.views.auth.GithubLoginHandler',
'EMAIL_WHITELIST': 'test@test.com',
},
r'^(test@test\.com)$',
id='valid auth provider config',
),
pytest.param(
{
'FLOWER_BASIC_AUTH': 'username:password',
},
None,
id='valid basic auth config',
),
],
)
def test_no_error_if_correctly_configured(self, env, expected_result, monkeypatch):
for key in {'FLOWER_BASIC_AUTH', 'FLOWER_AUTH_PROVIDER', 'EMAIL_WHITELIST'} - env.keys():
monkeypatch.delenv(key, raising=False)
for key, value in env.items():
monkeypatch.setenv(key, value)
assert validate_auth_config_and_get_whitelist() == expected_result
class TestCreateEmailRegex:
"""Tests for create_email_regex()."""
@pytest.mark.parametrize(
'emails,expected_regex',
[
(
['john.smith@example.com'],
r'^(john\.smith@example\.com)$'
),
(
['john.smith@example.com', 'roger.jones@example.com'],
r'^(john\.smith@example\.com|roger\.jones@example\.com)$'
),
(
['john.smith@example.com', 'roger.jones@example.com', 'me@me.com'],
r'^(john\.smith@example\.com|roger\.jones@example\.com|me@me\.com)$'
),
],
)
def test_returns_regex(self, emails, expected_regex):
assert create_email_regex(emails) == expected_regex
def test_raises_on_empty_list(self):
with pytest.raises(ValueError):
create_email_regex([])
@pytest.mark.parametrize(
'email_to_check,expected_result',
[
('john', False),
('manyjohn.smith@example.com', False),
('john.smith@example.com.uk', False),
('john.smith@example.roger', False),
('john.smith@example.comroger.jones@example.com', False),
('john.smith@example.com', True),
('roger.jones@example.com', True),
]
)
def test_regex_behaviour(self, email_to_check, expected_result):
emails = ['john.smith@example.com', 'roger.jones@example.com']
regex_pattern = create_email_regex(emails)
regex = re.compile(regex_pattern)
assert bool(regex.match(email_to_check)) == expected_result
class TestMain:
@pytest.mark.usefixtures('mock_environ')
def test_main(self, monkeypatch):
mock_flower_main = Mock()
monkeypatch.setattr('flower.__main__.main', mock_flower_main)
os.environ.pop('FLOWER_BASIC_AUTH', None)
os.environ['FLOWER_AUTH_PROVIDER'] = 'flower.views.auth.GithubLoginHandler'
os.environ['EMAIL_WHITELIST'] = 'test@example.com'
os.environ['REDIS_BASE_URL'] = 'redis://localhost'
os.environ['REDIS_BROKER_DB'] = '1'
main()
assert mock_flower_main.called
assert os.environ['FLOWER_AUTH'] == r'^(test@example\.com)$'
assert os.environ['CELERY_BROKER_URL'] == 'redis://localhost/1?'