def test_user_created_at_defaults_to_datetime(): user = User(email='*****@*****.**') user.save() assert bool(user.created_at) assert isinstance(user.created_at, dt.datetime) user.delete()
def load_user(id): int_id = None if not app.config.get('USE_SESSIONSTORE_NOT_DB'): try: int_id = int(id) except ValueError: return None return (app.config.get('USE_SESSIONSTORE_NOT_DB') # Load the dummy user if set to 'sessionstore' instead of 'db' and User.sessionstore_user() # Otherwise load the actual logged-in user from the DB. or User.get_by_id(int_id))
def load_user(id): int_id = None if not app.config.get('USE_SESSIONSTORE_NOT_DB'): try: int_id = int(id) except ValueError: return None return ( app.config.get('USE_SESSIONSTORE_NOT_DB') # Load the dummy user if set to 'sessionstore' instead of 'db' and User.sessionstore_user() # Otherwise load the actual logged-in user from the DB. or User.get_by_id(int_id))
def test_home_gallery_reorder_visible_sessionstore(app_sessionstore): app = app_sessionstore with app.test_request_context(): testapp = TestApp(app) user = User.sessionstore_user() # Goes to homepage res = testapp.get("/") # Fills out login form form = res.forms['loginForm'] form['email'] = user.email form['password'] = app.config['SESSIONSTORE_USER_PASSWORD'] # Submits res = form.submit().follow() form = res.forms['gi-reorder'] assert form['gallery_items-0-identifier'].value == '1' assert form['gallery_items-0-weight'].value == '0' assert form['gallery_items-1-identifier'].value == '2' assert form['gallery_items-1-weight'].value == '1' form['gallery_items-0-weight'] = '1' form['gallery_items-1-weight'] = '0' res = form.submit().follow() form = res.forms['gi-reorder'] assert form['gallery_items-0-identifier'].value == '1' assert form['gallery_items-0-weight'].value == '0' assert form['gallery_items-1-identifier'].value == '2' assert form['gallery_items-1-weight'].value == '1'
def validate(self): initial_validation = super(LoginForm, self).validate() if not initial_validation: return False # Log in with dummy credentials when we're set to 'sessionstore' # instead of 'db'. if app.config.get('USE_SESSIONSTORE_NOT_DB'): if self.email.data != app.config['SESSIONSTORE_USER_EMAIL']: self.email.errors.append('Unknown email') return False if self.password.data != app.config['SESSIONSTORE_USER_PASSWORD']: self.password.errors.append('Invalid password') return False self.user = User.sessionstore_user() # Regular login. else: self.user = User.query.filter_by(email=self.email.data).first() if not self.user: self.email.errors.append('Unknown email') return False if not self.user.check_password(self.password.data): self.password.errors.append('Invalid password') return False if not self.user.active: self.email.errors.append('User {0} not activated'.format( self.user.full_name_or_email)) return False return True
def test_home_event_delete_visible_sessionstore(app_sessionstore): app = app_sessionstore with app.test_request_context(): testapp = TestApp(app) user = User.sessionstore_user() # Goes to homepage res = testapp.get("/") # Fills out login form form = res.forms['loginForm'] form['email'] = user.email form['password'] = app.config['SESSIONSTORE_USER_PASSWORD'] # Submits res = form.submit().follow() assert ((len(res.session['event']) - 1) == app.config['EVENT_NUM_DEFAULT_ITEMS']) assert (len(res.html.findAll( 'article', {'class': 'events-item' })) == app.config['EVENT_NUM_DEFAULT_ITEMS']) form = res.forms['event-delete-1'] res = form.submit().follow() assert ((len(res.session['event']) - 1) == (app.config['EVENT_NUM_DEFAULT_ITEMS'] - 1)) assert (len(res.html.findAll( 'article', {'class': 'events-item' })) == (app.config['EVENT_NUM_DEFAULT_ITEMS'] - 1)) form = res.forms['event-add'] res = form.submit().follow() res = testapp.get(url_for('public.logout')).follow()
def test_home_textcontentblock_update_visible_sessionstore(app_sessionstore): app = app_sessionstore with app.test_request_context(): testapp = TestApp(app) user = User.sessionstore_user() # Goes to homepage res = testapp.get("/") # Fills out login form form = res.forms['loginForm'] form['email'] = user.email form['password'] = app.config['SESSIONSTORE_USER_PASSWORD'] # Submits res = form.submit().follow() default_content = ShortTextContentBlock.default_content() assert default_content['site-byline'].content in res form = res.forms[ 'short-text-form-short_text_content_block-content-site-byline'] new_byline = 'This is a rather mediocre byline.' form['content'] = new_byline res = form.submit().follow() assert new_byline in res assert not (default_content['site-byline'].content in res) res = testapp.get(url_for('public.logout')).follow()
def test_home_event_startdate_update_visible_sessionstore(app_sessionstore): app = app_sessionstore with app.test_request_context(): testapp = TestApp(app) user = User.sessionstore_user() # Goes to homepage res = testapp.get("/") # Fills out login form form = res.forms['loginForm'] form['email'] = user.email form['password'] = app.config['SESSIONSTORE_USER_PASSWORD'] # Submits res = form.submit().follow() event = res.session['event'][1] old_start_date = datetime.strptime(event['start_date'], '%Y-%m-%d') old_start_date_str = old_start_date.strftime('%d %b %Y') assert (('<input class="datepicker-enable" ' 'id="event-start_date-1" name="content" ' 'placeholder="Pick your start date" required type="text" ' 'value="{0}">').format(old_start_date_str) in res) form = res.forms['date-pick-form-event-start_date-1'] new_start_date = (old_start_date + timedelta(days=3)) new_start_date_str = new_start_date.strftime('%d %b %Y') form['content'] = new_start_date_str res = form.submit().follow() assert (('<input class="datepicker-enable" ' 'id="event-start_date-1" name="content" ' 'placeholder="Pick your start date" required type="text" ' 'value="{0}">').format(new_start_date_str) in res) res = testapp.get(url_for('public.logout')).follow()
def test_home_textcontentblock_update_visible_sessionstore( app_sessionstore): app = app_sessionstore with app.test_request_context(): testapp = TestApp(app) user = User.sessionstore_user() # Goes to homepage res = testapp.get("/") # Fills out login form form = res.forms['loginForm'] form['email'] = user.email form['password'] = app.config['SESSIONSTORE_USER_PASSWORD'] # Submits res = form.submit().follow() default_content = ShortTextContentBlock.default_content() assert default_content['site-byline'].content in res form = res.forms[ 'short-text-form-short_text_content_block-content-site-byline'] new_byline = 'This is a rather mediocre byline.' form['content'] = new_byline res = form.submit().follow() assert new_byline in res assert not(default_content['site-byline'].content in res) res = testapp.get(url_for('public.logout')).follow()
def test_user_check_password(): user = User.create(email="*****@*****.**", password="******") assert user.check_password('foobarbaz123') is True assert user.check_password("barfoobaz") is False user.delete()
def test_user_get_by_id(): user = User('foo', '*****@*****.**') user.save() retrieved = User.get_by_id(user.id) assert retrieved == user user.delete()
def test_home_imagecontentblock_update_visible_sessionstore(app_sessionstore): app = app_sessionstore with app.test_request_context(): testapp = TestApp(app) user = User.sessionstore_user() # Goes to homepage res = testapp.get("/") # Fills out login form form = res.forms['loginForm'] form['email'] = user.email form['password'] = app.config['SESSIONSTORE_USER_PASSWORD'] # Submits res = form.submit().follow() assert (( '<img class="img-responsive img-circle img-lesswidth" ' 'src="{0}" alt="{1}">').format( url_for( 'static', filename=thumb.thumbnail( app.config['EDITABLE_PLACEHOLDER_IMAGE_RELATIVE_PATH'], size='256x256', crop='fit')), app.config['SITE_NAME']) in res.text) form = res.forms['image-form-image_content_block-image-site-logo'] old_image_filepath = '{0}/{1}'.format( app.config['MEDIA_FOLDER'], app.config['EDITABLE_PLACEHOLDER_IMAGE_RELATIVE_PATH']) assert os.path.exists(old_image_filepath) old_image_file = open(old_image_filepath, 'rb') new_image_filename = ''.join( random.choice(string.ascii_lowercase) for _ in range(10)) new_image_filename += '.jpg' form['image'] = Upload(new_image_filename, old_image_file.read(), 'image/jpeg') old_image_file.close() res = form.submit().follow() assert (( '<img class="img-responsive img-circle img-lesswidth" ' 'src="{0}" alt="{1}">').format( url_for( 'static', filename=thumb.thumbnail( app.config['EDITABLE_PLACEHOLDER_IMAGE_RELATIVE_PATH'], size='256x256', crop='fit')), app.config['SITE_NAME']) in res.text) res = testapp.get(url_for('public.logout')).follow()
def test_home_event_starttime_update_visible_sessionstore(app_sessionstore): app = app_sessionstore with app.test_request_context(): testapp = TestApp(app) user = User.sessionstore_user() # Goes to homepage res = testapp.get("/") # Fills out login form form = res.forms['loginForm'] form['email'] = user.email form['password'] = app.config['SESSIONSTORE_USER_PASSWORD'] # Submits res = form.submit().follow() event = res.session['event'][1] dt_now_str = datetime.now().strftime('%Y-%m-%d') old_start_time_str = '' if (('start_time') in event) and event['start_time']: old_start_time = (datetime.strptime( dt_now_str + ' ' + event['start_time'], '%Y-%m-%d %H:%M:%S').time()) old_start_time_str = old_start_time.strftime('%H:%M') assert (('<input class="timepicker-enable" ' 'id="event-start_time-1" name="content" ' 'placeholder="Pick your start time" type="time" ' 'value="{0}">').format(old_start_time_str) in res) form = res.forms['time-pick-form-event-start_time-1'] dt_now = datetime.now() dt_midnighttoday = datetime(dt_now.year, dt_now.month, dt_now.day) rand_delta = timedelta(minutes=(15 * random.randrange(96))) new_start_time = (dt_midnighttoday + rand_delta).time() new_start_time_str = new_start_time.strftime('%H:%M') i = 0 while i < 3 and new_start_time_str == old_start_time_str: rand_delta = timedelta(minutes=(15 * random.randrange(96))) new_start_time = (dt_midnighttoday + rand_delta).time() new_start_time_str = new_start_time.strftime('%H:%M') form['content'] = new_start_time_str res = form.submit().follow() assert (('<input class="timepicker-enable" ' 'id="event-start_time-1" name="content" ' 'placeholder="Pick your start time" type="time" ' 'value="{0}">').format(new_start_time_str) in res) res = testapp.get(url_for('public.logout')).follow()
def create_user(self, email, first_name, last_name, password): from flask_editablesite.user.models import User u = User(email=email, first_name=first_name, last_name=last_name, active=True) u.set_password(password) u.save() return u
def test_login_returns_200_sessionstore(app_sessionstore): app = app_sessionstore with app.test_request_context(): testapp = TestApp(app_sessionstore) # Goes to homepage res = testapp.get("/") # Fills out login form form = res.forms['loginForm'] user = User.sessionstore_user() form['email'] = user.email form['password'] = app.config['SESSIONSTORE_USER_PASSWORD'] # Submits res = form.submit().follow() assert res.status_code == 200
def test_home_event_startdate_update_visible_sessionstore( app_sessionstore): app = app_sessionstore with app.test_request_context(): testapp = TestApp(app) user = User.sessionstore_user() # Goes to homepage res = testapp.get("/") # Fills out login form form = res.forms['loginForm'] form['email'] = user.email form['password'] = app.config['SESSIONSTORE_USER_PASSWORD'] # Submits res = form.submit().follow() event = res.session['event'][1] old_start_date = datetime.strptime( event['start_date'], '%Y-%m-%d') old_start_date_str = old_start_date.strftime('%d %b %Y') assert (( '<input class="datepicker-enable" ' 'id="event-start_date-1" name="content" ' 'placeholder="Pick your start date" required type="text" ' 'value="{0}">').format( old_start_date_str) in res) form = res.forms[ 'date-pick-form-event-start_date-1'] new_start_date = (old_start_date + timedelta(days=3)) new_start_date_str = new_start_date.strftime('%d %b %Y') form['content'] = new_start_date_str res = form.submit().follow() assert (( '<input class="datepicker-enable" ' 'id="event-start_date-1" name="content" ' 'placeholder="Pick your start date" required type="text" ' 'value="{0}">').format( new_start_date_str) in res) res = testapp.get(url_for('public.logout')).follow()
def test_home_event_delete_visible_sessionstore(app_sessionstore): app = app_sessionstore with app.test_request_context(): testapp = TestApp(app) user = User.sessionstore_user() # Goes to homepage res = testapp.get("/") # Fills out login form form = res.forms['loginForm'] form['email'] = user.email form['password'] = app.config['SESSIONSTORE_USER_PASSWORD'] # Submits res = form.submit().follow() assert ( (len(res.session['event']) - 1) == app.config['EVENT_NUM_DEFAULT_ITEMS']) assert ( len( res.html.findAll('article', {'class': 'events-item'})) == app.config['EVENT_NUM_DEFAULT_ITEMS']) form = res.forms['event-delete-1'] res = form.submit().follow() assert ( (len(res.session['event']) - 1) == ( app.config['EVENT_NUM_DEFAULT_ITEMS'] - 1)) assert ( len( res.html.findAll('article', {'class': 'events-item'})) == (app.config['EVENT_NUM_DEFAULT_ITEMS'] - 1)) form = res.forms['event-add'] res = form.submit().follow() res = testapp.get(url_for('public.logout')).follow()
def test_home_imagecontentblock_update_visible_sessionstore( app_sessionstore): app = app_sessionstore with app.test_request_context(): testapp = TestApp(app) user = User.sessionstore_user() # Goes to homepage res = testapp.get("/") # Fills out login form form = res.forms['loginForm'] form['email'] = user.email form['password'] = app.config['SESSIONSTORE_USER_PASSWORD'] # Submits res = form.submit().follow() assert ( ( '<img class="img-responsive img-circle img-lesswidth" ' 'src="{0}" alt="{1}">').format( url_for('static', filename=thumb.thumbnail( app.config[ 'EDITABLE_PLACEHOLDER_IMAGE_RELATIVE_PATH'], size='256x256', crop='fit')), app.config['SITE_NAME']) in res.text) form = res.forms[ 'image-form-image_content_block-image-site-logo'] old_image_filepath = '{0}/{1}'.format( app.config['MEDIA_FOLDER'], app.config['EDITABLE_PLACEHOLDER_IMAGE_RELATIVE_PATH']) assert os.path.exists(old_image_filepath) old_image_file = open(old_image_filepath, 'rb') new_image_filename = ''.join( random.choice(string.ascii_lowercase) for _ in range(10)) new_image_filename += '.jpg' form['image'] = Upload( new_image_filename, old_image_file.read(), 'image/jpeg') old_image_file.close() res = form.submit().follow() assert ( ( '<img class="img-responsive img-circle img-lesswidth" ' 'src="{0}" alt="{1}">').format( url_for('static', filename=thumb.thumbnail( app.config[ 'EDITABLE_PLACEHOLDER_IMAGE_RELATIVE_PATH'], size='256x256', crop='fit')), app.config['SITE_NAME']) in res.text) res = testapp.get(url_for('public.logout')).follow()
def test_user_password_is_nullable(): user = User(email='*****@*****.**') user.save() assert user.password is None user.delete()
def test_home_event_starttime_update_visible_sessionstore( app_sessionstore): app = app_sessionstore with app.test_request_context(): testapp = TestApp(app) user = User.sessionstore_user() # Goes to homepage res = testapp.get("/") # Fills out login form form = res.forms['loginForm'] form['email'] = user.email form['password'] = app.config['SESSIONSTORE_USER_PASSWORD'] # Submits res = form.submit().follow() event = res.session['event'][1] dt_now_str = datetime.now().strftime('%Y-%m-%d') old_start_time_str = '' if (('start_time') in event) and event['start_time']: old_start_time = ( datetime.strptime( dt_now_str + ' ' + event['start_time'], '%Y-%m-%d %H:%M:%S') .time()) old_start_time_str = old_start_time.strftime('%H:%M') assert (( '<input class="timepicker-enable" ' 'id="event-start_time-1" name="content" ' 'placeholder="Pick your start time" type="time" ' 'value="{0}">').format( old_start_time_str) in res) form = res.forms[ 'time-pick-form-event-start_time-1'] dt_now = datetime.now() dt_midnighttoday = datetime(dt_now.year, dt_now.month, dt_now.day) rand_delta = timedelta(minutes=(15 * random.randrange(96))) new_start_time = (dt_midnighttoday + rand_delta).time() new_start_time_str = new_start_time.strftime('%H:%M') i = 0 while i < 3 and new_start_time_str == old_start_time_str: rand_delta = timedelta(minutes=(15 * random.randrange(96))) new_start_time = (dt_midnighttoday + rand_delta).time() new_start_time_str = new_start_time.strftime('%H:%M') form['content'] = new_start_time_str res = form.submit().follow() assert (( '<input class="timepicker-enable" ' 'id="event-start_time-1" name="content" ' 'placeholder="Pick your start time" type="time" ' 'value="{0}">').format( new_start_time_str) in res) res = testapp.get(url_for('public.logout')).follow()