Coverage for tests/route_tests/test_superuser.py: 100%

49 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-05-05 14:02 +0000

1"""Tests for the user routes.""" 

2 

3# pylint: disable=redefined-outer-name 

4# flake8: noqa: F811 

5 

6import os 

7import sys 

8import pytest 

9from dotenv import load_dotenv 

10 

11# Add the root directory to the Python path 

12sys.path.append( 

13 os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) 

14) 

15 

16from core import shared 

17from core.database_mongo_manager import DatabaseMongoManager 

18 

19os.environ["IS_TEST"] = "True" 

20 

21load_dotenv() 

22 

23 

24@pytest.fixture() 

25def client(): 

26 """Fixture to create a test client.""" 

27 from ...app import app # pylint: disable=import-outside-toplevel 

28 

29 app.config["TESTING"] = True 

30 return app.test_client() 

31 

32 

33@pytest.fixture() 

34def database(): 

35 """Fixture to create a test database.""" 

36 

37 database = DatabaseMongoManager( 

38 shared.getenv("MONGO_URI"), shared.getenv("MONGO_DB_TEST", "cs3528_testing") 

39 ) 

40 

41 yield database 

42 

43 # Cleanup code 

44 database.connection.close() 

45 

46 

47@pytest.fixture() 

48def superuser_logged_in_client(client, database: DatabaseMongoManager): 

49 """Fixture to login a superuser.""" 

50 database.add_table("users") 

51 

52 superuser_email = shared.getenv("SUPERUSER_EMAIL") 

53 superuser_password = shared.getenv("SUPERUSER_PASSWORD") 

54 

55 url = "/user/login" 

56 

57 client.post( 

58 url, 

59 data={ 

60 "email": superuser_email, 

61 "password": superuser_password, 

62 }, 

63 content_type="application/x-www-form-urlencoded", 

64 ) 

65 

66 yield client 

67 

68 

69def test_login(client): 

70 """Test logging in a user.""" 

71 attempt_user = { 

72 "email": shared.getenv("SUPERUSER_EMAIL"), 

73 "password": shared.getenv("SUPERUSER_PASSWORD"), 

74 } 

75 

76 response = client.post( 

77 "/user/login", 

78 data=attempt_user, 

79 content_type="application/x-www-form-urlencoded", 

80 ) 

81 

82 assert response.status_code == 200 

83 assert response.json["message"] == "/user/search" 

84 

85 

86def test_configure_settings(superuser_logged_in_client, database): 

87 """Test configuring settings as a superuser.""" 

88 current_config = database.get_all("config") 

89 database.delete_all("config") 

90 response = superuser_logged_in_client.get("/superuser/configure") 

91 assert response.status_code == 200 

92 

93 response = superuser_logged_in_client.post( 

94 "/superuser/configure", 

95 data={ 

96 "max_skills": 5, 

97 "min_num_ranking_student_to_opportunity": 3, 

98 }, 

99 content_type="application/x-www-form-urlencoded", 

100 ) 

101 assert response.status_code == 200 

102 assert response.json["message"] == "Settings updated successfully" 

103 

104 for entry in current_config: 

105 database.insert("config", entry) 

106 

107 

108def test_get_config_page(superuser_logged_in_client): 

109 """Test getting the configure settings page.""" 

110 response = superuser_logged_in_client.get("/superuser/configure") 

111 assert response.status_code == 200 

112 

113 

114def test_configure_settings_invalid_input(superuser_logged_in_client): 

115 """Test configuring settings with invalid input.""" 

116 response = superuser_logged_in_client.post( 

117 "/superuser/configure", 

118 data={ 

119 "max_skills": "invalid", 

120 "min_num_ranking_student_to_opportunity": "invalid", 

121 }, 

122 content_type="application/x-www-form-urlencoded", 

123 ) 

124 assert response.status_code == 400 

125 assert response.json["error"] == "Invalid input"