SlimG commited on
Commit
438a969
·
1 Parent(s): 8f4a931

refactor password tests

Browse files
tests/conftest.py CHANGED
@@ -1,13 +1,42 @@
1
  import pytest
2
  import responses
 
3
  from responses.matchers import query_param_matcher
4
  from dotenv import load_dotenv
5
  import os
 
6
 
7
  load_dotenv()
8
 
9
  API_URL = os.getenv('API_URL')
10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  # -----------------------------------------------------------------
12
  # Fixtures for requests calls
13
  # -----------------------------------------------------------------
 
1
  import pytest
2
  import responses
3
+ from typing import Generator
4
  from responses.matchers import query_param_matcher
5
  from dotenv import load_dotenv
6
  import os
7
+ from streamlit.testing.v1 import AppTest
8
 
9
  load_dotenv()
10
 
11
  API_URL = os.getenv('API_URL')
12
 
13
+ # -----------------------------------------------------------------
14
+ # Fixtures for Streamlit
15
+ # -----------------------------------------------------------------
16
+ @pytest.fixture()
17
+ def page_1() -> Generator[AppTest, None, None]:
18
+ """
19
+ Fixture to get the page 1 of the AppTest instance.
20
+ """
21
+ script_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "app", "1_Prédictions.py"))
22
+ app = AppTest.from_file(script_path)
23
+ app.run()
24
+
25
+ yield app
26
+ # Put here any cleaning operation
27
+
28
+ @pytest.fixture()
29
+ def page_2() -> Generator[AppTest, None, None]:
30
+ """
31
+ Fixture to get the page 2 of the AppTest instance.
32
+ """
33
+ script_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "app", "pages", "2_Archives.py"))
34
+ app = AppTest.from_file(script_path)
35
+ app.run()
36
+
37
+ yield app
38
+ # Put here any cleaning operation
39
+
40
  # -----------------------------------------------------------------
41
  # Fixtures for requests calls
42
  # -----------------------------------------------------------------
tests/pwd_protection.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from streamlit.testing.v1 import AppTest
2
+
3
+
4
+ def pwd_protection(at: AppTest):
5
+ """
6
+ Test the login flow with a correct password.
7
+ """
8
+ import os
9
+ from dotenv import load_dotenv
10
+
11
+ load_dotenv()
12
+
13
+ STREAMLIT_PWD = os.getenv("STREAMLIT_PWD")
14
+ assert STREAMLIT_PWD is not None, "STREAMLIT_PWD environment variable is not set"
15
+
16
+ # Check expected session state updates
17
+ assert "is_authenticated" not in at.session_state, "Expected 'is_authenticated' key in session state"
18
+
19
+ # Simulate user typing the correct password
20
+ at.text_input[0].input(STREAMLIT_PWD).run()
21
+
22
+ # Check expected session state updates
23
+ assert "is_authenticated" in at.session_state, "Expected 'is_authenticated' key in session state"
24
+ assert at.session_state["is_authenticated"] is True, "Authentication should have succeeded"
25
+ assert "password" not in at.session_state, "Password should be cleared from session state"
26
+
27
+ def pwd_protection_pwd_incorrect(at: AppTest):
28
+ """
29
+ Test the login flow with an incorrect password.
30
+ """
31
+ import os
32
+ from dotenv import load_dotenv
33
+
34
+ load_dotenv()
35
+
36
+ STREAMLIT_PWD = os.getenv("STREAMLIT_PWD")
37
+ assert STREAMLIT_PWD is not None, "STREAMLIT_PWD environment variable is not set"
38
+
39
+ # Check expected session state updates
40
+ assert "is_authenticated" not in at.session_state, "Expected 'is_authenticated' key in session state"
41
+
42
+ # Simulate user typing an incorrect password
43
+ at.text_input[0].input('an_incorrect_password').run()
44
+
45
+ # Check expected session state updates
46
+ assert "is_authenticated" not in at.session_state, "Expected 'is_authenticated' key in session state"
tests/test_1_Prédictions.py CHANGED
@@ -1,50 +1,8 @@
1
  from streamlit.testing.v1 import AppTest
2
- import os
3
- from dotenv import load_dotenv
4
 
5
- load_dotenv()
 
6
 
7
- def _get_app_test():
8
- """
9
- Helper function to get the AppTest instance.
10
- """
11
- script_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "app", "1_Prédictions.py"))
12
- return AppTest.from_file(script_path).run()
13
-
14
- def test_pwd_protection():
15
- """
16
- Test the login flow with a correct password.
17
- """
18
- STREAMLIT_PWD = os.getenv("STREAMLIT_PWD")
19
- assert STREAMLIT_PWD is not None, "STREAMLIT_PWD environment variable is not set"
20
-
21
- at = _get_app_test()
22
-
23
- # Simulate user typing the correct password
24
- at.text_input[0].input(STREAMLIT_PWD).run()
25
-
26
- # Check expected session state updates
27
- assert "is_authenticated" in at.session_state, "Expected 'is_authenticated' key in session state"
28
- assert at.session_state["is_authenticated"] is True, "Authentication should have succeeded"
29
- assert "password" not in at.session_state, "Password should be cleared from session state"
30
-
31
- # Check there is no error
32
- assert len(at.error) == 0, f"Errors have been printed to the user: {at.error}"
33
-
34
- def test_pwd_protection_pwd_incorrect():
35
- """
36
- Test the login flow with a correct password.
37
- """
38
- STREAMLIT_PWD = os.getenv("STREAMLIT_PWD")
39
- assert STREAMLIT_PWD is not None, "STREAMLIT_PWD environment variable is not set"
40
-
41
- at = _get_app_test()
42
-
43
- # Simulate user typing an incorrect password
44
- at.text_input[0].input('an_incorrect_password').run()
45
-
46
- # Check expected session state updates
47
- assert "is_authenticated" not in at.session_state, "Expected 'is_authenticated' key in session state"
48
-
49
- # Check there is no error
50
- assert len(at.error) >= 1, "Errors should have been printed to the user"
 
1
  from streamlit.testing.v1 import AppTest
2
+ from tests.pwd_protection import pwd_protection, pwd_protection_pwd_incorrect
 
3
 
4
+ def test_pwd_protection(page_1: AppTest):
5
+ pwd_protection(page_1)
6
 
7
+ def test_pwd_protection_pwd_incorrect(page_1: AppTest):
8
+ pwd_protection_pwd_incorrect(page_1)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
tests/test_2_Archives.py CHANGED
@@ -1,50 +1,8 @@
1
  from streamlit.testing.v1 import AppTest
2
- import os
3
- from dotenv import load_dotenv
4
 
5
- load_dotenv()
 
6
 
7
- def _get_app_test():
8
- """
9
- Helper function to get the AppTest instance.
10
- """
11
- script_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "app", "pages", "2_Archives.py"))
12
- return AppTest.from_file(script_path).run()
13
-
14
- def test_pwd_protection():
15
- """
16
- Test the login flow with a correct password.
17
- """
18
- STREAMLIT_PWD = os.getenv("STREAMLIT_PWD")
19
- assert STREAMLIT_PWD is not None, "STREAMLIT_PWD environment variable is not set"
20
-
21
- at = _get_app_test()
22
-
23
- # Simulate user typing the correct password
24
- at.text_input[0].input(STREAMLIT_PWD).run()
25
-
26
- # Check expected session state updates
27
- assert "is_authenticated" in at.session_state, "Expected 'is_authenticated' key in session state"
28
- assert at.session_state["is_authenticated"] is True, "Authentication should have succeeded"
29
- assert "password" not in at.session_state, "Password should be cleared from session state"
30
-
31
- # Check there is no error
32
- assert len(at.error) == 0, f"Errors have been printed to the user: {at.error}"
33
-
34
- def test_pwd_protection_pwd_incorrect():
35
- """
36
- Test the login flow with a correct password.
37
- """
38
- STREAMLIT_PWD = os.getenv("STREAMLIT_PWD")
39
- assert STREAMLIT_PWD is not None, "STREAMLIT_PWD environment variable is not set"
40
-
41
- at = _get_app_test()
42
-
43
- # Simulate user typing an incorrect password
44
- at.text_input[0].input('an_incorrect_password').run()
45
-
46
- # Check expected session state updates
47
- assert "is_authenticated" not in at.session_state, "Expected 'is_authenticated' key in session state"
48
-
49
- # Check there is no error
50
- assert len(at.error) >= 1, "Errors should have been printed to the user"
 
1
  from streamlit.testing.v1 import AppTest
2
+ from tests.pwd_protection import pwd_protection, pwd_protection_pwd_incorrect
 
3
 
4
+ def test_pwd_protection(page_2: AppTest):
5
+ pwd_protection(page_2)
6
 
7
+ def test_pwd_protection_pwd_incorrect(page_2: AppTest):
8
+ pwd_protection_pwd_incorrect(page_2)