Coverage for tests/test_database_data.py: 100%

47 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-04-20 21:23 +0000

1import io 

2from app.models import Document 

3from app import db 

4from app.models import User 

5from werkzeug.datastructures import FileStorage 

6import os 

7 

8import pypdf 

9 

10 

11def login_user(client): 

12 test_user = User(username="testuser") 

13 test_user.set_password("password") 

14 db.session.add(test_user) 

15 db.session.commit() 

16 response = client.post( 

17 "/index", 

18 data={"username": "testuser", "password": "password"}, 

19 follow_redirects=True, 

20 ) 

21 assert response.status_code == 200 

22 

23 return client 

24 

25 

26def test_content_file(client, app): 

27 app.config["WTF_CSRF_ENABLED"] = ( 

28 False # In testing envionment, no need for CSRF_TOKEN 

29 ) 

30 with app.app_context(): 

31 with client: 

32 client = login_user(client) 

33 

34 # Path to the test file inside the test_data folder 

35 file_path = os.path.join(os.path.dirname(__file__), "test_data", "test.pdf") 

36 

37 # Open the file in binary read mode 

38 with open(file_path, "rb") as f: 

39 # Create a FileStorage object from the file content 

40 pdf_file = FileStorage( 

41 stream=io.BytesIO(f.read()), 

42 filename="test.pdf", 

43 content_type="application/pdf", 

44 ) 

45 

46 # Prepare the data dictionary with the file for upload 

47 data = { 

48 "pdf_file": (io.BytesIO(pdf_file.read()), "test.pdf"), 

49 } 

50 

51 # Post request testing the /uplouad route 

52 response = client.post( 

53 "/admin", data=data, content_type="multipart/form-data" 

54 ) 

55 

56 # Making sure that document was moved into database sucessfully 

57 assert response.status_code == 200 

58 

59 # Getting the information of the test.pdf and checking to see if the type is correct and the data 

60 

61 uploaded_file = Document.query.filter_by(document_name="test").first() 

62 assert uploaded_file.document_type == "pdf" 

63 

64 # Extracts text from a PDF given its binary content 

65 with io.BytesIO(uploaded_file.file_contents) as file: 

66 reader = pypdf.PdfReader(file) 

67 text = "" 

68 for page in reader.pages: 

69 

70 # Handle cases where extract_text() returns None 

71 text += page.extract_text() or "" 

72 extracted_text = text.strip() 

73 

74 assert extracted_text == "I like candy" 

75 

76 

77def test_validation(client, app): 

78 # Disable CSRF for testing purposes 

79 app.config["WTF_CSRF_ENABLED"] = False 

80 

81 with app.app_context(): 

82 test_user = User(username="testuser") 

83 test_user.set_password("password") 

84 db.session.add(test_user) 

85 db.session.commit() 

86 client.post("/index", data={"username": "testuser", "password": "password"}) 

87 # Testing for no document uploaded 

88 data = {} 

89 response = client.post("/admin", data=data, content_type="multipart/form-data") 

90 

91 # Making sure the error is being sent 

92 assert response.status_code == 400 

93 # Error message 

94 assert ( 

95 b"Invalid form data. Please ensure all fields are filled correctly." 

96 in response.data 

97 )