Skip to content

Commit

Permalink
added migration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
heartsucker committed Apr 17, 2019
1 parent efc006c commit 1582036
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions securedrop/tests/migrations/migration_e9fa666b089a.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# -*- coding: utf-8 -*-
from sqlalchemy import text
from uuid import uuid4

from db import db
from journalist_app import create_app
from .helpers import random_datetime


class UpgradeTester:

def __init__(self, config):
self.config = config
self.app = create_app(config)

def load_data(self):
'''
We load nothing because this migration simply creates a table.
'''
pass

def check_upgrade(self):
'''
We check nothing because this migration simply creates a table.
'''
pass


class DowngradeTester:

def __init__(self, config):
self.config = config
self.app = create_app(config)

def load_data(self):
with self.app.app_context():
jid = self.add_journalist()
self.add_revoked_token(jid)

def add_journalist(self):
params = {
'username': 'test user',
'uuid': str(uuid4()),
'pw_salt': 'testtesttesttest',
'pw_hash': 'testtesttesttest',
'is_admin': False,
'otp_secret': 'abcd1234',
'is_totp': False,
'hotp_counter': 0,
'last_token': '1234',
'created_on': random_datetime(nullable=True),
'last_access': random_datetime(nullable=True),
'passphrase_hash': 'abcd1234',
}
sql = '''INSERT INTO journalists (username, uuid, pw_salt, pw_hash, is_admin, otp_secret,
is_totp, hotp_counter, last_token, created_on, last_access, passphrase_hash)
VALUES (:username, :uuid, :pw_salt, :pw_hash, :is_admin, :otp_secret, :is_totp,
:hotp_counter, :last_token, :created_on, :last_access, :passphrase_hash);
'''
return db.engine.execute(text(sql), **params).lastrowid

def add_revoked_token(self, jid):
params = {
'journalist_id': jid,
'token': 'abc123',
}
sql = '''INSERT INTO revoked_tokens (journalist_id, token)
VALUES (:journalist_id, :token)
'''
db.engine.execute(text(sql), **params)

def check_downgrade(self):
'''
We check nothing because this migration simply drops a table. As long as the migration
passes, we are ok.
'''
pass

0 comments on commit 1582036

Please sign in to comment.