pelican-plugin-activitypub/pelican/plugins/activitypub/activitypub_test.py

62 lines
No EOL
3 KiB
Python

# SPDX-FileCopyrightText: 2024 Tobias Schmidl
#
# SPDX-License-Identifier: MIT
"""Unit tests for the ActivityPub plugin"""
import datetime
import unittest
from unittest.mock import MagicMock, patch
import os
import json
from activitypub import ap_article, ENCODING
class TestApArticle(unittest.TestCase):
"""Tests the ap_article function"""
@patch('os.makedirs')
@patch('builtins.open')
@patch('json.dump')
def test_ap_article(self, mock_json_dump, mock_open, mock_makedirs):
# Mock the generator and writer
generator = MagicMock()
writer = MagicMock()
# Mock settings
generator.settings = {
'AUTHOR': 'test_author',
'SITEURL': 'http://example.com',
'SITENAME': 'Test Site',
'ACTIVITYPUB_AUTHORS': {}
}
# Mock authors and articles
generator.authors = [('test_author', MagicMock(slug='test_author'))]
generator.articles = [MagicMock(slug='test_article', date=datetime.datetime.utcnow(), title='Test Article', content='Test Content', metadata={'tags': ['test_tag']}, author=MagicMock(slug='test_author'), url='test_article.html', translations=[])]
generator.tags = [MagicMock(slug='test_tag', name='test_tag')]
# Call the function
ap_article(generator, writer)
# Check if directories were created
mock_makedirs.assert_any_call(os.path.join(writer.output_path, '.well-known'), exist_ok=True)
mock_makedirs.assert_any_call(os.path.join(writer.output_path, 'activitypub/users'), exist_ok=True)
mock_makedirs.assert_any_call(os.path.join(writer.output_path, 'activitypub/posts'), exist_ok=True)
mock_makedirs.assert_any_call(os.path.join(writer.output_path, 'activitypub/tags'), exist_ok=True)
mock_makedirs.assert_any_call(os.path.join(writer.output_path, 'activitypub/collections/inbox'), exist_ok=True)
mock_makedirs.assert_any_call(os.path.join(writer.output_path, 'activitypub/collections/outbox'), exist_ok=True)
mock_makedirs.assert_any_call(os.path.join(writer.output_path, 'activitypub/collections/outbox_page', 'test_author'), exist_ok=True)
mock_makedirs.assert_any_call(os.path.join(writer.output_path, 'activitypub/collections/following'), exist_ok=True)
mock_makedirs.assert_any_call(os.path.join(writer.output_path, 'activitypub/collections/followers'), exist_ok=True)
# Check if files were written
mock_open.assert_any_call(os.path.join(writer.output_path, '.well-known/host-meta'), 'w', encoding=ENCODING)
mock_open.assert_any_call(os.path.join(writer.output_path, '.well-known/nodeinfo'), 'w', encoding=ENCODING)
mock_open.assert_any_call(os.path.join(writer.output_path, 'activitypub/nodeinfo'), 'w', encoding=ENCODING)
mock_open.assert_any_call(os.path.join(writer.output_path, '.well-known/webfinger'), 'w', encoding=ENCODING)
# Check if JSON data was dumped
self.assertTrue(mock_json_dump.called)
if __name__ == '__main__':
unittest.main()