Skip to content

Commit

Permalink
Victors fine test
Browse files Browse the repository at this point in the history
pushet opp for victor
  • Loading branch information
Gustav Svartsund committed Feb 22, 2024
1 parent a817c01 commit 30c5bae
Showing 1 changed file with 36 additions and 2 deletions.
38 changes: 36 additions & 2 deletions test_main.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from fastapi.testclient import TestClient
from main import app
from unittest.mock import patch, MagicMock
from unittest.mock import patch, MagicMock, mock_open
import tempfile
import zipfile
from main import zip_files
from main import send_email_with_attachment
from pathlib import Path
import pytest
import os
Expand Down Expand Up @@ -115,4 +117,36 @@ def test_zip_files(temp_dir_with_files):

# Verify all expected files are in the zipped file
for expected_file in expected_files:
assert expected_file in zipped_files
assert expected_file in zipped_files

# Send email with attachment mock test

@patch('builtins.open', mock_open(read_data=b"data"))
@patch('os.path.exists', return_value=True)
@patch('main.SendGridAPIClient')
def test_send_email_with_attachment(mock_sendgrid_client, mock_exists):
# Mock the os.path.exists to always return True
mock_exists.return_value = True

# Mock SendGridAPIClient's behavior
mock_sendgrid_response = MagicMock()
mock_sendgrid_response.status_code = 202 # SendGrid returns 202 for successful sends
mock_sendgrid_client.return_value.send.return_value = mock_sendgrid_response

# Parameters for send_email_with_attachment
to_emails = "[email protected]"
subject = "Test Subject"
content = "Test Content"
attachment_path = "path/to/test_attachment.zip"

# Call the function to be tested
send_email_with_attachment(to_emails, subject, content, attachment_path)

# Assert: Check that os.path.exists was called with the attachment path
mock_exists.assert_called_once_with(attachment_path)

# Verify SendGridAPIClient was instantiated with the dummy API key
mock_sendgrid_client.assert_called_once()

# Verify the send method was called on the SendGrid client
assert mock_sendgrid_client.return_value.send.called

0 comments on commit 30c5bae

Please sign in to comment.