mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-07-18 13:39:57 -04:00
Logout functionality and test added.
This commit is contained in:
@@ -4,5 +4,6 @@ main = Blueprint('main', __name__)
|
||||
|
||||
|
||||
from app.main.views import (
|
||||
index, sign_in, register, two_factor, verify, sms, add_service, code_not_received, jobs, dashboard, templates
|
||||
index, sign_in, sign_out, register, two_factor, verify, sms, add_service,
|
||||
code_not_received, jobs, dashboard, templates
|
||||
)
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
from flask import render_template
|
||||
|
||||
from flask_login import login_required
|
||||
from app.main import main
|
||||
|
||||
|
||||
from ._jobs import jobs
|
||||
|
||||
|
||||
@main.route("/dashboard")
|
||||
@login_required
|
||||
def dashboard():
|
||||
return render_template(
|
||||
'views/dashboard.html',
|
||||
|
||||
13
app/main/views/sign_out.py
Normal file
13
app/main/views/sign_out.py
Normal file
@@ -0,0 +1,13 @@
|
||||
from flask import (render_template, redirect, url_for)
|
||||
from flask import session
|
||||
from flask_login import login_required, logout_user
|
||||
|
||||
from app.main import main
|
||||
|
||||
|
||||
@main.route('/sign-out', methods=(['GET']))
|
||||
@login_required
|
||||
def sign_out():
|
||||
logout_user()
|
||||
return redirect(url_for('main.render_sign_in'))
|
||||
|
||||
@@ -28,6 +28,11 @@
|
||||
<div class="phase-banner-beta">
|
||||
<strong class="phase-tag">BETA</strong>
|
||||
</div>
|
||||
{% if current_user.is_authenticated %}
|
||||
<div class="">
|
||||
<a class="" href="{{ url_for('main.sign_out') }}">Sign out</a>
|
||||
</div>
|
||||
{% endif %}
|
||||
{% endblock %}
|
||||
|
||||
|
||||
|
||||
43
tests/app/main/views/test_sign_out.py
Normal file
43
tests/app/main/views/test_sign_out.py
Normal file
@@ -0,0 +1,43 @@
|
||||
from datetime import datetime
|
||||
from flask import url_for
|
||||
from app.main.dao import users_dao
|
||||
from app.models import User
|
||||
|
||||
from .test_sign_in import _set_up_mocker
|
||||
|
||||
|
||||
def test_render_sign_out_redirects_to_sign_in(notifications_admin):
|
||||
with notifications_admin.test_request_context():
|
||||
response = notifications_admin.test_client().get(
|
||||
url_for('main.sign_out'))
|
||||
assert response.status_code == 302
|
||||
assert response.location == url_for(
|
||||
'main.render_sign_in', _external=True, next=url_for('main.sign_out'))
|
||||
|
||||
|
||||
def test_sign_out_user(notifications_admin,
|
||||
notifications_admin_db,
|
||||
notify_db_session,
|
||||
mocker):
|
||||
with notifications_admin.test_request_context():
|
||||
_set_up_mocker(mocker)
|
||||
email = 'valid@example.gov.uk'
|
||||
password = 'val1dPassw0rd!'
|
||||
user = User(email_address=email,
|
||||
password=password,
|
||||
mobile_number='+441234123123',
|
||||
name='valid',
|
||||
created_at=datetime.now(),
|
||||
role_id=1,
|
||||
state='active')
|
||||
users_dao.insert_user(user)
|
||||
with notifications_admin.test_client() as client:
|
||||
client.login(user)
|
||||
# Check we are logged in
|
||||
response = client.get(
|
||||
url_for('main.dashboard'))
|
||||
assert response.status_code == 200
|
||||
response = client.get(url_for('main.sign_out'))
|
||||
assert response.status_code == 302
|
||||
assert response.location == url_for(
|
||||
'main.render_sign_in', _external=True)
|
||||
@@ -3,12 +3,28 @@ import os
|
||||
import pytest
|
||||
from alembic.command import upgrade
|
||||
from alembic.config import Config
|
||||
from flask import url_for
|
||||
from flask.ext.migrate import Migrate, MigrateCommand
|
||||
from flask.ext.script import Manager
|
||||
from flask_login import login_user
|
||||
from sqlalchemy.schema import MetaData
|
||||
from flask.testing import FlaskClient
|
||||
from app.main.dao import verify_codes_dao
|
||||
|
||||
from app import create_app, db
|
||||
|
||||
class TestClient(FlaskClient):
|
||||
def login(self, user):
|
||||
# Skipping authentication here and just log them in
|
||||
with self.session_transaction() as session:
|
||||
session['user_id'] = user.id
|
||||
verify_codes_dao.add_code(user_id=user.id, code='12345', code_type='sms')
|
||||
response = self.post('/two-factor',
|
||||
data={'sms_code': '12345'})
|
||||
|
||||
def logout(self, user):
|
||||
self.get(url_for("main.logout"))
|
||||
|
||||
|
||||
@pytest.fixture(scope='session')
|
||||
def notifications_admin(request):
|
||||
@@ -21,6 +37,7 @@ def notifications_admin(request):
|
||||
ctx.pop()
|
||||
|
||||
request.addfinalizer(teardown)
|
||||
app.test_client_class = TestClient
|
||||
return app
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user