def setUp(self) -> None: with app.app_context(): # user for login example = User() example.firstname = 'Admin' example.lastname = 'Admin' example.email = '*****@*****.**' example.dateofbirth = datetime.datetime(2020, 10, 5) example.is_admin = True example.set_password('admin') db.session.add(example) # dummy user dummy_user = User() dummy_user.firstname = 'Dummy' dummy_user.lastname = 'Dummy' dummy_user.email = '*****@*****.**' dummy_user.dateofbirth = datetime.datetime(2020, 10, 5) dummy_user.is_admin = True dummy_user.set_password('admin') db.session.add(dummy_user) db.session.commit() dummy_id = User.query.filter( User.email == '*****@*****.**').first().id test_story = Story() test_story.text = "Test story from admin user" test_story.author_id = 1 test_story.is_draft = 0 test_story.figures = "#Test#admin#" dummy_story = Story() dummy_story.text = "Test story from dummy user" dummy_story.author_id = dummy_id dummy_story.is_draft = 0 dummy_story.figures = "#Test#dummy#" db.session.add(test_story) db.session.add(dummy_story) db.session.commit() payload = {'email': '*****@*****.**', 'password': '******'} form = LoginForm(data=payload) self.client.post('/users/login', data=form.data, follow_redirects=True)
def setUp(self) -> None: with app.app_context(): # user for login example = User() example.firstname = 'Admin' example.lastname = 'Admin' example.email = '*****@*****.**' example.dateofbirth = datetime.datetime(2020, 10, 5) example.is_admin = True example.set_password('admin') db.session.add(example) db.session.commit() # reacted story test_story = Story() test_story.text = "Test story from admin user" test_story.author_id = 1 test_story.is_draft = 0 test_story.figures = "#Test#admin#" db.session.add(test_story) db.session.commit() # login payload = {'email': '*****@*****.**', 'password': '******'} form = LoginForm(data=payload) self.client.post('/users/login', data=form.data, follow_redirects=True)
def test_random_recent_story(self): # Random recent story as anonymous user self.client.get('/stories/random', follow_redirects=True) self.assert_template_used('story.html') self.assertEqual(self.get_context_variable('story').text, 'Just another story') # Login as Admin payload = {'email': '*****@*****.**', 'password': '******'} form = LoginForm(data=payload) self.client.post('/users/login', data=form.data, follow_redirects=True) # No recent stories self.client.get('/stories/random', follow_redirects=True) self.assert_template_used('stories.html') self.assert_message_flashed('Oops, there are no recent stories by other users!') # Create a new recent story by Admin2 example = Story() example.text = 'This is a valid recent story' example.date = datetime.datetime.now() example.author_id = 2 example.figures = 'story#recent' example.is_draft = False db.session.add(example) db.session.commit() # Get the only recent story not written by Admin response = self.client.get('/stories/random', follow_redirects=True) self.assert_template_used('story.html') self.assertEqual(self.get_context_variable('story').text, 'This is a valid recent story')
def setUp(self) -> None: with app.app_context(): # user for login example = User() example.firstname = 'Admin' example.lastname = 'Admin' example.email = '*****@*****.**' example.dateofbirth = datetime.datetime(2020, 10, 5) example.is_admin = True example.set_password('admin') db.session.add(example) db.session.commit() # reacted story test_story = Story() test_story.text = "Test story from admin user" test_story.author_id = 1 test_story.is_draft = 0 test_story.figures = "#admin#cat#" db.session.add(test_story) db.session.commit()
def _write_story(id_story=None, message='', status=200): form = StoryForm() # Setting session to modify draft if 'GET' == request.method and id_story is not None: story = Story.query.filter(Story.id == id_story).first() if story is not None and story.author_id == current_user.id and story.is_draft: form.text.data = story.text session['figures'] = story.figures.split('#') session['figures'] = session['figures'][1:-1] session['id_story'] = story.id else: flash( 'Request is invalid, check if you are the author of the story and it is still a draft', 'error') return redirect( url_for('users._user_drafts', id_user=current_user.id)) # Check if there are the words to write the story if 'figures' not in session: flash('Request is invalid, you need to set a story before', 'error') return redirect(url_for('home.index')) elif 'POST' == request.method: if form.validate_on_submit(): draft = bool(int(form.as_draft.data)) if draft: if 'id_story' in session: # Update a draft db.session.query(Story).filter_by( id=session['id_story']).update({ 'text': form.text.data, 'date': datetime.datetime.now() }) db.session.commit() session.pop('id_story') else: # Save new story as draft new_story = Story() new_story.author_id = current_user.id new_story.figures = '#' + '#'.join( session['figures']) + '#' new_story.is_draft = True form.populate_obj(new_story) db.session.add(new_story) db.session.commit() session.pop('figures') return redirect( url_for('users._user_drafts', id_user=current_user.id)) else: # Check validity dice_figures = session['figures'].copy() trans = str.maketrans(string.punctuation, ' ' * len(string.punctuation)) new_s = form['text'].data.translate(trans).lower() story_words = new_s.split() for w in story_words: if w in dice_figures: dice_figures.remove(w) if not dice_figures: break if len(dice_figures) > 0: status = 400 message = 'Your story doesn\'t contain all the words. Missing: ' for w in dice_figures: message += w + ' ' else: if 'id_story' in session: # Publish a draft date_format = "%Y %m %d %H:%M" date = datetime.datetime.strptime( datetime.datetime.now().strftime(date_format), date_format) db.session.query(Story).filter_by( id=session['id_story']).update({ 'text': form.text.data, 'date': date, 'is_draft': False }) db.session.commit() session.pop('id_story') else: # Publish a new story new_story = Story() new_story.author_id = current_user.id new_story.figures = '#' + '#'.join( session['figures']) + '#' new_story.is_draft = False form.populate_obj(new_story) db.session.add(new_story) db.session.commit() session.pop('figures') flash('Your story is a valid one! It has been published') return redirect( url_for('users._user_stories', id_user=current_user.id, _external=True)) return make_response( render_template("write_story.html", home_url=HOME_URL, form=form, words=session['figures'], message=message), status)