|
| 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 | +import hashlib |
| 13 | + |
| 14 | +import pretend |
| 15 | + |
| 16 | +from warehouse import db |
| 17 | +from warehouse.cli import hashing |
| 18 | + |
| 19 | +from ...common.db.packaging import JournalEntry, JournalEntryFactory |
| 20 | + |
| 21 | + |
| 22 | +def remote_addr_salty_hash(remote_addr, salt): |
| 23 | + return hashlib.sha256(f"{remote_addr}{salt}".encode()).hexdigest() |
| 24 | + |
| 25 | + |
| 26 | +class TestHashingJournalEntry: |
| 27 | + def test_no_records_to_hash(self, cli, db_request, monkeypatch): |
| 28 | + engine = pretend.stub() |
| 29 | + config = pretend.stub(registry={"sqlalchemy.engine": engine}) |
| 30 | + session_cls = pretend.call_recorder(lambda bind: db_request.db) |
| 31 | + monkeypatch.setattr(db, "Session", session_cls) |
| 32 | + |
| 33 | + assert db_request.db.query(JournalEntry).count() == 0 |
| 34 | + |
| 35 | + args = ["--salt", "test"] |
| 36 | + |
| 37 | + result = cli.invoke(hashing.journal_entry, args, obj=config) |
| 38 | + |
| 39 | + assert result.exit_code == 0 |
| 40 | + assert result.output.strip() == "No rows to hash. Done!" |
| 41 | + |
| 42 | + def tests_hashes_records(self, cli, db_request, remote_addr, monkeypatch): |
| 43 | + engine = pretend.stub() |
| 44 | + config = pretend.stub(registry={"sqlalchemy.engine": engine}) |
| 45 | + session_cls = pretend.call_recorder(lambda bind: db_request.db) |
| 46 | + monkeypatch.setattr(db, "Session", session_cls) |
| 47 | + |
| 48 | + # create some JournalEntry records with unhashed ip addresses |
| 49 | + JournalEntryFactory.create_batch(3, submitted_from=remote_addr) |
| 50 | + assert db_request.db.query(JournalEntry).count() == 3 |
| 51 | + |
| 52 | + salt = "NaCl" |
| 53 | + salted_hash = remote_addr_salty_hash(remote_addr, salt) |
| 54 | + |
| 55 | + args = [ |
| 56 | + "--salt", |
| 57 | + salt, |
| 58 | + "--batch-size", |
| 59 | + "2", |
| 60 | + ] |
| 61 | + |
| 62 | + result = cli.invoke(hashing.journal_entry, args, obj=config) |
| 63 | + |
| 64 | + assert result.exit_code == 0 |
| 65 | + assert result.output.strip() == "Hashing 2 rows...\nHashed 2 rows" |
| 66 | + # check that two of the ip addresses have been hashed |
| 67 | + assert ( |
| 68 | + db_request.db.query(JournalEntry) |
| 69 | + .filter_by(submitted_from=remote_addr) |
| 70 | + .one() |
| 71 | + ) |
| 72 | + assert ( |
| 73 | + db_request.db.query(JournalEntry) |
| 74 | + .filter_by(submitted_from=salted_hash) |
| 75 | + .count() |
| 76 | + == 2 |
| 77 | + ) |
| 78 | + |
| 79 | + def test_continue_until_done(self, cli, db_request, remote_addr, monkeypatch): |
| 80 | + engine = pretend.stub() |
| 81 | + config = pretend.stub(registry={"sqlalchemy.engine": engine}) |
| 82 | + session_cls = pretend.call_recorder(lambda bind: db_request.db) |
| 83 | + monkeypatch.setattr(db, "Session", session_cls) |
| 84 | + |
| 85 | + # create some JournalEntry records with unhashed ip addresses |
| 86 | + JournalEntryFactory.create_batch(3, submitted_from=remote_addr) |
| 87 | + |
| 88 | + salt = "NaCl" |
| 89 | + salted_hash = remote_addr_salty_hash(remote_addr, salt) |
| 90 | + |
| 91 | + args = [ |
| 92 | + "--salt", |
| 93 | + salt, |
| 94 | + "--batch-size", |
| 95 | + "1", |
| 96 | + "--sleep-time", |
| 97 | + "0", |
| 98 | + "--continue-until-done", |
| 99 | + ] |
| 100 | + |
| 101 | + result = cli.invoke(hashing.journal_entry, args, obj=config) |
| 102 | + |
| 103 | + assert result.exit_code == 0 |
| 104 | + # check that all the ip addresses have been hashed |
| 105 | + assert ( |
| 106 | + db_request.db.query(JournalEntry) |
| 107 | + .filter_by(submitted_from=salted_hash) |
| 108 | + .count() |
| 109 | + == 3 |
| 110 | + ) |
| 111 | + assert ( |
| 112 | + db_request.db.query(JournalEntry) |
| 113 | + .filter_by(submitted_from=remote_addr) |
| 114 | + .count() |
| 115 | + == 0 |
| 116 | + ) |
0 commit comments