|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +import io |
| 3 | +import os |
| 4 | +import random |
| 5 | +import uuid |
| 6 | + |
| 7 | +from os import path |
| 8 | +from sqlalchemy import text |
| 9 | +from sqlalchemy.exc import NoSuchColumnError |
| 10 | + |
| 11 | +from db import db |
| 12 | +from journalist_app import create_app |
| 13 | +from .helpers import random_chars, random_datetime |
| 14 | + |
| 15 | +random.seed('ᕕ( ᐛ )ᕗ') |
| 16 | + |
| 17 | +DATA = b'wat' |
| 18 | +DATA_CHECKSUM = 'sha256:f00a787f7492a95e165b470702f4fe9373583fbdc025b2c8bdf0262cc48fcff4' |
| 19 | + |
| 20 | + |
| 21 | +class Helper: |
| 22 | + |
| 23 | + def __init__(self): |
| 24 | + self.journalist_id = None |
| 25 | + self.source_id = None |
| 26 | + self._counter = 0 |
| 27 | + |
| 28 | + @property |
| 29 | + def counter(self): |
| 30 | + self._counter += 1 |
| 31 | + return self._counter |
| 32 | + |
| 33 | + def create_journalist(self): |
| 34 | + if self.journalist_id is not None: |
| 35 | + raise RuntimeError('Journalist already created') |
| 36 | + |
| 37 | + params = { |
| 38 | + 'uuid': str(uuid.uuid4()), |
| 39 | + 'username': random_chars(50), |
| 40 | + } |
| 41 | + sql = '''INSERT INTO journalists (uuid, username) |
| 42 | + VALUES (:uuid, :username) |
| 43 | + ''' |
| 44 | + self.journalist_id = db.engine.execute(text(sql), **params).lastrowid |
| 45 | + |
| 46 | + def create_source(self): |
| 47 | + if self.source_id is not None: |
| 48 | + raise RuntimeError('Source already created') |
| 49 | + |
| 50 | + self.source_filesystem_id = 'aliruhglaiurhgliaurg-{}'.format(self.counter) |
| 51 | + params = { |
| 52 | + 'filesystem_id': self.source_filesystem_id, |
| 53 | + 'uuid': str(uuid.uuid4()), |
| 54 | + 'journalist_designation': random_chars(50), |
| 55 | + 'flagged': False, |
| 56 | + 'last_updated': random_datetime(nullable=True), |
| 57 | + 'pending': False, |
| 58 | + 'interaction_count': 0, |
| 59 | + } |
| 60 | + sql = '''INSERT INTO sources (filesystem_id, uuid, journalist_designation, flagged, |
| 61 | + last_updated, pending, interaction_count) |
| 62 | + VALUES (:filesystem_id, :uuid, :journalist_designation, :flagged, :last_updated, |
| 63 | + :pending, :interaction_count) |
| 64 | + ''' |
| 65 | + self.source_id = db.engine.execute(text(sql), **params).lastrowid |
| 66 | + |
| 67 | + def create_submission(self, checksum=False): |
| 68 | + filename = str(uuid.uuid4()) |
| 69 | + params = { |
| 70 | + 'uuid': str(uuid.uuid4()), |
| 71 | + 'source_id': self.source_id, |
| 72 | + 'filename': filename, |
| 73 | + 'size': random.randint(10, 1000), |
| 74 | + 'downloaded': False, |
| 75 | + |
| 76 | + } |
| 77 | + |
| 78 | + if checksum: |
| 79 | + params['checksum'] = \ |
| 80 | + 'sha256:f00a787f7492a95e165b470702f4fe9373583fbdc025b2c8bdf0262cc48fcff4' |
| 81 | + sql = '''INSERT INTO submissions (uuid, source_id, filename, size, downloaded, checksum) |
| 82 | + VALUES (:uuid, :source_id, :filename, :size, :downloaded, :checksum) |
| 83 | + ''' |
| 84 | + else: |
| 85 | + sql = '''INSERT INTO submissions (uuid, source_id, filename, size, downloaded) |
| 86 | + VALUES (:uuid, :source_id, :filename, :size, :downloaded) |
| 87 | + ''' |
| 88 | + |
| 89 | + return (db.engine.execute(text(sql), **params).lastrowid, filename) |
| 90 | + |
| 91 | + def create_reply(self, checksum=False): |
| 92 | + filename = str(uuid.uuid4()) |
| 93 | + params = { |
| 94 | + 'uuid': str(uuid.uuid4()), |
| 95 | + 'source_id': self.source_id, |
| 96 | + 'journalist_id': self.journalist_id, |
| 97 | + 'filename': filename, |
| 98 | + 'size': random.randint(10, 1000), |
| 99 | + 'deleted_by_source': False, |
| 100 | + } |
| 101 | + |
| 102 | + if checksum: |
| 103 | + params['checksum'] = \ |
| 104 | + 'sha256:f00a787f7492a95e165b470702f4fe9373583fbdc025b2c8bdf0262cc48fcff4' |
| 105 | + sql = '''INSERT INTO replies (uuid, source_id, journalist_id, filename, size, |
| 106 | + deleted_by_source, checksum) |
| 107 | + VALUES (:uuid, :source_id, :journalist_id, :filename, :size, |
| 108 | + :deleted_by_source, :checksum) |
| 109 | + ''' |
| 110 | + else: |
| 111 | + sql = '''INSERT INTO replies (uuid, source_id, journalist_id, filename, size, |
| 112 | + deleted_by_source) |
| 113 | + VALUES (:uuid, :source_id, :journalist_id, :filename, :size, |
| 114 | + :deleted_by_source) |
| 115 | + ''' |
| 116 | + return (db.engine.execute(text(sql), **params).lastrowid, filename) |
| 117 | + |
| 118 | + |
| 119 | +class UpgradeTester(Helper): |
| 120 | + |
| 121 | + def __init__(self, config): |
| 122 | + Helper.__init__(self) |
| 123 | + self.config = config |
| 124 | + self.app = create_app(config) |
| 125 | + |
| 126 | + def load_data(self): |
| 127 | + global DATA |
| 128 | + with self.app.app_context(): |
| 129 | + self.create_journalist() |
| 130 | + self.create_source() |
| 131 | + |
| 132 | + submission_id, submission_filename = self.create_submission() |
| 133 | + reply_id, reply_filename = self.create_reply() |
| 134 | + |
| 135 | + # we need to actually create files and write data to them so the RQ worker can hash them |
| 136 | + for fn in [submission_filename, reply_filename]: |
| 137 | + full_path = self.app.storage.path(self.source_filesystem_id, fn) |
| 138 | + |
| 139 | + dirname = path.dirname(full_path) |
| 140 | + if not path.exists(dirname): |
| 141 | + os.mkdir(dirname) |
| 142 | + |
| 143 | + with io.open(full_path, 'wb') as f: |
| 144 | + f.write(DATA) |
| 145 | + |
| 146 | + def check_upgrade(self): |
| 147 | + ''' |
| 148 | + We cannot inject the `SDConfig` object provided by the fixture `config` into the alembic |
| 149 | + subprocess that actually performs the migration. This is needed to get both the value of the |
| 150 | + DB URL and access to the function `storage.path`. These values are passed to the `rqworker`, |
| 151 | + and without being able to inject this config, the checksum function won't succeed. The above |
| 152 | + `load_data` function provides data that can be manually verified by checking the `rqworker` |
| 153 | + log file in `/tmp/`. |
| 154 | + ''' |
| 155 | + pass |
| 156 | + |
| 157 | + |
| 158 | +class DowngradeTester(Helper): |
| 159 | + |
| 160 | + def __init__(self, config): |
| 161 | + Helper.__init__(self) |
| 162 | + self.config = config |
| 163 | + self.app = create_app(config) |
| 164 | + |
| 165 | + def load_data(self): |
| 166 | + with self.app.app_context(): |
| 167 | + self.create_journalist() |
| 168 | + self.create_source() |
| 169 | + |
| 170 | + # create a submission and a reply that we don't add checksums to |
| 171 | + self.create_submission(checksum=False) |
| 172 | + self.create_reply(checksum=False) |
| 173 | + |
| 174 | + # create a submission and a reply that have checksums added |
| 175 | + self.create_submission(checksum=True) |
| 176 | + self.create_reply(checksum=True) |
| 177 | + |
| 178 | + def check_downgrade(self): |
| 179 | + ''' |
| 180 | + Verify that the checksum column is now gone. |
| 181 | + ''' |
| 182 | + with self.app.app_context(): |
| 183 | + sql = "SELECT * FROM submissions" |
| 184 | + submissions = db.engine.execute(text(sql)).fetchall() |
| 185 | + for submission in submissions: |
| 186 | + try: |
| 187 | + # this should produce an exception since the column is gone |
| 188 | + submission['checksum'] |
| 189 | + except NoSuchColumnError: |
| 190 | + pass |
| 191 | + |
| 192 | + sql = "SELECT * FROM replies" |
| 193 | + replies = db.engine.execute(text(sql)).fetchall() |
| 194 | + for reply in replies: |
| 195 | + try: |
| 196 | + # this should produce an exception since the column is gone |
| 197 | + submission['checksum'] |
| 198 | + except NoSuchColumnError: |
| 199 | + pass |
0 commit comments