Skip to content

Commit 80b1e10

Browse files
committed
Allow admins to still write
1 parent 8b269c2 commit 80b1e10

File tree

4 files changed

+19
-45
lines changed

4 files changed

+19
-45
lines changed

tests/unit/admin/views/test_flags.py

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
# See the License for the specific language governing permissions and
1111
# limitations under the License.
1212

13-
import pretend
1413
import pytest
1514

1615
from warehouse.admin.flags import AdminFlag
@@ -99,31 +98,3 @@ def test_edit_flag(
9998

10099
assert flag.enabled == expected_enabled
101100
assert flag.description == expected_description
102-
103-
def test_edit_read_only_flag(self, db_request, monkeypatch):
104-
# Clear out any existing flags added from migrations
105-
db_request.db.query(AdminFlag).delete()
106-
107-
active = pretend.stub()
108-
doomed = pretend.stub()
109-
txn = pretend.stub(status=doomed)
110-
monkeypatch.setattr(
111-
views.transaction._transaction.Status, 'ACTIVE', active
112-
)
113-
114-
db_request.tm = pretend.stub(
115-
isDoomed=lambda: True,
116-
get=lambda: txn,
117-
)
118-
db_request.POST = {
119-
'id': 'read-only',
120-
'description': 'some new description',
121-
}
122-
db_request.route_path = lambda *a: '/the/redirect'
123-
db_request.flash = lambda *a: None
124-
125-
AdminFlagFactory(id='read-only', enabled=True)
126-
127-
views.edit_flag(db_request)
128-
129-
assert txn.status == active

tests/unit/test_db.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -203,15 +203,23 @@ def test_create_session(monkeypatch, read_only, tx_status):
203203
connection.connection.rollback.calls == [pretend.call()]
204204

205205

206-
@pytest.mark.parametrize("admin_flag, doom_calls", [
207-
(None, []),
208-
(pretend.stub(enabled=False), []),
209-
(
210-
pretend.stub(enabled=True, description='flag description'),
211-
[pretend.call()],
212-
),
213-
])
214-
def test_create_session_read_only_mode(admin_flag, doom_calls, monkeypatch):
206+
@pytest.mark.parametrize(
207+
"admin_flag, is_superuser, doom_calls",
208+
[
209+
(None, True, []),
210+
(None, False, []),
211+
(pretend.stub(enabled=False), True, []),
212+
(pretend.stub(enabled=False), False, []),
213+
(pretend.stub(enabled=True, description='flag description'), True, []),
214+
(
215+
pretend.stub(enabled=True, description='flag description'),
216+
False,
217+
[pretend.call()],
218+
),
219+
],
220+
)
221+
def test_create_session_read_only_mode(
222+
admin_flag, is_superuser, doom_calls, monkeypatch):
215223
get = pretend.call_recorder(lambda *a: admin_flag)
216224
session_obj = pretend.stub(
217225
close=lambda: None,
@@ -238,6 +246,7 @@ def test_create_session_read_only_mode(admin_flag, doom_calls, monkeypatch):
238246
tm=pretend.stub(doom=pretend.call_recorder(lambda: None)),
239247
read_only=False,
240248
add_finished_callback=lambda callback: None,
249+
user=pretend.stub(is_superuser=is_superuser),
241250
)
242251

243252
assert _create_session(request) is session_obj

warehouse/admin/views/flags.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
from pyramid.httpexceptions import HTTPSeeOther
1414
from pyramid.view import view_config
15-
import transaction
1615

1716
from warehouse.admin.flags import AdminFlag
1817

@@ -42,11 +41,6 @@ def edit_flag(request):
4241
flag.description = request.POST['description']
4342
flag.enabled = bool(request.POST.get('enabled'))
4443

45-
if flag.id == 'read-only' and request.tm.isDoomed():
46-
# We are trying to disable the `read-only` flag, but the flag has
47-
# alreeady doomed the transaction, so we need to manually un-doom it
48-
request.tm.get().status = transaction._transaction.Status.ACTIVE
49-
5044
request.session.flash(
5145
f'Successfully edited flag {flag.id!r}',
5246
queue='success',

warehouse/db.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ def cleanup(request):
165165
# Check if we're in read-only mode
166166
from warehouse.admin.flags import AdminFlag
167167
flag = session.query(AdminFlag).get('read-only')
168-
if flag and flag.enabled:
168+
if flag and flag.enabled and not request.user.is_superuser:
169169
request.tm.doom()
170170

171171
# Return our session now that it's created and registered

0 commit comments

Comments
 (0)