1+ from datetime import datetime , timedelta
2+ from flask .testing import FlaskClient
3+ import pytest
4+ from pytest_mock import MockerFixture
5+
6+ from elekto import APP , SESSION , constants
7+ from elekto .models import meta
8+ from elekto .models .sql import User , drop_all , migrate
9+
10+ @pytest .fixture (scope = "module" , autouse = True )
11+ def reset_db ():
12+ with APP .app_context ():
13+ drop_all (APP .config .get ('DATABASE_URL' ))
14+
15+
16+ @pytest .fixture ()
17+ def client ():
18+ with APP .app_context ():
19+ migrate (APP .config .get ('DATABASE_URL' ))
20+ yield APP .test_client ()
21+ SESSION .close ()
22+ drop_all (APP .config .get ('DATABASE_URL' ))
23+
24+ def test_dashboard (client : FlaskClient ):
25+ token = "token"
26+ with APP .app_context ():
27+ SESSION .add (User (username = "carson" ,
28+ name = "Carson Weeks" ,
29+ token = token ,
30+ token_expires_at = datetime .max ))
31+ SESSION .commit ()
32+ with client .session_transaction () as session :
33+ session [constants .AUTH_STATE ] = token
34+ response = client .get ("/app" )
35+ assert response .status_code == 200
36+ assert b'Welcome! Carson Weeks' in response .data
37+ assert b'Sit back and Relax, there is not to do yet.' in response .data
38+
39+ def test_unauthenticated_dashboard (client : FlaskClient ):
40+ with client .session_transaction () as session :
41+ session [constants .AUTH_STATE ] = None
42+ response = client .get ("/app" )
43+ assert response .status_code == 302
44+
45+ def test_elections_running_dashboard (client : FlaskClient , mocker : MockerFixture ):
46+ mocker .patch ("elekto.meta.Election.where" , return_value = [{"name" : "Demo Election" ,
47+ "organization" : "kubernetes" ,
48+ "start_datetime" : datetime .min ,
49+ "end_datetime" : datetime .max }])
50+ token = "token"
51+ with APP .app_context ():
52+ SESSION .add (User (username = "carson" ,
53+ name = "Carson Weeks" ,
54+ token = token ,
55+ token_expires_at = datetime .max ))
56+ SESSION .commit ()
57+ with client .session_transaction () as session :
58+ session [constants .AUTH_STATE ] = token
59+ response = client .get ("/app" )
60+ assert response .status_code == 200
61+ assert b"Demo Election" in response .data
62+ assert not b"Sit back and Relax, there is not to do yet." in response .data
0 commit comments