def test_init_app_invaliduri(self): """Test that `init_app()` raises `InvalidURI`.""" url = 'http://example.com' self.app.config['MONGO_URI'] = url simon = Simon() with self.assertRaises(InvalidURI): simon.init_app(self.app)
def test_init_app_valueerror(self): """Test that `init_app()` raises `ValueError`.""" url = 'mongodb://*****:*****@localhost:27017/' self.app.config['MONGO_URI'] = url simon = Simon() with self.assertRaises(ValueError) as e: simon.init_app(self.app) expected = 'MONGO_URI does not contain a database name.' actual = '{0}'.format(e.exception) self.assertEqual(actual, expected)
def test_init_app_prefix(self): """Test the `init_app()` method with a different prefix.""" url = 'mongodb://*****:*****@localhost:27017/test-simon' self.app.config['SIMON_URI'] = url simon = Simon() with mock.patch('simon.connection.connect') as connect: simon.init_app(self.app, prefix='SIMON') connect.assert_called_with(url, name='test-simon', alias=None, username='******', password='******', replica_set=None)
def test_init(self): """Test the `__init__()` method.""" with mock.patch.object(Simon, 'init_app') as init_app: # Simon.init_app() should not be called Simon() self.assertFalse(init_app.called) with mock.patch.object(Simon, 'init_app') as init_app: # Simon.init_app() should be called with self.app Simon(self.app, 'MONGODB', 'simon') init_app.assert_called_with(self.app, 'MONGODB', 'simon')
def test_init_app_settings(self): """Test the `init_app()` method with config settings.""" self.app.config['MONGO_HOST'] = 'localhost' self.app.config['MONGO_PORT'] = 27017 self.app.config['MONGO_DBNAME'] = 'test-simon' simon = Simon() with mock.patch('simon.connection.connect') as connect: simon.init_app(self.app) connect.assert_called_with('localhost:27017', name='test-simon', alias=None, username=None, password=None, replica_set=None)
def test_init_app_uri(self): """Test the `init_app()` method with `MONGO_URI`.""" url = ('mongodb://*****:*****@localhost:27017/test-simon' '?replicaSet=rs-simon') self.app.config['MONGO_URI'] = url simon = Simon() with mock.patch('simon.connection.connect') as connect: simon.init_app(self.app) connect.assert_called_with(url, name='test-simon', alias=None, username='******', password='******', replica_set='rs-simon')
def test_objectidconverter(self): """Test that `objectid` is registered as a converter.""" with mock.patch('simon.connection.connect'): Simon(self.app) self.assertIn('objectid', self.app.url_map.converters)
def test_init_app(self): """Test the `init_app()` method.""" simon = Simon() with mock.patch('simon.connection.connect') as connect: simon.init_app(self.app) connect.assert_called_with('localhost:27017', name='test', alias=None, username=None, password=None, replica_set=None) simon.init_app(self.app, prefix='SIMON', alias='simon') connect.assert_called_with('localhost:27017', name='test', alias='simon', username=None, password=None, replica_set=None)
def test_init_app_multiple_connections(self): """Test the `init_app()` method with multiple connections.""" url1 = 'mongodb://*****:*****@localhost:27017/simon' self.app.config['MONGO_URI'] = url1 url2 = 'mongodb://*****:*****@localhost:27017/test' self.app.config['SIMON_URI'] = url2 simon = Simon() with mock.patch('simon.connection.connect') as connect: simon.init_app(self.app, prefix='MONGO') connect.assert_called_with(url1, name='simon', alias=None, username='******', password='******', replica_set=None) simon.init_app(self.app, prefix='SIMON') connect.assert_called_with(url2, name='test', alias=None, username='******', password='******', replica_set=None)
from flask import Flask, redirect, render_template, request, url_for from flask.ext.login import (AnonymousUser, LoginManager, UserMixin, login_required, login_user, logout_user) from flask.ext.simon import Simon, Model app = Flask(__name__) app.secret_key = 'Keep this value secret' Simon(app) login_manager = LoginManager() login_manager.setup_app(app) class Guest(AnonymousUser): """A basic guest user""" login_manager.anonymous_user = Guest class User(Model, UserMixin): """A basic user model, it hooks into Flask-Login using UserMixin""" def get_id(self): """Retrieves the documents ID This is required by Flask-Login """ return getattr(self, 'id') def is_anonymous(self):