|
| 1 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 2 | +# you may not use this file except in compliance with the License. |
| 3 | +# You may obtain a copy of the License at |
| 4 | +# |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# |
| 7 | +# Unless required by applicable law or agreed to in writing, software |
| 8 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +# See the License for the specific language governing permissions and |
| 11 | +# limitations under the License. |
| 12 | + |
| 13 | +import pretend |
| 14 | +import pytest |
| 15 | + |
| 16 | +from pyramid.httpexceptions import HTTPBadRequest, HTTPSeeOther |
| 17 | + |
| 18 | +from warehouse.admin.views import prohibited_email_domains as views |
| 19 | + |
| 20 | +from ....common.db.accounts import ProhibitedEmailDomain, ProhibitedEmailDomainFactory |
| 21 | + |
| 22 | + |
| 23 | +class TestProhibitedEmailDomainsList: |
| 24 | + def test_no_query(self, db_request): |
| 25 | + prohibited = sorted( |
| 26 | + ProhibitedEmailDomainFactory.create_batch(30), |
| 27 | + key=lambda b: b.created, |
| 28 | + ) |
| 29 | + |
| 30 | + result = views.prohibited_email_domains(db_request) |
| 31 | + |
| 32 | + assert result == {"prohibited_email_domains": prohibited[:25], "query": None} |
| 33 | + |
| 34 | + def test_with_page(self, db_request): |
| 35 | + prohibited = sorted( |
| 36 | + ProhibitedEmailDomainFactory.create_batch(30), |
| 37 | + key=lambda b: b.created, |
| 38 | + ) |
| 39 | + db_request.GET["page"] = "2" |
| 40 | + |
| 41 | + result = views.prohibited_email_domains(db_request) |
| 42 | + |
| 43 | + assert result == {"prohibited_email_domains": prohibited[25:], "query": None} |
| 44 | + |
| 45 | + def test_with_invalid_page(self): |
| 46 | + request = pretend.stub(params={"page": "not an integer"}) |
| 47 | + |
| 48 | + with pytest.raises(HTTPBadRequest): |
| 49 | + views.prohibited_email_domains(request) |
| 50 | + |
| 51 | + def test_basic_query(self, db_request): |
| 52 | + prohibited = sorted( |
| 53 | + ProhibitedEmailDomainFactory.create_batch(30), |
| 54 | + key=lambda b: b.created, |
| 55 | + ) |
| 56 | + db_request.GET["q"] = prohibited[0].domain |
| 57 | + |
| 58 | + result = views.prohibited_email_domains(db_request) |
| 59 | + |
| 60 | + assert result == { |
| 61 | + "prohibited_email_domains": [prohibited[0]], |
| 62 | + "query": prohibited[0].domain, |
| 63 | + } |
| 64 | + |
| 65 | + def test_wildcard_query(self, db_request): |
| 66 | + prohibited = sorted( |
| 67 | + ProhibitedEmailDomainFactory.create_batch(30), |
| 68 | + key=lambda b: b.created, |
| 69 | + ) |
| 70 | + db_request.GET["q"] = f"{prohibited[0].domain[:-1]}%" |
| 71 | + |
| 72 | + result = views.prohibited_email_domains(db_request) |
| 73 | + |
| 74 | + assert result == { |
| 75 | + "prohibited_email_domains": [prohibited[0]], |
| 76 | + "query": f"{prohibited[0].domain[:-1]}%", |
| 77 | + } |
| 78 | + |
| 79 | + |
| 80 | +class TestProhibitedEmailDomainsAdd: |
| 81 | + def test_no_email_domain(self, db_request): |
| 82 | + db_request.method = "POST" |
| 83 | + db_request.route_path = lambda a: "/admin/prohibited_email_domains/add/" |
| 84 | + db_request.session = pretend.stub( |
| 85 | + flash=pretend.call_recorder(lambda *a, **kw: None) |
| 86 | + ) |
| 87 | + db_request.POST = {} |
| 88 | + |
| 89 | + with pytest.raises(HTTPSeeOther): |
| 90 | + views.add_prohibited_email_domain(db_request) |
| 91 | + |
| 92 | + assert db_request.session.flash.calls == [ |
| 93 | + pretend.call("Email domain is required.", queue="error") |
| 94 | + ] |
| 95 | + |
| 96 | + def test_invalid_domain(self, db_request): |
| 97 | + db_request.method = "POST" |
| 98 | + db_request.route_path = lambda a: "/admin/prohibited_email_domains/add/" |
| 99 | + db_request.session = pretend.stub( |
| 100 | + flash=pretend.call_recorder(lambda *a, **kw: None) |
| 101 | + ) |
| 102 | + db_request.POST = {"email_domain": "invalid"} |
| 103 | + |
| 104 | + with pytest.raises(HTTPSeeOther): |
| 105 | + views.add_prohibited_email_domain(db_request) |
| 106 | + |
| 107 | + assert db_request.session.flash.calls == [ |
| 108 | + pretend.call("Invalid domain name 'invalid'", queue="error") |
| 109 | + ] |
| 110 | + |
| 111 | + def test_duplicate_domain(self, db_request): |
| 112 | + existing_domain = ProhibitedEmailDomainFactory.create() |
| 113 | + db_request.method = "POST" |
| 114 | + db_request.route_path = lambda a: "/admin/prohibited_email_domains/add/" |
| 115 | + db_request.session = pretend.stub( |
| 116 | + flash=pretend.call_recorder(lambda *a, **kw: None) |
| 117 | + ) |
| 118 | + db_request.POST = {"email_domain": existing_domain.domain} |
| 119 | + |
| 120 | + with pytest.raises(HTTPSeeOther): |
| 121 | + views.add_prohibited_email_domain(db_request) |
| 122 | + |
| 123 | + assert db_request.session.flash.calls == [ |
| 124 | + pretend.call( |
| 125 | + f"Email domain '{existing_domain.domain}' already exists.", |
| 126 | + queue="error", |
| 127 | + ) |
| 128 | + ] |
| 129 | + |
| 130 | + @pytest.mark.parametrize( |
| 131 | + ("input_domain", "expected_domain"), |
| 132 | + [ |
| 133 | + ("example.com", "example.com"), |
| 134 | + ("mail.example.co.uk", "example.co.uk"), |
| 135 | + ("https://example.com/", "example.com"), |
| 136 | + ], |
| 137 | + ) |
| 138 | + def test_success(self, db_request, input_domain, expected_domain): |
| 139 | + db_request.method = "POST" |
| 140 | + db_request.route_path = lambda a: "/admin/prohibited_email_domains/list/" |
| 141 | + db_request.session = pretend.stub( |
| 142 | + flash=pretend.call_recorder(lambda *a, **kw: None) |
| 143 | + ) |
| 144 | + db_request.POST = { |
| 145 | + "email_domain": input_domain, |
| 146 | + "is_mx_record": "on", |
| 147 | + "comment": "testing", |
| 148 | + } |
| 149 | + |
| 150 | + response = views.add_prohibited_email_domain(db_request) |
| 151 | + |
| 152 | + assert response.status_code == 303 |
| 153 | + assert response.headers["Location"] == "/admin/prohibited_email_domains/list/" |
| 154 | + assert db_request.session.flash.calls == [ |
| 155 | + pretend.call("Prohibited email domain added.", queue="success") |
| 156 | + ] |
| 157 | + |
| 158 | + query = db_request.db.query(ProhibitedEmailDomain).filter( |
| 159 | + ProhibitedEmailDomain.domain == expected_domain |
| 160 | + ) |
| 161 | + assert query.count() == 1 |
| 162 | + assert query.one().is_mx_record |
| 163 | + assert query.one().comment == "testing" |
| 164 | + |
| 165 | + |
| 166 | +class TestProhibitedEmailDomainsRemove: |
| 167 | + def test_no_domain_name(self, db_request): |
| 168 | + db_request.method = "POST" |
| 169 | + db_request.route_path = lambda a: "/admin/prohibited_email_domains/remove/" |
| 170 | + db_request.session = pretend.stub( |
| 171 | + flash=pretend.call_recorder(lambda *a, **kw: None) |
| 172 | + ) |
| 173 | + db_request.POST = {} |
| 174 | + |
| 175 | + with pytest.raises(HTTPSeeOther): |
| 176 | + views.remove_prohibited_email_domain(db_request) |
| 177 | + |
| 178 | + assert db_request.session.flash.calls == [ |
| 179 | + pretend.call("Domain name is required.", queue="error") |
| 180 | + ] |
| 181 | + |
| 182 | + def test_domain_not_found(self, db_request): |
| 183 | + db_request.method = "POST" |
| 184 | + db_request.route_path = lambda a: "/admin/prohibited_email_domains/remove/" |
| 185 | + db_request.session = pretend.stub( |
| 186 | + flash=pretend.call_recorder(lambda *a, **kw: None) |
| 187 | + ) |
| 188 | + db_request.POST = {"domain_name": "example.com"} |
| 189 | + |
| 190 | + with pytest.raises(HTTPSeeOther): |
| 191 | + views.remove_prohibited_email_domain(db_request) |
| 192 | + |
| 193 | + assert db_request.session.flash.calls == [ |
| 194 | + pretend.call("Domain not found.", queue="error") |
| 195 | + ] |
| 196 | + |
| 197 | + def test_success(self, db_request): |
| 198 | + domain = ProhibitedEmailDomainFactory.create() |
| 199 | + db_request.method = "POST" |
| 200 | + db_request.route_path = lambda a: "/admin/prohibited_email_domains/list/" |
| 201 | + db_request.session = pretend.stub( |
| 202 | + flash=pretend.call_recorder(lambda *a, **kw: None) |
| 203 | + ) |
| 204 | + db_request.POST = {"domain_name": domain.domain} |
| 205 | + |
| 206 | + response = views.remove_prohibited_email_domain(db_request) |
| 207 | + |
| 208 | + assert response.status_code == 303 |
| 209 | + assert response.headers["Location"] == "/admin/prohibited_email_domains/list/" |
| 210 | + assert db_request.session.flash.calls == [ |
| 211 | + pretend.call( |
| 212 | + f"Prohibited email domain '{domain.domain}' removed.", queue="success" |
| 213 | + ) |
| 214 | + ] |
| 215 | + |
| 216 | + query = db_request.db.query(ProhibitedEmailDomain).filter( |
| 217 | + ProhibitedEmailDomain.domain == domain.domain |
| 218 | + ) |
| 219 | + assert query.count() == 0 |
0 commit comments