Coverage for tests/model_tests/test_superuser.py: 95%

65 statements  

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

1"""Tests for the superuser model.""" 

2 

3import os 

4import sys 

5from unittest import mock 

6from dotenv import load_dotenv 

7import pytest 

8 

9 

10# pylint: disable=redefined-outer-name 

11# flake8: noqa: F811 

12 

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

14load_dotenv() 

15sys.path.append( 

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

17) 

18from core import shared 

19from core.database_mongo_manager import DatabaseMongoManager 

20 

21 

22@pytest.fixture() 

23def app(): 

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

25 from app import app 

26 

27 app.config["TESTING"] = True 

28 return app 

29 

30 

31def database(): 

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

33 

34 database = DatabaseMongoManager( 

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

36 ) 

37 

38 yield database 

39 

40 # Cleanup code 

41 database.connection.close() 

42 

43 

44@pytest.fixture() 

45def data_model(): 

46 """Fixture to create a test data model.""" 

47 from superuser.model import Superuser 

48 

49 return Superuser() 

50 

51 

52def test_login_success(app, data_model): 

53 """Test logging in a user successfully.""" 

54 attempt_user = { 

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

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

57 } 

58 

59 with app.app_context(): 

60 with app.test_request_context(): 

61 response = data_model.login(attempt_user) 

62 

63 assert response[0].status_code == 200 

64 assert response[0].json == {"message": "/user/search"} 

65 

66 

67def test_login_missing_credentials(app, data_model): 

68 """Test logging in with missing credentials.""" 

69 attempt_user = {} 

70 

71 with app.app_context(): 

72 with app.test_request_context(): 

73 response = data_model.login(attempt_user) 

74 

75 assert response[1] == 400 

76 assert response[0].json == {"error": "Missing email or password"} 

77 

78 

79def test_login_invalid_credentials(app, data_model): 

80 """Test logging in with invalid credentials.""" 

81 attempt_user = { 

82 "email": "invalid@example.com", 

83 "password": "wrongpassword", 

84 } 

85 

86 with app.app_context(): 

87 with app.test_request_context(): 

88 response = data_model.login(attempt_user) 

89 

90 assert response[1] == 401 

91 assert response[0].json == {"error": "Invalid login credentials"} 

92 

93 

94def test_configure_settings_success(app, data_model): 

95 """Test configuring settings successfully.""" 

96 max_skills = 10 

97 min_num_ranking_student_to_opportunity = 5 

98 

99 with app.app_context(): 

100 with app.test_request_context(): 

101 with mock.patch("app.CONFIG_MANAGER") as mock_config_manager: 

102 response = data_model.configure_settings( 

103 max_skills, min_num_ranking_student_to_opportunity 

104 ) 

105 

106 assert response[1] == 200 

107 assert response[0].json == {"message": "Settings updated successfully"} 

108 mock_config_manager.set_num_of_skills.assert_called_once_with(max_skills) 

109 mock_config_manager.set_min_num_ranking_student_to_opportunities.assert_called_once_with( 

110 min_num_ranking_student_to_opportunity 

111 ) 

112 

113 

114def test_configure_settings_failure(app, data_model): 

115 """Test configuring settings with an exception.""" 

116 max_skills = 10 

117 min_num_ranking_student_to_opportunity = 5 

118 

119 with app.app_context(): 

120 with app.test_request_context(): 

121 with mock.patch("app.CONFIG_MANAGER") as mock_config_manager: 

122 mock_config_manager.set_num_of_skills.side_effect = Exception( 

123 "Test exception" 

124 ) 

125 response = data_model.configure_settings( 

126 max_skills, min_num_ranking_student_to_opportunity 

127 ) 

128 

129 assert response[1] == 500 

130 assert response[0].json == {"error": "Test exception"}