def create_task(): if 'user' not in session: return redirect(url_for('login')) connection = model.connect() user_id = session['user']['id'] tasks_user = model.tasks_user(connection, user_id) try: task_do = int(request.cookies['task_do']) except: task_do = None list_task = model.tasks(connection) form = TasksForm(request.form) if form.validate_on_submit(): try: connection = model.connect() date = datetime.datetime.now() task = { 'id': None, 'title': form.title.data, 'description': form.description.data, 'creation_date': date, 'due_date': form.due_date.data, 'id_user': user_id } model.insert_task(connection, task) return redirect('/liste') except Exception as exception: app.log_exception(exception) return render_template('liste.html', form=form, liste=list_task, tasks_user=tasks_user, task_do=task_do)
def test_task_exception(): connection = model.connect(":memory:") model.create_database(connection) model.fill_database(connection) with pytest.raises(Exception) as exception_info: model.task(connection, 1000) assert str(exception_info.value) == 'Task inconnu'
def test_user_exception(): connection = model.connect(":memory:") model.create_database(connection) model.fill_database(connection) with pytest.raises(Exception) as exception_info: model.user(connection, 1000) assert str(exception_info.value) == 'Équipe inconnue'
def test_users_and_insert_user(): connection = model.connect(":memory:") model.create_database(connection) users = data.users() model.insert_user(connection, users[1]) model.insert_user(connection, users[0]) assert model.users(connection) == [users[0], users[1]]
def home(): if 'user' not in session: return redirect(url_for('login')) connection = model.connect() user_id = session['user']['id'] limitated_tasks_user = model.limitated_tasks_user(connection, user_id) return render_template('home.html', limitated_tasks_user=limitated_tasks_user)
def test_add_user_email_unique(): connection = model.connect(":memory:") model.create_database(connection) model.create_database(connection) model.add_user(connection, '*****@*****.**', 'secret1') with pytest.raises(Exception) as exception_info: model.add_user(connection, '*****@*****.**', 'secret2') assert str(exception_info.value) == 'UNIQUE constraint failed: users.email'
def test_change_password_old_password_check(): connection = model.connect(":memory:") model.create_database(connection) model.add_user(connection, '*****@*****.**', 'secret1') with pytest.raises(Exception) as exception_info: model.change_password(connection, '*****@*****.**', 'secret2', 'secret1') assert str(exception_info.value) == 'Utilisateur inconnu'
def test_add_and_get_user(): connection = model.connect(":memory:") model.create_database(connection) model.add_user(connection, '*****@*****.**', 'secret1') model.add_user(connection, '*****@*****.**', 'secret2') user1 = model.get_user(connection, '*****@*****.**', 'secret1') user2 = model.get_user(connection, '*****@*****.**', 'secret2') assert user1 == {'id': 1, 'email': '*****@*****.**'} assert user2 == {'id': 2, 'email': '*****@*****.**'}
def test_change_password(): connection = model.connect(":memory:") model.create_database(connection) model.add_user(connection, '*****@*****.**', 'secret1') model.change_password(connection, '*****@*****.**', 'secret1', 'secret2') user = model.get_user(connection, '*****@*****.**', 'secret2') assert user == {'id': 1, 'email': '*****@*****.**'} with pytest.raises(Exception) as exception_info: model.get_user(connection, '*****@*****.**', 'secret1') assert str(exception_info.value) == 'Utilisateur inconnu'
def test_tasks_and_insert_task(): connection = model.connect(":memory:") model.create_database(connection) tasks = data.tasks() users = data.users() model.insert_user(connection, users[0]) model.insert_user(connection, users[1]) model.insert_task(connection, tasks[6]) model.insert_task(connection, tasks[1]) model.insert_task(connection, tasks[5]) assert model.tasks(connection) == [tasks[1], tasks[5], tasks[6]]
def login(): form = LoginForm() if form.validate_on_submit(): try: connection = model.connect() user = model.get_user(connection, form.email.data, form.password.data) session['user'] = user return redirect(url_for('home')) except Exception as exception: app.log_exception(exception) return render_template('login.html', form=form)
def test_get_user_exception(): connection = model.connect(":memory:") model.create_database(connection) model.add_user(connection, '*****@*****.**', 'secret') with pytest.raises(Exception) as exception_info: model.get_user(connection, '*****@*****.**', 'secret') assert str(exception_info.value) == 'Utilisateur inconnu' with pytest.raises(Exception) as exception_info: model.get_user(connection, '*****@*****.**', 'secret1') assert str(exception_info.value) == 'Utilisateur inconnu' with pytest.raises(Exception) as exception_info: model.get_user(connection, '*****@*****.**', 'secret1') assert str(exception_info.value) == 'Utilisateur inconnu'
def test_delete_task(): connection = model.connect(":memory:") model.create_database(connection) model.create_database(connection) model.fill_database(connection) model.delete_task(connection, 1) model.delete_task(connection, 4) model.delete_task(connection, 100) assert model.task(connection, 2) == { 'id': 2, 'title': 'faire la vaisselle', 'description': 'je suis user 2', 'creation_date': '2020-12-01 00:00:00', 'due_date': '2020-12-07', 'id_user': 2 }
def task_delete(task_id): if 'user' not in session: return redirect(url_for('login')) connection = model.connect() model.delete_task(connection, task_id) return redirect(url_for('create_task'))
from flask_app import data from flask_app import model connection = model.connect() model.create_database(connection) model.fill_database(connection) model.tasks(connection) model.users(connection) #liste = model.tasks(connection)
def test_task(): connection = model.connect(":memory:") model.create_database(connection) model.fill_database(connection) for task in data.tasks(): assert model.task(connection, task['id']) == task
def test_user(): connection = model.connect(":memory:") model.create_database(connection) model.fill_database(connection) for user in data.users(): assert model.user(connection, user['id']) == user
def test_fill_database(): connection = model.connect(":memory:") model.create_database(connection) model.fill_database(connection) assert model.users(connection) == data.users() assert model.tasks(connection) == data.tasks()