Example #1
0
def test_app(app, test_ip):
    """A Webtest app."""

    app.install(IPAuthPlugin(token_manager=TokenManager()))

    # It is not possible to use in-memory SQLite for testing. Because for every new connection table schema will be
    # empty. So temp file will be used for test database
    fd, dbfile = tempfile.mkstemp(suffix='.sqlite')
    sqlite_plugin = app.install(SQLitePlugin(dbfile=dbfile))
    # Create database
    with sqlite3.connect(dbfile) as conn:
        conn.execute('CREATE TABLE access_log\n'
                     '            (\n'
                     '                id INTEGER PRIMARY KEY,\n'
                     '                file_key BLOB,\n'
                     '                access_date DATETIME,\n'
                     '                last_access_date DATETIME\n'
                     '            )')
        conn.commit()

        # Register IP
        t_app = TestApp(app, extra_environ={'REMOTE_ADDR': test_ip})
        res = t_app.get('/register')
        assert 200 == res.status_code
        t_app.token = res.json['Token']
        assert t_app.token is not None

        yield t_app

    # teardown
    if conn:
        conn.close()
    os.close(fd)
    os.unlink(sqlite_plugin.dbfile)