"""Sirius models.
-Revision ID: 4a0698610fcd
+Revision ID: 3be2a0e43b88
Revises: None
-Create Date: 2015-01-15 11:27:22.373207
+Create Date: 2015-01-15 15:33:10.629707
"""
# revision identifiers, used by Alembic.
-revision = '4a0698610fcd'
+revision = '3be2a0e43b88'
down_revision = None
from alembic import op
def upgrade():
### commands auto generated by Alembic - please adjust! ###
+ op.create_table('user',
+ sa.Column('id', sa.Integer(), nullable=False),
+ sa.Column('created', sa.DateTime(), nullable=True),
+ sa.Column('username', sa.String(), nullable=True),
+ sa.PrimaryKeyConstraint('id')
+ )
op.create_table('bridge',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('created', sa.DateTime(), nullable=True),
sa.Column('entry', sa.String(), nullable=True),
sa.PrimaryKeyConstraint('id')
)
- op.create_table('user',
+ op.create_table('printer',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('created', sa.DateTime(), nullable=True),
- sa.Column('username', sa.String(), nullable=True),
+ sa.Column('device_address', sa.String(), nullable=True),
+ sa.Column('hardware_xor', sa.Integer(), nullable=True),
+ sa.Column('name', sa.String(), nullable=True),
+ sa.Column('owner_id', sa.Integer(), nullable=True),
+ sa.Column('used_claim_code', sa.String(), nullable=True),
+ sa.ForeignKeyConstraint(['owner_id'], ['user.id'], ),
sa.PrimaryKeyConstraint('id')
)
- op.create_table('twitter_o_auth',
- sa.Column('id', sa.Integer(), nullable=False),
- sa.Column('created', sa.DateTime(), nullable=True),
- sa.Column('user_id', sa.Integer(), nullable=True),
- sa.Column('screen_name', sa.String(), nullable=True),
- sa.Column('token', sa.String(), nullable=True),
- sa.ForeignKeyConstraint(['user_id'], [u'user.id'], ),
- sa.PrimaryKeyConstraint('id'),
- sa.UniqueConstraint('screen_name'),
- sa.UniqueConstraint('token')
- )
op.create_table('claim_code',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('created', sa.DateTime(), nullable=True),
sa.ForeignKeyConstraint(['by_id'], ['user.id'], ),
sa.PrimaryKeyConstraint('id')
)
- op.create_table('printer',
+ op.create_table('twitter_o_auth',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('created', sa.DateTime(), nullable=True),
- sa.Column('device_address', sa.String(), nullable=True),
- sa.Column('hardware_xor', sa.Integer(), nullable=True),
- sa.Column('name', sa.String(), nullable=True),
- sa.Column('owner_id', sa.Integer(), nullable=True),
- sa.Column('used_claim_code', sa.String(), nullable=True),
- sa.ForeignKeyConstraint(['owner_id'], ['user.id'], ),
- sa.PrimaryKeyConstraint('id')
+ sa.Column('user_id', sa.Integer(), nullable=True),
+ sa.Column('screen_name', sa.String(), nullable=True),
+ sa.Column('token', sa.String(), nullable=True),
+ sa.ForeignKeyConstraint(['user_id'], [u'user.id'], ),
+ sa.PrimaryKeyConstraint('id'),
+ sa.UniqueConstraint('screen_name'),
+ sa.UniqueConstraint('token')
)
### end Alembic commands ###
def downgrade():
### commands auto generated by Alembic - please adjust! ###
- op.drop_table('printer')
- op.drop_table('claim_code')
op.drop_table('twitter_o_auth')
- op.drop_table('user')
+ op.drop_table('claim_code')
+ op.drop_table('printer')
op.drop_table('device_log')
op.drop_table('bridge')
+ op.drop_table('user')
### end Alembic commands ###
from sirius.coding import encoders
from sirius.coding import decoders
from sirius import stats
-from sirius import database
+
+from sirius.models import hardware
logger = logging.getLogger(__name__)
:param bridge_state: The BridgeState for this connection
"""
if type(x) == messages.DeviceConnect:
+ hardware.Printer.phone_home(x.device_address)
bridge_state.connected_devices.add(x.device_address)
elif type(x) == messages.DeviceDisconnect:
pass # TODO - write log to a place
elif type(x) == messages.EncryptionKeyRequired:
+ hardware.Printer.phone_home(x.device_address)
bridge_state.connected_devices.add(x.device_address)
- claimed_device = database.get_claim_code(x.hardware_xor)
- if claimed_device is None:
+ claim_code = hardware.Printer.get_claim_code(x.device_address)
+ if claim_code is None:
# It's OK to have an unclaimed device, we just ignore it
# for now.
stats.inc('unclaimed.encryption_key_required.count')
add_key = messages.AddDeviceEncryptionKey(
bridge_address=x.bridge_address,
device_address=x.device_address,
- claim_code=claimed_device.claim_code,
+ claim_code=claim_code,
)
send_message(x.device_address, add_key)
elif type(x) == messages.DeviceHeartbeat:
+ hardware.Printer.phone_home(x.device_address)
bridge_state.connected_devices.add(x.device_address)
elif type(x) == messages.PowerOn:
$ bin/nosetests sirius/
"""
-import unittest
+from flask.ext import testing
+
+# TODO: It's a bit ugly that the protocol loop depends on the flask setup
+# for database access but it's the easiest solution for now.
+from sirius.web import webapp
+from sirius.models.db import db
+from sirius.models import hardware
from sirius.protocol import messages
from sirius.protocol import protocol_loop
timestamp=0,
)
+class BaseTest(testing.TestCase):
+
+ def create_app(self):
+ app = webapp.create_app('test')
+ return app
+
+ def setUp(self):
+ testing.TestCase.setUp(self)
+ db.create_all()
+
+ def tearDown(self):
+ db.session.remove()
+ db.drop_all()
+
+
class FakeWebsocket:
"Use to create an in-memory a BridgeState."
def __init__(self):
self.messages.append(message)
-class TestNormalFlow(unittest.TestCase):
+class TestNormalFlow(BaseTest):
"A bunch of smoke tests that exercise the _accept_step."
def setUp(self):
+ BaseTest.setUp(self)
self.bridge_state = protocol_loop.BridgeState(
FakeWebsocket(), m_power_on.bridge_address)
self.assertNotIn(m_hearbeat.device_address, self.bridge_state.connected_devices)
def test_encryption_key_required(self):
+ # Pretend this printer is claimed:
+ hardware.Printer.phone_home('000d6f000273ce0b')
+ p = hardware.Printer.query.first()
+ p.used_claim_code = 'n5ry-p6x6-kth7-7hc4'
+ db.session.add(p)
+ db.session.commit()
+
protocol_loop.bridge_by_address = {
self.bridge_state.address: self.bridge_state
}
self.assertEquals(len(self.bridge_state.websocket.messages), 1)
-class TestBrokenFlow(unittest.TestCase):
+class TestBrokenFlow(BaseTest):
"Check unexpected interactions."
def setUp(self):
+ BaseTest.setUp(self)
self.bridge_state = protocol_loop.BridgeState(
None, m_power_on.bridge_address)
protocol_loop._accept_step(m_command_response, self.bridge_state)
-class TestMultiDevice(unittest.TestCase):
+class TestMultiDevice(BaseTest):
"What happens when we have more than one device."
def setUp(self):
+ BaseTest.setUp(self)
self.bridge_state = protocol_loop.BridgeState(
FakeWebsocket(), m_power_on.bridge_address)