Skip to content

Commit 1582036

Browse files
committed
added migration tests
1 parent efc006c commit 1582036

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# -*- coding: utf-8 -*-
2+
from sqlalchemy import text
3+
from uuid import uuid4
4+
5+
from db import db
6+
from journalist_app import create_app
7+
from .helpers import random_datetime
8+
9+
10+
class UpgradeTester:
11+
12+
def __init__(self, config):
13+
self.config = config
14+
self.app = create_app(config)
15+
16+
def load_data(self):
17+
'''
18+
We load nothing because this migration simply creates a table.
19+
'''
20+
pass
21+
22+
def check_upgrade(self):
23+
'''
24+
We check nothing because this migration simply creates a table.
25+
'''
26+
pass
27+
28+
29+
class DowngradeTester:
30+
31+
def __init__(self, config):
32+
self.config = config
33+
self.app = create_app(config)
34+
35+
def load_data(self):
36+
with self.app.app_context():
37+
jid = self.add_journalist()
38+
self.add_revoked_token(jid)
39+
40+
def add_journalist(self):
41+
params = {
42+
'username': 'test user',
43+
'uuid': str(uuid4()),
44+
'pw_salt': 'testtesttesttest',
45+
'pw_hash': 'testtesttesttest',
46+
'is_admin': False,
47+
'otp_secret': 'abcd1234',
48+
'is_totp': False,
49+
'hotp_counter': 0,
50+
'last_token': '1234',
51+
'created_on': random_datetime(nullable=True),
52+
'last_access': random_datetime(nullable=True),
53+
'passphrase_hash': 'abcd1234',
54+
}
55+
sql = '''INSERT INTO journalists (username, uuid, pw_salt, pw_hash, is_admin, otp_secret,
56+
is_totp, hotp_counter, last_token, created_on, last_access, passphrase_hash)
57+
VALUES (:username, :uuid, :pw_salt, :pw_hash, :is_admin, :otp_secret, :is_totp,
58+
:hotp_counter, :last_token, :created_on, :last_access, :passphrase_hash);
59+
'''
60+
return db.engine.execute(text(sql), **params).lastrowid
61+
62+
def add_revoked_token(self, jid):
63+
params = {
64+
'journalist_id': jid,
65+
'token': 'abc123',
66+
}
67+
sql = '''INSERT INTO revoked_tokens (journalist_id, token)
68+
VALUES (:journalist_id, :token)
69+
'''
70+
db.engine.execute(text(sql), **params)
71+
72+
def check_downgrade(self):
73+
'''
74+
We check nothing because this migration simply drops a table. As long as the migration
75+
passes, we are ok.
76+
'''
77+
pass

0 commit comments

Comments
 (0)