epicyon/tests.py

7693 lines
307 KiB
Python
Raw Normal View History

2020-04-05 13:25:47 +00:00
__filename__ = "tests.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
2022-02-03 13:58:20 +00:00
__version__ = "1.3.0"
2020-04-05 13:25:47 +00:00
__maintainer__ = "Bob Mottram"
2021-09-10 16:14:50 +00:00
__email__ = "bob@libreserver.org"
2020-04-05 13:25:47 +00:00
__status__ = "Production"
2021-06-25 16:10:09 +00:00
__module_group__ = "Testing"
2020-04-05 13:25:47 +00:00
import base64
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.serialization import load_pem_private_key
from cryptography.hazmat.primitives.serialization import load_pem_public_key
from cryptography.hazmat.primitives.asymmetric import padding
from cryptography.hazmat.primitives.asymmetric import utils as hazutils
2019-06-30 20:14:03 +00:00
import time
2020-04-05 13:25:47 +00:00
import os
2019-06-30 21:20:02 +00:00
import shutil
2019-08-16 10:36:41 +00:00
import json
2021-05-09 19:11:05 +00:00
import datetime
2021-08-04 12:44:24 +00:00
from shutil import copyfile
2021-05-10 13:43:38 +00:00
from random import randint
2019-08-15 18:21:43 +00:00
from time import gmtime, strftime
2019-07-09 14:20:23 +00:00
from pprint import pprint
2021-12-29 21:55:09 +00:00
from httpsig import get_digest_algorithm_from_headers
from httpsig import get_digest_prefix
from httpsig import create_signed_header
from httpsig import sign_post_headers
from httpsig import sign_post_headers_new
from httpsig import verify_post_headers
from httpsig import message_content_digest
from cache import store_person_in_cache
from cache import get_person_from_cache
2021-12-28 21:36:27 +00:00
from threads import thread_with_trace
2021-12-29 21:55:09 +00:00
from daemon import run_daemon
2021-12-28 16:56:57 +00:00
from session import create_session
2021-12-29 21:55:09 +00:00
from session import get_json
from posts import get_actor_from_in_reply_to
from posts import regenerate_index_for_box
2021-12-28 19:33:29 +00:00
from posts import remove_post_interactions
2021-12-29 21:55:09 +00:00
from posts import get_mentioned_people
from posts import valid_content_warning
from posts import delete_all_posts
2021-12-28 19:33:29 +00:00
from posts import create_public_post
2021-12-29 21:55:09 +00:00
from posts import send_post
from posts import no_of_followers_on_domain
from posts import group_followers_by_domain
from posts import archive_posts_for_person
from posts import send_post_via_server
from posts import seconds_between_published
from follow import clear_follows
from follow import clear_followers
2022-03-13 12:15:52 +00:00
from follow import send_follow_request_via_server
2021-12-29 21:55:09 +00:00
from follow import send_unfollow_request_via_server
from siteactive import site_is_active
2022-09-25 17:26:11 +00:00
from utils import remove_inverted_text
2022-10-05 17:55:24 +00:00
from utils import remove_square_capitals
from utils import standardize_text
2022-06-21 11:58:50 +00:00
from utils import remove_eol
2022-06-10 11:43:33 +00:00
from utils import text_in_file
2022-02-25 21:00:53 +00:00
from utils import convert_published_to_local_timezone
2021-12-29 10:39:46 +00:00
from utils import convert_to_snake_case
2021-12-26 12:13:46 +00:00
from utils import get_sha_256
2021-12-27 21:44:48 +00:00
from utils import dangerous_svg
2021-12-28 12:15:46 +00:00
from utils import can_reply_to
2021-12-26 17:33:24 +00:00
from utils import is_group_account
2021-12-26 10:22:19 +00:00
from utils import get_actor_languages_list
2021-12-26 17:18:34 +00:00
from utils import get_category_types
2021-12-26 17:26:55 +00:00
from utils import get_supported_languages
2021-12-27 20:38:02 +00:00
from utils import set_config_param
2021-12-26 17:41:07 +00:00
from utils import is_group_actor
2021-12-26 18:01:02 +00:00
from utils import date_string_to_seconds
2021-12-26 17:55:38 +00:00
from utils import date_seconds_to_string
2021-12-26 18:05:54 +00:00
from utils import valid_password
2021-12-26 18:37:07 +00:00
from utils import user_agent_domain
2021-12-26 20:39:35 +00:00
from utils import camel_case_split
2021-12-26 12:21:31 +00:00
from utils import decoded_host
2021-12-26 12:45:03 +00:00
from utils import get_full_domain
2021-12-28 14:41:10 +00:00
from utils import valid_nickname
2021-12-27 15:52:08 +00:00
from utils import first_paragraph_from_string
2021-12-27 11:20:57 +00:00
from utils import remove_id_ending
2021-12-28 14:24:14 +00:00
from utils import update_recent_posts_cache
2021-12-27 17:08:19 +00:00
from utils import follow_person
2021-12-27 22:19:18 +00:00
from utils import get_nickname_from_actor
2021-12-27 19:05:25 +00:00
from utils import get_domain_from_actor
2019-09-29 18:48:34 +00:00
from utils import copytree
2021-12-26 15:13:34 +00:00
from utils import load_json
2021-12-26 14:47:21 +00:00
from utils import save_json
2021-12-27 17:42:35 +00:00
from utils import get_status_number
2022-01-13 15:10:41 +00:00
from utils import valid_hash_tag
2021-12-27 11:31:04 +00:00
from utils import get_followers_of_person
2021-12-27 15:43:22 +00:00
from utils import remove_html
2021-12-27 21:42:08 +00:00
from utils import dangerous_markup
2021-12-26 12:02:29 +00:00
from utils import acct_dir
2021-12-29 21:55:09 +00:00
from pgp import extract_pgp_public_key
from pgp import pgp_public_key_upload
2021-12-26 19:15:36 +00:00
from utils import contains_pgp_public_key
2021-12-29 21:55:09 +00:00
from follow import add_follower_of_person
2021-12-28 20:32:11 +00:00
from follow import unfollow_account
2021-12-29 21:55:09 +00:00
from follow import unfollower_of_account
2021-12-28 20:32:11 +00:00
from follow import send_follow_request
2021-12-29 21:55:09 +00:00
from person import create_person
from person import create_group
from person import set_display_nickname
from person import set_bio
# from person import generate_rsa_key
from skills import set_skill_level
2021-12-28 20:32:11 +00:00
from skills import actor_skill_value
2021-12-29 21:55:09 +00:00
from skills import set_skills_from_dict
2021-12-28 20:32:11 +00:00
from skills import actor_has_skill
2022-09-02 18:06:13 +00:00
from roles import actor_roles_from_list
2021-12-28 22:22:09 +00:00
from roles import set_role
2021-12-29 21:55:09 +00:00
from roles import actor_has_role
from auth import constant_time_string_check
2021-12-28 21:36:27 +00:00
from auth import create_basic_auth_header
from auth import authorize_basic
from auth import store_basic_credentials
2021-12-29 21:55:09 +00:00
from like import like_post
from like import send_like_via_server
from reaction import reaction_post
from reaction import send_reaction_via_server
from reaction import valid_emoji_content
from announce import announce_public
from announce import send_announce_via_server
from city import parse_nogo_string
from city import spoof_geolocation
from city import point_in_nogo
from media import get_image_dimensions
from media import get_media_path
from media import get_attachment_media_type
from delete import send_delete_via_server
from inbox import json_post_allows_comments
from inbox import valid_inbox
from inbox import valid_inbox_filenames
2022-05-26 15:14:48 +00:00
from inbox import cache_svg_images
2021-12-29 21:55:09 +00:00
from categories import guess_hashtag_category
from content import combine_textarea_lines
2022-07-05 12:30:21 +00:00
from content import detect_dogwhistles
from content import remove_script
2022-04-11 12:13:04 +00:00
from content import create_edits_html
2022-04-10 19:19:40 +00:00
from content import content_diff
2022-03-24 13:14:41 +00:00
from content import bold_reading_string
2022-01-14 10:20:37 +00:00
from content import safe_web_text
2021-12-29 21:55:09 +00:00
from content import words_similarity
from content import get_price_from_string
from content import limit_repeated_words
from content import switch_words
from content import extract_text_fields_in_post
from content import html_replace_email_quote
from content import html_replace_quote_marks
from content import dangerous_css
from content import add_web_links
from content import replace_emoji_from_tags
from content import add_html_tags
from content import remove_long_words
from content import replace_content_duplicates
from content import remove_text_formatting
2021-12-30 20:24:05 +00:00
from content import remove_html_tag
2022-05-18 16:06:26 +00:00
from theme import get_themes_list
2021-12-29 21:55:09 +00:00
from theme import update_default_themes_list
2022-01-26 23:17:53 +00:00
from theme import set_css_param
2021-12-29 21:55:09 +00:00
from theme import scan_themes_for_scripts
from linked_data_sig import generate_json_signature
from linked_data_sig import verify_json_signature
from newsdaemon import hashtag_rule_tree
from newsdaemon import hashtag_rule_resolve
2022-01-12 17:23:13 +00:00
from newswire import get_link_from_rss_item
from newswire import xml_podcast_to_dict
2021-12-29 21:55:09 +00:00
from newswire import get_newswire_tags
from newswire import parse_feed_date
from newswire import limit_word_lengths
from mastoapiv1 import get_masto_api_v1id_from_nickname
from mastoapiv1 import get_nickname_from_masto_api_v1id
from webapp_post import prepare_html_post_nickname
from speaker import speaker_replace_links
from markdown import markdown_to_html
from languages import set_actor_languages
from languages import get_actor_languages
from languages import get_links_from_content
from languages import add_links_to_content
2021-08-08 11:16:18 +00:00
from languages import libretranslate
2021-12-29 21:55:09 +00:00
from languages import libretranslate_languages
2021-12-28 21:36:27 +00:00
from shares import authorize_shared_items
2021-12-29 21:55:09 +00:00
from shares import generate_shared_item_federation_tokens
from shares import create_shared_item_federation_token
from shares import update_shared_item_federation_token
from shares import merge_shared_item_tokens
from shares import send_share_via_server
from shares import get_shared_items_catalog_via_server
2021-12-28 21:55:38 +00:00
from blocking import load_cw_lists
2021-12-30 10:16:57 +00:00
from blocking import add_cw_from_lists
2022-02-24 16:55:37 +00:00
from happening import dav_month_via_server
2022-02-24 17:23:41 +00:00
from happening import dav_day_via_server
2022-05-18 16:06:26 +00:00
from webapp_theme_designer import color_contrast
from maps import get_map_links_from_post_content
2022-08-22 20:44:14 +00:00
from maps import geocoords_from_map_link
2022-02-24 16:55:37 +00:00
2019-06-30 20:14:03 +00:00
2022-01-04 20:14:19 +00:00
TEST_SERVER_GROUP_RUNNING = False
TEST_SERVER_ALICE_RUNNING = False
TEST_SERVER_BOB_RUNNING = False
TEST_SERVER_EVE_RUNNING = False
THR_GROUP = None
THR_ALICE = None
THR_BOB = None
THR_EVE = None
2020-04-05 13:25:47 +00:00
2019-06-30 21:27:25 +00:00
2021-12-29 21:55:09 +00:00
def _test_http_signed_get(base_dir: str):
2022-01-04 20:14:19 +00:00
print('test_http_signed_get')
2021-12-25 17:09:22 +00:00
http_prefix = 'https'
2021-09-15 10:44:44 +00:00
debug = True
boxpath = "/users/Actor"
host = "epicyon.libreserver.org"
content_length = "0"
user_agent = "http.rb/4.4.1 (Mastodon/3.4.1; +https://octodon.social/)"
2022-01-04 20:14:19 +00:00
date_str = 'Wed, 01 Sep 2021 16:11:10 GMT'
2021-09-15 10:44:44 +00:00
accept_encoding = 'gzip'
accept = \
'application/activity+json, application/ld+json'
signature = \
'keyId="https://octodon.social/actor#main-key",' + \
'algorithm="rsa-sha256",' + \
'headers="(request-target) host date accept",' + \
'signature="Fe53PS9A2OSP4x+W/svhA' + \
'jUKHBvnAR73Ez+H32au7DQklLk08Lvm8al' + \
'LS7pCor28yfyx+DfZADgq6G1mLLRZo0OOn' + \
'PFSog7DhdcygLhBUMS0KlT5KVGwUS0tw' + \
'jdiHv4OC83RiCr/ZySBgOv65YLHYmGCi5B' + \
'IqSZJRkqi8+SLmLGESlNOEzKu+jIxOBY' + \
'mEEdIpNrDeE5YrFKpfTC3vS2GnxGOo5J/4' + \
'lB2h+dlUpso+sv5rDz1d1FsqRWK8waV7' + \
'4HUfLV+qbgYRceOTyZIi50vVqLvt9CTQes' + \
'KZHG3GrrPfaBuvoUbR4MCM3BUvpB7EzL' + \
'9F17Y+Ea9mo8zjqzZm8HaZQ=="'
2022-01-04 20:14:19 +00:00
public_key_pem = \
2021-09-15 10:44:44 +00:00
'-----BEGIN PUBLIC KEY-----\n' + \
'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMII' + \
'BCgKCAQEA1XT+ov/i4LDYuaXCwh4r\n' + \
'2rVfWtnz68wnFx3knwymwtRoAc/SFGzp9ye' + \
'5ogG1uPcbe7MeirZHhaBICynPlL32\n' + \
's9OYootI7MsQWn+vu7azxiXO7qcTPByvGcl' + \
'0vpLhtT/ApmlMintkRTVXdzBdJVM0\n' + \
'UsmYKg6U+IHNL+a1gURHGXep2Ih0BJMh4Aa' + \
'DbaID6jtpJZvbIkYgJ4IJucOe+A3T\n' + \
'YPMwkBA84ew+hso+vKQfTunyDInuPQbEzrA' + \
'zMJXEHS7IpBhdS4/cEox86BoDJ/q0\n' + \
'KOEOUpUDniFYWb9k1+9B387OviRDLIcLxNZ' + \
'nf+bNq8d+CwEXY2xGsToBle/q74d8\n' + \
'BwIDAQAB\n' + \
'-----END PUBLIC KEY-----\n'
headers = {
"user-agent": user_agent,
"content-length": content_length,
"host": host,
2022-01-04 20:14:19 +00:00
"date": date_str,
2021-09-15 10:44:44 +00:00
"accept": accept,
"accept-encoding": accept_encoding,
"signature": signature
}
2022-01-04 20:14:19 +00:00
getreq_method = True
message_body_digest = None
message_body_json_str = ''
no_recency_check = True
assert verify_post_headers(http_prefix, public_key_pem, headers,
boxpath, getreq_method, message_body_digest,
message_body_json_str, debug, no_recency_check)
2021-09-15 10:44:44 +00:00
# Change a single character and the signature should fail
headers['date'] = headers['date'].replace(':10', ':11')
2022-01-04 20:14:19 +00:00
assert not verify_post_headers(http_prefix, public_key_pem, headers,
boxpath, getreq_method, message_body_digest,
message_body_json_str, debug,
no_recency_check)
2021-09-15 10:44:44 +00:00
2021-12-25 16:17:53 +00:00
path = base_dir + '/.testHttpsigGET'
2021-09-15 10:44:44 +00:00
if os.path.isdir(path):
2021-10-29 18:48:15 +00:00
shutil.rmtree(path, ignore_errors=False, onerror=None)
2021-09-15 10:44:44 +00:00
os.mkdir(path)
os.chdir(path)
nickname = 'testactor'
2022-01-04 20:14:19 +00:00
host_domain = 'someother.instance'
2021-09-15 10:44:44 +00:00
domain = 'argumentative.social'
2021-12-25 17:09:22 +00:00
http_prefix = 'https'
2021-09-15 10:44:44 +00:00
port = 443
2022-01-04 20:14:19 +00:00
with_digest = False
2021-09-15 10:44:44 +00:00
password = 'SuperSecretPassword'
2022-01-04 20:14:19 +00:00
no_recency_check = True
private_key_pem, public_key_pem, _, _ = \
2021-12-29 21:55:09 +00:00
create_person(path, nickname, domain, port, http_prefix,
False, False, password)
2022-01-04 20:14:19 +00:00
assert private_key_pem
assert public_key_pem
message_body_json_str = ''
2021-09-15 10:44:44 +00:00
2022-01-04 20:14:19 +00:00
headers_domain = get_full_domain(host_domain, port)
2021-09-15 10:44:44 +00:00
2022-01-04 20:14:19 +00:00
date_str = 'Tue, 14 Sep 2021 16:19:00 GMT'
2021-09-15 10:44:44 +00:00
boxpath = '/inbox'
accept = 'application/json'
# accept = 'application/activity+json'
headers = {
2022-02-03 13:58:20 +00:00
'user-agent': 'Epicyon/1.3.0; +https://' + domain + '/',
2022-01-04 20:14:19 +00:00
'host': headers_domain,
'date': date_str,
2021-09-15 10:44:44 +00:00
'accept': accept,
'content-length': 0
}
2022-01-04 20:14:19 +00:00
signature_header = \
create_signed_header(date_str,
private_key_pem, nickname,
domain, port,
host_domain, port,
boxpath, http_prefix, False,
None, accept)
headers['signature'] = signature_header['signature']
getreq_method = not with_digest
assert verify_post_headers(http_prefix, public_key_pem, headers,
boxpath, getreq_method, None,
message_body_json_str, debug, no_recency_check)
2021-09-15 10:44:44 +00:00
if os.path.isdir(path):
2021-10-29 18:48:15 +00:00
shutil.rmtree(path, ignore_errors=False, onerror=None)
2021-09-15 10:44:44 +00:00
2021-12-29 21:55:09 +00:00
def _test_sign_and_verify() -> None:
2022-01-04 20:14:19 +00:00
print('test_sign_and_verify')
public_key_pem = \
'-----BEGIN RSA PUBLIC KEY-----\n' + \
'MIIBCgKCAQEAhAKYdtoeoy8zcAcR874L8' + \
'cnZxKzAGwd7v36APp7Pv6Q2jdsPBRrw\n' + \
'WEBnez6d0UDKDwGbc6nxfEXAy5mbhgajz' + \
'rw3MOEt8uA5txSKobBpKDeBLOsdJKFq\n' + \
'MGmXCQvEG7YemcxDTRPxAleIAgYYRjTSd' + \
'/QBwVW9OwNFhekro3RtlinV0a75jfZg\n' + \
'kne/YiktSvLG34lw2zqXBDTC5NHROUqGT' + \
'lML4PlNZS5Ri2U4aCNx2rUPRcKIlE0P\n' + \
'uKxI4T+HIaFpv8+rdV6eUgOrB2xeI1dSF' + \
'Fn/nnv5OoZJEIB+VmuKn3DCUcCZSFlQ\n' + \
'PSXSfBDiUGhwOw76WuSSsf1D4b/vLoJ10wIDAQAB\n' + \
'-----END RSA PUBLIC KEY-----\n'
2022-01-04 20:14:19 +00:00
private_key_pem = \
'-----BEGIN RSA PRIVATE KEY-----\n' + \
'MIIEqAIBAAKCAQEAhAKYdtoeoy8zcAcR8' + \
'74L8cnZxKzAGwd7v36APp7Pv6Q2jdsP\n' + \
'BRrwWEBnez6d0UDKDwGbc6nxfEXAy5mbh' + \
'gajzrw3MOEt8uA5txSKobBpKDeBLOsd\n' + \
'JKFqMGmXCQvEG7YemcxDTRPxAleIAgYYR' + \
'jTSd/QBwVW9OwNFhekro3RtlinV0a75\n' + \
'jfZgkne/YiktSvLG34lw2zqXBDTC5NHRO' + \
'UqGTlML4PlNZS5Ri2U4aCNx2rUPRcKI\n' + \
'lE0PuKxI4T+HIaFpv8+rdV6eUgOrB2xeI' + \
'1dSFFn/nnv5OoZJEIB+VmuKn3DCUcCZ\n' + \
'SFlQPSXSfBDiUGhwOw76WuSSsf1D4b/vL' + \
'oJ10wIDAQABAoIBAG/JZuSWdoVHbi56\n' + \
'vjgCgkjg3lkO1KrO3nrdm6nrgA9P9qaPj' + \
'xuKoWaKO1cBQlE1pSWp/cKncYgD5WxE\n' + \
'CpAnRUXG2pG4zdkzCYzAh1i+c34L6oZoH' + \
'sirK6oNcEnHveydfzJL5934egm6p8DW\n' + \
'+m1RQ70yUt4uRc0YSor+q1LGJvGQHReF0' + \
'WmJBZHrhz5e63Pq7lE0gIwuBqL8SMaA\n' + \
'yRXtK+JGxZpImTq+NHvEWWCu09SCq0r83' + \
'8ceQI55SvzmTkwqtC+8AT2zFviMZkKR\n' + \
'Qo6SPsrqItxZWRty2izawTF0Bf5S2VAx7' + \
'O+6t3wBsQ1sLptoSgX3QblELY5asI0J\n' + \
'YFz7LJECgYkAsqeUJmqXE3LP8tYoIjMIA' + \
'KiTm9o6psPlc8CrLI9CH0UbuaA2JCOM\n' + \
'cCNq8SyYbTqgnWlB9ZfcAm/cFpA8tYci9' + \
'm5vYK8HNxQr+8FS3Qo8N9RJ8d0U5Csw\n' + \
'DzMYfRghAfUGwmlWj5hp1pQzAuhwbOXFt' + \
'xKHVsMPhz1IBtF9Y8jvgqgYHLbmyiu1\n' + \
'mwJ5AL0pYF0G7x81prlARURwHo0Yf52kE' + \
'w1dxpx+JXER7hQRWQki5/NsUEtv+8RT\n' + \
'qn2m6qte5DXLyn83b1qRscSdnCCwKtKWU' + \
'ug5q2ZbwVOCJCtmRwmnP131lWRYfj67\n' + \
'B/xJ1ZA6X3GEf4sNReNAtaucPEelgR2ns' + \
'N0gKQKBiGoqHWbK1qYvBxX2X3kbPDkv\n' + \
'9C+celgZd2PW7aGYLCHq7nPbmfDV0yHcW' + \
'jOhXZ8jRMjmANVR/eLQ2EfsRLdW69bn\n' + \
'f3ZD7JS1fwGnO3exGmHO3HZG+6AvberKY' + \
'VYNHahNFEw5TsAcQWDLRpkGybBcxqZo\n' + \
'81YCqlqidwfeO5YtlO7etx1xLyqa2NsCe' + \
'G9A86UjG+aeNnXEIDk1PDK+EuiThIUa\n' + \
'/2IxKzJKWl1BKr2d4xAfR0ZnEYuRrbeDQ' + \
'YgTImOlfW6/GuYIxKYgEKCFHFqJATAG\n' + \
'IxHrq1PDOiSwXd2GmVVYyEmhZnbcp8Cxa' + \
'EMQoevxAta0ssMK3w6UsDtvUvYvF22m\n' + \
'qQKBiD5GwESzsFPy3Ga0MvZpn3D6EJQLg' + \
'snrtUPZx+z2Ep2x0xc5orneB5fGyF1P\n' + \
'WtP+fG5Q6Dpdz3LRfm+KwBCWFKQjg7uTx' + \
'cjerhBWEYPmEMKYwTJF5PBG9/ddvHLQ\n' + \
'EQeNC8fHGg4UXU8mhHnSBt3EA10qQJfRD' + \
's15M38eG2cYwB1PZpDHScDnDA0=\n' + \
'-----END RSA PRIVATE KEY-----'
# sign
2022-01-04 20:14:19 +00:00
signed_header_text = \
'(request-target): get /actor\n' + \
'host: octodon.social\n' + \
'date: Tue, 14 Sep 2021 16:19:00 GMT\n' + \
'accept: application/json'
2022-01-04 20:14:19 +00:00
header_digest = get_sha_256(signed_header_text.encode('ascii'))
key = load_pem_private_key(private_key_pem.encode('utf-8'),
None, backend=default_backend())
2022-01-04 20:14:19 +00:00
raw_signature = key.sign(header_digest,
padding.PKCS1v15(),
hazutils.Prehashed(hashes.SHA256()))
signature1 = base64.b64encode(raw_signature).decode('ascii')
# verify
2022-01-04 20:14:19 +00:00
padding_str = padding.PKCS1v15()
alg = hazutils.Prehashed(hashes.SHA256())
2022-01-04 20:14:19 +00:00
pubkey = load_pem_public_key(public_key_pem.encode('utf-8'),
backend=default_backend())
signature2 = base64.b64decode(signature1)
2022-01-04 20:14:19 +00:00
pubkey.verify(signature2, header_digest, padding_str, alg)
2022-01-04 20:14:19 +00:00
def _test_http_sig_new(algorithm: str, digest_algorithm: str):
print('test_http_sig_new')
2021-12-25 17:09:22 +00:00
http_prefix = 'https'
port = 443
debug = True
2022-01-04 20:14:19 +00:00
message_body_json = {"hello": "world"}
message_body_json_str = json.dumps(message_body_json)
nickname = 'foo'
2022-01-04 20:14:19 +00:00
path_str = "/" + nickname + "?param=value&pet=dog HTTP/1.1"
domain = 'example.com'
2022-01-04 20:14:19 +00:00
date_str = 'Tue, 20 Apr 2021 02:07:55 GMT'
digest_prefix = get_digest_prefix(digest_algorithm)
digest_str = \
digest_prefix + '=X48E9qOokqqrvdts8nOJRJN3OWDUoyWxBf7kbu9DBPE='
body_digest = \
message_content_digest(message_body_json_str, digest_algorithm)
assert body_digest in digest_str
content_length = 18
2021-12-26 15:32:00 +00:00
content_type = 'application/activity+json'
2022-01-04 20:14:19 +00:00
public_key_pem = \
'-----BEGIN RSA PUBLIC KEY-----\n' + \
'MIIBCgKCAQEAhAKYdtoeoy8zcAcR874L8' + \
'cnZxKzAGwd7v36APp7Pv6Q2jdsPBRrw\n' + \
'WEBnez6d0UDKDwGbc6nxfEXAy5mbhgajz' + \
'rw3MOEt8uA5txSKobBpKDeBLOsdJKFq\n' + \
'MGmXCQvEG7YemcxDTRPxAleIAgYYRjTSd' + \
'/QBwVW9OwNFhekro3RtlinV0a75jfZg\n' + \
'kne/YiktSvLG34lw2zqXBDTC5NHROUqGT' + \
'lML4PlNZS5Ri2U4aCNx2rUPRcKIlE0P\n' + \
'uKxI4T+HIaFpv8+rdV6eUgOrB2xeI1dSF' + \
'Fn/nnv5OoZJEIB+VmuKn3DCUcCZSFlQ\n' + \
'PSXSfBDiUGhwOw76WuSSsf1D4b/vLoJ10wIDAQAB\n' + \
'-----END RSA PUBLIC KEY-----\n'
2022-01-04 20:14:19 +00:00
private_key_pem = \
'-----BEGIN RSA PRIVATE KEY-----\n' + \
'MIIEqAIBAAKCAQEAhAKYdtoeoy8zcAcR8' + \
'74L8cnZxKzAGwd7v36APp7Pv6Q2jdsP\n' + \
'BRrwWEBnez6d0UDKDwGbc6nxfEXAy5mbh' + \
'gajzrw3MOEt8uA5txSKobBpKDeBLOsd\n' + \
'JKFqMGmXCQvEG7YemcxDTRPxAleIAgYYR' + \
'jTSd/QBwVW9OwNFhekro3RtlinV0a75\n' + \
'jfZgkne/YiktSvLG34lw2zqXBDTC5NHRO' + \
'UqGTlML4PlNZS5Ri2U4aCNx2rUPRcKI\n' + \
'lE0PuKxI4T+HIaFpv8+rdV6eUgOrB2xeI' + \
'1dSFFn/nnv5OoZJEIB+VmuKn3DCUcCZ\n' + \
'SFlQPSXSfBDiUGhwOw76WuSSsf1D4b/vL' + \
'oJ10wIDAQABAoIBAG/JZuSWdoVHbi56\n' + \
'vjgCgkjg3lkO1KrO3nrdm6nrgA9P9qaPj' + \
'xuKoWaKO1cBQlE1pSWp/cKncYgD5WxE\n' + \
'CpAnRUXG2pG4zdkzCYzAh1i+c34L6oZoH' + \
'sirK6oNcEnHveydfzJL5934egm6p8DW\n' + \
'+m1RQ70yUt4uRc0YSor+q1LGJvGQHReF0' + \
'WmJBZHrhz5e63Pq7lE0gIwuBqL8SMaA\n' + \
'yRXtK+JGxZpImTq+NHvEWWCu09SCq0r83' + \
'8ceQI55SvzmTkwqtC+8AT2zFviMZkKR\n' + \
'Qo6SPsrqItxZWRty2izawTF0Bf5S2VAx7' + \
'O+6t3wBsQ1sLptoSgX3QblELY5asI0J\n' + \
'YFz7LJECgYkAsqeUJmqXE3LP8tYoIjMIA' + \
'KiTm9o6psPlc8CrLI9CH0UbuaA2JCOM\n' + \
'cCNq8SyYbTqgnWlB9ZfcAm/cFpA8tYci9' + \
'm5vYK8HNxQr+8FS3Qo8N9RJ8d0U5Csw\n' + \
'DzMYfRghAfUGwmlWj5hp1pQzAuhwbOXFt' + \
'xKHVsMPhz1IBtF9Y8jvgqgYHLbmyiu1\n' + \
'mwJ5AL0pYF0G7x81prlARURwHo0Yf52kE' + \
'w1dxpx+JXER7hQRWQki5/NsUEtv+8RT\n' + \
'qn2m6qte5DXLyn83b1qRscSdnCCwKtKWU' + \
'ug5q2ZbwVOCJCtmRwmnP131lWRYfj67\n' + \
'B/xJ1ZA6X3GEf4sNReNAtaucPEelgR2ns' + \
'N0gKQKBiGoqHWbK1qYvBxX2X3kbPDkv\n' + \
'9C+celgZd2PW7aGYLCHq7nPbmfDV0yHcW' + \
'jOhXZ8jRMjmANVR/eLQ2EfsRLdW69bn\n' + \
'f3ZD7JS1fwGnO3exGmHO3HZG+6AvberKY' + \
'VYNHahNFEw5TsAcQWDLRpkGybBcxqZo\n' + \
'81YCqlqidwfeO5YtlO7etx1xLyqa2NsCe' + \
'G9A86UjG+aeNnXEIDk1PDK+EuiThIUa\n' + \
'/2IxKzJKWl1BKr2d4xAfR0ZnEYuRrbeDQ' + \
'YgTImOlfW6/GuYIxKYgEKCFHFqJATAG\n' + \
'IxHrq1PDOiSwXd2GmVVYyEmhZnbcp8Cxa' + \
'EMQoevxAta0ssMK3w6UsDtvUvYvF22m\n' + \
'qQKBiD5GwESzsFPy3Ga0MvZpn3D6EJQLg' + \
'snrtUPZx+z2Ep2x0xc5orneB5fGyF1P\n' + \
'WtP+fG5Q6Dpdz3LRfm+KwBCWFKQjg7uTx' + \
'cjerhBWEYPmEMKYwTJF5PBG9/ddvHLQ\n' + \
'EQeNC8fHGg4UXU8mhHnSBt3EA10qQJfRD' + \
's15M38eG2cYwB1PZpDHScDnDA0=\n' + \
'-----END RSA PRIVATE KEY-----'
headers = {
"host": domain,
2022-01-04 20:14:19 +00:00
"date": date_str,
"digest": f'{digest_prefix}={body_digest}',
2021-12-26 15:32:00 +00:00
"content-type": content_type,
2022-01-04 20:14:19 +00:00
"content-length": str(content_length)
}
2022-01-04 20:14:19 +00:00
signature_index_header, signature_header = \
sign_post_headers_new(date_str, private_key_pem, nickname,
2021-12-29 21:55:09 +00:00
domain, port,
domain, port,
2022-01-04 20:14:19 +00:00
path_str, http_prefix, message_body_json_str,
algorithm, digest_algorithm, debug)
print('signature_index_header1: ' + str(signature_index_header))
print('signature_header1: ' + str(signature_header))
sig_input = "keyId=\"https://example.com/users/foo#main-key\"; " + \
"alg=hs2019; created=1618884475; " + \
"sig1=(@request-target, @created, host, date, digest, " + \
"content-type, content-length)"
2022-01-04 20:14:19 +00:00
assert signature_index_header == sig_input
sig = "sig1=:NXAQ7AtDMR2iwhmH1qCwiZw5PVTjOw5+5kSu0Tsx/3gqz0D" + \
"py7OQbWqFHrNB7MmS4TukX/vDyQOFdElY5yxnEhbgRwKACq0AP4QH9H" + \
"CiRyCE8UXDdAkY4VUd6jrWjRHKRoqQN7I+Q5tb2Fu5cDfifw/PQc86Z" + \
"NmMhPrg3OjUJ9Q2Gj29NhgJ+4el1ECg0cAy4yG1M9AQ3KvQooQFvlg1" + \
"vp0H2xfbJQjv8FsR/lKiRdaVHqGR2CKrvxvPRPaOsFANp2wzEtiMk3O" + \
"TrBTYU+Zb53mIspfEeLxsNtcGmBDmQKZ9Pud8f99XGJrP+uDd3zKtnr" + \
"f3fUnRRqy37yhB7WVwkg==:"
2022-01-04 20:14:19 +00:00
assert signature_header == sig
debug = True
2022-01-04 20:14:19 +00:00
headers['path'] = path_str
headers['signature'] = sig
2022-01-04 20:14:19 +00:00
headers['signature-input'] = sig_input
assert verify_post_headers(http_prefix, public_key_pem, headers,
path_str, False, None,
message_body_json_str, debug, True)
# make a deliberate mistake
debug = False
headers['signature'] = headers['signature'].replace('V', 'B')
2022-01-04 20:14:19 +00:00
assert not verify_post_headers(http_prefix, public_key_pem, headers,
path_str, False, None,
message_body_json_str, debug, True)
2022-01-04 20:14:19 +00:00
def _test_httpsig_base(with_digest: bool, base_dir: str):
print('test_httpsig(' + str(with_digest) + ')')
2019-08-09 09:46:33 +00:00
2021-12-25 16:17:53 +00:00
path = base_dir + '/.testHttpsigBase'
2019-08-09 09:46:33 +00:00
if os.path.isdir(path):
2021-10-29 18:48:15 +00:00
shutil.rmtree(path, ignore_errors=False, onerror=None)
2019-08-09 09:46:33 +00:00
os.mkdir(path)
os.chdir(path)
2021-11-23 11:41:40 +00:00
algorithm = 'rsa-sha256'
2022-01-04 20:14:19 +00:00
digest_algorithm = 'rsa-sha256'
2021-12-26 15:32:00 +00:00
content_type = 'application/activity+json'
2020-04-05 13:25:47 +00:00
nickname = 'socrates'
2022-01-04 20:14:19 +00:00
host_domain = 'someother.instance'
2020-04-05 13:25:47 +00:00
domain = 'argumentative.social'
2021-12-25 17:09:22 +00:00
http_prefix = 'https'
2020-04-05 13:25:47 +00:00
port = 5576
password = 'SuperSecretPassword'
2022-01-04 20:14:19 +00:00
private_key_pem, public_key_pem, person, wf_endpoint = \
2021-12-29 21:55:09 +00:00
create_person(path, nickname, domain, port, http_prefix,
False, False, password)
2022-01-04 20:14:19 +00:00
assert private_key_pem
assert public_key_pem
assert person
assert wf_endpoint
if with_digest:
message_body_json = {
2021-09-15 10:44:44 +00:00
"a key": "a value",
"another key": "A string",
"yet another key": "Another string"
}
2022-01-04 20:14:19 +00:00
message_body_json_str = json.dumps(message_body_json)
2021-09-15 10:44:44 +00:00
else:
2022-01-04 20:14:19 +00:00
message_body_json_str = ''
2019-07-01 09:31:02 +00:00
2022-01-04 20:14:19 +00:00
headers_domain = get_full_domain(host_domain, port)
2019-07-01 09:31:02 +00:00
2022-01-04 20:14:19 +00:00
date_str = strftime("%a, %d %b %Y %H:%M:%S %Z", gmtime())
2020-04-05 13:25:47 +00:00
boxpath = '/inbox'
2022-01-04 20:14:19 +00:00
if not with_digest:
2020-04-05 13:25:47 +00:00
headers = {
2022-01-04 20:14:19 +00:00
'host': headers_domain,
'date': date_str,
2021-12-26 15:32:00 +00:00
'accept': content_type
2020-03-22 20:36:19 +00:00
}
2022-01-04 20:14:19 +00:00
signature_header = \
sign_post_headers(date_str, private_key_pem, nickname,
2021-12-29 21:55:09 +00:00
domain, port,
2022-01-04 20:14:19 +00:00
host_domain, port,
2021-12-29 21:55:09 +00:00
boxpath, http_prefix, None, content_type,
algorithm, None)
2019-06-30 20:14:03 +00:00
else:
2022-01-04 20:14:19 +00:00
digest_prefix = get_digest_prefix(digest_algorithm)
body_digest = \
message_content_digest(message_body_json_str, digest_algorithm)
content_length = len(message_body_json_str)
2020-04-05 13:25:47 +00:00
headers = {
2022-01-04 20:14:19 +00:00
'host': headers_domain,
'date': date_str,
'digest': f'{digest_prefix}={body_digest}',
2021-12-26 15:32:00 +00:00
'content-type': content_type,
2022-01-04 20:14:19 +00:00
'content-length': str(content_length)
2020-03-22 20:36:19 +00:00
}
2022-01-04 20:14:19 +00:00
assert get_digest_algorithm_from_headers(headers) == digest_algorithm
signature_header = \
sign_post_headers(date_str, private_key_pem, nickname,
2021-12-29 21:55:09 +00:00
domain, port,
2022-01-04 20:14:19 +00:00
host_domain, port,
boxpath, http_prefix, message_body_json_str,
content_type, algorithm, digest_algorithm)
2019-07-01 09:31:02 +00:00
2022-01-04 20:14:19 +00:00
headers['signature'] = signature_header
getreq_method = not with_digest
2021-11-22 11:52:55 +00:00
debug = True
2022-01-04 20:14:19 +00:00
assert verify_post_headers(http_prefix, public_key_pem, headers,
boxpath, getreq_method, None,
message_body_json_str, debug)
if with_digest:
2019-11-12 18:21:52 +00:00
# everything correct except for content-length
2022-01-04 20:14:19 +00:00
headers['content-length'] = str(content_length + 2)
assert verify_post_headers(http_prefix, public_key_pem, headers,
boxpath, getreq_method, None,
message_body_json_str, False) is False
assert verify_post_headers(http_prefix, public_key_pem, headers,
'/parambulator' + boxpath, getreq_method, None,
message_body_json_str, False) is False
assert verify_post_headers(http_prefix, public_key_pem, headers,
boxpath, not getreq_method, None,
message_body_json_str, False) is False
if not with_digest:
2019-06-30 20:14:03 +00:00
# fake domain
2020-04-05 13:25:47 +00:00
headers = {
2020-03-22 20:36:19 +00:00
'host': 'bogon.domain',
2022-01-04 20:14:19 +00:00
'date': date_str,
2021-12-26 15:32:00 +00:00
'content-type': content_type
2020-03-22 20:36:19 +00:00
}
2019-06-30 20:14:03 +00:00
else:
# correct domain but fake message
2022-01-04 20:14:19 +00:00
message_body_json_str = \
2020-04-05 13:25:47 +00:00
'{"a key": "a value", "another key": "Fake GNUs", ' + \
'"yet another key": "More Fake GNUs"}'
2022-01-04 20:14:19 +00:00
content_length = len(message_body_json_str)
digest_prefix = get_digest_prefix(digest_algorithm)
body_digest = \
message_content_digest(message_body_json_str, digest_algorithm)
2020-04-05 13:25:47 +00:00
headers = {
2020-03-22 20:36:19 +00:00
'host': domain,
2022-01-04 20:14:19 +00:00
'date': date_str,
'digest': f'{digest_prefix}={body_digest}',
2021-12-26 15:32:00 +00:00
'content-type': content_type,
2022-01-04 20:14:19 +00:00
'content-length': str(content_length)
2020-03-22 20:36:19 +00:00
}
2022-01-04 20:14:19 +00:00
assert get_digest_algorithm_from_headers(headers) == digest_algorithm
headers['signature'] = signature_header
assert verify_post_headers(http_prefix, public_key_pem, headers,
boxpath, not getreq_method, None,
message_body_json_str, False) is False
2019-11-12 18:21:52 +00:00
2021-12-25 16:17:53 +00:00
os.chdir(base_dir)
2021-10-29 18:48:15 +00:00
shutil.rmtree(path, ignore_errors=False, onerror=None)
2019-06-30 20:14:03 +00:00
2020-04-05 13:25:47 +00:00
2021-12-29 21:55:09 +00:00
def _test_httpsig(base_dir: str):
_test_httpsig_base(True, base_dir)
_test_httpsig_base(False, base_dir)
2019-06-30 20:14:03 +00:00
2020-04-05 13:25:47 +00:00
2021-12-29 21:55:09 +00:00
def _test_cache():
2022-01-04 20:14:19 +00:00
print('test_cache')
person_url = "cat@cardboard.box"
person_json = {
2020-04-05 13:25:47 +00:00
"id": 123456,
"test": "This is a test"
}
2021-12-25 22:17:49 +00:00
person_cache = {}
2022-01-04 20:14:19 +00:00
store_person_in_cache(None, person_url, person_json, person_cache, True)
2022-06-09 16:54:44 +00:00
result = get_person_from_cache(None, person_url, person_cache)
2020-04-05 13:25:47 +00:00
assert result['id'] == 123456
assert result['test'] == 'This is a test'
2019-06-30 20:14:03 +00:00
def _test_threads_function(param1: str, param2: str):
2022-01-04 20:14:19 +00:00
for _ in range(10000):
2019-06-30 20:14:03 +00:00
time.sleep(2)
2020-04-05 13:25:47 +00:00
2021-12-29 21:55:09 +00:00
def _test_threads():
2022-01-04 20:14:19 +00:00
print('test_threads')
2020-04-05 13:25:47 +00:00
thr = \
2021-12-29 21:55:09 +00:00
thread_with_trace(target=_test_threads_function,
args=('test', 'test2'),
2021-12-28 21:36:27 +00:00
daemon=True)
2019-06-30 20:14:03 +00:00
thr.start()
2020-12-18 15:29:12 +00:00
assert thr.is_alive() is True
2019-06-30 20:14:03 +00:00
time.sleep(1)
thr.kill()
thr.join()
2020-12-18 15:29:12 +00:00
assert thr.is_alive() is False
2019-06-30 21:20:02 +00:00
2020-04-05 13:25:47 +00:00
2021-12-29 21:55:09 +00:00
def create_server_alice(path: str, domain: str, port: int,
2022-01-04 20:14:19 +00:00
bob_address: str, federation_list: [],
2022-06-08 20:20:27 +00:00
has_follows: bool, has_posts: bool,
2021-12-29 21:55:09 +00:00
send_threads: []):
2020-04-05 13:25:47 +00:00
print('Creating test server: Alice on port ' + str(port))
2019-06-30 21:20:02 +00:00
if os.path.isdir(path):
2021-10-29 18:48:15 +00:00
shutil.rmtree(path, ignore_errors=False, onerror=None)
2019-06-30 21:20:02 +00:00
os.mkdir(path)
os.chdir(path)
2021-12-25 18:05:01 +00:00
shared_items_federated_domains = []
2021-12-25 23:03:28 +00:00
system_language = 'en'
languages_understood = [system_language]
2020-04-05 13:25:47 +00:00
nickname = 'alice'
2021-12-25 17:09:22 +00:00
http_prefix = 'http'
2021-12-25 21:09:22 +00:00
proxy_type = None
2020-04-05 13:25:47 +00:00
password = 'alicepass'
2021-12-25 21:11:35 +00:00
max_replies = 64
2021-12-25 21:13:55 +00:00
domain_max_posts_per_day = 1000
2021-12-25 21:18:07 +00:00
account_max_posts_per_day = 1000
2021-12-25 21:29:53 +00:00
allow_deletion = True
2021-12-25 18:20:56 +00:00
low_bandwidth = True
2022-01-04 20:14:19 +00:00
private_key_pem, public_key_pem, person, wf_endpoint = \
2021-12-29 21:55:09 +00:00
create_person(path, nickname, domain, port, http_prefix, True,
False, password)
2022-01-04 20:14:19 +00:00
assert private_key_pem
assert public_key_pem
assert person
assert wf_endpoint
2021-12-29 21:55:09 +00:00
delete_all_posts(path, nickname, domain, 'inbox')
delete_all_posts(path, nickname, domain, 'outbox')
assert set_skill_level(path, nickname, domain, 'hacking', 90)
2021-12-28 22:22:09 +00:00
assert set_role(path, nickname, domain, 'guru')
2022-06-08 20:20:27 +00:00
if has_follows:
2022-01-04 20:14:19 +00:00
follow_person(path, nickname, domain, 'bob', bob_address,
2021-12-27 17:08:19 +00:00
federation_list, False, False)
2022-01-04 20:14:19 +00:00
add_follower_of_person(path, nickname, domain, 'bob', bob_address,
2021-12-29 21:55:09 +00:00
federation_list, False, False)
2022-06-08 20:20:27 +00:00
if has_posts:
2022-01-04 20:14:19 +00:00
test_save_to_file = True
2021-12-25 20:39:35 +00:00
client_to_server = False
2022-01-04 20:14:19 +00:00
test_comments_enabled = True
test_attach_image_filename = None
test_media_type = None
test_image_description = None
test_city = 'London, England'
test_in_reply_to = None
test_in_reply_to_atom_uri = None
test_subject = None
test_schedule_post = False
test_event_date = None
test_event_time = None
2022-05-23 12:14:36 +00:00
test_event_end_time = None
2022-01-04 20:14:19 +00:00
test_location = None
test_is_article = False
conversation_id = None
2022-07-18 16:18:04 +00:00
translate = {}
2021-12-25 17:13:38 +00:00
content_license_url = 'https://creativecommons.org/licenses/by/4.0'
2021-12-28 19:33:29 +00:00
create_public_post(path, nickname, domain, port, http_prefix,
"No wise fish would go anywhere without a porpoise",
2022-01-04 20:14:19 +00:00
test_save_to_file,
2021-12-28 19:33:29 +00:00
client_to_server,
2022-01-04 20:14:19 +00:00
test_comments_enabled,
test_attach_image_filename,
test_media_type,
test_image_description, test_city,
test_in_reply_to, test_in_reply_to_atom_uri,
test_subject, test_schedule_post,
2022-05-23 12:14:36 +00:00
test_event_date, test_event_time,
test_event_end_time, test_location,
2022-01-04 20:14:19 +00:00
test_is_article, system_language, conversation_id,
low_bandwidth, content_license_url,
2022-07-18 16:18:04 +00:00
languages_understood, translate)
2021-12-28 19:33:29 +00:00
create_public_post(path, nickname, domain, port, http_prefix,
"Curiouser and curiouser!",
2022-01-04 20:14:19 +00:00
test_save_to_file,
2021-12-28 19:33:29 +00:00
client_to_server,
2022-01-04 20:14:19 +00:00
test_comments_enabled,
test_attach_image_filename,
test_media_type,
test_image_description, test_city,
test_in_reply_to, test_in_reply_to_atom_uri,
test_subject, test_schedule_post,
2022-05-23 12:14:36 +00:00
test_event_date, test_event_time,
test_event_end_time, test_location,
2022-01-04 20:14:19 +00:00
test_is_article, system_language, conversation_id,
low_bandwidth, content_license_url,
2022-07-18 16:18:04 +00:00
languages_understood, translate)
2021-12-28 19:33:29 +00:00
create_public_post(path, nickname, domain, port, http_prefix,
"In the gardens of memory, in the palace " +
"of dreams, that is where you and I shall meet",
2022-01-04 20:14:19 +00:00
test_save_to_file,
2021-12-28 19:33:29 +00:00
client_to_server,
2022-01-04 20:14:19 +00:00
test_comments_enabled,
test_attach_image_filename,
test_media_type,
test_image_description, test_city,
test_in_reply_to, test_in_reply_to_atom_uri,
test_subject, test_schedule_post,
2022-05-23 12:14:36 +00:00
test_event_date, test_event_time,
test_event_end_time, test_location,
2022-01-04 20:14:19 +00:00
test_is_article, system_language, conversation_id,
low_bandwidth, content_license_url,
2022-07-18 16:18:04 +00:00
languages_understood, translate)
2021-12-29 21:55:09 +00:00
regenerate_index_for_box(path, nickname, domain, 'outbox')
2022-01-04 20:14:19 +00:00
global TEST_SERVER_ALICE_RUNNING
TEST_SERVER_ALICE_RUNNING = True
2021-12-25 21:02:44 +00:00
max_mentions = 10
2021-12-25 21:04:51 +00:00
max_emoji = 10
2021-12-25 20:43:43 +00:00
onion_domain = None
2021-12-25 20:50:24 +00:00
i2p_domain = None
2021-12-25 18:54:50 +00:00
allow_local_network_access = True
2021-12-25 18:49:19 +00:00
max_newswire_posts = 20
2021-12-25 18:47:04 +00:00
dormant_months = 3
2021-12-25 18:44:18 +00:00
send_threads_timeout_mins = 30
2021-12-25 19:37:10 +00:00
max_followers = 10
2021-12-25 18:40:32 +00:00
verify_all_signatures = True
2021-12-25 18:38:19 +00:00
broch_mode = False
2021-12-25 18:32:17 +00:00
show_node_info_accounts = True
2021-12-25 18:35:24 +00:00
show_node_info_version = True
2021-05-09 19:29:53 +00:00
city = 'London, England'
2021-12-25 18:29:29 +00:00
log_login_failures = False
2021-12-25 18:27:11 +00:00
user_agents_blocked = []
2021-12-25 18:23:12 +00:00
max_like_count = 10
2021-12-25 17:31:22 +00:00
default_reply_interval_hrs = 9999999999
2021-12-25 18:12:13 +00:00
lists_enabled = ''
2021-12-25 17:13:38 +00:00
content_license_url = 'https://creativecommons.org/licenses/by/4.0'
2022-01-26 22:13:23 +00:00
dyslexic_font = False
2022-03-06 12:31:58 +00:00
crawlers_allowed = []
2022-03-31 12:00:36 +00:00
check_actor_timeout = 2
preferred_podcast_formats = None
2022-07-25 12:04:27 +00:00
clacks = None
2022-08-23 10:09:24 +00:00
map_format = 'gpx'
2019-06-30 21:20:02 +00:00
print('Server running: Alice')
2022-08-23 10:09:24 +00:00
run_daemon(map_format,
clacks, preferred_podcast_formats,
check_actor_timeout,
2022-03-31 12:00:36 +00:00
crawlers_allowed,
2022-03-06 12:31:58 +00:00
dyslexic_font,
2022-01-26 22:13:23 +00:00
content_license_url,
2021-12-29 21:55:09 +00:00
lists_enabled, default_reply_interval_hrs,
low_bandwidth, max_like_count,
shared_items_federated_domains,
user_agents_blocked,
log_login_failures, city,
show_node_info_accounts,
show_node_info_version,
broch_mode,
verify_all_signatures,
send_threads_timeout_mins,
dormant_months, max_newswire_posts,
allow_local_network_access,
2048, False, True, False, False, True, max_followers,
0, 100, 1024, 5, False,
0, False, 1, False, False, False,
5, True, True, 'en', __version__,
"instance_id", False, path, domain,
onion_domain, i2p_domain, None, None, port, port,
http_prefix, federation_list, max_mentions, max_emoji, False,
proxy_type, max_replies,
domain_max_posts_per_day, account_max_posts_per_day,
allow_deletion, True, True, False, send_threads,
False)
def create_server_bob(path: str, domain: str, port: int,
2022-01-04 20:14:19 +00:00
alice_address: str, federation_list: [],
2022-06-08 20:20:27 +00:00
has_follows: bool, has_posts: bool,
2021-12-29 21:55:09 +00:00
send_threads: []):
2020-04-05 13:25:47 +00:00
print('Creating test server: Bob on port ' + str(port))
2019-06-30 21:20:02 +00:00
if os.path.isdir(path):
2021-10-29 18:48:15 +00:00
shutil.rmtree(path, ignore_errors=False, onerror=None)
2019-06-30 21:20:02 +00:00
os.mkdir(path)
os.chdir(path)
2021-12-25 18:05:01 +00:00
shared_items_federated_domains = []
2021-12-25 23:03:28 +00:00
system_language = 'en'
languages_understood = [system_language]
2020-04-05 13:25:47 +00:00
nickname = 'bob'
2021-12-25 17:09:22 +00:00
http_prefix = 'http'
2021-12-25 21:09:22 +00:00
proxy_type = None
2021-12-25 20:39:35 +00:00
client_to_server = False
2020-04-05 13:25:47 +00:00
password = 'bobpass'
2021-12-25 21:11:35 +00:00
max_replies = 64
2021-12-25 21:13:55 +00:00
domain_max_posts_per_day = 1000
2021-12-25 21:18:07 +00:00
account_max_posts_per_day = 1000
2021-12-25 21:29:53 +00:00
allow_deletion = True
2021-12-25 18:20:56 +00:00
low_bandwidth = True
2022-01-04 20:14:19 +00:00
private_key_pem, public_key_pem, person, wf_endpoint = \
2021-12-29 21:55:09 +00:00
create_person(path, nickname, domain, port, http_prefix, True,
False, password)
2022-01-04 20:14:19 +00:00
assert private_key_pem
assert public_key_pem
assert person
assert wf_endpoint
2021-12-29 21:55:09 +00:00
delete_all_posts(path, nickname, domain, 'inbox')
delete_all_posts(path, nickname, domain, 'outbox')
2022-06-08 20:20:27 +00:00
if has_follows and alice_address:
2021-12-27 17:08:19 +00:00
follow_person(path, nickname, domain,
2022-01-04 20:14:19 +00:00
'alice', alice_address, federation_list, False, False)
2021-12-29 21:55:09 +00:00
add_follower_of_person(path, nickname, domain,
2022-01-04 20:14:19 +00:00
'alice', alice_address, federation_list,
2021-12-29 21:55:09 +00:00
False, False)
2022-06-08 20:20:27 +00:00
if has_posts:
2022-01-04 20:14:19 +00:00
test_save_to_file = True
test_comments_enabled = True
test_attach_image_filename = None
test_image_description = None
test_media_type = None
test_city = 'London, England'
test_in_reply_to = None
test_in_reply_to_atom_uri = None
test_subject = None
test_schedule_post = False
test_event_date = None
test_event_time = None
2022-05-23 12:14:36 +00:00
test_event_end_time = None
2022-01-04 20:14:19 +00:00
test_location = None
test_is_article = False
conversation_id = None
2021-12-25 17:13:38 +00:00
content_license_url = 'https://creativecommons.org/licenses/by/4.0'
2022-07-18 16:18:04 +00:00
translate = {}
2021-12-28 19:33:29 +00:00
create_public_post(path, nickname, domain, port, http_prefix,
"It's your life, live it your way.",
2022-01-04 20:14:19 +00:00
test_save_to_file,
2021-12-28 19:33:29 +00:00
client_to_server,
2022-01-04 20:14:19 +00:00
test_comments_enabled,
test_attach_image_filename,
test_media_type,
test_image_description, test_city,
test_in_reply_to, test_in_reply_to_atom_uri,
test_subject, test_schedule_post,
2022-05-23 12:14:36 +00:00
test_event_date, test_event_time,
test_event_end_time, test_location,
2022-01-04 20:14:19 +00:00
test_is_article, system_language, conversation_id,
low_bandwidth, content_license_url,
2022-07-18 16:18:04 +00:00
languages_understood, translate)
2021-12-28 19:33:29 +00:00
create_public_post(path, nickname, domain, port, http_prefix,
"One of the things I've realised is that " +
"I am very simple",
2022-01-04 20:14:19 +00:00
test_save_to_file,
2021-12-28 19:33:29 +00:00
client_to_server,
2022-01-04 20:14:19 +00:00
test_comments_enabled,
test_attach_image_filename,
test_media_type,
test_image_description, test_city,
test_in_reply_to, test_in_reply_to_atom_uri,
test_subject, test_schedule_post,
2022-05-23 12:14:36 +00:00
test_event_date, test_event_time,
test_event_end_time, test_location,
2022-01-04 20:14:19 +00:00
test_is_article, system_language, conversation_id,
low_bandwidth, content_license_url,
2022-07-18 16:18:04 +00:00
languages_understood, translate)
2021-12-28 19:33:29 +00:00
create_public_post(path, nickname, domain, port, http_prefix,
"Quantum physics is a bit of a passion of mine",
2022-01-04 20:14:19 +00:00
test_save_to_file,
2021-12-28 19:33:29 +00:00
client_to_server,
2022-01-04 20:14:19 +00:00
test_comments_enabled,
test_attach_image_filename,
test_media_type,
test_image_description, test_city,
test_in_reply_to, test_in_reply_to_atom_uri,
test_subject, test_schedule_post,
2022-05-23 12:14:36 +00:00
test_event_date, test_event_time,
test_event_end_time, test_location,
2022-01-04 20:14:19 +00:00
test_is_article, system_language, conversation_id,
low_bandwidth, content_license_url,
2022-07-18 16:18:04 +00:00
languages_understood, translate)
2021-12-29 21:55:09 +00:00
regenerate_index_for_box(path, nickname, domain, 'outbox')
2022-01-04 20:14:19 +00:00
global TEST_SERVER_BOB_RUNNING
TEST_SERVER_BOB_RUNNING = True
2021-12-25 21:02:44 +00:00
max_mentions = 10
2021-12-25 21:04:51 +00:00
max_emoji = 10
2021-12-25 20:43:43 +00:00
onion_domain = None
2021-12-25 20:50:24 +00:00
i2p_domain = None
2021-12-25 18:54:50 +00:00
allow_local_network_access = True
2021-12-25 18:49:19 +00:00
max_newswire_posts = 20
2021-12-25 18:47:04 +00:00
dormant_months = 3
2021-12-25 18:44:18 +00:00
send_threads_timeout_mins = 30
2021-12-25 19:37:10 +00:00
max_followers = 10
2021-12-25 18:40:32 +00:00
verify_all_signatures = True
2021-12-25 18:38:19 +00:00
broch_mode = False
2021-12-25 18:32:17 +00:00
show_node_info_accounts = True
2021-12-25 18:35:24 +00:00
show_node_info_version = True
2021-05-09 19:29:53 +00:00
city = 'London, England'
2021-12-25 18:29:29 +00:00
log_login_failures = False
2021-12-25 18:27:11 +00:00
user_agents_blocked = []
2021-12-25 18:23:12 +00:00
max_like_count = 10
2021-12-25 17:31:22 +00:00
default_reply_interval_hrs = 9999999999
2021-12-25 18:12:13 +00:00
lists_enabled = ''
2021-12-25 17:13:38 +00:00
content_license_url = 'https://creativecommons.org/licenses/by/4.0'
2022-01-26 22:13:23 +00:00
dyslexic_font = False
2022-03-06 12:31:58 +00:00
crawlers_allowed = []
2022-03-31 12:00:36 +00:00
check_actor_timeout = 2
preferred_podcast_formats = None
2022-07-25 12:04:27 +00:00
clacks = None
2022-08-23 10:09:24 +00:00
map_format = 'gpx'
2019-06-30 21:20:02 +00:00
print('Server running: Bob')
2022-08-23 10:09:24 +00:00
run_daemon(map_format,
clacks, preferred_podcast_formats,
check_actor_timeout,
2022-03-31 12:00:36 +00:00
crawlers_allowed,
2022-03-06 12:31:58 +00:00
dyslexic_font,
2022-01-26 22:13:23 +00:00
content_license_url,
2021-12-29 21:55:09 +00:00
lists_enabled, default_reply_interval_hrs,
low_bandwidth, max_like_count,
shared_items_federated_domains,
user_agents_blocked,
log_login_failures, city,
show_node_info_accounts,
show_node_info_version,
broch_mode,
verify_all_signatures,
send_threads_timeout_mins,
dormant_months, max_newswire_posts,
allow_local_network_access,
2048, False, True, False, False, True, max_followers,
0, 100, 1024, 5, False, 0,
False, 1, False, False, False,
5, True, True, 'en', __version__,
"instance_id", False, path, domain,
onion_domain, i2p_domain, None, None, port, port,
http_prefix, federation_list, max_mentions, max_emoji, False,
proxy_type, max_replies,
domain_max_posts_per_day, account_max_posts_per_day,
allow_deletion, True, True, False, send_threads,
False)
def create_server_eve(path: str, domain: str, port: int, federation_list: [],
2022-06-08 20:20:27 +00:00
has_follows: bool, has_posts: bool,
2021-12-29 21:55:09 +00:00
send_threads: []):
2020-04-05 13:25:47 +00:00
print('Creating test server: Eve on port ' + str(port))
if os.path.isdir(path):
2021-10-29 18:48:15 +00:00
shutil.rmtree(path, ignore_errors=False, onerror=None)
os.mkdir(path)
os.chdir(path)
2021-12-25 18:05:01 +00:00
shared_items_federated_domains = []
2020-04-05 13:25:47 +00:00
nickname = 'eve'
2021-12-25 17:09:22 +00:00
http_prefix = 'http'
2021-12-25 21:09:22 +00:00
proxy_type = None
2020-04-05 13:25:47 +00:00
password = 'evepass'
2021-12-25 21:11:35 +00:00
max_replies = 64
2021-12-25 21:29:53 +00:00
allow_deletion = True
2022-01-04 20:14:19 +00:00
private_key_pem, public_key_pem, person, wf_endpoint = \
2021-12-29 21:55:09 +00:00
create_person(path, nickname, domain, port, http_prefix, True,
False, password)
2022-01-04 20:14:19 +00:00
assert private_key_pem
assert public_key_pem
assert person
assert wf_endpoint
2021-12-29 21:55:09 +00:00
delete_all_posts(path, nickname, domain, 'inbox')
delete_all_posts(path, nickname, domain, 'outbox')
2022-01-04 20:14:19 +00:00
global TEST_SERVER_EVE_RUNNING
TEST_SERVER_EVE_RUNNING = True
2021-12-25 21:02:44 +00:00
max_mentions = 10
2021-12-25 21:04:51 +00:00
max_emoji = 10
2021-12-25 20:43:43 +00:00
onion_domain = None
2021-12-25 20:50:24 +00:00
i2p_domain = None
2021-12-25 18:54:50 +00:00
allow_local_network_access = True
2021-12-25 18:49:19 +00:00
max_newswire_posts = 20
2021-12-25 18:47:04 +00:00
dormant_months = 3
2021-12-25 18:44:18 +00:00
send_threads_timeout_mins = 30
2021-12-25 19:37:10 +00:00
max_followers = 10
2021-12-25 18:40:32 +00:00
verify_all_signatures = True
2021-12-25 18:38:19 +00:00
broch_mode = False
2021-12-25 18:32:17 +00:00
show_node_info_accounts = True
2021-12-25 18:35:24 +00:00
show_node_info_version = True
2021-05-09 19:29:53 +00:00
city = 'London, England'
2021-12-25 18:29:29 +00:00
log_login_failures = False
2021-12-25 18:27:11 +00:00
user_agents_blocked = []
2021-12-25 18:23:12 +00:00
max_like_count = 10
2021-12-25 18:20:56 +00:00
low_bandwidth = True
2021-12-25 17:31:22 +00:00
default_reply_interval_hrs = 9999999999
2021-12-25 18:12:13 +00:00
lists_enabled = ''
2021-12-25 17:13:38 +00:00
content_license_url = 'https://creativecommons.org/licenses/by/4.0'
2022-01-26 22:13:23 +00:00
dyslexic_font = False
2022-03-06 12:31:58 +00:00
crawlers_allowed = []
2022-03-31 12:00:36 +00:00
check_actor_timeout = 2
preferred_podcast_formats = None
2022-07-25 12:04:27 +00:00
clacks = None
2022-08-23 10:09:24 +00:00
map_format = 'gpx'
print('Server running: Eve')
2022-08-23 10:09:24 +00:00
run_daemon(map_format,
clacks, preferred_podcast_formats,
check_actor_timeout,
2022-03-31 12:00:36 +00:00
crawlers_allowed,
2022-03-06 12:31:58 +00:00
dyslexic_font,
2022-01-26 22:13:23 +00:00
content_license_url,
2021-12-29 21:55:09 +00:00
lists_enabled, default_reply_interval_hrs,
low_bandwidth, max_like_count,
shared_items_federated_domains,
user_agents_blocked,
log_login_failures, city,
show_node_info_accounts,
show_node_info_version,
broch_mode,
verify_all_signatures,
send_threads_timeout_mins,
dormant_months, max_newswire_posts,
allow_local_network_access,
2048, False, True, False, False, True, max_followers,
0, 100, 1024, 5, False, 0,
False, 1, False, False, False,
5, True, True, 'en', __version__,
"instance_id", False, path, domain,
onion_domain, i2p_domain, None, None, port, port,
http_prefix, federation_list, max_mentions, max_emoji, False,
proxy_type, max_replies, allow_deletion, True, True, False,
send_threads, False)
def create_server_group(path: str, domain: str, port: int,
federation_list: [],
2022-06-08 20:20:27 +00:00
has_follows: bool, has_posts: bool,
2021-12-29 21:55:09 +00:00
send_threads: []):
2021-07-30 19:20:49 +00:00
print('Creating test server: Group on port ' + str(port))
if os.path.isdir(path):
2021-10-29 18:48:15 +00:00
shutil.rmtree(path, ignore_errors=False, onerror=None)
2021-07-30 19:20:49 +00:00
os.mkdir(path)
os.chdir(path)
2021-12-25 18:05:01 +00:00
shared_items_federated_domains = []
2021-12-25 23:03:28 +00:00
# system_language = 'en'
2021-07-30 19:20:49 +00:00
nickname = 'testgroup'
2021-12-25 17:09:22 +00:00
http_prefix = 'http'
2021-12-25 21:09:22 +00:00
proxy_type = None
2021-07-30 19:20:49 +00:00
password = 'testgrouppass'
2021-12-25 21:11:35 +00:00
max_replies = 64
2021-12-25 21:13:55 +00:00
domain_max_posts_per_day = 1000
2021-12-25 21:18:07 +00:00
account_max_posts_per_day = 1000
2021-12-25 21:29:53 +00:00
allow_deletion = True
2022-01-04 20:14:19 +00:00
private_key_pem, public_key_pem, person, wf_endpoint = \
2021-12-29 21:55:09 +00:00
create_group(path, nickname, domain, port, http_prefix, True,
password)
2022-01-04 20:14:19 +00:00
assert private_key_pem
assert public_key_pem
assert person
assert wf_endpoint
2021-12-29 21:55:09 +00:00
delete_all_posts(path, nickname, domain, 'inbox')
delete_all_posts(path, nickname, domain, 'outbox')
2022-01-04 20:14:19 +00:00
global TEST_SERVER_GROUP_RUNNING
TEST_SERVER_GROUP_RUNNING = True
2021-12-25 21:02:44 +00:00
max_mentions = 10
2021-12-25 21:04:51 +00:00
max_emoji = 10
2021-12-25 20:43:43 +00:00
onion_domain = None
2021-12-25 20:50:24 +00:00
i2p_domain = None
2021-12-25 18:54:50 +00:00
allow_local_network_access = True
2021-12-25 18:49:19 +00:00
max_newswire_posts = 20
2021-12-25 18:47:04 +00:00
dormant_months = 3
2021-12-25 18:44:18 +00:00
send_threads_timeout_mins = 30
2021-12-25 19:37:10 +00:00
max_followers = 10
2021-12-25 18:40:32 +00:00
verify_all_signatures = True
2021-12-25 18:38:19 +00:00
broch_mode = False
2021-12-25 18:32:17 +00:00
show_node_info_accounts = True
2021-12-25 18:35:24 +00:00
show_node_info_version = True
2021-07-30 19:20:49 +00:00
city = 'London, England'
2021-12-25 18:29:29 +00:00
log_login_failures = False
2021-12-25 18:27:11 +00:00
user_agents_blocked = []
2021-12-25 18:23:12 +00:00
max_like_count = 10
2021-12-25 18:20:56 +00:00
low_bandwidth = True
2021-12-25 17:31:22 +00:00
default_reply_interval_hrs = 9999999999
2021-12-25 18:12:13 +00:00
lists_enabled = ''
2021-12-25 17:13:38 +00:00
content_license_url = 'https://creativecommons.org/licenses/by/4.0'
2022-01-26 22:13:23 +00:00
dyslexic_font = False
2022-03-06 12:31:58 +00:00
crawlers_allowed = []
2022-03-31 12:00:36 +00:00
check_actor_timeout = 2
preferred_podcast_formats = None
2022-07-25 12:04:27 +00:00
clacks = None
2022-08-23 10:09:24 +00:00
map_format = 'gpx'
2021-07-30 19:20:49 +00:00
print('Server running: Group')
2022-08-23 10:09:24 +00:00
run_daemon(map_format,
clacks, preferred_podcast_formats,
check_actor_timeout,
2022-03-31 12:00:36 +00:00
crawlers_allowed,
2022-03-06 12:31:58 +00:00
dyslexic_font,
2022-01-26 22:13:23 +00:00
content_license_url,
2021-12-29 21:55:09 +00:00
lists_enabled, default_reply_interval_hrs,
low_bandwidth, max_like_count,
shared_items_federated_domains,
user_agents_blocked,
log_login_failures, city,
show_node_info_accounts,
show_node_info_version,
broch_mode,
verify_all_signatures,
send_threads_timeout_mins,
dormant_months, max_newswire_posts,
allow_local_network_access,
2048, False, True, False, False, True, max_followers,
0, 100, 1024, 5, False,
0, False, 1, False, False, False,
5, True, True, 'en', __version__,
"instance_id", False, path, domain,
onion_domain, i2p_domain, None, None, port, port,
http_prefix, federation_list, max_mentions, max_emoji, False,
proxy_type, max_replies,
domain_max_posts_per_day, account_max_posts_per_day,
allow_deletion, True, True, False, send_threads,
False)
def test_post_message_between_servers(base_dir: str) -> None:
2019-06-30 21:20:02 +00:00
print('Testing sending message from one server to the inbox of another')
2019-06-30 21:27:25 +00:00
2022-01-04 20:14:19 +00:00
global TEST_SERVER_ALICE_RUNNING
global TEST_SERVER_BOB_RUNNING
TEST_SERVER_ALICE_RUNNING = False
TEST_SERVER_BOB_RUNNING = False
2019-06-30 21:27:25 +00:00
2021-12-25 23:03:28 +00:00
system_language = 'en'
languages_understood = [system_language]
2021-12-25 17:09:22 +00:00
http_prefix = 'http'
2021-12-25 21:09:22 +00:00
proxy_type = None
2021-12-25 17:13:38 +00:00
content_license_url = 'https://creativecommons.org/licenses/by/4.0'
2019-06-30 22:56:37 +00:00
2021-12-25 16:17:53 +00:00
if os.path.isdir(base_dir + '/.tests'):
shutil.rmtree(base_dir + '/.tests', ignore_errors=False, onerror=None)
os.mkdir(base_dir + '/.tests')
2019-06-30 21:20:02 +00:00
# create the servers
2022-01-04 20:14:19 +00:00
alice_dir = base_dir + '/.tests/alice'
alice_domain = '127.0.0.50'
alice_port = 61935
alice_address = alice_domain + ':' + str(alice_port)
bob_dir = base_dir + '/.tests/bob'
bob_domain = '127.0.0.100'
bob_port = 61936
federation_list = [bob_domain, alice_domain]
alice_send_threads = []
bob_send_threads = []
bob_address = bob_domain + ':' + str(bob_port)
global THR_ALICE
if THR_ALICE:
while THR_ALICE.is_alive():
THR_ALICE.stop()
2020-02-19 12:27:21 +00:00
time.sleep(1)
2022-01-04 20:14:19 +00:00
THR_ALICE.kill()
2020-02-19 12:27:21 +00:00
2022-01-04 20:14:19 +00:00
THR_ALICE = \
2021-12-29 21:55:09 +00:00
thread_with_trace(target=create_server_alice,
2022-01-04 20:14:19 +00:00
args=(alice_dir, alice_domain, alice_port,
bob_address, federation_list, False, False,
alice_send_threads),
2021-12-28 21:36:27 +00:00
daemon=True)
2020-02-19 12:27:21 +00:00
2022-01-04 20:14:19 +00:00
global THR_BOB
if THR_BOB:
while THR_BOB.is_alive():
THR_BOB.stop()
2020-02-19 12:27:21 +00:00
time.sleep(1)
2022-01-04 20:14:19 +00:00
THR_BOB.kill()
2020-02-19 12:27:21 +00:00
2022-01-04 20:14:19 +00:00
THR_BOB = \
2021-12-29 21:55:09 +00:00
thread_with_trace(target=create_server_bob,
2022-01-04 20:14:19 +00:00
args=(bob_dir, bob_domain, bob_port, alice_address,
2021-12-28 21:36:27 +00:00
federation_list, False, False,
2022-01-04 20:14:19 +00:00
bob_send_threads),
2021-12-28 21:36:27 +00:00
daemon=True)
2019-07-01 21:01:43 +00:00
2022-01-04 20:14:19 +00:00
THR_ALICE.start()
THR_BOB.start()
assert THR_ALICE.is_alive() is True
assert THR_BOB.is_alive() is True
2019-06-30 21:20:02 +00:00
2019-06-30 21:27:25 +00:00
# wait for both servers to be running
2022-01-04 20:14:19 +00:00
while not (TEST_SERVER_ALICE_RUNNING and TEST_SERVER_BOB_RUNNING):
2019-06-30 21:27:25 +00:00
time.sleep(1)
2020-03-22 21:16:02 +00:00
2019-07-04 20:25:19 +00:00
time.sleep(1)
2019-06-30 21:20:02 +00:00
2019-07-11 12:29:31 +00:00
print('\n\n*******************************************************')
2019-06-30 22:56:37 +00:00
print('Alice sends to Bob')
2022-01-04 20:14:19 +00:00
os.chdir(alice_dir)
session_alice = create_session(proxy_type)
in_reply_to = None
in_reply_to_atom_uri = None
2020-04-05 13:25:47 +00:00
subject = None
2022-01-04 20:14:19 +00:00
alice_post_log = []
save_to_file = True
2021-12-25 20:39:35 +00:00
client_to_server = False
2022-01-04 20:14:19 +00:00
cc_url = None
alice_person_cache = {}
alice_cached_webfingers = {}
alice_shared_items_federated_domains = []
alice_shared_item_federation_tokens = {}
attached_image_filename = base_dir + '/img/logo.png'
test_image_width, test_image_height = \
get_image_dimensions(attached_image_filename)
assert test_image_width
assert test_image_height
media_type = get_attachment_media_type(attached_image_filename)
attached_image_description = 'Logo'
is_article = False
2021-05-09 19:29:53 +00:00
city = 'London, England'
2019-07-11 12:29:31 +00:00
# nothing in Alice's outbox
2022-01-04 20:14:19 +00:00
outbox_path = alice_dir + '/accounts/alice@' + alice_domain + '/outbox'
assert len([name for name in os.listdir(outbox_path)
if os.path.isfile(os.path.join(outbox_path, name))]) == 0
2021-12-25 18:20:56 +00:00
low_bandwidth = False
2021-12-25 23:03:28 +00:00
signing_priv_key_pem = None
2022-07-18 16:18:04 +00:00
translate = {}
2022-01-04 20:14:19 +00:00
send_result = \
2021-12-29 21:55:09 +00:00
send_post(signing_priv_key_pem, __version__,
2022-01-04 20:14:19 +00:00
session_alice, alice_dir, 'alice', alice_domain, alice_port,
'bob', bob_domain, bob_port, cc_url, http_prefix,
2021-12-29 21:55:09 +00:00
'Why is a mouse when it spins? ' +
'यह एक परीक्षण है #sillyquestion',
2022-01-04 20:14:19 +00:00
save_to_file, client_to_server, True,
attached_image_filename, media_type,
attached_image_description, city, federation_list,
alice_send_threads, alice_post_log, alice_cached_webfingers,
alice_person_cache, is_article, system_language,
languages_understood,
2022-01-04 20:14:19 +00:00
alice_shared_items_federated_domains,
alice_shared_item_federation_tokens, low_bandwidth,
2022-07-18 16:18:04 +00:00
content_license_url, translate,
2022-01-04 20:14:19 +00:00
in_reply_to, in_reply_to_atom_uri, subject)
print('send_result: ' + str(send_result))
queue_path = bob_dir + '/accounts/bob@' + bob_domain + '/queue'
inbox_path = bob_dir + '/accounts/bob@' + bob_domain + '/inbox'
m_path = get_media_path()
media_path = alice_dir + '/' + m_path
for _ in range(30):
if os.path.isdir(inbox_path):
if len([name for name in os.listdir(inbox_path)
if os.path.isfile(os.path.join(inbox_path, name))]) > 0:
if len([name for name in os.listdir(outbox_path)
if os.path.isfile(os.path.join(outbox_path,
2020-04-05 13:25:47 +00:00
name))]) == 1:
2022-01-04 20:14:19 +00:00
if len([name for name in os.listdir(media_path)
if os.path.isfile(os.path.join(media_path,
2020-04-05 13:25:47 +00:00
name))]) > 0:
2022-01-04 20:14:19 +00:00
if len([name for name in os.listdir(queue_path)
if os.path.isfile(os.path.join(queue_path,
2020-04-05 13:25:47 +00:00
name))]) == 0:
2019-10-19 17:50:05 +00:00
break
2019-07-01 21:01:43 +00:00
time.sleep(1)
2019-07-09 08:52:53 +00:00
2020-12-06 15:05:22 +00:00
# check that a news account exists
2022-01-04 20:14:19 +00:00
news_actor_dir = alice_dir + '/accounts/news@' + alice_domain
print("news_actor_dir: " + news_actor_dir)
assert os.path.isdir(news_actor_dir)
news_actor_file = news_actor_dir + '.json'
assert os.path.isfile(news_actor_file)
news_actor_json = load_json(news_actor_file)
assert news_actor_json
assert news_actor_json.get("id")
2020-12-06 15:05:22 +00:00
# check the id of the news actor
2022-01-04 20:14:19 +00:00
print('News actor Id: ' + news_actor_json["id"])
assert (news_actor_json["id"] ==
http_prefix + '://' + alice_address + '/users/news')
2020-12-06 15:05:22 +00:00
2019-07-12 19:26:54 +00:00
# Image attachment created
2022-01-04 20:14:19 +00:00
assert len([name for name in os.listdir(media_path)
if os.path.isfile(os.path.join(media_path, name))]) > 0
2019-07-11 12:29:31 +00:00
# inbox item created
2022-01-04 20:14:19 +00:00
assert len([name for name in os.listdir(inbox_path)
if os.path.isfile(os.path.join(inbox_path, name))]) == 1
2019-07-11 12:29:31 +00:00
# queue item removed
2022-01-04 20:14:19 +00:00
testval = len([name for name in os.listdir(queue_path)
if os.path.isfile(os.path.join(queue_path, name))])
print('queue_path: ' + queue_path + ' '+str(testval))
2020-04-05 13:25:47 +00:00
assert testval == 0
2022-01-04 20:14:19 +00:00
assert valid_inbox(bob_dir, 'bob', bob_domain)
assert valid_inbox_filenames(bob_dir, 'bob', bob_domain,
alice_domain, alice_port)
2019-11-09 09:50:58 +00:00
print('Check that message received from Alice contains the expected text')
2022-01-04 20:14:19 +00:00
for name in os.listdir(inbox_path):
filename = os.path.join(inbox_path, name)
2019-11-09 09:50:58 +00:00
assert os.path.isfile(filename)
2022-01-04 20:14:19 +00:00
received_json = load_json(filename, 0)
if received_json:
pprint(received_json['object']['content'])
assert received_json
2020-04-05 13:25:47 +00:00
assert 'Why is a mouse when it spins?' in \
2022-01-04 20:14:19 +00:00
received_json['object']['content']
assert 'Why is a mouse when it spins?' in \
2022-01-04 20:14:19 +00:00
received_json['object']['contentMap'][system_language]
assert 'यह एक परीक्षण है' in received_json['object']['content']
print('Check that message received from Alice contains an attachment')
2022-01-04 20:14:19 +00:00
assert received_json['object']['attachment']
assert len(received_json['object']['attachment']) == 1
attached = received_json['object']['attachment'][0]
pprint(attached)
assert attached.get('type')
assert attached.get('url')
assert attached['mediaType'] == 'image/png'
if '/system/media_attachments/files/' not in attached['url']:
2021-10-12 18:20:40 +00:00
print(attached['url'])
assert '/system/media_attachments/files/' in attached['url']
assert attached['url'].endswith('.png')
assert attached.get('width')
assert attached.get('height')
assert attached['width'] > 0
assert attached['height'] > 0
2019-11-09 09:52:09 +00:00
2019-07-11 12:59:00 +00:00
print('\n\n*******************************************************')
print("Bob likes Alice's post")
2022-01-04 20:14:19 +00:00
alice_domain_str = alice_domain + ':' + str(alice_port)
add_follower_of_person(bob_dir, 'bob', bob_domain, 'alice',
alice_domain_str, federation_list, False, False)
bob_domain_str = bob_domain + ':' + str(bob_port)
follow_person(alice_dir, 'alice', alice_domain, 'bob',
bob_domain_str, federation_list, False, False)
session_bob = create_session(proxy_type)
bob_post_log = []
bob_person_cache = {}
bob_cached_webfingers = {}
status_number = None
outbox_post_filename = None
outbox_path = alice_dir + '/accounts/alice@' + alice_domain + '/outbox'
for name in os.listdir(outbox_path):
2019-07-11 12:59:00 +00:00
if '#statuses#' in name:
2022-01-04 20:14:19 +00:00
status_number = \
2020-04-05 13:25:47 +00:00
int(name.split('#statuses#')[1].replace('.json', ''))
2022-01-04 20:14:19 +00:00
outbox_post_filename = outbox_path + '/' + name
assert status_number > 0
assert outbox_post_filename
assert like_post({}, session_bob, bob_dir, federation_list,
'bob', bob_domain, bob_port, http_prefix,
'alice', alice_domain, alice_port, [],
status_number, False, bob_send_threads, bob_post_log,
bob_person_cache, bob_cached_webfingers,
True, __version__, signing_priv_key_pem,
bob_domain, None, None)
2019-07-11 12:59:00 +00:00
2022-01-04 20:14:19 +00:00
for _ in range(20):
2022-06-10 13:01:39 +00:00
if text_in_file('likes', outbox_post_filename):
2019-07-11 12:59:00 +00:00
break
time.sleep(1)
2022-01-04 20:14:19 +00:00
alice_post_json = load_json(outbox_post_filename, 0)
if alice_post_json:
pprint(alice_post_json)
2020-03-22 21:16:02 +00:00
2022-06-10 13:01:39 +00:00
assert text_in_file('likes', outbox_post_filename)
2019-07-11 17:55:10 +00:00
2021-11-10 12:16:03 +00:00
print('\n\n*******************************************************')
print("Bob reacts to Alice's post")
2022-01-04 20:14:19 +00:00
assert reaction_post({}, session_bob, bob_dir, federation_list,
'bob', bob_domain, bob_port, http_prefix,
'alice', alice_domain, alice_port, [],
status_number, '😀',
False, bob_send_threads, bob_post_log,
bob_person_cache, bob_cached_webfingers,
True, __version__, signing_priv_key_pem,
bob_domain, None, None)
2021-11-10 12:16:03 +00:00
2022-06-08 20:20:27 +00:00
for _ in range(20):
2022-06-10 13:01:39 +00:00
if text_in_file('reactions', outbox_post_filename):
2021-11-10 12:16:03 +00:00
break
time.sleep(1)
2022-01-04 20:14:19 +00:00
alice_post_json = load_json(outbox_post_filename, 0)
if alice_post_json:
pprint(alice_post_json)
2021-11-10 12:16:03 +00:00
2022-03-04 15:21:21 +00:00
# TODO: fix reactions unit test
2022-06-10 13:01:39 +00:00
# assert text_in_file('reactions', outbox_post_filename)
2021-11-10 12:16:03 +00:00
2019-07-11 17:55:10 +00:00
print('\n\n*******************************************************')
print("Bob repeats Alice's post")
2022-01-04 20:14:19 +00:00
object_url = \
http_prefix + '://' + alice_domain + ':' + str(alice_port) + \
'/users/alice/statuses/' + str(status_number)
inbox_path = alice_dir + '/accounts/alice@' + alice_domain + '/inbox'
outbox_path = bob_dir + '/accounts/bob@' + bob_domain + '/outbox'
outbox_before_announce_count = \
len([name for name in os.listdir(outbox_path)
if os.path.isfile(os.path.join(outbox_path, name))])
before_announce_count = \
len([name for name in os.listdir(inbox_path)
if os.path.isfile(os.path.join(inbox_path, name))])
print('inbox items before announce: ' + str(before_announce_count))
print('outbox items before announce: ' + str(outbox_before_announce_count))
assert outbox_before_announce_count == 0
assert before_announce_count == 0
announce_public(session_bob, bob_dir, federation_list,
'bob', bob_domain, bob_port, http_prefix,
object_url,
False, bob_send_threads, bob_post_log,
bob_person_cache, bob_cached_webfingers,
True, __version__, signing_priv_key_pem,
bob_domain, None, None)
2022-01-04 20:14:19 +00:00
announce_message_arrived = False
outbox_message_arrived = False
2022-06-08 20:20:27 +00:00
for _ in range(20):
2019-07-11 19:31:02 +00:00
time.sleep(1)
2022-01-04 20:14:19 +00:00
if not os.path.isdir(inbox_path):
2020-03-27 19:54:41 +00:00
continue
2022-01-04 20:14:19 +00:00
if len([name for name in os.listdir(outbox_path)
if os.path.isfile(os.path.join(outbox_path, name))]) > 0:
outbox_message_arrived = True
2020-03-27 19:54:41 +00:00
print('Announce created by Bob')
2022-01-04 20:14:19 +00:00
if len([name for name in os.listdir(inbox_path)
if os.path.isfile(os.path.join(inbox_path, name))]) > 0:
announce_message_arrived = True
2020-03-27 19:54:41 +00:00
print('Announce message sent to Alice!')
2022-01-04 20:14:19 +00:00
if announce_message_arrived and outbox_message_arrived:
2020-03-27 19:54:41 +00:00
break
2022-01-04 20:14:19 +00:00
after_announce_count = \
len([name for name in os.listdir(inbox_path)
if os.path.isfile(os.path.join(inbox_path, name))])
outbox_after_announce_count = \
len([name for name in os.listdir(outbox_path)
if os.path.isfile(os.path.join(outbox_path, name))])
print('inbox items after announce: ' + str(after_announce_count))
print('outbox items after announce: ' + str(outbox_after_announce_count))
assert after_announce_count == before_announce_count + 1
assert outbox_after_announce_count == outbox_before_announce_count + 1
2019-06-30 21:20:02 +00:00
# stop the servers
2022-01-04 20:14:19 +00:00
THR_ALICE.kill()
THR_ALICE.join()
assert THR_ALICE.is_alive() is False
2019-06-30 21:20:02 +00:00
2022-01-04 20:14:19 +00:00
THR_BOB.kill()
THR_BOB.join()
assert THR_BOB.is_alive() is False
2019-06-30 21:20:02 +00:00
2021-12-25 16:17:53 +00:00
os.chdir(base_dir)
2022-01-04 20:14:19 +00:00
shutil.rmtree(alice_dir, ignore_errors=False, onerror=None)
shutil.rmtree(bob_dir, ignore_errors=False, onerror=None)
2020-04-05 13:25:47 +00:00
2021-12-29 21:55:09 +00:00
def test_follow_between_servers(base_dir: str) -> None:
2019-10-15 09:31:10 +00:00
print('Testing sending a follow request from one server to another')
2022-01-04 20:14:19 +00:00
global TEST_SERVER_ALICE_RUNNING
global TEST_SERVER_BOB_RUNNING
TEST_SERVER_ALICE_RUNNING = False
TEST_SERVER_BOB_RUNNING = False
2019-10-15 09:31:10 +00:00
2021-12-25 23:03:28 +00:00
system_language = 'en'
languages_understood = [system_language]
2021-12-25 17:09:22 +00:00
http_prefix = 'http'
2021-12-25 21:09:22 +00:00
proxy_type = None
2021-12-25 23:45:30 +00:00
federation_list = []
2021-12-25 17:13:38 +00:00
content_license_url = 'https://creativecommons.org/licenses/by/4.0'
2019-10-15 09:31:10 +00:00
2021-12-25 16:17:53 +00:00
if os.path.isdir(base_dir + '/.tests'):
shutil.rmtree(base_dir + '/.tests', ignore_errors=False, onerror=None)
os.mkdir(base_dir + '/.tests')
2019-10-15 09:31:10 +00:00
# create the servers
2022-01-04 20:14:19 +00:00
alice_dir = base_dir + '/.tests/alice'
alice_domain = '127.0.0.47'
alice_port = 61935
alice_send_threads = []
alice_address = alice_domain + ':' + str(alice_port)
bob_dir = base_dir + '/.tests/bob'
bob_domain = '127.0.0.79'
bob_port = 61936
bob_send_threads = []
bob_address = bob_domain + ':' + str(bob_port)
global THR_ALICE
if THR_ALICE:
while THR_ALICE.is_alive():
THR_ALICE.stop()
2020-02-19 12:27:21 +00:00
time.sleep(1)
2022-01-04 20:14:19 +00:00
THR_ALICE.kill()
2020-02-19 12:27:21 +00:00
2022-01-04 20:14:19 +00:00
THR_ALICE = \
2021-12-29 21:55:09 +00:00
thread_with_trace(target=create_server_alice,
2022-01-04 20:14:19 +00:00
args=(alice_dir, alice_domain, alice_port,
bob_address, federation_list, False, False,
alice_send_threads),
2021-12-28 21:36:27 +00:00
daemon=True)
2019-10-15 09:31:10 +00:00
2022-01-04 20:14:19 +00:00
global THR_BOB
if THR_BOB:
while THR_BOB.is_alive():
THR_BOB.stop()
2020-02-19 12:27:21 +00:00
time.sleep(1)
2022-01-04 20:14:19 +00:00
THR_BOB.kill()
2020-02-19 12:27:21 +00:00
2022-01-04 20:14:19 +00:00
THR_BOB = \
2021-12-29 21:55:09 +00:00
thread_with_trace(target=create_server_bob,
2022-01-04 20:14:19 +00:00
args=(bob_dir, bob_domain, bob_port, alice_address,
2021-12-28 21:36:27 +00:00
federation_list, False, False,
2022-01-04 20:14:19 +00:00
bob_send_threads),
2021-12-28 21:36:27 +00:00
daemon=True)
2019-10-15 09:31:10 +00:00
2022-01-04 20:14:19 +00:00
THR_ALICE.start()
THR_BOB.start()
assert THR_ALICE.is_alive() is True
assert THR_BOB.is_alive() is True
2019-10-15 09:31:10 +00:00
# wait for all servers to be running
2020-04-05 13:25:47 +00:00
ctr = 0
2022-01-04 20:14:19 +00:00
while not (TEST_SERVER_ALICE_RUNNING and TEST_SERVER_BOB_RUNNING):
2019-10-15 09:31:10 +00:00
time.sleep(1)
2020-04-05 13:25:47 +00:00
ctr += 1
if ctr > 60:
2019-10-15 09:31:10 +00:00
break
2022-01-04 20:14:19 +00:00
print('Alice online: ' + str(TEST_SERVER_ALICE_RUNNING))
print('Bob online: ' + str(TEST_SERVER_BOB_RUNNING))
2020-04-05 13:25:47 +00:00
assert ctr <= 60
2019-10-15 09:31:10 +00:00
time.sleep(1)
# In the beginning all was calm and there were no follows
print('*********************************************************')
print('Alice sends a follow request to Bob')
2022-01-04 20:14:19 +00:00
os.chdir(alice_dir)
session_alice = create_session(proxy_type)
in_reply_to = None
in_reply_to_atom_uri = None
2020-04-05 13:25:47 +00:00
subject = None
2022-01-04 20:14:19 +00:00
alice_post_log = []
save_to_file = True
2021-12-25 20:39:35 +00:00
client_to_server = False
2022-01-04 20:14:19 +00:00
cc_url = None
alice_person_cache = {}
alice_cached_webfingers = {}
alice_post_log = []
bob_actor = http_prefix + '://' + bob_address + '/users/bob'
2021-12-25 23:03:28 +00:00
signing_priv_key_pem = None
2022-01-04 20:14:19 +00:00
send_result = \
send_follow_request(session_alice, alice_dir,
'alice', alice_domain,
alice_domain, alice_port, http_prefix,
2022-01-04 20:14:19 +00:00
'bob', bob_domain, bob_actor,
bob_port, http_prefix,
2021-12-28 20:32:11 +00:00
client_to_server, federation_list,
2022-01-04 20:14:19 +00:00
alice_send_threads, alice_post_log,
alice_cached_webfingers, alice_person_cache,
True, __version__, signing_priv_key_pem,
alice_domain, None, None)
2022-01-04 20:14:19 +00:00
print('send_result: ' + str(send_result))
for _ in range(16):
if os.path.isfile(bob_dir + '/accounts/bob@' +
bob_domain + '/followers.txt'):
if os.path.isfile(alice_dir + '/accounts/alice@' +
alice_domain + '/following.txt'):
if os.path.isfile(alice_dir + '/accounts/alice@' +
alice_domain + '/followingCalendar.txt'):
break
2019-10-15 09:31:10 +00:00
time.sleep(1)
2022-01-04 20:14:19 +00:00
assert valid_inbox(bob_dir, 'bob', bob_domain)
assert valid_inbox_filenames(bob_dir, 'bob', bob_domain,
alice_domain, alice_port)
2022-06-10 13:01:39 +00:00
assert text_in_file('alice@' + alice_domain, bob_dir + '/accounts/bob@' +
bob_domain + '/followers.txt')
assert text_in_file('bob@' + bob_domain,
alice_dir + '/accounts/alice@' +
alice_domain + '/following.txt')
assert text_in_file('bob@' + bob_domain,
alice_dir + '/accounts/alice@' +
alice_domain + '/followingCalendar.txt')
2022-01-04 20:14:19 +00:00
assert not is_group_actor(alice_dir, bob_actor, alice_person_cache)
assert not is_group_account(alice_dir, 'alice', alice_domain)
2019-10-15 09:31:10 +00:00
print('\n\n*********************************************************')
print('Alice sends a message to Bob')
2022-01-04 20:14:19 +00:00
alice_post_log = []
alice_person_cache = {}
alice_cached_webfingers = {}
alice_shared_items_federated_domains = []
alice_shared_item_federation_tokens = {}
alice_post_log = []
is_article = False
2021-05-09 19:29:53 +00:00
city = 'London, England'
2021-12-25 18:20:56 +00:00
low_bandwidth = False
2021-12-25 23:03:28 +00:00
signing_priv_key_pem = None
2022-07-18 16:18:04 +00:00
translate = {}
2022-01-04 20:14:19 +00:00
send_result = \
2021-12-29 21:55:09 +00:00
send_post(signing_priv_key_pem, __version__,
2022-01-04 20:14:19 +00:00
session_alice, alice_dir, 'alice', alice_domain, alice_port,
'bob', bob_domain, bob_port, cc_url,
2022-05-31 16:51:56 +00:00
http_prefix, 'Alice message', save_to_file,
2021-12-29 21:55:09 +00:00
client_to_server, True,
None, None, None, city, federation_list,
2022-01-04 20:14:19 +00:00
alice_send_threads, alice_post_log, alice_cached_webfingers,
alice_person_cache, is_article, system_language,
languages_understood,
2022-01-04 20:14:19 +00:00
alice_shared_items_federated_domains,
alice_shared_item_federation_tokens, low_bandwidth,
2022-07-18 16:18:04 +00:00
content_license_url, translate,
2022-01-04 20:14:19 +00:00
in_reply_to, in_reply_to_atom_uri, subject)
print('send_result: ' + str(send_result))
2020-04-05 13:25:47 +00:00
2022-01-04 20:14:19 +00:00
queue_path = bob_dir + '/accounts/bob@' + bob_domain + '/queue'
inbox_path = bob_dir + '/accounts/bob@' + bob_domain + '/inbox'
alice_message_arrived = False
2022-06-08 20:20:27 +00:00
for _ in range(20):
time.sleep(1)
2022-01-04 20:14:19 +00:00
if os.path.isdir(inbox_path):
if len([name for name in os.listdir(inbox_path)
if os.path.isfile(os.path.join(inbox_path, name))]) > 0:
alice_message_arrived = True
print('Alice message sent to Bob!')
break
2022-01-04 20:14:19 +00:00
assert alice_message_arrived is True
print('Message from Alice to Bob succeeded')
# stop the servers
2022-01-04 20:14:19 +00:00
THR_ALICE.kill()
THR_ALICE.join()
assert THR_ALICE.is_alive() is False
2022-01-04 20:14:19 +00:00
THR_BOB.kill()
THR_BOB.join()
assert THR_BOB.is_alive() is False
# queue item removed
time.sleep(4)
2022-01-04 20:14:19 +00:00
assert len([name for name in os.listdir(queue_path)
if os.path.isfile(os.path.join(queue_path, name))]) == 0
2021-12-25 16:17:53 +00:00
os.chdir(base_dir)
shutil.rmtree(base_dir + '/.tests', ignore_errors=False, onerror=None)
2021-12-29 21:55:09 +00:00
def test_shared_items_federation(base_dir: str) -> None:
print('Testing federation of shared items between Alice and Bob')
2022-01-04 20:14:19 +00:00
global TEST_SERVER_ALICE_RUNNING
global TEST_SERVER_BOB_RUNNING
TEST_SERVER_ALICE_RUNNING = False
TEST_SERVER_BOB_RUNNING = False
2021-12-25 23:03:28 +00:00
system_language = 'en'
languages_understood = [system_language]
2021-12-25 17:09:22 +00:00
http_prefix = 'http'
2021-12-25 21:09:22 +00:00
proxy_type = None
2021-12-25 23:45:30 +00:00
federation_list = []
2021-12-25 17:13:38 +00:00
content_license_url = 'https://creativecommons.org/licenses/by/4.0'
2021-12-25 16:17:53 +00:00
if os.path.isdir(base_dir + '/.tests'):
shutil.rmtree(base_dir + '/.tests', ignore_errors=False, onerror=None)
os.mkdir(base_dir + '/.tests')
# create the servers
2022-01-04 20:14:19 +00:00
alice_dir = base_dir + '/.tests/alice'
alice_domain = '127.0.0.74'
alice_port = 61917
alice_send_threads = []
alice_address = alice_domain + ':' + str(alice_port)
bob_dir = base_dir + '/.tests/bob'
bob_domain = '127.0.0.81'
bob_port = 61983
bob_send_threads = []
bob_address = bob_domain + ':' + str(bob_port)
bob_password = 'bobpass'
bob_cached_webfingers = {}
bob_person_cache = {}
global THR_ALICE
if THR_ALICE:
while THR_ALICE.is_alive():
THR_ALICE.stop()
time.sleep(1)
2022-01-04 20:14:19 +00:00
THR_ALICE.kill()
2022-01-04 20:14:19 +00:00
THR_ALICE = \
2021-12-29 21:55:09 +00:00
thread_with_trace(target=create_server_alice,
2022-01-04 20:14:19 +00:00
args=(alice_dir, alice_domain, alice_port,
bob_address, federation_list, False, False,
alice_send_threads),
2021-12-28 21:36:27 +00:00
daemon=True)
2022-01-04 20:14:19 +00:00
global THR_BOB
if THR_BOB:
while THR_BOB.is_alive():
THR_BOB.stop()
time.sleep(1)
2022-01-04 20:14:19 +00:00
THR_BOB.kill()
2022-01-04 20:14:19 +00:00
THR_BOB = \
2021-12-29 21:55:09 +00:00
thread_with_trace(target=create_server_bob,
2022-01-04 20:14:19 +00:00
args=(bob_dir, bob_domain, bob_port, alice_address,
2021-12-28 21:36:27 +00:00
federation_list, False, False,
2022-01-04 20:14:19 +00:00
bob_send_threads),
2021-12-28 21:36:27 +00:00
daemon=True)
2022-01-04 20:14:19 +00:00
THR_ALICE.start()
THR_BOB.start()
assert THR_ALICE.is_alive() is True
assert THR_BOB.is_alive() is True
# wait for all servers to be running
ctr = 0
2022-01-04 20:14:19 +00:00
while not (TEST_SERVER_ALICE_RUNNING and TEST_SERVER_BOB_RUNNING):
time.sleep(1)
ctr += 1
if ctr > 60:
break
2022-01-04 20:14:19 +00:00
print('Alice online: ' + str(TEST_SERVER_ALICE_RUNNING))
print('Bob online: ' + str(TEST_SERVER_BOB_RUNNING))
assert ctr <= 60
time.sleep(1)
2021-12-25 23:03:28 +00:00
signing_priv_key_pem = None
2022-01-04 20:14:19 +00:00
session_client = create_session(proxy_type)
2021-08-31 18:30:39 +00:00
# Get Bob's instance actor
print('\n\n*********************************************************')
print("Test Bob's instance actor")
2022-01-04 20:14:19 +00:00
profile_str = 'https://www.w3.org/ns/activitystreams'
test_headers = {
'host': bob_address,
'Accept': 'application/ld+json; profile="' + profile_str + '"'
2021-08-31 18:30:39 +00:00
}
2022-01-04 20:14:19 +00:00
bob_instance_actor_json = \
get_json(signing_priv_key_pem, session_client,
'http://' + bob_address + '/@actor', test_headers, {}, True,
2022-09-10 09:40:13 +00:00
__version__, 'http', 'somedomain.or.other', 10, False)
if not bob_instance_actor_json:
print('Unable to get json for ' + 'http://' + bob_address + '/@actor')
2022-01-04 20:14:19 +00:00
assert bob_instance_actor_json
pprint(bob_instance_actor_json)
assert bob_instance_actor_json['name'] == 'ACTOR'
2021-08-31 18:30:39 +00:00
# In the beginning all was calm and there were no follows
2021-08-05 11:24:24 +00:00
print('\n\n*********************************************************')
print("Alice and Bob agree to share items catalogs")
2022-01-04 20:14:19 +00:00
assert os.path.isdir(alice_dir)
assert os.path.isdir(bob_dir)
set_config_param(alice_dir, 'sharedItemsFederatedDomains', bob_address)
set_config_param(bob_dir, 'sharedItemsFederatedDomains', alice_address)
2021-08-05 11:24:24 +00:00
print('*********************************************************')
print('Alice sends a follow request to Bob')
2022-01-04 20:14:19 +00:00
os.chdir(alice_dir)
session_alice = create_session(proxy_type)
in_reply_to = None
in_reply_to_atom_uri = None
subject = None
2022-01-04 20:14:19 +00:00
alice_post_log = []
save_to_file = True
2021-12-25 20:39:35 +00:00
client_to_server = False
2022-01-04 20:14:19 +00:00
cc_url = None
alice_person_cache = {}
alice_cached_webfingers = {}
alice_post_log = []
bob_actor = http_prefix + '://' + bob_address + '/users/bob'
send_result = \
send_follow_request(session_alice, alice_dir,
'alice', alice_domain,
alice_domain, alice_port, http_prefix,
2022-01-04 20:14:19 +00:00
'bob', bob_domain, bob_actor,
bob_port, http_prefix,
2021-12-28 20:32:11 +00:00
client_to_server, federation_list,
2022-01-04 20:14:19 +00:00
alice_send_threads, alice_post_log,
alice_cached_webfingers, alice_person_cache,
True, __version__, signing_priv_key_pem,
alice_domain, None, None)
2022-01-04 20:14:19 +00:00
print('send_result: ' + str(send_result))
for _ in range(16):
if os.path.isfile(bob_dir + '/accounts/bob@' +
bob_domain + '/followers.txt'):
if os.path.isfile(alice_dir + '/accounts/alice@' +
alice_domain + '/following.txt'):
if os.path.isfile(alice_dir + '/accounts/alice@' +
alice_domain + '/followingCalendar.txt'):
break
time.sleep(1)
2022-01-04 20:14:19 +00:00
assert valid_inbox(bob_dir, 'bob', bob_domain)
assert valid_inbox_filenames(bob_dir, 'bob', bob_domain,
alice_domain, alice_port)
2022-06-10 13:01:39 +00:00
assert text_in_file('alice@' + alice_domain,
bob_dir + '/accounts/bob@' +
bob_domain + '/followers.txt')
assert text_in_file('bob@' + bob_domain,
alice_dir + '/accounts/alice@' +
alice_domain + '/following.txt')
assert text_in_file('bob@' + bob_domain,
alice_dir + '/accounts/alice@' +
alice_domain + '/followingCalendar.txt')
2022-01-04 20:14:19 +00:00
assert not is_group_actor(alice_dir, bob_actor, alice_person_cache)
assert not is_group_account(bob_dir, 'bob', bob_domain)
print('\n\n*********************************************************')
2021-08-04 12:04:35 +00:00
print('Bob publishes some shared items')
2022-01-04 20:14:19 +00:00
if os.path.isdir(bob_dir + '/ontology'):
shutil.rmtree(bob_dir + '/ontology', ignore_errors=False, onerror=None)
os.mkdir(bob_dir + '/ontology')
copyfile(base_dir + '/img/logo.png', bob_dir + '/logo.png')
2021-12-25 16:17:53 +00:00
copyfile(base_dir + '/ontology/foodTypes.json',
2022-01-04 20:14:19 +00:00
bob_dir + '/ontology/foodTypes.json')
2021-12-25 16:17:53 +00:00
copyfile(base_dir + '/ontology/toolTypes.json',
2022-01-04 20:14:19 +00:00
bob_dir + '/ontology/toolTypes.json')
2021-12-25 16:17:53 +00:00
copyfile(base_dir + '/ontology/clothesTypes.json',
2022-01-04 20:14:19 +00:00
bob_dir + '/ontology/clothesTypes.json')
2021-12-25 16:17:53 +00:00
copyfile(base_dir + '/ontology/medicalTypes.json',
2022-01-04 20:14:19 +00:00
bob_dir + '/ontology/medicalTypes.json')
2021-12-25 16:17:53 +00:00
copyfile(base_dir + '/ontology/accommodationTypes.json',
2022-01-04 20:14:19 +00:00
bob_dir + '/ontology/accommodationTypes.json')
assert os.path.isfile(bob_dir + '/logo.png')
assert os.path.isfile(bob_dir + '/ontology/foodTypes.json')
assert os.path.isfile(bob_dir + '/ontology/toolTypes.json')
assert os.path.isfile(bob_dir + '/ontology/clothesTypes.json')
assert os.path.isfile(bob_dir + '/ontology/medicalTypes.json')
assert os.path.isfile(bob_dir + '/ontology/accommodationTypes.json')
shared_item_name = 'cheddar'
shared_item_description = 'Some cheese'
shared_item_image_filename = 'logo.png'
shared_item_qty = 1
shared_item_type = 'Cheese'
shared_item_category = 'Food'
shared_item_location = "Bob's location"
shared_item_duration = "10 days"
shared_item_price = "1.30"
shared_item_currency = "EUR"
2021-12-25 23:03:28 +00:00
signing_priv_key_pem = None
2022-01-04 20:14:19 +00:00
session_bob = create_session(proxy_type)
share_json = \
send_share_via_server(bob_dir, session_bob,
'bob', bob_password,
bob_domain, bob_port,
http_prefix, shared_item_name,
shared_item_description,
shared_item_image_filename,
shared_item_qty, shared_item_type,
shared_item_category,
shared_item_location, shared_item_duration,
bob_cached_webfingers, bob_person_cache,
2021-12-29 21:55:09 +00:00
True, __version__,
2022-01-04 20:14:19 +00:00
shared_item_price, shared_item_currency,
2021-12-29 21:55:09 +00:00
signing_priv_key_pem)
2022-01-04 20:14:19 +00:00
assert share_json
assert isinstance(share_json, dict)
shared_item_name = 'Epicyon T-shirt'
shared_item_description = 'A fashionable item'
shared_item_image_filename = 'logo.png'
shared_item_qty = 1
shared_item_type = 'T-Shirt'
shared_item_category = 'Clothes'
shared_item_location = "Bob's location"
shared_item_duration = "5 days"
shared_item_price = "0"
shared_item_currency = "EUR"
share_json = \
send_share_via_server(bob_dir, session_bob,
'bob', bob_password,
bob_domain, bob_port,
http_prefix, shared_item_name,
shared_item_description,
shared_item_image_filename,
shared_item_qty, shared_item_type,
shared_item_category,
shared_item_location, shared_item_duration,
bob_cached_webfingers, bob_person_cache,
2021-12-29 21:55:09 +00:00
True, __version__,
2022-01-04 20:14:19 +00:00
shared_item_price, shared_item_currency,
2021-12-29 21:55:09 +00:00
signing_priv_key_pem)
2022-01-04 20:14:19 +00:00
assert share_json
assert isinstance(share_json, dict)
shared_item_name = 'Soldering iron'
shared_item_description = 'A soldering iron'
shared_item_image_filename = 'logo.png'
shared_item_qty = 1
shared_item_type = 'Soldering iron'
shared_item_category = 'Tools'
shared_item_location = "Bob's location"
shared_item_duration = "9 days"
shared_item_price = "10.00"
shared_item_currency = "EUR"
share_json = \
send_share_via_server(bob_dir, session_bob,
'bob', bob_password,
bob_domain, bob_port,
http_prefix, shared_item_name,
shared_item_description,
shared_item_image_filename,
shared_item_qty, shared_item_type,
shared_item_category,
shared_item_location, shared_item_duration,
bob_cached_webfingers, bob_person_cache,
2021-12-29 21:55:09 +00:00
True, __version__,
2022-01-04 20:14:19 +00:00
shared_item_price, shared_item_currency,
2021-12-29 21:55:09 +00:00
signing_priv_key_pem)
2022-01-04 20:14:19 +00:00
assert share_json
assert isinstance(share_json, dict)
2021-08-04 12:44:24 +00:00
time.sleep(2)
2021-08-04 12:04:35 +00:00
print('\n\n*********************************************************')
2021-08-04 12:44:24 +00:00
print('Bob has a shares.json file containing the uploaded items')
2021-08-04 12:04:35 +00:00
2022-01-04 20:14:19 +00:00
shares_filename = bob_dir + '/accounts/bob@' + bob_domain + '/shares.json'
assert os.path.isfile(shares_filename)
shares_json = load_json(shares_filename)
assert shares_json
pprint(shares_json)
assert len(shares_json.items()) == 3
for item_id, item in shares_json.items():
if not item.get('dfcId'):
2021-08-06 16:52:12 +00:00
pprint(item)
2022-01-04 20:14:19 +00:00
print(item_id + ' does not have dfcId field')
assert item.get('dfcId')
2021-08-04 12:04:35 +00:00
print('\n\n*********************************************************')
print('Bob can read the shared items catalog on his own instance')
2021-12-25 23:03:28 +00:00
signing_priv_key_pem = None
2022-01-04 20:14:19 +00:00
catalog_json = \
2022-06-12 22:14:47 +00:00
get_shared_items_catalog_via_server(session_bob,
2022-01-04 20:14:19 +00:00
'bob', bob_password,
bob_domain, bob_port,
2021-12-29 21:55:09 +00:00
http_prefix, True,
signing_priv_key_pem)
2022-01-04 20:14:19 +00:00
assert catalog_json
pprint(catalog_json)
assert 'DFC:supplies' in catalog_json
assert len(catalog_json.get('DFC:supplies')) == 3
2021-08-04 12:04:35 +00:00
print('\n\n*********************************************************')
print('Alice sends a message to Bob')
2022-01-04 20:14:19 +00:00
alice_tokens_filename = \
alice_dir + '/accounts/sharedItemsFederationTokens.json'
assert os.path.isfile(alice_tokens_filename)
alice_shared_item_federation_tokens = load_json(alice_tokens_filename)
assert alice_shared_item_federation_tokens
2021-08-05 11:24:24 +00:00
print('Alice shared item federation tokens:')
2022-01-04 20:14:19 +00:00
pprint(alice_shared_item_federation_tokens)
assert len(alice_shared_item_federation_tokens.items()) > 0
for host_str, token in alice_shared_item_federation_tokens.items():
assert ':' in host_str
alice_post_log = []
alice_person_cache = {}
alice_cached_webfingers = {}
alice_shared_items_federated_domains = [bob_address]
alice_post_log = []
is_article = False
city = 'London, England'
2021-12-25 18:20:56 +00:00
low_bandwidth = False
2021-12-25 23:03:28 +00:00
signing_priv_key_pem = None
2022-07-18 16:18:04 +00:00
translate = {}
2022-01-04 20:14:19 +00:00
send_result = \
2021-12-29 21:55:09 +00:00
send_post(signing_priv_key_pem, __version__,
2022-01-04 20:14:19 +00:00
session_alice, alice_dir, 'alice', alice_domain, alice_port,
'bob', bob_domain, bob_port, cc_url,
2022-05-31 16:51:56 +00:00
http_prefix, 'Alice message', save_to_file,
2021-12-29 21:55:09 +00:00
client_to_server, True,
None, None, None, city, federation_list,
2022-01-04 20:14:19 +00:00
alice_send_threads, alice_post_log, alice_cached_webfingers,
alice_person_cache, is_article, system_language,
languages_understood,
2022-01-04 20:14:19 +00:00
alice_shared_items_federated_domains,
alice_shared_item_federation_tokens, low_bandwidth,
2022-07-18 16:18:04 +00:00
content_license_url, translate, True,
2022-01-04 20:14:19 +00:00
in_reply_to, in_reply_to_atom_uri, subject)
print('send_result: ' + str(send_result))
2022-01-04 20:14:19 +00:00
queue_path = bob_dir + '/accounts/bob@' + bob_domain + '/queue'
inbox_path = bob_dir + '/accounts/bob@' + bob_domain + '/inbox'
alice_message_arrived = False
for _ in range(20):
2019-10-15 09:31:10 +00:00
time.sleep(1)
2022-01-04 20:14:19 +00:00
if os.path.isdir(inbox_path):
if len([name for name in os.listdir(inbox_path)
if os.path.isfile(os.path.join(inbox_path, name))]) > 0:
alice_message_arrived = True
2019-10-15 09:31:10 +00:00
print('Alice message sent to Bob!')
break
2022-01-04 20:14:19 +00:00
assert alice_message_arrived is True
2019-10-15 09:31:10 +00:00
print('Message from Alice to Bob succeeded')
2021-08-05 11:24:24 +00:00
print('\n\n*********************************************************')
print('Check that Alice received the shared items authorization')
print('token from Bob')
2022-01-04 20:14:19 +00:00
alice_tokens_filename = \
alice_dir + '/accounts/sharedItemsFederationTokens.json'
bob_tokens_filename = \
bob_dir + '/accounts/sharedItemsFederationTokens.json'
assert os.path.isfile(alice_tokens_filename)
assert os.path.isfile(bob_tokens_filename)
alice_tokens = load_json(alice_tokens_filename)
assert alice_tokens
for host_str, token in alice_tokens.items():
assert ':' in host_str
assert alice_tokens.get(alice_address)
2021-08-05 11:24:24 +00:00
print('Alice tokens')
2022-01-04 20:14:19 +00:00
pprint(alice_tokens)
bob_tokens = load_json(bob_tokens_filename)
assert bob_tokens
for host_str, token in bob_tokens.items():
assert ':' in host_str
assert bob_tokens.get(bob_address)
2021-08-05 11:24:24 +00:00
print("Check that Bob now has Alice's token")
2022-01-04 20:14:19 +00:00
assert bob_tokens.get(alice_address)
2021-08-05 11:24:24 +00:00
print('Bob tokens')
2022-01-04 20:14:19 +00:00
pprint(bob_tokens)
2021-08-05 11:24:24 +00:00
print('\n\n*********************************************************')
print('Alice can read the federated shared items catalog of Bob')
headers = {
2022-01-04 20:14:19 +00:00
'Origin': alice_address,
'Authorization': bob_tokens[bob_address],
'host': bob_address,
'Accept': 'application/json'
}
2022-01-04 20:14:19 +00:00
url = http_prefix + '://' + bob_address + '/catalog'
2021-12-25 23:03:28 +00:00
signing_priv_key_pem = None
2022-01-04 20:14:19 +00:00
catalog_json = get_json(signing_priv_key_pem, session_alice, url, headers,
None, True)
assert catalog_json
pprint(catalog_json)
assert 'DFC:supplies' in catalog_json
assert len(catalog_json.get('DFC:supplies')) == 3
# queue item removed
ctr = 0
2022-01-04 20:14:19 +00:00
while len([name for name in os.listdir(queue_path)
if os.path.isfile(os.path.join(queue_path, name))]) > 0:
ctr += 1
if ctr > 10:
break
time.sleep(1)
2022-01-04 20:14:19 +00:00
# assert len([name for name in os.listdir(queue_path)
# if os.path.isfile(os.path.join(queue_path, name))]) == 0
2019-10-15 09:31:10 +00:00
# stop the servers
2022-01-04 20:14:19 +00:00
THR_ALICE.kill()
THR_ALICE.join()
assert THR_ALICE.is_alive() is False
2019-10-15 09:31:10 +00:00
2022-01-04 20:14:19 +00:00
THR_BOB.kill()
THR_BOB.join()
assert THR_BOB.is_alive() is False
2020-03-22 21:16:02 +00:00
2021-12-25 16:17:53 +00:00
os.chdir(base_dir)
shutil.rmtree(base_dir + '/.tests', ignore_errors=False, onerror=None)
2021-08-04 12:04:35 +00:00
print('Testing federation of shared items between ' +
'Alice and Bob is complete')
2020-04-05 13:25:47 +00:00
2019-07-06 13:49:25 +00:00
2021-12-29 21:55:09 +00:00
def test_group_follow(base_dir: str) -> None:
2021-07-30 19:20:49 +00:00
print('Testing following of a group')
2022-01-04 20:14:19 +00:00
global TEST_SERVER_ALICE_RUNNING
global TEST_SERVER_BOB_RUNNING
global TEST_SERVER_GROUP_RUNNING
2021-12-25 23:03:28 +00:00
system_language = 'en'
languages_understood = [system_language]
2022-01-04 20:14:19 +00:00
TEST_SERVER_ALICE_RUNNING = False
TEST_SERVER_BOB_RUNNING = False
TEST_SERVER_GROUP_RUNNING = False
2021-07-30 19:20:49 +00:00
2021-12-25 23:03:28 +00:00
# system_language = 'en'
2021-12-25 17:09:22 +00:00
http_prefix = 'http'
2021-12-25 21:09:22 +00:00
proxy_type = None
2021-12-25 23:45:30 +00:00
federation_list = []
2021-12-25 17:13:38 +00:00
content_license_url = 'https://creativecommons.org/licenses/by/4.0'
2021-07-30 19:20:49 +00:00
2021-12-25 16:17:53 +00:00
if os.path.isdir(base_dir + '/.tests'):
shutil.rmtree(base_dir + '/.tests', ignore_errors=False, onerror=None)
os.mkdir(base_dir + '/.tests')
2021-07-30 19:20:49 +00:00
# create the servers
2022-01-04 20:14:19 +00:00
alice_dir = base_dir + '/.tests/alice'
alice_domain = '127.0.0.57'
alice_port = 61927
alice_send_threads = []
alice_address = alice_domain + ':' + str(alice_port)
bob_dir = base_dir + '/.tests/bob'
bob_domain = '127.0.0.59'
bob_port = 61814
bob_send_threads = []
# bob_address = bob_domain + ':' + str(bob_port)
testgroup_dir = base_dir + '/.tests/testgroup'
testgroup_domain = '127.0.0.63'
2021-07-30 19:20:49 +00:00
testgroupPort = 61925
testgroupSendThreads = []
2022-01-04 20:14:19 +00:00
testgroupAddress = testgroup_domain + ':' + str(testgroupPort)
2021-07-30 19:20:49 +00:00
2022-01-04 20:14:19 +00:00
global THR_ALICE
if THR_ALICE:
while THR_ALICE.is_alive():
THR_ALICE.stop()
2021-07-30 19:20:49 +00:00
time.sleep(1)
2022-01-04 20:14:19 +00:00
THR_ALICE.kill()
2021-07-30 19:20:49 +00:00
2022-01-04 20:14:19 +00:00
THR_ALICE = \
2021-12-29 21:55:09 +00:00
thread_with_trace(target=create_server_alice,
2022-01-04 20:14:19 +00:00
args=(alice_dir, alice_domain, alice_port,
2021-12-28 21:36:27 +00:00
testgroupAddress,
federation_list, False, True,
2022-01-04 20:14:19 +00:00
alice_send_threads),
2021-12-28 21:36:27 +00:00
daemon=True)
2021-07-30 19:20:49 +00:00
2022-01-04 20:14:19 +00:00
global THR_BOB
if THR_BOB:
while THR_BOB.is_alive():
THR_BOB.stop()
2021-08-01 13:25:11 +00:00
time.sleep(1)
2022-01-04 20:14:19 +00:00
THR_BOB.kill()
2021-08-01 13:25:11 +00:00
2022-01-04 20:14:19 +00:00
THR_BOB = \
2021-12-29 21:55:09 +00:00
thread_with_trace(target=create_server_bob,
2022-01-04 20:14:19 +00:00
args=(bob_dir, bob_domain, bob_port, None,
2021-12-28 21:36:27 +00:00
federation_list, False, False,
2022-01-04 20:14:19 +00:00
bob_send_threads),
2021-12-28 21:36:27 +00:00
daemon=True)
2021-08-01 13:25:11 +00:00
2022-01-04 20:14:19 +00:00
global THR_GROUP
if THR_GROUP:
while THR_GROUP.is_alive():
THR_GROUP.stop()
2021-07-30 19:20:49 +00:00
time.sleep(1)
2022-01-04 20:14:19 +00:00
THR_GROUP.kill()
2021-07-30 19:20:49 +00:00
2022-01-04 20:14:19 +00:00
THR_GROUP = \
2021-12-29 21:55:09 +00:00
thread_with_trace(target=create_server_group,
2022-01-04 20:14:19 +00:00
args=(testgroup_dir, testgroup_domain, testgroupPort,
2021-12-28 21:36:27 +00:00
federation_list, False, False,
testgroupSendThreads),
daemon=True)
2021-07-30 19:20:49 +00:00
2022-01-04 20:14:19 +00:00
THR_ALICE.start()
THR_BOB.start()
THR_GROUP.start()
assert THR_ALICE.is_alive() is True
assert THR_BOB.is_alive() is True
assert THR_GROUP.is_alive() is True
2021-07-30 19:20:49 +00:00
# wait for all servers to be running
ctr = 0
2022-01-04 20:14:19 +00:00
while not (TEST_SERVER_ALICE_RUNNING and
TEST_SERVER_BOB_RUNNING and
TEST_SERVER_GROUP_RUNNING):
2021-07-30 19:20:49 +00:00
time.sleep(1)
ctr += 1
if ctr > 60:
break
2022-01-04 20:14:19 +00:00
print('Alice online: ' + str(TEST_SERVER_ALICE_RUNNING))
print('Bob online: ' + str(TEST_SERVER_BOB_RUNNING))
print('Test Group online: ' + str(TEST_SERVER_GROUP_RUNNING))
2021-07-30 19:20:49 +00:00
assert ctr <= 60
time.sleep(1)
2021-08-01 19:19:45 +00:00
print('*********************************************************')
print('Alice has some outbox posts')
2022-01-04 20:14:19 +00:00
alice_outbox = 'http://' + alice_address + '/users/alice/outbox'
2021-12-28 16:56:57 +00:00
session = create_session(None)
2022-01-04 20:14:19 +00:00
profile_str = 'https://www.w3.org/ns/activitystreams'
as_header = {
'Accept': 'application/ld+json; profile="' + profile_str + '"'
2021-08-01 19:19:45 +00:00
}
2021-12-25 23:03:28 +00:00
signing_priv_key_pem = None
2022-01-04 20:14:19 +00:00
outbox_json = get_json(signing_priv_key_pem, session, alice_outbox,
as_header, None, True, __version__, 'http', None)
assert outbox_json
pprint(outbox_json)
assert outbox_json['type'] == 'OrderedCollection'
assert 'first' in outbox_json
first_page = outbox_json['first']
assert 'totalItems' in outbox_json
print('Alice outbox totalItems: ' + str(outbox_json['totalItems']))
assert outbox_json['totalItems'] == 3
outbox_json = get_json(signing_priv_key_pem, session,
first_page, as_header,
None, True, __version__, 'http', None)
assert outbox_json
pprint(outbox_json)
assert 'orderedItems' in outbox_json
assert outbox_json['type'] == 'OrderedCollectionPage'
2021-08-01 19:19:45 +00:00
print('Alice outbox orderedItems: ' +
2022-01-04 20:14:19 +00:00
str(len(outbox_json['orderedItems'])))
assert len(outbox_json['orderedItems']) == 3
2021-08-01 19:19:45 +00:00
2022-01-04 20:14:19 +00:00
queue_path = \
testgroup_dir + '/accounts/testgroup@' + testgroup_domain + '/queue'
2021-07-30 19:20:49 +00:00
# In the beginning the test group had no followers
print('*********************************************************')
print('Alice sends a follow request to the test group')
2022-01-04 20:14:19 +00:00
os.chdir(alice_dir)
session_alice = create_session(proxy_type)
in_reply_to = None
in_reply_to_atom_uri = None
2021-07-31 11:56:28 +00:00
subject = None
2022-01-04 20:14:19 +00:00
alice_post_log = []
save_to_file = True
2021-12-25 20:39:35 +00:00
client_to_server = False
2022-01-04 20:14:19 +00:00
cc_url = None
alice_person_cache = {}
alice_cached_webfingers = {}
alice_post_log = []
# aliceActor = http_prefix + '://' + alice_address + '/users/alice'
testgroup_actor = \
2021-12-25 17:09:22 +00:00
http_prefix + '://' + testgroupAddress + '/users/testgroup'
2021-12-25 23:03:28 +00:00
signing_priv_key_pem = None
2022-01-04 20:14:19 +00:00
send_result = \
send_follow_request(session_alice, alice_dir,
'alice', alice_domain,
alice_domain, alice_port, http_prefix,
2022-01-04 20:14:19 +00:00
'testgroup', testgroup_domain, testgroup_actor,
2021-12-28 20:32:11 +00:00
testgroupPort, http_prefix,
client_to_server, federation_list,
2022-01-04 20:14:19 +00:00
alice_send_threads, alice_post_log,
alice_cached_webfingers, alice_person_cache,
True, __version__, signing_priv_key_pem,
alice_domain, None, None)
2022-01-04 20:14:19 +00:00
print('send_result: ' + str(send_result))
2021-07-30 19:20:49 +00:00
2022-01-04 20:14:19 +00:00
alice_following_filename = \
alice_dir + '/accounts/alice@' + alice_domain + '/following.txt'
alice_following_calendar_filename = \
alice_dir + '/accounts/alice@' + alice_domain + \
2021-07-31 11:56:28 +00:00
'/followingCalendar.txt'
2022-01-04 20:14:19 +00:00
testgroup_followers_filename = \
testgroup_dir + '/accounts/testgroup@' + testgroup_domain + \
2021-07-31 11:56:28 +00:00
'/followers.txt'
2022-01-04 20:14:19 +00:00
for _ in range(16):
if os.path.isfile(testgroup_followers_filename):
if os.path.isfile(alice_following_filename):
if os.path.isfile(alice_following_calendar_filename):
2021-07-30 19:20:49 +00:00
break
time.sleep(1)
2022-01-04 20:14:19 +00:00
assert valid_inbox(testgroup_dir, 'testgroup', testgroup_domain)
assert valid_inbox_filenames(testgroup_dir, 'testgroup', testgroup_domain,
alice_domain, alice_port)
2022-06-10 13:01:39 +00:00
assert text_in_file('alice@' + alice_domain, testgroup_followers_filename)
assert not text_in_file('!alice@' + alice_domain,
testgroup_followers_filename)
2022-01-04 20:14:19 +00:00
testgroup_webfinger_filename = \
testgroup_dir + '/wfendpoints/testgroup@' + \
testgroup_domain + ':' + str(testgroupPort) + '.json'
assert os.path.isfile(testgroup_webfinger_filename)
2022-06-10 13:01:39 +00:00
assert text_in_file('acct:testgroup@', testgroup_webfinger_filename)
print('acct: exists within the webfinger endpoint for testgroup')
2021-07-31 11:56:28 +00:00
2022-01-04 20:14:19 +00:00
testgroup_handle = 'testgroup@' + testgroup_domain
following_str = ''
2022-06-09 14:46:30 +00:00
with open(alice_following_filename, 'r', encoding='utf-8') as fp_foll:
2022-01-04 20:14:19 +00:00
following_str = fp_foll.read()
print('Alice following.txt:\n\n' + following_str)
if '!testgroup' not in following_str:
2021-07-31 11:56:28 +00:00
print('Alice following.txt does not contain !testgroup@' +
2022-01-04 20:14:19 +00:00
testgroup_domain + ':' + str(testgroupPort))
assert is_group_actor(alice_dir, testgroup_actor, alice_person_cache)
assert not is_group_account(alice_dir, 'alice', alice_domain)
assert is_group_account(testgroup_dir, 'testgroup', testgroup_domain)
assert '!testgroup' in following_str
2022-06-10 13:01:39 +00:00
assert text_in_file(testgroup_handle, alice_following_filename)
assert text_in_file(testgroup_handle, alice_following_calendar_filename)
2021-08-01 13:25:11 +00:00
print('\n\n*********************************************************')
2021-07-31 11:56:28 +00:00
print('Alice follows the test group')
2021-08-01 13:25:11 +00:00
print('*********************************************************')
print('Bob sends a follow request to the test group')
2022-01-04 20:14:19 +00:00
os.chdir(bob_dir)
session_bob = create_session(proxy_type)
in_reply_to = None
in_reply_to_atom_uri = None
2021-08-01 13:25:11 +00:00
subject = None
2022-01-04 20:14:19 +00:00
bob_post_log = []
save_to_file = True
2021-12-25 20:39:35 +00:00
client_to_server = False
2022-01-04 20:14:19 +00:00
cc_url = None
bob_person_cache = {}
bob_cached_webfingers = {}
bob_post_log = []
# bob_actor = http_prefix + '://' + bob_address + '/users/bob'
testgroup_actor = \
2021-12-25 17:09:22 +00:00
http_prefix + '://' + testgroupAddress + '/users/testgroup'
2021-12-25 23:03:28 +00:00
signing_priv_key_pem = None
2022-01-04 20:14:19 +00:00
send_result = \
send_follow_request(session_bob, bob_dir,
'bob', bob_domain,
bob_domain, bob_port, http_prefix,
2022-01-04 20:14:19 +00:00
'testgroup', testgroup_domain, testgroup_actor,
2021-12-28 20:32:11 +00:00
testgroupPort, http_prefix,
client_to_server, federation_list,
2022-01-04 20:14:19 +00:00
bob_send_threads, bob_post_log,
bob_cached_webfingers, bob_person_cache,
True, __version__, signing_priv_key_pem,
bob_domain, None, None)
2022-01-04 20:14:19 +00:00
print('send_result: ' + str(send_result))
2021-08-01 13:25:11 +00:00
2022-01-04 20:14:19 +00:00
bob_following_filename = \
bob_dir + '/accounts/bob@' + bob_domain + '/following.txt'
bob_following_calendar_filename = \
bob_dir + '/accounts/bob@' + bob_domain + \
2021-08-01 13:25:11 +00:00
'/followingCalendar.txt'
2022-01-04 20:14:19 +00:00
testgroup_followers_filename = \
testgroup_dir + '/accounts/testgroup@' + testgroup_domain + \
2021-08-01 13:25:11 +00:00
'/followers.txt'
2022-01-04 20:14:19 +00:00
for _ in range(16):
if os.path.isfile(testgroup_followers_filename):
if os.path.isfile(bob_following_filename):
if os.path.isfile(bob_following_calendar_filename):
2021-08-01 13:25:11 +00:00
break
time.sleep(1)
2022-01-04 20:14:19 +00:00
assert valid_inbox(testgroup_dir, 'testgroup', testgroup_domain)
assert valid_inbox_filenames(testgroup_dir, 'testgroup', testgroup_domain,
bob_domain, bob_port)
2022-06-10 13:01:39 +00:00
assert text_in_file('bob@' + bob_domain, testgroup_followers_filename)
assert not text_in_file('!bob@' + bob_domain,
testgroup_followers_filename)
2022-01-04 20:14:19 +00:00
testgroup_webfinger_filename = \
testgroup_dir + '/wfendpoints/testgroup@' + \
testgroup_domain + ':' + str(testgroupPort) + '.json'
assert os.path.isfile(testgroup_webfinger_filename)
2022-06-10 13:01:39 +00:00
assert text_in_file('acct:testgroup@', testgroup_webfinger_filename)
print('acct: exists within the webfinger endpoint for testgroup')
2021-08-01 13:25:11 +00:00
2022-01-04 20:14:19 +00:00
testgroup_handle = 'testgroup@' + testgroup_domain
following_str = ''
2022-06-09 14:46:30 +00:00
with open(bob_following_filename, 'r', encoding='utf-8') as fp_foll:
2022-01-04 20:14:19 +00:00
following_str = fp_foll.read()
print('Bob following.txt:\n\n' + following_str)
if '!testgroup' not in following_str:
2021-08-01 13:25:11 +00:00
print('Bob following.txt does not contain !testgroup@' +
2022-01-04 20:14:19 +00:00
testgroup_domain + ':' + str(testgroupPort))
assert is_group_actor(bob_dir, testgroup_actor, bob_person_cache)
assert '!testgroup' in following_str
2022-06-10 13:01:39 +00:00
assert text_in_file(testgroup_handle, bob_following_filename)
assert text_in_file(testgroup_handle, bob_following_calendar_filename)
2021-08-01 13:25:11 +00:00
print('Bob follows the test group')
2021-07-31 11:56:28 +00:00
print('\n\n*********************************************************')
print('Alice posts to the test group')
2022-01-04 20:14:19 +00:00
inbox_path_bob = \
bob_dir + '/accounts/bob@' + bob_domain + '/inbox'
start_posts_bob = \
len([name for name in os.listdir(inbox_path_bob)
if os.path.isfile(os.path.join(inbox_path_bob, name))])
assert start_posts_bob == 0
alice_post_log = []
alice_person_cache = {}
alice_cached_webfingers = {}
alice_shared_items_federated_domains = []
alice_shared_item_federation_tokens = {}
alice_post_log = []
is_article = False
2021-07-31 11:56:28 +00:00
city = 'London, England'
2021-12-25 18:20:56 +00:00
low_bandwidth = False
2021-12-25 23:03:28 +00:00
signing_priv_key_pem = None
2021-09-18 10:12:07 +00:00
2022-01-04 20:14:19 +00:00
queue_path = \
testgroup_dir + '/accounts/testgroup@' + testgroup_domain + '/queue'
inbox_path = \
testgroup_dir + '/accounts/testgroup@' + testgroup_domain + '/inbox'
outbox_path = \
testgroup_dir + '/accounts/testgroup@' + testgroup_domain + '/outbox'
alice_message_arrived = False
start_posts_inbox = \
len([name for name in os.listdir(inbox_path)
if os.path.isfile(os.path.join(inbox_path, name))])
start_posts_outbox = \
len([name for name in os.listdir(outbox_path)
if os.path.isfile(os.path.join(outbox_path, name))])
2022-07-18 16:18:04 +00:00
translate = {}
2022-01-04 20:14:19 +00:00
send_result = \
2021-12-29 21:55:09 +00:00
send_post(signing_priv_key_pem, __version__,
2022-01-04 20:14:19 +00:00
session_alice, alice_dir, 'alice', alice_domain, alice_port,
'testgroup', testgroup_domain, testgroupPort, cc_url,
2022-05-31 16:51:56 +00:00
http_prefix, "Alice group message",
2022-01-04 20:14:19 +00:00
save_to_file, client_to_server, True,
2021-12-29 21:55:09 +00:00
None, None, None, city, federation_list,
2022-01-04 20:14:19 +00:00
alice_send_threads, alice_post_log, alice_cached_webfingers,
alice_person_cache, is_article, system_language,
languages_understood,
2022-01-04 20:14:19 +00:00
alice_shared_items_federated_domains,
alice_shared_item_federation_tokens, low_bandwidth,
2022-07-18 16:18:04 +00:00
content_license_url, translate,
2022-01-04 20:14:19 +00:00
in_reply_to, in_reply_to_atom_uri, subject)
print('send_result: ' + str(send_result))
2021-07-31 11:56:28 +00:00
2022-01-04 20:14:19 +00:00
for _ in range(20):
2021-07-31 11:56:28 +00:00
time.sleep(1)
2022-01-04 20:14:19 +00:00
if os.path.isdir(inbox_path):
curr_posts_inbox = \
len([name for name in os.listdir(inbox_path)
if os.path.isfile(os.path.join(inbox_path, name))])
curr_posts_outbox = \
len([name for name in os.listdir(outbox_path)
if os.path.isfile(os.path.join(outbox_path, name))])
if curr_posts_inbox > start_posts_inbox and \
curr_posts_outbox > start_posts_outbox:
alice_message_arrived = True
2021-07-31 11:56:28 +00:00
print('Alice post sent to test group!')
break
2022-01-04 20:14:19 +00:00
assert alice_message_arrived is True
2021-08-01 13:25:11 +00:00
print('\n\n*********************************************************')
2021-07-31 11:56:28 +00:00
print('Post from Alice to test group succeeded')
2021-08-01 19:19:45 +00:00
2021-08-01 13:25:11 +00:00
print('\n\n*********************************************************')
print('Check that post was relayed from test group to bob')
2022-01-04 20:14:19 +00:00
bob_message_arrived = False
for _ in range(20):
2021-08-01 13:25:11 +00:00
time.sleep(1)
2022-01-04 20:14:19 +00:00
if os.path.isdir(inbox_path_bob):
curr_posts_bob = \
len([name for name in os.listdir(inbox_path_bob)
if os.path.isfile(os.path.join(inbox_path_bob, name))])
if curr_posts_bob > start_posts_bob:
bob_message_arrived = True
2021-08-01 13:25:11 +00:00
print('Bob received relayed group post!')
break
2022-01-04 20:14:19 +00:00
assert bob_message_arrived is True
2021-07-30 19:20:49 +00:00
2021-09-18 10:29:04 +00:00
# check that the received post has an id from the group,
# not from the original sender (alice)
2022-01-04 20:14:19 +00:00
group_id_checked = False
for name in os.listdir(inbox_path_bob):
filename = os.path.join(inbox_path_bob, name)
2021-09-18 10:29:04 +00:00
if os.path.isfile(filename):
2022-01-04 20:14:19 +00:00
received_json = load_json(filename)
assert received_json
print('Received group post ' + received_json['id'])
assert '/testgroup/statuses/' in received_json['id']
group_id_checked = True
2021-09-18 10:29:04 +00:00
break
2022-01-04 20:14:19 +00:00
assert group_id_checked
2021-09-18 10:29:04 +00:00
2021-07-30 19:20:49 +00:00
# stop the servers
2022-01-04 20:14:19 +00:00
THR_ALICE.kill()
THR_ALICE.join()
assert THR_ALICE.is_alive() is False
2021-07-30 19:20:49 +00:00
2022-01-04 20:14:19 +00:00
THR_BOB.kill()
THR_BOB.join()
assert THR_BOB.is_alive() is False
2021-08-01 13:25:11 +00:00
2022-01-04 20:14:19 +00:00
THR_GROUP.kill()
THR_GROUP.join()
assert THR_GROUP.is_alive() is False
2021-07-30 19:20:49 +00:00
# queue item removed
time.sleep(4)
2022-01-04 20:14:19 +00:00
assert len([name for name in os.listdir(queue_path)
if os.path.isfile(os.path.join(queue_path, name))]) == 0
2021-07-30 19:20:49 +00:00
2021-12-25 16:17:53 +00:00
os.chdir(base_dir)
shutil.rmtree(base_dir + '/.tests', ignore_errors=False, onerror=None)
2021-07-31 11:56:28 +00:00
print('Testing following of a group is complete')
2021-07-30 19:20:49 +00:00
2021-12-29 21:55:09 +00:00
def _test_followers_of_person(base_dir: str) -> None:
2022-01-04 20:14:19 +00:00
print('test_followers_of_person')
curr_dir = base_dir
2020-04-05 13:25:47 +00:00
nickname = 'mxpop'
domain = 'diva.domain'
password = 'birb'
port = 80
2021-12-25 17:09:22 +00:00
http_prefix = 'https'
2021-12-25 23:45:30 +00:00
federation_list = []
2022-01-04 20:14:19 +00:00
base_dir = curr_dir + '/.tests_followersofperson'
2021-12-25 16:17:53 +00:00
if os.path.isdir(base_dir):
shutil.rmtree(base_dir, ignore_errors=False, onerror=None)
os.mkdir(base_dir)
os.chdir(base_dir)
2021-12-29 21:55:09 +00:00
create_person(base_dir, nickname, domain, port,
http_prefix, True, False, password)
create_person(base_dir, 'maxboardroom', domain, port,
http_prefix, True, False, password)
create_person(base_dir, 'ultrapancake', domain, port,
http_prefix, True, False, password)
create_person(base_dir, 'drokk', domain, port,
http_prefix, True, False, password)
create_person(base_dir, 'sausagedog', domain, port,
http_prefix, True, False, password)
clear_follows(base_dir, nickname, domain)
2021-12-27 17:08:19 +00:00
follow_person(base_dir, nickname, domain, 'maxboardroom', domain,
federation_list, False, False)
follow_person(base_dir, 'drokk', domain, 'ultrapancake', domain,
federation_list, False, False)
# deliberate duplication
2021-12-27 17:08:19 +00:00
follow_person(base_dir, 'drokk', domain, 'ultrapancake', domain,
federation_list, False, False)
follow_person(base_dir, 'sausagedog', domain, 'ultrapancake', domain,
federation_list, False, False)
follow_person(base_dir, nickname, domain, 'ultrapancake', domain,
federation_list, False, False)
follow_person(base_dir, nickname, domain, 'someother', 'randodomain.net',
federation_list, False, False)
2020-04-05 13:25:47 +00:00
2022-01-04 20:14:19 +00:00
follow_list = get_followers_of_person(base_dir, 'ultrapancake', domain)
assert len(follow_list) == 3
assert 'mxpop@' + domain in follow_list
assert 'drokk@' + domain in follow_list
assert 'sausagedog@' + domain in follow_list
os.chdir(curr_dir)
2021-12-25 16:17:53 +00:00
shutil.rmtree(base_dir, ignore_errors=False, onerror=None)
2020-04-05 13:25:47 +00:00
2021-12-29 21:55:09 +00:00
def _test_followers_on_domain(base_dir: str) -> None:
2022-01-04 20:14:19 +00:00
print('test_followers_on_domain')
curr_dir = base_dir
2020-04-05 13:25:47 +00:00
nickname = 'mxpop'
domain = 'diva.domain'
otherdomain = 'soup.dragon'
password = 'birb'
port = 80
2021-12-25 17:09:22 +00:00
http_prefix = 'https'
2021-12-25 23:45:30 +00:00
federation_list = []
2022-01-04 20:14:19 +00:00
base_dir = curr_dir + '/.tests_nooffollowersOndomain'
2021-12-25 16:17:53 +00:00
if os.path.isdir(base_dir):
shutil.rmtree(base_dir, ignore_errors=False, onerror=None)
os.mkdir(base_dir)
os.chdir(base_dir)
2021-12-29 21:55:09 +00:00
create_person(base_dir, nickname, domain, port, http_prefix, True,
False, password)
create_person(base_dir, 'maxboardroom', otherdomain, port,
http_prefix, True, False, password)
create_person(base_dir, 'ultrapancake', otherdomain, port,
http_prefix, True, False, password)
create_person(base_dir, 'drokk', otherdomain, port,
http_prefix, True, False, password)
create_person(base_dir, 'sausagedog', otherdomain, port,
http_prefix, True, False, password)
2020-04-05 13:25:47 +00:00
2021-12-27 17:08:19 +00:00
follow_person(base_dir, 'drokk', otherdomain, nickname, domain,
federation_list, False, False)
follow_person(base_dir, 'sausagedog', otherdomain, nickname, domain,
federation_list, False, False)
follow_person(base_dir, 'maxboardroom', otherdomain, nickname, domain,
federation_list, False, False)
2020-04-05 13:25:47 +00:00
2021-12-29 21:55:09 +00:00
add_follower_of_person(base_dir, nickname, domain,
'cucumber', 'sandwiches.party',
federation_list, False, False)
add_follower_of_person(base_dir, nickname, domain,
'captainsensible', 'damned.zone',
federation_list, False, False)
add_follower_of_person(base_dir, nickname, domain,
'pilchard', 'zombies.attack',
federation_list, False, False)
add_follower_of_person(base_dir, nickname, domain, 'drokk', otherdomain,
federation_list, False, False)
add_follower_of_person(base_dir, nickname, domain,
'sausagedog', otherdomain,
federation_list, False, False)
add_follower_of_person(base_dir, nickname, domain,
'maxboardroom', otherdomain,
federation_list, False, False)
2020-04-05 13:25:47 +00:00
2022-01-04 20:14:19 +00:00
followers_on_other_domain = \
2021-12-29 21:55:09 +00:00
no_of_followers_on_domain(base_dir,
nickname + '@' + domain, otherdomain)
2022-01-04 20:14:19 +00:00
assert followers_on_other_domain == 3
2020-04-05 13:25:47 +00:00
2021-12-29 21:55:09 +00:00
unfollower_of_account(base_dir, nickname, domain, 'sausagedog',
otherdomain, False, False)
2022-01-04 20:14:19 +00:00
followers_on_other_domain = \
2021-12-29 21:55:09 +00:00
no_of_followers_on_domain(base_dir,
nickname + '@' + domain, otherdomain)
2022-01-04 20:14:19 +00:00
assert followers_on_other_domain == 2
2020-03-22 21:16:02 +00:00
2022-01-04 20:14:19 +00:00
os.chdir(curr_dir)
2021-12-25 16:17:53 +00:00
shutil.rmtree(base_dir, ignore_errors=False, onerror=None)
2020-04-05 13:25:47 +00:00
2021-12-29 21:55:09 +00:00
def _test_group_followers(base_dir: str) -> None:
print('test_group_followers')
2022-01-04 20:14:19 +00:00
curr_dir = base_dir
2020-04-05 13:25:47 +00:00
nickname = 'test735'
domain = 'mydomain.com'
password = 'somepass'
port = 80
2021-12-25 17:09:22 +00:00
http_prefix = 'https'
2021-12-25 23:45:30 +00:00
federation_list = []
2022-01-04 20:14:19 +00:00
base_dir = curr_dir + '/.tests_testgroupfollowers'
2021-12-25 16:17:53 +00:00
if os.path.isdir(base_dir):
shutil.rmtree(base_dir, ignore_errors=False, onerror=None)
os.mkdir(base_dir)
os.chdir(base_dir)
2021-12-29 21:55:09 +00:00
create_person(base_dir, nickname, domain, port, http_prefix, True,
False, password)
clear_followers(base_dir, nickname, domain)
add_follower_of_person(base_dir, nickname, domain, 'badger', 'wild.domain',
federation_list, False, False)
add_follower_of_person(base_dir, nickname, domain,
'squirrel', 'wild.domain',
federation_list, False, False)
add_follower_of_person(base_dir, nickname, domain, 'rodent', 'wild.domain',
federation_list, False, False)
add_follower_of_person(base_dir, nickname, domain,
'utterly', 'clutterly.domain',
federation_list, False, False)
add_follower_of_person(base_dir, nickname, domain, 'zonked', 'zzz.domain',
federation_list, False, False)
add_follower_of_person(base_dir, nickname, domain, 'nap', 'zzz.domain',
federation_list, False, False)
grouped = group_followers_by_domain(base_dir, nickname, domain)
2020-04-05 13:25:47 +00:00
assert len(grouped.items()) == 3
assert grouped.get('zzz.domain')
assert grouped.get('clutterly.domain')
assert grouped.get('wild.domain')
2020-04-05 13:25:47 +00:00
assert len(grouped['zzz.domain']) == 2
assert len(grouped['wild.domain']) == 3
assert len(grouped['clutterly.domain']) == 1
2020-03-22 21:16:02 +00:00
2022-01-04 20:14:19 +00:00
os.chdir(curr_dir)
2021-12-25 16:17:53 +00:00
shutil.rmtree(base_dir, ignore_errors=False, onerror=None)
2020-03-22 21:16:02 +00:00
2021-12-29 21:55:09 +00:00
def _test_follows(base_dir: str) -> None:
2022-01-04 20:14:19 +00:00
print('test_follows')
curr_dir = base_dir
2020-04-05 13:25:47 +00:00
nickname = 'test529'
domain = 'testdomain.com'
password = 'mypass'
port = 80
2021-12-25 17:09:22 +00:00
http_prefix = 'https'
2021-12-25 23:45:30 +00:00
federation_list = ['wild.com', 'mesh.com']
2022-01-04 20:14:19 +00:00
base_dir = curr_dir + '/.tests_testfollows'
2021-12-25 16:17:53 +00:00
if os.path.isdir(base_dir):
shutil.rmtree(base_dir, ignore_errors=False, onerror=None)
os.mkdir(base_dir)
os.chdir(base_dir)
2021-12-29 21:55:09 +00:00
create_person(base_dir, nickname, domain, port, http_prefix, True,
False, password)
2020-04-05 13:25:47 +00:00
2021-12-29 21:55:09 +00:00
clear_follows(base_dir, nickname, domain)
2021-12-27 17:08:19 +00:00
follow_person(base_dir, nickname, domain, 'badger', 'wild.com',
federation_list, False, False)
follow_person(base_dir, nickname, domain, 'squirrel', 'secret.com',
federation_list, False, False)
follow_person(base_dir, nickname, domain, 'rodent', 'drainpipe.com',
federation_list, False, False)
follow_person(base_dir, nickname, domain, 'batman', 'mesh.com',
federation_list, False, False)
follow_person(base_dir, nickname, domain, 'giraffe', 'trees.com',
federation_list, False, False)
2020-04-05 13:25:47 +00:00
2022-01-04 20:14:19 +00:00
account_dir = acct_dir(base_dir, nickname, domain)
2022-06-09 14:46:30 +00:00
with open(account_dir + '/following.txt', 'r',
encoding='utf-8') as fp_foll:
2022-01-04 20:14:19 +00:00
domain_found = False
for following_domain in fp_foll:
test_domain = following_domain.split('@')[1]
2022-06-21 11:58:50 +00:00
test_domain = remove_eol(test_domain)
2022-01-04 20:14:19 +00:00
if test_domain == 'mesh.com':
domain_found = True
if test_domain not in federation_list:
print(test_domain)
assert False
assert domain_found
unfollow_account(base_dir, nickname, domain, 'batman', 'mesh.com',
True, False)
domain_found = False
for following_domain in fp_foll:
test_domain = following_domain.split('@')[1]
2022-06-21 11:58:50 +00:00
test_domain = remove_eol(test_domain)
2022-01-04 20:14:19 +00:00
if test_domain == 'mesh.com':
domain_found = True
assert domain_found is False
2020-04-05 13:25:47 +00:00
2021-12-29 21:55:09 +00:00
clear_followers(base_dir, nickname, domain)
add_follower_of_person(base_dir, nickname, domain, 'badger', 'wild.com',
federation_list, False, False)
add_follower_of_person(base_dir, nickname, domain,
'squirrel', 'secret.com',
federation_list, False, False)
add_follower_of_person(base_dir, nickname, domain,
'rodent', 'drainpipe.com',
federation_list, False, False)
add_follower_of_person(base_dir, nickname, domain, 'batman', 'mesh.com',
federation_list, False, False)
add_follower_of_person(base_dir, nickname, domain, 'giraffe', 'trees.com',
federation_list, False, False)
2020-04-05 13:25:47 +00:00
2022-01-04 20:14:19 +00:00
account_dir = acct_dir(base_dir, nickname, domain)
2022-06-09 14:46:30 +00:00
with open(account_dir + '/followers.txt', 'r',
encoding='utf-8') as fp_foll:
2022-01-04 20:14:19 +00:00
for follower_domain in fp_foll:
test_domain = follower_domain.split('@')[1]
2022-06-21 11:58:50 +00:00
test_domain = remove_eol(test_domain)
2022-01-04 20:14:19 +00:00
if test_domain not in federation_list:
print(test_domain)
assert False
2019-07-03 09:24:55 +00:00
2022-01-04 20:14:19 +00:00
os.chdir(curr_dir)
2021-12-25 16:17:53 +00:00
shutil.rmtree(base_dir, ignore_errors=False, onerror=None)
2019-07-03 10:04:23 +00:00
2020-04-05 13:25:47 +00:00
2021-12-29 21:55:09 +00:00
def _test_create_person_account(base_dir: str):
2022-01-04 20:14:19 +00:00
print('test_create_person_account')
2021-12-25 23:03:28 +00:00
system_language = 'en'
languages_understood = [system_language]
2022-01-04 20:14:19 +00:00
curr_dir = base_dir
2020-04-05 13:25:47 +00:00
nickname = 'test382'
domain = 'badgerdomain.com'
password = 'mypass'
port = 80
2021-12-25 17:09:22 +00:00
http_prefix = 'https'
2021-12-25 20:39:35 +00:00
client_to_server = False
2022-01-04 20:14:19 +00:00
base_dir = curr_dir + '/.tests_createperson'
2021-12-25 16:17:53 +00:00
if os.path.isdir(base_dir):
shutil.rmtree(base_dir, ignore_errors=False, onerror=None)
os.mkdir(base_dir)
os.chdir(base_dir)
2020-03-22 21:16:02 +00:00
2022-01-04 20:14:19 +00:00
private_key_pem, public_key_pem, person, wf_endpoint = \
2021-12-29 21:55:09 +00:00
create_person(base_dir, nickname, domain, port,
http_prefix, True, False, password)
2022-01-04 20:14:19 +00:00
assert private_key_pem
assert public_key_pem
assert person
assert wf_endpoint
2021-12-25 16:17:53 +00:00
assert os.path.isfile(base_dir + '/accounts/passwords')
2021-12-29 21:55:09 +00:00
delete_all_posts(base_dir, nickname, domain, 'inbox')
delete_all_posts(base_dir, nickname, domain, 'outbox')
set_display_nickname(base_dir, nickname, domain, 'badger')
set_bio(base_dir, nickname, domain, 'Randomly roaming in your backyard')
archive_posts_for_person(nickname, domain, base_dir, 'inbox', None, {}, 4)
archive_posts_for_person(nickname, domain, base_dir, 'outbox', None, {}, 4)
2022-01-04 20:14:19 +00:00
test_in_reply_to = None
test_in_reply_to_atom_uri = None
test_subject = None
test_schedule_post = False
test_event_date = None
test_event_time = None
2022-05-23 12:14:36 +00:00
test_event_end_time = None
2022-01-04 20:14:19 +00:00
test_location = None
test_is_article = False
save_to_file = True
comments_enabled = True
attach_image_filename = None
media_type = None
conversation_id = None
2021-12-25 18:20:56 +00:00
low_bandwidth = True
2022-07-18 16:18:04 +00:00
translate = {}
2021-12-25 17:13:38 +00:00
content_license_url = 'https://creativecommons.org/licenses/by/4.0'
2022-02-13 14:38:36 +00:00
content = \
"If your \"independent organization\" is government funded...\n\n" + \
"(yawn)\n\n...then it's not really independent.\n\n" + \
"Politicians will threaten to withdraw funding if you do " + \
"anything which challenges middle class sensibilities or incomes."
test_post_json = \
create_public_post(base_dir, nickname, domain, port, http_prefix,
2022-05-31 16:20:16 +00:00
content, save_to_file,
2022-02-13 14:38:36 +00:00
client_to_server,
comments_enabled, attach_image_filename, media_type,
'Not suitable for Vogons', 'London, England',
test_in_reply_to, test_in_reply_to_atom_uri,
test_subject, test_schedule_post,
2022-05-23 12:14:36 +00:00
test_event_date, test_event_time,
test_event_end_time, test_location,
2022-02-13 14:38:36 +00:00
test_is_article, system_language, conversation_id,
low_bandwidth, content_license_url,
2022-07-18 16:18:04 +00:00
languages_understood, translate)
2022-02-13 14:38:36 +00:00
assert test_post_json
assert test_post_json.get('object')
assert test_post_json['object']['content']
assert '(yawn)' in test_post_json['object']['content']
2019-07-03 10:04:23 +00:00
content = \
'I would regard fediverse as being things based on ActivityPub ' + \
'or OStatus. i.e. things whose protocol lineage can be traced ' + \
'back to identica/statusnet/pumpio.\n' + \
'\nFediverse is a vague term though ' + \
'and I know some people regard Matrix and Diaspora as being ' + \
'fediverse. If fediverse just means any federated system ' + \
'then email would be somequitelongword.\nAnotherlongwordhere sentence.'
test_post_json = \
create_public_post(base_dir, nickname, domain, port, http_prefix,
2022-05-31 16:20:16 +00:00
content, save_to_file,
client_to_server,
comments_enabled, attach_image_filename, media_type,
'Not suitable for Vogons', 'London, England',
test_in_reply_to, test_in_reply_to_atom_uri,
test_subject, test_schedule_post,
2022-05-23 12:14:36 +00:00
test_event_date, test_event_time,
test_event_end_time, test_location,
test_is_article, system_language, conversation_id,
low_bandwidth, content_license_url,
2022-07-18 16:18:04 +00:00
languages_understood, translate)
assert test_post_json
assert test_post_json.get('object')
assert test_post_json['object']['content']
assert 'Fediverse' in test_post_json['object']['content']
content_str = test_post_json['object']['content']
object_content = remove_long_words(content_str, 40, [])
assert 'Fediverse' in object_content
bold_reading = False
object_content = remove_text_formatting(object_content, bold_reading)
assert 'Fediverse' in object_content
object_content = limit_repeated_words(object_content, 6)
assert 'Fediverse' in object_content
object_content = html_replace_email_quote(object_content)
assert 'Fediverse' in object_content
object_content = html_replace_quote_marks(object_content)
assert 'Fediverse' in object_content
2022-01-04 20:14:19 +00:00
os.chdir(curr_dir)
2021-12-25 16:17:53 +00:00
shutil.rmtree(base_dir, ignore_errors=False, onerror=None)
2019-07-03 10:04:23 +00:00
2020-04-05 13:25:47 +00:00
2022-01-04 20:14:19 +00:00
def show_test_boxes(name: str, inbox_path: str, outbox_path: str) -> None:
inbox_posts = \
len([name for name in os.listdir(inbox_path)
if os.path.isfile(os.path.join(inbox_path, name))])
outbox_posts = \
len([name for name in os.listdir(outbox_path)
if os.path.isfile(os.path.join(outbox_path, name))])
2021-08-20 11:22:20 +00:00
print('EVENT: ' + name +
2022-01-04 20:14:19 +00:00
' inbox has ' + str(inbox_posts) + ' posts and ' +
str(outbox_posts) + ' outbox posts')
2021-08-20 11:22:20 +00:00
2021-12-29 21:55:09 +00:00
def _test_authentication(base_dir: str) -> None:
2022-01-04 20:14:19 +00:00
print('test_authentication')
curr_dir = base_dir
2020-04-05 13:25:47 +00:00
nickname = 'test8743'
password = 'SuperSecretPassword12345'
2019-07-03 18:24:44 +00:00
2022-01-04 20:14:19 +00:00
base_dir = curr_dir + '/.tests_authentication'
2021-12-25 16:17:53 +00:00
if os.path.isdir(base_dir):
shutil.rmtree(base_dir, ignore_errors=False, onerror=None)
os.mkdir(base_dir)
os.chdir(base_dir)
2019-07-03 18:24:44 +00:00
2021-12-28 21:36:27 +00:00
assert store_basic_credentials(base_dir, 'othernick', 'otherpass')
assert store_basic_credentials(base_dir, 'bad:nick', 'otherpass') is False
assert store_basic_credentials(base_dir, 'badnick', 'otherpa:ss') is False
assert store_basic_credentials(base_dir, nickname, password)
2019-07-03 18:24:44 +00:00
2022-01-04 20:14:19 +00:00
auth_header = create_basic_auth_header(nickname, password)
2021-12-28 21:36:27 +00:00
assert authorize_basic(base_dir, '/users/' + nickname + '/inbox',
2022-01-04 20:14:19 +00:00
auth_header, False)
2021-12-28 21:36:27 +00:00
assert authorize_basic(base_dir, '/users/' + nickname,
2022-01-04 20:14:19 +00:00
auth_header, False) is False
2021-12-28 21:36:27 +00:00
assert authorize_basic(base_dir, '/users/othernick/inbox',
2022-01-04 20:14:19 +00:00
auth_header, False) is False
2019-07-03 18:24:44 +00:00
2022-01-04 20:14:19 +00:00
auth_header = create_basic_auth_header(nickname, password + '1')
2021-12-28 21:36:27 +00:00
assert authorize_basic(base_dir, '/users/' + nickname + '/inbox',
2022-01-04 20:14:19 +00:00
auth_header, False) is False
2019-07-03 18:24:44 +00:00
2020-04-05 13:25:47 +00:00
password = 'someOtherPassword'
2021-12-28 21:36:27 +00:00
assert store_basic_credentials(base_dir, nickname, password)
2019-07-03 19:13:23 +00:00
2022-01-04 20:14:19 +00:00
auth_header = create_basic_auth_header(nickname, password)
2021-12-28 21:36:27 +00:00
assert authorize_basic(base_dir, '/users/' + nickname + '/inbox',
2022-01-04 20:14:19 +00:00
auth_header, False)
2019-07-03 19:13:23 +00:00
2022-01-04 20:14:19 +00:00
os.chdir(curr_dir)
2021-12-25 16:17:53 +00:00
shutil.rmtree(base_dir, ignore_errors=False, onerror=None)
2019-07-16 10:19:04 +00:00
2020-04-05 13:25:47 +00:00
2021-12-29 21:55:09 +00:00
def test_client_to_server(base_dir: str):
2021-08-20 11:22:20 +00:00
print('EVENT: Testing sending a post via c2s')
2019-07-16 10:19:04 +00:00
2022-01-04 20:14:19 +00:00
global TEST_SERVER_ALICE_RUNNING
global TEST_SERVER_BOB_RUNNING
2021-12-25 17:13:38 +00:00
content_license_url = 'https://creativecommons.org/licenses/by/4.0'
2022-01-04 20:14:19 +00:00
TEST_SERVER_ALICE_RUNNING = False
TEST_SERVER_BOB_RUNNING = False
2019-07-16 10:19:04 +00:00
2021-12-25 23:03:28 +00:00
system_language = 'en'
languages_understood = [system_language]
2021-12-25 17:09:22 +00:00
http_prefix = 'http'
2021-12-25 21:09:22 +00:00
proxy_type = None
2021-12-25 23:45:30 +00:00
federation_list = []
2021-12-25 18:20:56 +00:00
low_bandwidth = False
2019-07-16 10:19:04 +00:00
2021-12-25 16:17:53 +00:00
if os.path.isdir(base_dir + '/.tests'):
shutil.rmtree(base_dir + '/.tests', ignore_errors=False, onerror=None)
os.mkdir(base_dir + '/.tests')
2019-07-16 10:19:04 +00:00
# create the servers
2022-01-04 20:14:19 +00:00
alice_dir = base_dir + '/.tests/alice'
alice_domain = '127.0.0.42'
alice_port = 61935
alice_send_threads = []
alice_address = alice_domain + ':' + str(alice_port)
bob_dir = base_dir + '/.tests/bob'
bob_domain = '127.0.0.64'
bob_port = 61936
bob_send_threads = []
bob_address = bob_domain + ':' + str(bob_port)
global THR_ALICE
if THR_ALICE:
while THR_ALICE.is_alive():
THR_ALICE.stop()
2020-02-19 12:27:21 +00:00
time.sleep(1)
2022-01-04 20:14:19 +00:00
THR_ALICE.kill()
2020-02-19 12:27:21 +00:00
2022-01-04 20:14:19 +00:00
THR_ALICE = \
2021-12-29 21:55:09 +00:00
thread_with_trace(target=create_server_alice,
2022-01-04 20:14:19 +00:00
args=(alice_dir, alice_domain, alice_port,
bob_address, federation_list, False, False,
alice_send_threads),
2021-12-28 21:36:27 +00:00
daemon=True)
2020-03-22 21:16:02 +00:00
2022-01-04 20:14:19 +00:00
global THR_BOB
if THR_BOB:
while THR_BOB.is_alive():
THR_BOB.stop()
2020-02-19 12:27:21 +00:00
time.sleep(1)
2022-01-04 20:14:19 +00:00
THR_BOB.kill()
2020-02-19 12:27:21 +00:00
2022-01-04 20:14:19 +00:00
THR_BOB = \
2021-12-29 21:55:09 +00:00
thread_with_trace(target=create_server_bob,
2022-01-04 20:14:19 +00:00
args=(bob_dir, bob_domain, bob_port, alice_address,
2021-12-28 21:36:27 +00:00
federation_list, False, False,
2022-01-04 20:14:19 +00:00
bob_send_threads),
2021-12-28 21:36:27 +00:00
daemon=True)
2019-07-16 10:19:04 +00:00
2022-01-04 20:14:19 +00:00
THR_ALICE.start()
THR_BOB.start()
assert THR_ALICE.is_alive() is True
assert THR_BOB.is_alive() is True
2019-07-16 10:19:04 +00:00
# wait for both servers to be running
2020-04-05 13:25:47 +00:00
ctr = 0
2022-01-04 20:14:19 +00:00
while not (TEST_SERVER_ALICE_RUNNING and TEST_SERVER_BOB_RUNNING):
2019-07-16 10:19:04 +00:00
time.sleep(1)
2020-04-05 13:25:47 +00:00
ctr += 1
if ctr > 60:
2019-07-16 10:19:04 +00:00
break
2022-01-04 20:14:19 +00:00
print('Alice online: ' + str(TEST_SERVER_ALICE_RUNNING))
print('Bob online: ' + str(TEST_SERVER_BOB_RUNNING))
2019-07-16 10:19:04 +00:00
time.sleep(1)
2020-03-22 21:16:02 +00:00
2022-02-24 14:59:10 +00:00
# set bob to be following the calendar of alice
print('Bob follows the calendar of Alice')
following_cal_path = \
bob_dir + '/accounts/bob@' + bob_domain + '/followingCalendar.txt'
2022-06-09 14:46:30 +00:00
with open(following_cal_path, 'w+', encoding='utf-8') as fp_foll:
2022-06-08 20:20:27 +00:00
fp_foll.write('alice@' + alice_domain + '\n')
2022-02-24 14:59:10 +00:00
2019-07-16 10:19:04 +00:00
print('\n\n*******************************************************')
2021-08-20 11:22:20 +00:00
print('EVENT: Alice sends to Bob via c2s')
2019-07-16 10:19:04 +00:00
2022-01-04 20:14:19 +00:00
session_alice = create_session(proxy_type)
attached_image_filename = base_dir + '/img/logo.png'
media_type = get_attachment_media_type(attached_image_filename)
attached_image_description = 'Logo'
2021-05-09 19:29:53 +00:00
city = 'London, England'
2022-01-04 20:14:19 +00:00
is_article = False
2021-12-25 22:28:18 +00:00
cached_webfingers = {}
2021-12-25 22:17:49 +00:00
person_cache = {}
2020-04-05 13:25:47 +00:00
password = 'alicepass'
2022-01-04 20:14:19 +00:00
conversation_id = None
alice_inbox_path = alice_dir + '/accounts/alice@' + alice_domain + '/inbox'
alice_outbox_path = \
alice_dir + '/accounts/alice@' + alice_domain + '/outbox'
bob_inbox_path = bob_dir + '/accounts/bob@' + bob_domain + '/inbox'
bob_outbox_path = bob_dir + '/accounts/bob@' + bob_domain + '/outbox'
outbox_path = alice_dir + '/accounts/alice@' + alice_domain + '/outbox'
inbox_path = bob_dir + '/accounts/bob@' + bob_domain + '/inbox'
show_test_boxes('alice', alice_inbox_path, alice_outbox_path)
show_test_boxes('bob', bob_inbox_path, bob_outbox_path)
assert len([name for name in os.listdir(alice_inbox_path)
if os.path.isfile(os.path.join(alice_inbox_path, name))]) == 0
assert len([name for name in os.listdir(alice_outbox_path)
if os.path.isfile(os.path.join(alice_outbox_path, name))]) == 0
assert len([name for name in os.listdir(bob_inbox_path)
if os.path.isfile(os.path.join(bob_inbox_path, name))]) == 0
assert len([name for name in os.listdir(bob_outbox_path)
if os.path.isfile(os.path.join(bob_outbox_path, name))]) == 0
2021-08-20 11:22:20 +00:00
print('EVENT: all inboxes and outboxes are empty')
2021-12-25 23:03:28 +00:00
signing_priv_key_pem = None
test_date = datetime.datetime.now()
event_date = \
str(test_date.year) + '-' + str(test_date.month) + '-' + \
str(test_date.day)
event_time = '11:45'
2022-05-23 12:14:36 +00:00
event_end_time = '12:30'
location = "Kinshasa"
2022-07-18 16:18:04 +00:00
translate = {}
2022-01-04 20:14:19 +00:00
send_result = \
2021-12-29 21:55:09 +00:00
send_post_via_server(signing_priv_key_pem, __version__,
2022-01-04 20:14:19 +00:00
alice_dir, session_alice, 'alice', password,
alice_domain, alice_port,
'bob', bob_domain, bob_port, None,
2021-12-29 21:55:09 +00:00
http_prefix, 'Sent from my ActivityPub client',
2022-05-31 16:51:56 +00:00
True, attached_image_filename, media_type,
2022-01-04 20:14:19 +00:00
attached_image_description, city,
cached_webfingers, person_cache, is_article,
system_language, languages_understood,
low_bandwidth, content_license_url,
2022-05-23 12:14:36 +00:00
event_date, event_time, event_end_time, location,
2022-07-18 16:18:04 +00:00
translate, True, None, None,
2022-01-04 20:14:19 +00:00
conversation_id, None)
print('send_result: ' + str(send_result))
2019-07-16 10:19:04 +00:00
2022-01-04 20:14:19 +00:00
for _ in range(30):
if os.path.isdir(outbox_path):
if len([name for name in os.listdir(outbox_path)
if os.path.isfile(os.path.join(outbox_path, name))]) == 1:
2019-07-16 10:19:04 +00:00
break
time.sleep(1)
2019-07-16 11:33:40 +00:00
2022-01-04 20:14:19 +00:00
show_test_boxes('alice', alice_inbox_path, alice_outbox_path)
show_test_boxes('bob', bob_inbox_path, bob_outbox_path)
assert len([name for name in os.listdir(alice_inbox_path)
if os.path.isfile(os.path.join(alice_inbox_path, name))]) == 0
assert len([name for name in os.listdir(alice_outbox_path)
if os.path.isfile(os.path.join(alice_outbox_path, name))]) == 1
2021-08-20 11:22:20 +00:00
print(">>> c2s post arrived in Alice's outbox\n\n\n")
2020-03-22 21:16:02 +00:00
2022-01-04 20:14:19 +00:00
for _ in range(30):
if os.path.isdir(inbox_path):
if len([name for name in os.listdir(bob_inbox_path)
if os.path.isfile(os.path.join(bob_inbox_path,
name))]) == 1:
2019-07-16 11:33:40 +00:00
break
time.sleep(1)
2022-01-04 20:14:19 +00:00
show_test_boxes('alice', alice_inbox_path, alice_outbox_path)
show_test_boxes('bob', bob_inbox_path, bob_outbox_path)
assert len([name for name in os.listdir(bob_inbox_path)
if os.path.isfile(os.path.join(bob_inbox_path, name))]) == 1
assert len([name for name in os.listdir(bob_outbox_path)
if os.path.isfile(os.path.join(bob_outbox_path, name))]) == 0
2021-08-20 11:22:20 +00:00
2019-07-16 11:33:40 +00:00
print(">>> s2s post arrived in Bob's inbox")
2022-02-24 14:59:10 +00:00
2022-02-26 17:07:14 +00:00
time.sleep(2)
2022-02-24 14:59:10 +00:00
calendar_path = bob_dir + '/accounts/bob@' + bob_domain + '/calendar'
if not os.path.isdir(calendar_path):
print('Missing calendar path: ' + calendar_path)
assert os.path.isdir(calendar_path)
assert os.path.isdir(calendar_path + '/' + str(test_date.year))
assert os.path.isfile(calendar_path + '/' + str(test_date.year) + '/' +
str(test_date.month) + '.txt')
print(">>> calendar entry created for s2s post which arrived at " +
"Bob's inbox")
2021-08-20 11:22:20 +00:00
print("c2s send success\n\n\n")
2019-07-16 11:33:40 +00:00
2021-08-20 11:22:20 +00:00
print('\n\nEVENT: Getting message id for the post')
2022-01-04 20:14:19 +00:00
status_number = 0
outbox_post_filename = None
outbox_post_id = None
for name in os.listdir(outbox_path):
2019-07-16 19:07:45 +00:00
if '#statuses#' in name:
2022-01-04 20:14:19 +00:00
status_number = name.split('#statuses#')[1].replace('.json', '')
status_number = int(status_number.replace('#activity', ''))
outbox_post_filename = outbox_path + '/' + name
post_json_object = load_json(outbox_post_filename, 0)
2021-12-25 22:09:19 +00:00
if post_json_object:
2022-01-04 20:14:19 +00:00
outbox_post_id = remove_id_ending(post_json_object['id'])
assert outbox_post_id
print('message id obtained: ' + outbox_post_id)
assert valid_inbox(bob_dir, 'bob', bob_domain)
assert valid_inbox_filenames(bob_dir, 'bob', bob_domain,
alice_domain, alice_port)
2019-07-16 21:38:06 +00:00
print('\n\nAlice follows Bob')
2021-12-25 23:03:28 +00:00
signing_priv_key_pem = None
2022-03-13 12:15:52 +00:00
send_follow_request_via_server(alice_dir, session_alice,
'alice', password,
alice_domain, alice_port,
'bob', bob_domain, bob_port,
http_prefix,
cached_webfingers, person_cache,
True, __version__, signing_priv_key_pem)
2022-01-04 20:14:19 +00:00
alice_petnames_filename = alice_dir + '/accounts/' + \
'alice@' + alice_domain + '/petnames.txt'
alice_following_filename = \
alice_dir + '/accounts/alice@' + alice_domain + '/following.txt'
bob_followers_filename = \
bob_dir + '/accounts/bob@' + bob_domain + '/followers.txt'
for _ in range(10):
if os.path.isfile(bob_followers_filename):
2022-06-10 13:01:39 +00:00
test_str = 'alice@' + alice_domain + ':' + str(alice_port)
if text_in_file(test_str, bob_followers_filename):
2022-01-04 20:14:19 +00:00
if os.path.isfile(alice_following_filename) and \
os.path.isfile(alice_petnames_filename):
2022-06-10 13:01:39 +00:00
test_str = 'bob@' + bob_domain + ':' + str(bob_port)
if text_in_file(test_str, alice_following_filename):
2019-07-17 11:24:11 +00:00
break
2019-07-16 21:38:06 +00:00
time.sleep(1)
2022-01-04 20:14:19 +00:00
assert os.path.isfile(bob_followers_filename)
assert os.path.isfile(alice_following_filename)
assert os.path.isfile(alice_petnames_filename)
2022-06-10 13:01:39 +00:00
assert text_in_file('bob bob@' + bob_domain, alice_petnames_filename)
2022-01-04 20:14:19 +00:00
print('alice@' + alice_domain + ':' + str(alice_port) + ' in ' +
bob_followers_filename)
2022-06-10 13:01:39 +00:00
test_str = 'alice@' + alice_domain + ':' + str(alice_port)
assert text_in_file(test_str, bob_followers_filename)
2022-01-04 20:14:19 +00:00
print('bob@' + bob_domain + ':' + str(bob_port) + ' in ' +
alice_following_filename)
2022-06-10 13:01:39 +00:00
test_str = 'bob@' + bob_domain + ':' + str(bob_port)
assert text_in_file(test_str, alice_following_filename)
2022-01-04 20:14:19 +00:00
assert valid_inbox(bob_dir, 'bob', bob_domain)
assert valid_inbox_filenames(bob_dir, 'bob', bob_domain,
alice_domain, alice_port)
2019-07-17 17:16:48 +00:00
2021-08-20 11:22:20 +00:00
print('\n\nEVENT: Bob follows Alice')
2022-03-13 12:15:52 +00:00
send_follow_request_via_server(alice_dir, session_alice,
'bob', 'bobpass',
bob_domain, bob_port,
'alice', alice_domain, alice_port,
http_prefix,
cached_webfingers, person_cache,
True, __version__, signing_priv_key_pem)
2022-06-14 11:42:45 +00:00
for _ in range(20):
2022-01-04 20:14:19 +00:00
if os.path.isfile(alice_dir + '/accounts/alice@' + alice_domain +
2020-04-05 13:25:47 +00:00
'/followers.txt'):
2022-06-10 13:01:39 +00:00
test_str = 'bob@' + bob_domain + ':' + str(bob_port)
test_filename = \
alice_dir + '/accounts/alice@' + \
alice_domain + '/followers.txt'
if text_in_file(test_str, test_filename):
2022-01-04 20:14:19 +00:00
if os.path.isfile(bob_dir + '/accounts/bob@' + bob_domain +
2020-04-05 13:25:47 +00:00
'/following.txt'):
2022-01-04 20:14:19 +00:00
alice_handle_str = \
'alice@' + alice_domain + ':' + str(alice_port)
2022-06-10 13:01:39 +00:00
if text_in_file(alice_handle_str,
bob_dir + '/accounts/bob@' + bob_domain +
'/following.txt'):
2022-01-04 20:14:19 +00:00
if os.path.isfile(bob_dir + '/accounts/bob@' +
bob_domain +
2020-07-03 21:56:38 +00:00
'/followingCalendar.txt'):
2022-06-10 13:01:39 +00:00
if text_in_file(alice_handle_str,
bob_dir + '/accounts/bob@' +
bob_domain +
'/followingCalendar.txt'):
2020-07-03 21:56:38 +00:00
break
2019-07-17 17:16:48 +00:00
time.sleep(1)
2022-01-04 20:14:19 +00:00
assert os.path.isfile(alice_dir + '/accounts/alice@' + alice_domain +
2020-04-05 13:25:47 +00:00
'/followers.txt')
2022-01-04 20:14:19 +00:00
assert os.path.isfile(bob_dir + '/accounts/bob@' + bob_domain +
2020-04-05 13:25:47 +00:00
'/following.txt')
2022-06-10 13:01:39 +00:00
test_str = 'bob@' + bob_domain + ':' + str(bob_port)
assert text_in_file(test_str, alice_dir + '/accounts/alice@' +
alice_domain + '/followers.txt')
test_str = 'alice@' + alice_domain + ':' + str(alice_port)
assert text_in_file(test_str,
bob_dir + '/accounts/bob@' +
bob_domain + '/following.txt')
2022-01-04 20:14:19 +00:00
session_bob = create_session(proxy_type)
2020-04-05 13:25:47 +00:00
password = 'bobpass'
2022-01-04 20:14:19 +00:00
outbox_path = bob_dir + '/accounts/bob@' + bob_domain + '/outbox'
inbox_path = alice_dir + '/accounts/alice@' + alice_domain + '/inbox'
print(str(len([name for name in os.listdir(bob_outbox_path)
if os.path.isfile(os.path.join(bob_outbox_path, name))])))
show_test_boxes('alice', alice_inbox_path, alice_outbox_path)
show_test_boxes('bob', bob_inbox_path, bob_outbox_path)
assert len([name for name in os.listdir(bob_outbox_path)
if os.path.isfile(os.path.join(bob_outbox_path, name))]) == 1
print(str(len([name for name in os.listdir(alice_inbox_path)
if os.path.isfile(os.path.join(alice_inbox_path, name))])))
show_test_boxes('alice', alice_inbox_path, alice_outbox_path)
show_test_boxes('bob', bob_inbox_path, bob_outbox_path)
assert len([name for name in os.listdir(alice_inbox_path)
if os.path.isfile(os.path.join(alice_inbox_path, name))]) == 0
2022-02-24 16:55:37 +00:00
print('\n\nEVENT: Bob checks his calendar via caldav')
2022-02-24 17:29:12 +00:00
# test caldav result for a month
2022-02-24 16:55:37 +00:00
result = \
dav_month_via_server(session_bob, http_prefix,
'bob', bob_domain, bob_port, True,
test_date.year, test_date.month,
'bobpass')
print('response: ' + str(result))
2022-02-24 17:23:41 +00:00
assert 'VCALENDAR' in str(result)
assert 'VEVENT' in str(result)
2022-02-24 17:29:12 +00:00
# test caldav result for a day
2022-02-24 17:23:41 +00:00
result = \
dav_day_via_server(session_bob, http_prefix,
'bob', bob_domain, bob_port, True,
test_date.year, test_date.month,
test_date.day, 'bobpass')
print('response: ' + str(result))
2022-02-24 16:55:37 +00:00
assert 'VCALENDAR' in str(result)
assert 'VEVENT' in str(result)
2022-02-24 17:29:12 +00:00
# test for incorrect caldav login
result = \
dav_day_via_server(session_bob, http_prefix,
'bob', bob_domain, bob_port, True,
test_date.year, test_date.month,
test_date.day, 'wrongpass')
assert 'VCALENDAR' not in str(result)
assert 'VEVENT' not in str(result)
2022-02-24 17:34:59 +00:00
2021-08-20 11:22:20 +00:00
print('\n\nEVENT: Bob likes the post')
2022-01-04 20:14:19 +00:00
send_like_via_server(bob_dir, session_bob,
2021-12-29 21:55:09 +00:00
'bob', 'bobpass',
2022-01-04 20:14:19 +00:00
bob_domain, bob_port,
http_prefix, outbox_post_id,
2021-12-29 21:55:09 +00:00
cached_webfingers, person_cache,
True, __version__, signing_priv_key_pem)
2022-01-04 20:14:19 +00:00
for _ in range(20):
if os.path.isdir(outbox_path) and os.path.isdir(inbox_path):
if len([name for name in os.listdir(outbox_path)
if os.path.isfile(os.path.join(outbox_path, name))]) == 2:
test = len([name for name in os.listdir(inbox_path)
if os.path.isfile(os.path.join(inbox_path, name))])
2020-04-05 13:25:47 +00:00
if test == 1:
break
time.sleep(1)
2022-01-04 20:14:19 +00:00
show_test_boxes('alice', alice_inbox_path, alice_outbox_path)
show_test_boxes('bob', bob_inbox_path, bob_outbox_path)
bob_outbox_path_ctr = \
len([name for name in os.listdir(bob_outbox_path)
if os.path.isfile(os.path.join(bob_outbox_path, name))])
print('bob_outbox_path_ctr: ' + str(bob_outbox_path_ctr))
assert bob_outbox_path_ctr == 2
alice_inbox_path_ctr = \
len([name for name in os.listdir(alice_inbox_path)
if os.path.isfile(os.path.join(alice_inbox_path, name))])
print('alice_inbox_path_ctr: ' + str(alice_inbox_path_ctr))
assert alice_inbox_path_ctr == 0
2021-08-20 11:22:20 +00:00
print('EVENT: Post liked')
2020-03-22 21:16:02 +00:00
2021-11-10 12:16:03 +00:00
print('\n\nEVENT: Bob reacts to the post')
2022-01-04 20:14:19 +00:00
send_reaction_via_server(bob_dir, session_bob,
2021-12-29 21:55:09 +00:00
'bob', 'bobpass',
2022-01-04 20:14:19 +00:00
bob_domain, bob_port,
http_prefix, outbox_post_id, '😃',
2021-12-29 21:55:09 +00:00
cached_webfingers, person_cache,
True, __version__, signing_priv_key_pem)
2022-01-04 20:14:19 +00:00
for _ in range(20):
if os.path.isdir(outbox_path) and os.path.isdir(inbox_path):
if len([name for name in os.listdir(outbox_path)
if os.path.isfile(os.path.join(outbox_path, name))]) == 3:
test = len([name for name in os.listdir(inbox_path)
if os.path.isfile(os.path.join(inbox_path, name))])
2021-11-10 12:16:03 +00:00
if test == 1:
break
time.sleep(1)
2022-01-04 20:14:19 +00:00
show_test_boxes('alice', alice_inbox_path, alice_outbox_path)
show_test_boxes('bob', bob_inbox_path, bob_outbox_path)
bob_outbox_path_ctr = \
len([name for name in os.listdir(bob_outbox_path)
if os.path.isfile(os.path.join(bob_outbox_path, name))])
print('bob_outbox_path_ctr: ' + str(bob_outbox_path_ctr))
assert bob_outbox_path_ctr == 3
alice_inbox_path_ctr = \
len([name for name in os.listdir(alice_inbox_path)
if os.path.isfile(os.path.join(alice_inbox_path, name))])
print('alice_inbox_path_ctr: ' + str(alice_inbox_path_ctr))
assert alice_inbox_path_ctr == 0
2021-11-10 12:16:03 +00:00
print('EVENT: Post reacted to')
2022-01-04 20:14:19 +00:00
print(str(len([name for name in os.listdir(outbox_path)
if os.path.isfile(os.path.join(outbox_path, name))])))
show_test_boxes('alice', alice_inbox_path, alice_outbox_path)
show_test_boxes('bob', bob_inbox_path, bob_outbox_path)
outbox_path_ctr = \
len([name for name in os.listdir(outbox_path)
if os.path.isfile(os.path.join(outbox_path, name))])
print('outbox_path_ctr: ' + str(outbox_path_ctr))
assert outbox_path_ctr == 3
inbox_path_ctr = \
len([name for name in os.listdir(inbox_path)
if os.path.isfile(os.path.join(inbox_path, name))])
print('inbox_path_ctr: ' + str(inbox_path_ctr))
assert inbox_path_ctr == 0
show_test_boxes('alice', alice_inbox_path, alice_outbox_path)
show_test_boxes('bob', bob_inbox_path, bob_outbox_path)
2021-08-20 11:22:20 +00:00
print('\n\nEVENT: Bob repeats the post')
2021-12-25 23:03:28 +00:00
signing_priv_key_pem = None
2022-01-04 20:14:19 +00:00
send_announce_via_server(bob_dir, session_bob, 'bob', password,
bob_domain, bob_port,
http_prefix, outbox_post_id,
2021-12-29 21:55:09 +00:00
cached_webfingers,
person_cache, True, __version__,
signing_priv_key_pem)
2022-06-15 12:39:08 +00:00
for _ in range(30):
2022-01-04 20:14:19 +00:00
if os.path.isdir(outbox_path) and os.path.isdir(inbox_path):
if len([name for name in os.listdir(outbox_path)
if os.path.isfile(os.path.join(outbox_path, name))]) == 4:
if len([name for name in os.listdir(inbox_path)
if os.path.isfile(os.path.join(inbox_path,
2020-04-05 13:25:47 +00:00
name))]) == 2:
2019-07-16 22:57:45 +00:00
break
2019-07-16 19:07:45 +00:00
time.sleep(1)
2022-01-04 20:14:19 +00:00
show_test_boxes('alice', alice_inbox_path, alice_outbox_path)
show_test_boxes('bob', bob_inbox_path, bob_outbox_path)
bob_outbox_path_ctr = \
len([name for name in os.listdir(bob_outbox_path)
if os.path.isfile(os.path.join(bob_outbox_path, name))])
print('bob_outbox_path_ctr: ' + str(bob_outbox_path_ctr))
assert bob_outbox_path_ctr == 5
alice_inbox_path_ctr = \
len([name for name in os.listdir(alice_inbox_path)
if os.path.isfile(os.path.join(alice_inbox_path, name))])
print('alice_inbox_path_ctr: ' + str(alice_inbox_path_ctr))
assert alice_inbox_path_ctr == 1
2021-08-20 11:22:20 +00:00
print('EVENT: Post repeated')
2019-07-17 11:54:13 +00:00
2022-01-04 20:14:19 +00:00
inbox_path = bob_dir + '/accounts/bob@' + bob_domain + '/inbox'
outbox_path = alice_dir + '/accounts/alice@' + alice_domain + '/outbox'
bob_posts_before = \
2022-01-04 20:14:19 +00:00
len([name for name in os.listdir(inbox_path)
if os.path.isfile(os.path.join(inbox_path, name))])
alice_posts_before = \
len([name for name in os.listdir(outbox_path)
if os.path.isfile(os.path.join(outbox_path, name))])
2022-01-04 20:14:19 +00:00
print('\n\nEVENT: Alice deletes her post: ' + outbox_post_id + ' ' +
str(alice_posts_before))
2020-04-05 13:25:47 +00:00
password = 'alicepass'
2022-01-04 20:14:19 +00:00
send_delete_via_server(alice_dir, session_alice, 'alice', password,
alice_domain, alice_port,
http_prefix, outbox_post_id,
2021-12-29 21:55:09 +00:00
cached_webfingers, person_cache,
True, __version__, signing_priv_key_pem)
2022-01-04 20:14:19 +00:00
for _ in range(30):
if os.path.isdir(inbox_path):
test = len([name for name in os.listdir(inbox_path)
if os.path.isfile(os.path.join(inbox_path, name))])
if test == bob_posts_before-1:
2019-07-17 17:16:48 +00:00
break
time.sleep(1)
2022-01-04 20:14:19 +00:00
test = len([name for name in os.listdir(inbox_path)
if os.path.isfile(os.path.join(inbox_path, name))])
assert test == bob_posts_before - 1
print(">>> post was deleted from Bob's inbox")
2022-05-01 17:28:02 +00:00
test = len([name for name in os.listdir(outbox_path)
if os.path.isfile(os.path.join(outbox_path, name))])
# this should be unchanged because a delete post was added
# at the outbox and one was removed
assert test == alice_posts_before
print(">>> post deleted from Alice's outbox")
2022-01-04 20:14:19 +00:00
assert valid_inbox(bob_dir, 'bob', bob_domain)
assert valid_inbox_filenames(bob_dir, 'bob', bob_domain,
alice_domain, alice_port)
2020-03-22 21:16:02 +00:00
2021-08-20 11:22:20 +00:00
print('\n\nEVENT: Alice unfollows Bob')
2020-04-05 13:25:47 +00:00
password = 'alicepass'
2022-01-04 20:14:19 +00:00
send_unfollow_request_via_server(base_dir, session_alice,
2021-12-29 21:55:09 +00:00
'alice', password,
2022-01-04 20:14:19 +00:00
alice_domain, alice_port,
'bob', bob_domain, bob_port,
2021-12-29 21:55:09 +00:00
http_prefix,
cached_webfingers, person_cache,
True, __version__, signing_priv_key_pem)
2022-01-04 20:14:19 +00:00
for _ in range(10):
2022-06-10 13:01:39 +00:00
test_str = 'alice@' + alice_domain + ':' + str(alice_port)
if not text_in_file(test_str, bob_followers_filename):
test_str = 'bob@' + bob_domain + ':' + str(bob_port)
if not text_in_file(test_str, alice_following_filename):
2019-07-17 11:54:13 +00:00
break
time.sleep(1)
2022-01-04 20:14:19 +00:00
assert os.path.isfile(bob_followers_filename)
assert os.path.isfile(alice_following_filename)
2022-06-10 11:43:33 +00:00
test_str = 'alice@' + alice_domain + ':' + str(alice_port)
assert not text_in_file(test_str, bob_followers_filename)
test_str = 'bob@' + bob_domain + ':' + str(bob_port)
assert not text_in_file(test_str, alice_following_filename)
2022-01-04 20:14:19 +00:00
assert valid_inbox(bob_dir, 'bob', bob_domain)
assert valid_inbox_filenames(bob_dir, 'bob', bob_domain,
alice_domain, alice_port)
assert valid_inbox(alice_dir, 'alice', alice_domain)
assert valid_inbox_filenames(alice_dir, 'alice', alice_domain,
bob_domain, bob_port)
2019-07-17 11:54:13 +00:00
2019-07-16 11:33:40 +00:00
# stop the servers
2022-01-04 20:14:19 +00:00
THR_ALICE.kill()
THR_ALICE.join()
assert THR_ALICE.is_alive() is False
2019-07-16 11:33:40 +00:00
2022-01-04 20:14:19 +00:00
THR_BOB.kill()
THR_BOB.join()
assert THR_BOB.is_alive() is False
2019-07-16 11:33:40 +00:00
2021-12-25 16:17:53 +00:00
os.chdir(base_dir)
2022-01-04 20:14:19 +00:00
# shutil.rmtree(alice_dir, ignore_errors=False, onerror=None)
# shutil.rmtree(bob_dir, ignore_errors=False, onerror=None)
2020-04-05 13:25:47 +00:00
2019-07-16 10:19:04 +00:00
2021-12-29 21:55:09 +00:00
def _test_actor_parsing():
2022-01-04 20:14:19 +00:00
print('test_actor_parsing')
2020-04-05 13:25:47 +00:00
actor = 'https://mydomain:72/users/mynick'
2021-12-27 19:05:25 +00:00
domain, port = get_domain_from_actor(actor)
2020-04-05 13:25:47 +00:00
assert domain == 'mydomain'
assert port == 72
2021-12-27 22:19:18 +00:00
nickname = get_nickname_from_actor(actor)
2020-04-05 13:25:47 +00:00
assert nickname == 'mynick'
2020-08-13 16:41:02 +00:00
actor = 'https://element/accounts/badger'
2021-12-27 19:05:25 +00:00
domain, port = get_domain_from_actor(actor)
2020-08-13 16:41:02 +00:00
assert domain == 'element'
2021-12-27 22:19:18 +00:00
nickname = get_nickname_from_actor(actor)
2020-08-13 16:41:02 +00:00
assert nickname == 'badger'
actor = 'egg@chicken.com'
2021-12-27 19:05:25 +00:00
domain, port = get_domain_from_actor(actor)
2020-08-13 16:41:02 +00:00
assert domain == 'chicken.com'
2021-12-27 22:19:18 +00:00
nickname = get_nickname_from_actor(actor)
2020-08-13 16:41:02 +00:00
assert nickname == 'egg'
actor = '@waffle@cardboard'
2021-12-27 19:05:25 +00:00
domain, port = get_domain_from_actor(actor)
2020-08-13 16:41:02 +00:00
assert domain == 'cardboard'
2021-12-27 22:19:18 +00:00
nickname = get_nickname_from_actor(actor)
2020-08-13 16:41:02 +00:00
assert nickname == 'waffle'
actor = 'https://astral/channel/sky'
2021-12-27 19:05:25 +00:00
domain, port = get_domain_from_actor(actor)
2020-08-13 16:41:02 +00:00
assert domain == 'astral'
2021-12-27 22:19:18 +00:00
nickname = get_nickname_from_actor(actor)
2020-08-13 16:41:02 +00:00
assert nickname == 'sky'
2020-04-05 13:25:47 +00:00
actor = 'https://randomain/users/rando'
2021-12-27 19:05:25 +00:00
domain, port = get_domain_from_actor(actor)
2020-04-05 13:25:47 +00:00
assert domain == 'randomain'
2021-12-27 22:19:18 +00:00
nickname = get_nickname_from_actor(actor)
2020-04-05 13:25:47 +00:00
assert nickname == 'rando'
actor = 'https://otherdomain:49/@othernick'
2021-12-27 19:05:25 +00:00
domain, port = get_domain_from_actor(actor)
2020-04-05 13:25:47 +00:00
assert domain == 'otherdomain'
assert port == 49
2021-12-27 22:19:18 +00:00
nickname = get_nickname_from_actor(actor)
2020-04-05 13:25:47 +00:00
assert nickname == 'othernick'
2019-08-21 16:35:46 +00:00
2021-12-29 21:55:09 +00:00
def _test_web_links():
2022-01-04 20:14:19 +00:00
print('test_web_links')
2020-01-24 10:52:59 +00:00
example_text = \
'<p>Some text!</p><p><a href=\"https://videosite.whatever/video' + \
'/A3JpZMovL25kci1kZS32MGE0NCg4YB1lMLQwLTRkMGEtYkYxMS5kNmQ1MjJqY' + \
'WZjKzd\">https://videosite.whatever/video/A3JpZMovL25kci1kZS32' + \
'MGE0NCg4YB1lMLQwLTRkMGEtYkYxMS5kNmQ1MjJqYWZjKzd</a></p>'
linked_text = add_web_links(example_text)
expected_text = \
'<p>Some text!</p><p><a href="https://videosite.whatever/video/' + \
'A3JpZMovL25kci1kZS32MGE0NCg4YB1lMLQwLTRkMGEtYkYxMS5kNmQ1MjJqYW' + \
'ZjKzd">https://videosite.whatever/video/A3JpZM</a></p>'
assert linked_text == expected_text
2022-01-04 20:14:19 +00:00
example_text = \
2021-03-17 21:17:27 +00:00
"<p>Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + \
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + \
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + \
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + \
" <a href=\"https://domain.ugh/tags/turbot\" class=\"mention " + \
"hashtag\" rel=\"tag\">#<span>turbot</span></a> <a href=\"" + \
"https://domain.ugh/tags/haddock\" class=\"mention hashtag\"" + \
" rel=\"tag\">#<span>haddock</span></a></p>"
2022-01-04 20:14:19 +00:00
result_text = remove_long_words(example_text, 40, [])
assert result_text == "<p>Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + \
2021-03-17 21:17:27 +00:00
" <a href=\"https://domain.ugh/tags/turbot\" class=\"mention " + \
"hashtag\" rel=\"tag\">#<span>turbot</span></a> " + \
"<a href=\"https://domain.ugh/tags/haddock\" " + \
"class=\"mention hashtag\" rel=\"tag\">#<span>haddock</span></a></p>"
2022-01-04 20:14:19 +00:00
example_text = \
2020-04-05 13:25:47 +00:00
'<p><span class=\"h-card\"><a href=\"https://something/@orother' + \
'\" class=\"u-url mention\">@<span>foo</span></a></span> Some ' + \
'random text.</p><p>AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' + \
'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' + \
'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' + \
'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' + \
'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' + \
'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA</p>'
2022-01-04 20:14:19 +00:00
result_text = remove_long_words(example_text, 40, [])
assert result_text == \
2020-04-05 13:25:47 +00:00
'<p><span class="h-card"><a href="https://something/@orother"' + \
' class="u-url mention">@<span>foo</span></a></span> ' + \
'Some random text.</p>'
2022-01-04 20:14:19 +00:00
example_text = \
2020-04-05 13:25:47 +00:00
'This post has a web links https://somesite.net\n\nAnd some other text'
2022-01-04 20:14:19 +00:00
linked_text = add_web_links(example_text)
2022-05-25 12:57:31 +00:00
expected_text = \
'<a href="https://somesite.net" tabindex="10" ' + \
'rel="nofollow noopener noreferrer"' + \
2020-04-05 13:25:47 +00:00
' target="_blank"><span class="invisible">https://' + \
2022-05-25 12:57:31 +00:00
'</span><span class="ellipsis">somesite.net</span></a'
if expected_text not in linked_text:
print(expected_text + '\n')
print(linked_text)
assert expected_text in linked_text
2020-04-05 13:25:47 +00:00
2022-01-04 20:14:19 +00:00
example_text = \
2020-04-05 13:25:47 +00:00
'This post has a very long web link\n\nhttp://' + \
'cbwebewuvfuftdiudbqd33dddbbyuef23fyug3bfhcyu2fct2' + \
'cuyqbcbucuwvckiwyfgewfvqejbchevbhwevuevwbqebqekve' + \
'qvuvjfkf.onion\n\nAnd some other text'
2022-01-04 20:14:19 +00:00
linked_text = add_web_links(example_text)
assert 'ellipsis' in linked_text
2020-01-24 10:52:59 +00:00
2022-01-04 20:14:19 +00:00
example_text = \
2020-04-05 13:25:47 +00:00
'<p>1. HAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAH' + \
'AHAHAHHAHAHAHAHAHAHAHAHAHAHAHAHHAHAHAHAHAHAHAHAH</p>'
2022-01-04 20:14:19 +00:00
result_text = remove_long_words(example_text, 40, [])
assert result_text == '<p>1. HAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHA</p>'
2020-04-05 13:25:47 +00:00
2022-01-04 20:14:19 +00:00
example_text = \
2020-04-05 13:25:47 +00:00
'<p>Tox address is 88AB9DED6F9FBEF43E105FB72060A2D89F9B93C74' + \
'4E8C45AB3C5E42C361C837155AFCFD9D448 </p>'
2022-01-04 20:14:19 +00:00
result_text = remove_long_words(example_text, 40, [])
assert result_text == example_text
2020-04-05 13:25:47 +00:00
2022-01-04 20:14:19 +00:00
example_text = \
2020-10-31 23:10:38 +00:00
'some.incredibly.long.and.annoying.word.which.should.be.removed: ' + \
'The remaining text'
2022-01-04 20:14:19 +00:00
result_text = remove_long_words(example_text, 40, [])
assert result_text == \
2020-10-31 23:10:38 +00:00
'some.incredibly.long.and.annoying.word.w\n' + \
'hich.should.be.removed: The remaining text'
2022-01-04 20:14:19 +00:00
example_text = \
2020-04-05 13:25:47 +00:00
'<p>Tox address is 88AB9DED6F9FBEF43E105FB72060A2D89F9B93C74' + \
'4E8C45AB3C5E42C361C837155AFCFD9D448</p>'
2022-01-04 20:14:19 +00:00
result_text = remove_long_words(example_text, 40, [])
assert result_text == \
2020-04-05 13:25:47 +00:00
'<p>Tox address is 88AB9DED6F9FBEF43E105FB72060A2D89F9B93C7\n' + \
'44E8C45AB3C5E42C361C837155AFCFD9D448</p>'
2022-01-04 20:14:19 +00:00
example_text = \
2020-04-05 13:25:47 +00:00
'<p>ABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCA' + \
'BCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCAB' + \
'CABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABC' + \
'ABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCA' + \
'BCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCAB' + \
'CABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABC' + \
'ABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCA' + \
'BCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCAB' + \
'CABCABCABCABCABCABCABCABC</p>'
2022-01-04 20:14:19 +00:00
result_text = remove_long_words(example_text, 40, [])
assert result_text == r'<p>ABCABCABCABCABCABCABCABCABCABCABCABCABCA<\p>'
2020-04-05 13:25:47 +00:00
2022-01-04 20:14:19 +00:00
example_text = \
2020-04-05 13:25:47 +00:00
'"the nucleus of mutual-support institutions, habits, and customs ' + \
'remains alive with the millions; it keeps them together; and ' + \
'they prefer to cling to their customs, beliefs, and traditions ' + \
'rather than to accept the teachings of a war of each ' + \
'against all"\n\n--Peter Kropotkin'
2022-01-04 20:14:19 +00:00
test_fn_str = add_web_links(example_text)
result_text = remove_long_words(test_fn_str, 40, [])
assert result_text == example_text
assert 'ellipsis' not in result_text
2020-03-29 09:59:54 +00:00
2022-01-04 20:14:19 +00:00
example_text = \
2020-05-12 09:34:58 +00:00
'<p>' + \
'<<\\p>'
2022-01-04 20:14:19 +00:00
result_text = replace_content_duplicates(example_text)
assert result_text == \
2020-05-12 09:34:58 +00:00
'<p>' + \
''
2022-01-04 20:14:19 +00:00
example_text = \
2020-08-11 17:18:22 +00:00
'<p>Test1 test2 #YetAnotherExcessivelyLongwindedAndBoringHashtag</p>'
2022-01-04 20:14:19 +00:00
test_fn_str = add_web_links(example_text)
result_text = remove_long_words(test_fn_str, 40, [])
2022-09-21 20:17:21 +00:00
assert (result_text ==
'<p>Test1 test2 '
'#YetAnotherExcessivelyLongwindedAndBorin\ngHashtag</p>')
2020-08-07 20:51:34 +00:00
2022-01-04 20:14:19 +00:00
example_text = \
2020-12-06 14:47:06 +00:00
"<p>Don't remove a p2p link " + \
"rad:git:hwd1yrerc3mcgn8ga9rho3dqi4w33nep7kxmqezss4topyfgmexihp" + \
"33xcw</p>"
2022-01-04 20:14:19 +00:00
test_fn_str = add_web_links(example_text)
result_text = remove_long_words(test_fn_str, 40, [])
assert result_text == example_text
2020-12-06 10:18:41 +00:00
2020-01-24 10:52:59 +00:00
2021-12-29 21:55:09 +00:00
def _test_addemoji(base_dir: str):
2022-01-04 20:14:19 +00:00
print('test_addemoji')
2020-04-05 13:25:47 +00:00
content = "Emoji :lemon: :strawberry: :banana:"
2021-12-25 17:09:22 +00:00
http_prefix = 'http'
2020-04-05 13:25:47 +00:00
nickname = 'testuser'
domain = 'testdomain.net'
port = 3682
recipients = []
2022-07-18 16:18:04 +00:00
translate = {}
2020-04-05 13:25:47 +00:00
hashtags = {}
2022-01-04 20:14:19 +00:00
base_dir_original = base_dir
2021-12-25 16:17:53 +00:00
path = base_dir + '/.tests'
2019-09-29 18:48:34 +00:00
if not os.path.isdir(path):
os.mkdir(path)
2021-12-25 16:17:53 +00:00
path = base_dir + '/.tests/emoji'
2019-09-29 18:48:34 +00:00
if os.path.isdir(path):
2021-10-29 18:48:15 +00:00
shutil.rmtree(path, ignore_errors=False, onerror=None)
2020-03-22 21:16:02 +00:00
os.mkdir(path)
2021-12-25 16:17:53 +00:00
base_dir = path
path = base_dir + '/emoji'
2019-09-29 18:48:34 +00:00
if os.path.isdir(path):
2021-10-29 18:48:15 +00:00
shutil.rmtree(path, ignore_errors=False, onerror=None)
2020-03-22 21:16:02 +00:00
os.mkdir(path)
2022-01-04 20:14:19 +00:00
copytree(base_dir_original + '/emoji', base_dir + '/emoji')
2021-12-25 16:17:53 +00:00
os.chdir(base_dir)
2022-01-04 20:14:19 +00:00
private_key_pem, public_key_pem, person, wf_endpoint = \
2021-12-29 21:55:09 +00:00
create_person(base_dir, nickname, domain, port,
http_prefix, True, False, 'password')
2022-01-04 20:14:19 +00:00
assert private_key_pem
assert public_key_pem
assert person
assert wf_endpoint
content_modified = \
2021-12-29 21:55:09 +00:00
add_html_tags(base_dir, http_prefix,
nickname, domain, content,
2022-07-18 16:18:04 +00:00
recipients, hashtags, translate, True)
2022-01-04 20:14:19 +00:00
assert ':lemon:' in content_modified
assert content_modified.startswith('<p>')
assert content_modified.endswith('</p>')
2020-04-05 13:25:47 +00:00
tags = []
2022-01-04 20:14:19 +00:00
for _, tag in hashtags.items():
2019-09-29 18:48:34 +00:00
tags.append(tag)
2022-01-04 20:14:19 +00:00
content = content_modified
content_modified = \
2022-04-21 13:03:40 +00:00
replace_emoji_from_tags(None, base_dir, content, tags, 'content',
True, True)
expected_content = '<p>Emoji 🍋 🍓 🍌</p>'
if content_modified != expected_content:
print('expected_content: ' + expected_content)
print('content_modified: ' + content_modified)
assert content_modified == expected_content
content_modified = \
replace_emoji_from_tags(None, base_dir, content, tags, 'content',
True, False)
expected_content = '<p>Emoji <span aria-hidden="true">🍋</span>' + \
' <span aria-hidden="true">🍓</span> ' + \
'<span aria-hidden="true">🍌</span></p>'
if content_modified != expected_content:
print('expected_content: ' + expected_content)
print('content_modified: ' + content_modified)
assert content_modified == expected_content
2019-09-29 18:48:34 +00:00
2022-01-04 20:14:19 +00:00
os.chdir(base_dir_original)
shutil.rmtree(base_dir_original + '/.tests',
2021-10-29 18:48:15 +00:00
ignore_errors=False, onerror=None)
2020-04-05 13:25:47 +00:00
2019-09-29 18:48:34 +00:00
2021-12-29 21:55:09 +00:00
def _test_get_status_number():
2022-01-04 20:14:19 +00:00
print('test_get_status_number')
prev_status_number = None
for _ in range(1, 20):
status_number, _ = get_status_number()
if prev_status_number:
assert len(status_number) == 18
assert int(status_number) > prev_status_number
prev_status_number = int(status_number)
2020-04-05 13:25:47 +00:00
2019-10-12 12:45:53 +00:00
2021-12-29 21:55:09 +00:00
def _test_json_string() -> None:
2022-01-04 20:14:19 +00:00
print('test_json_string')
2021-12-29 21:55:09 +00:00
filename = '.epicyon_tests_test_json_string.json'
2022-01-04 20:14:19 +00:00
message_str = "Crème brûlée यह एक परीक्षण ह"
test_json = {
"content": message_str
2019-11-09 12:13:39 +00:00
}
2022-01-04 20:14:19 +00:00
assert save_json(test_json, filename)
received_json = load_json(filename, 0)
assert received_json
assert received_json['content'] == message_str
encoded_str = json.dumps(test_json, ensure_ascii=False)
assert message_str in encoded_str
try:
os.remove(filename)
2021-11-25 18:42:38 +00:00
except OSError:
pass
2019-11-09 12:13:39 +00:00
2020-04-05 13:25:47 +00:00
2021-12-29 21:55:09 +00:00
def _test_save_load_json():
2022-01-04 20:14:19 +00:00
print('test_save_load_json')
test_json = {
2019-11-23 10:13:57 +00:00
"param1": 3,
2019-11-23 10:20:30 +00:00
"param2": '"Crème brûlée यह एक परीक्षण ह"'
2019-11-23 10:13:57 +00:00
}
2022-01-04 20:14:19 +00:00
test_filename = '.epicyon_tests_test_save_load_json.json'
if os.path.isfile(test_filename):
try:
2022-01-04 20:14:19 +00:00
os.remove(test_filename)
2021-11-25 18:42:38 +00:00
except OSError:
pass
2022-01-04 20:14:19 +00:00
assert save_json(test_json, test_filename)
assert os.path.isfile(test_filename)
test_load_json = load_json(test_filename)
assert test_load_json
assert test_load_json.get('param1')
assert test_load_json.get('param2')
assert test_load_json['param1'] == 3
assert test_load_json['param2'] == '"Crème brûlée यह एक परीक्षण ह"'
try:
2022-01-04 20:14:19 +00:00
os.remove(test_filename)
2021-11-25 18:42:38 +00:00
except OSError:
pass
2019-11-23 13:04:11 +00:00
2020-04-05 13:25:47 +00:00
2021-12-29 21:55:09 +00:00
def _test_theme():
2022-01-04 20:14:19 +00:00
print('test_theme')
2020-04-05 13:25:47 +00:00
css = 'somestring --background-value: 24px; --foreground-value: 24px;'
2022-01-26 23:17:53 +00:00
result = set_css_param(css, 'background-value', '32px')
2020-04-05 13:25:47 +00:00
assert result == \
'somestring --background-value: 32px; --foreground-value: 24px;'
css = \
'somestring --background-value: 24px; --foreground-value: 24px; ' + \
'--background-value: 24px;'
2022-01-26 23:17:53 +00:00
result = set_css_param(css, 'background-value', '32px')
2020-04-05 13:25:47 +00:00
assert result == \
'somestring --background-value: 32px; --foreground-value: 24px; ' + \
'--background-value: 32px;'
css = '--background-value: 24px; --foreground-value: 24px;'
2022-01-26 23:17:53 +00:00
result = set_css_param(css, 'background-value', '32px')
2020-04-05 13:25:47 +00:00
assert result == '--background-value: 32px; --foreground-value: 24px;'
2019-11-23 13:04:11 +00:00
2021-12-29 21:55:09 +00:00
def _test_recent_posts_cache():
2022-01-04 20:14:19 +00:00
print('test_recent_posts_cache')
2021-12-26 20:01:37 +00:00
recent_posts_cache = {}
2021-12-25 20:28:06 +00:00
max_recent_posts = 3
2022-01-04 20:14:19 +00:00
html_str = '<html></html>'
for i in range(5):
2021-12-25 22:09:19 +00:00
post_json_object = {
2021-06-22 12:42:52 +00:00
"id": "https://somesite.whatever/users/someuser/statuses/" + str(i)
}
2021-12-28 14:24:14 +00:00
update_recent_posts_cache(recent_posts_cache, max_recent_posts,
2022-01-04 20:14:19 +00:00
post_json_object, html_str)
2021-12-26 20:01:37 +00:00
assert len(recent_posts_cache['index']) == max_recent_posts
assert len(recent_posts_cache['json'].items()) == max_recent_posts
assert len(recent_posts_cache['html'].items()) == max_recent_posts
2020-04-05 13:25:47 +00:00
2021-12-29 21:55:09 +00:00
def _test_remove_txt_formatting():
2022-01-04 20:14:19 +00:00
print('test_remove_txt_formatting')
test_str = '<p>Text without formatting</p>'
2022-03-24 14:40:28 +00:00
result_str = remove_text_formatting(test_str, False)
2022-01-04 20:14:19 +00:00
assert result_str == test_str
test_str = '<p>Text <i>with</i> <h3>formatting</h3></p>'
2022-03-24 14:40:28 +00:00
result_str = remove_text_formatting(test_str, False)
2022-01-04 20:14:19 +00:00
assert result_str == '<p>Text with formatting</p>'
2021-12-29 21:55:09 +00:00
def _test_jsonld():
2022-01-04 20:14:19 +00:00
print("test_jsonld")
2022-01-04 20:14:19 +00:00
jld_document = {
"@context": "https://www.w3.org/ns/activitystreams",
"actor": "https://somesite.net/users/gerbil",
2020-06-15 12:37:53 +00:00
"description": "My json document",
"numberField": 83582,
"object": {
2021-01-03 14:34:27 +00:00
"content": "valid content"
2020-06-15 12:37:53 +00:00
}
}
2022-01-04 20:14:19 +00:00
# private_key_pem, public_key_pem = generate_rsa_key()
private_key_pem = '-----BEGIN RSA PRIVATE KEY-----\n' \
2020-06-15 12:37:53 +00:00
'MIIEowIBAAKCAQEAod9iHfIn4ugY/2byFrFjUprrFLkkH5bCrjiBq2/MdHFg99IQ\n' \
'7li2x2mg5fkBMhU5SJIxlN8kiZMFq7JUXSA97Yo4puhVubqTSHihIh6Xn2mTjTgs\n' \
'zNo9SBbmN3YiyBPTcr0rF4jGWZAduJ8u6i7Eky2QH+UBKyUNRZrcfoVq+7grHUIA\n' \
'45pE7vAfEEWtgRiw32Nwlx55N3hayHax0y8gMdKEF/vfYKRLcM7rZgEASMtlCpgy\n' \
'fsyHwFCDzl/BP8AhP9u3dM+SEundeAvF58AiXx1pKvBpxqttDNAsKWCRQ06/WI/W\n' \
'2Rwihl9yCjobqRoFsZ/cTEi6FG9AbDAds5YjTwIDAQABAoIBAERL3rbpy8Bl0t43\n' \
'jh7a+yAIMvVMZBxb3InrV3KAug/LInGNFQ2rKnsaawN8uu9pmwCuhfLc7yqIeJUH\n' \
'qaadCuPlNJ/fWQQC309tbfbaV3iv78xejjBkSATZfIqb8nLeQpGflMXaNG3na1LQ\n' \
'/tdZoiDC0ZNTaNnOSTo765oKKqhHUTQkwkGChrwG3Js5jekV4zpPMLhUafXk6ksd\n' \
'8XLlZdCF3RUnuguXAg2xP/duxMYmTCx3eeGPkXBPQl0pahu8/6OtBoYvBrqNdQcx\n' \
'jnEtYX9PCqDY3hAXW9GWsxNfu02DKhWigFHFNRUQtMI++438+QIfzXPslE2bTQIt\n' \
'0OXUlwECgYEAxTKUZ7lwIBb5XKPJq53RQmX66M3ArxI1RzFSKm1+/CmxvYiN0c+5\n' \
'2Aq62WEIauX6hoZ7yQb4zhdeNRzinLR7rsmBvIcP12FidXG37q9v3Vu70KmHniJE\n' \
'TPbt5lHQ0bNACFxkar4Ab/JZN4CkMRgJdlcZ5boYNmcGOYCvw9izuM8CgYEA0iQ1\n' \
'khIFZ6fCiXwVRGvEHmqSnkBmBHz8MY8fczv2Z4Gzfq3Tlh9VxpigK2F2pFt7keWc\n' \
'53HerYFHFpf5otDhEyRwA1LyIcwbj5HopumxsB2WG+/M2as45lLfWa6KO73OtPpU\n' \
'wGZYW+i/otdk9eFphceYtw19mxI+3lYoeI8EjYECgYBxOtTKJkmCs45lqkp/d3QT\n' \
'2zjSempcXGkpQuG6KPtUUaCUgxdj1RISQj792OCbeQh8PDZRvOYaeIKInthkQKIQ\n' \
'P/Z1yVvIQUvmwfBqZmQmR6k1bFLJ80UiqFr7+BiegH2RD3Q9cnIP1aly3DPrWLD+\n' \
'OY9OQKfsfQWu+PxzyTeRMwKBgD8Zjlh5PtQ8RKcB8mTkMzSq7bHFRpzsZtH+1wPE\n' \
'Kp40DRDp41H9wMTsiZPdJUH/EmDh4LaCs8nHuu/m3JfuPtd/pn7pBjntzwzSVFji\n' \
'bW+jwrJK1Gk8B87pbZXBWlLMEOi5Dn/je37Fqd2c7f0DHauFHq9AxsmsteIPXwGs\n' \
'eEKBAoGBAIzJX/5yFp3ObkPracIfOJ/U/HF1UdP6Y8qmOJBZOg5s9Y+JAdY76raK\n' \
'0SbZPsOpuFUdTiRkSI3w/p1IuM5dPxgCGH9MHqjqogU5QwXr3vLF+a/PFhINkn1x\n' \
'lozRZjDcF1y6xHfExotPC973UZnKEviq9/FqOsovZpvSQkzAYSZF\n' \
'-----END RSA PRIVATE KEY-----'
2022-01-04 20:14:19 +00:00
public_key_pem = '-----BEGIN PUBLIC KEY-----\n' \
2020-06-15 12:37:53 +00:00
'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAod9iHfIn4ugY/2byFrFj\n' \
'UprrFLkkH5bCrjiBq2/MdHFg99IQ7li2x2mg5fkBMhU5SJIxlN8kiZMFq7JUXSA9\n' \
'7Yo4puhVubqTSHihIh6Xn2mTjTgszNo9SBbmN3YiyBPTcr0rF4jGWZAduJ8u6i7E\n' \
'ky2QH+UBKyUNRZrcfoVq+7grHUIA45pE7vAfEEWtgRiw32Nwlx55N3hayHax0y8g\n' \
'MdKEF/vfYKRLcM7rZgEASMtlCpgyfsyHwFCDzl/BP8AhP9u3dM+SEundeAvF58Ai\n' \
'Xx1pKvBpxqttDNAsKWCRQ06/WI/W2Rwihl9yCjobqRoFsZ/cTEi6FG9AbDAds5Yj\n' \
'TwIDAQAB\n' \
'-----END PUBLIC KEY-----'
2022-01-04 20:14:19 +00:00
signed_document = jld_document.copy()
generate_json_signature(signed_document, private_key_pem)
assert signed_document
assert signed_document.get('signature')
assert signed_document['signature'].get('signatureValue')
assert signed_document['signature'].get('nonce')
2022-01-04 20:14:19 +00:00
assert signed_document['signature'].get('type')
assert len(signed_document['signature']['signatureValue']) > 50
assert signed_document['signature']['type'] == 'RsaSignature2017'
assert verify_json_signature(signed_document, public_key_pem)
# alter the signed document
2022-01-04 20:14:19 +00:00
signed_document['object']['content'] = 'forged content'
assert not verify_json_signature(signed_document, public_key_pem)
2020-06-15 12:37:53 +00:00
2022-01-04 20:14:19 +00:00
jld_document2 = {
"@context": "https://www.w3.org/ns/activitystreams",
"actor": "https://somesite.net/users/gerbil",
2021-01-03 15:27:59 +00:00
"description": "Another json document",
"numberField": 13353,
"object": {
"content": "More content"
}
}
2022-01-04 20:14:19 +00:00
signed_document2 = jld_document2.copy()
generate_json_signature(signed_document2, private_key_pem)
assert signed_document2
assert signed_document2.get('signature')
assert signed_document2['signature'].get('signatureValue')
2021-01-03 15:27:59 +00:00
# changed signature on different document
2022-01-04 20:14:19 +00:00
if signed_document['signature']['signatureValue'] == \
signed_document2['signature']['signatureValue']:
2021-01-03 15:27:59 +00:00
print('json signature has not changed for different documents')
2022-01-04 20:14:19 +00:00
assert '.' not in str(signed_document['signature']['signatureValue'])
assert len(str(signed_document['signature']['signatureValue'])) > 340
assert (signed_document['signature']['signatureValue'] !=
signed_document2['signature']['signatureValue'])
print('json-ld tests passed')
2021-01-03 15:27:59 +00:00
2020-06-15 12:37:53 +00:00
2021-12-29 21:55:09 +00:00
def _test_site_active():
2022-01-04 20:14:19 +00:00
print('test_site_is_active')
timeout = 10
2022-03-04 15:28:55 +00:00
# at least one site should resolve
if not site_is_active('https://archive.org', timeout):
if not site_is_active('https://wikipedia.org', timeout):
assert site_is_active('https://mastodon.social', timeout)
2022-01-04 20:14:19 +00:00
assert not site_is_active('https://notarealwebsite.a.b.c', timeout)
2020-06-22 16:55:19 +00:00
2021-12-29 21:55:09 +00:00
def _test_strip_html():
2022-01-04 20:14:19 +00:00
print('test_remove_html')
test_str = 'This string has no html.'
assert remove_html(test_str) == test_str
test_str = 'This string <a href="1234.567">has html</a>.'
assert remove_html(test_str) == 'This string has html.'
test_str = '<label>This string has.</label><label>Two labels.</label>'
assert remove_html(test_str) == 'This string has. Two labels.'
test_str = '<p>This string has.</p><p>Two paragraphs.</p>'
assert remove_html(test_str) == 'This string has.\n\nTwo paragraphs.'
test_str = 'This string has.<br>A new line.'
assert remove_html(test_str) == 'This string has.\nA new line.'
test_str = '<p>This string contains a url http://somesite.or.other</p>'
assert remove_html(test_str) == \
'This string contains a url http://somesite.or.other'
2021-12-29 21:55:09 +00:00
def _test_danger_css(base_dir: str) -> None:
2022-01-04 20:14:19 +00:00
print('test_dangerous_css')
for _, _, files in os.walk(base_dir):
for fname in files:
if not fname.endswith('.css'):
2020-12-13 14:48:45 +00:00
continue
2022-01-04 20:14:19 +00:00
assert not dangerous_css(base_dir + '/' + fname, False)
2020-12-13 14:48:45 +00:00
break
2021-12-29 21:55:09 +00:00
def _test_danger_svg(base_dir: str) -> None:
2022-01-04 21:19:06 +00:00
print('test_dangerous_svg')
2022-01-04 20:14:19 +00:00
svg_content = \
' <svg viewBox="0 0 10 10" xmlns="http://www.w3.org/2000/svg">' + \
' <circle cx="5" cy="5" r="4" />' + \
'</svg>'
2022-01-04 20:14:19 +00:00
assert not dangerous_svg(svg_content, False)
2022-05-26 12:17:56 +00:00
cleaned_up = remove_script(svg_content, None, None, None)
assert cleaned_up == svg_content
2022-01-04 20:14:19 +00:00
svg_content = \
' <svg viewBox="0 0 10 10" xmlns="http://www.w3.org/2000/svg">' + \
' <script>' + \
' // <![CDATA[' + \
" window.addEventListener('DOMContentLoaded', () => {" + \
' function attackScript () {' + \
' return `#${OWO}`' + \
' }' + \
'' + \
" document.querySelector('circle')." + \
"addEventListener('click', (e) => {" + \
' e.target.style.fill = attackScript()' + \
' })' + \
' })' + \
' // ]]>' + \
' </script>' + \
'' + \
' <circle cx="5" cy="5" r="4" />' + \
'</svg>'
2022-01-04 20:14:19 +00:00
assert dangerous_svg(svg_content, False)
svg_clean = \
' <svg viewBox="0 0 10 10" xmlns="http://www.w3.org/2000/svg">' + \
' <circle cx="5" cy="5" r="4" />' + \
'</svg>'
2022-05-26 12:17:56 +00:00
cleaned_up = remove_script(svg_content, None, None, None)
assert '<script' not in cleaned_up
assert '/script>' not in cleaned_up
if cleaned_up != svg_clean:
print(cleaned_up)
assert cleaned_up == svg_clean
2022-05-26 15:14:48 +00:00
session = None
http_prefix = 'https'
nickname = 'amplifier'
domain = 'ratsratsrats.live'
domain_full = domain
onion_domain = None
i2p_domain = None
federation_list = []
debug = True
svg_image_filename = base_dir + '/.unit_test_safe.svg'
post_json_object = {
"object": {
"id": "1234",
"attributedTo": "someactor",
"attachment": [
{
"mediaType": "svg",
"url": "https://somesiteorother.net/media/wibble.svg"
}
]
}
}
with open(svg_image_filename, 'wb+') as fp_svg:
fp_svg.write(svg_content.encode('utf-8'))
assert os.path.isfile(svg_image_filename)
assert svg_content != svg_clean
assert cache_svg_images(session, base_dir, http_prefix,
nickname, domain, domain_full,
onion_domain, i2p_domain,
post_json_object,
federation_list, debug,
svg_image_filename)
url = post_json_object['object']['attachment'][0]['url']
assert url == 'https://ratsratsrats.live/media/1234_wibble.svg'
with open(svg_image_filename, 'rb') as fp_svg:
cached_content = fp_svg.read().decode()
os.remove(svg_image_filename)
assert cached_content == svg_clean
2021-12-29 21:55:09 +00:00
assert not scan_themes_for_scripts(base_dir)
2021-09-13 18:17:01 +00:00
2021-12-29 21:55:09 +00:00
def _test_danger_markup():
2022-01-04 21:19:06 +00:00
print('test_dangerous_markup')
2021-12-25 18:54:50 +00:00
allow_local_network_access = False
2020-07-10 14:15:01 +00:00
content = '<p>This is a valid message</p>'
2022-01-04 20:14:19 +00:00
assert not dangerous_markup(content, allow_local_network_access)
2020-11-11 09:42:48 +00:00
2020-07-10 14:15:01 +00:00
content = 'This is a valid message without markup'
2022-01-04 20:14:19 +00:00
assert not dangerous_markup(content, allow_local_network_access)
2020-11-11 09:42:48 +00:00
2020-07-10 14:15:01 +00:00
content = '<p>This is a valid-looking message. But wait... ' + \
'<script>document.getElementById("concentrated")' + \
'.innerHTML = "evil";</script></p>'
2022-01-04 20:14:19 +00:00
assert dangerous_markup(content, allow_local_network_access)
2020-11-11 09:42:48 +00:00
2021-05-19 11:29:37 +00:00
content = '<p>This is a valid-looking message. But wait... ' + \
'&lt;script&gt;document.getElementById("concentrated")' + \
'.innerHTML = "evil";&lt;/script&gt;</p>'
2022-01-04 20:14:19 +00:00
assert dangerous_markup(content, allow_local_network_access)
2021-05-19 11:29:37 +00:00
2020-11-15 10:36:24 +00:00
content = '<p>This html contains more than you expected... ' + \
'<script language="javascript">document.getElementById("abc")' + \
'.innerHTML = "def";</script></p>'
2022-01-04 20:14:19 +00:00
assert dangerous_markup(content, allow_local_network_access)
2020-11-15 10:36:24 +00:00
2020-07-10 14:15:01 +00:00
content = '<p>This is a valid-looking message. But wait... ' + \
'<script src="https://evilsite/payload.js" /></p>'
2022-01-04 20:14:19 +00:00
assert dangerous_markup(content, allow_local_network_access)
2020-11-11 09:42:48 +00:00
2022-03-22 18:22:09 +00:00
content = '<p>This is a valid-looking message. But it contains ' + \
'spyware. <amp-analytics type="gtag" ' + \
'data-credentials="include"></amp-analytics></p>'
assert dangerous_markup(content, allow_local_network_access)
content = '<p>This is a valid-looking message. But it contains ' + \
'<a href="something.googleapis.com/anotherthing">spyware.</a></p>'
assert dangerous_markup(content, allow_local_network_access)
2020-07-10 14:15:01 +00:00
content = '<p>This message embeds an evil frame.' + \
'<iframe src="somesite"></iframe></p>'
2022-01-04 20:14:19 +00:00
assert dangerous_markup(content, allow_local_network_access)
2020-11-11 09:42:48 +00:00
2020-07-10 14:15:01 +00:00
content = '<p>This message tries to obfuscate an evil frame.' + \
'< iframe src = "somesite"></ iframe ></p>'
2022-01-04 20:14:19 +00:00
assert dangerous_markup(content, allow_local_network_access)
2020-11-11 09:42:48 +00:00
2020-07-10 14:15:01 +00:00
content = '<p>This message is not necessarily evil, but annoying.' + \
'<hr><br><br><br><br><br><br><br><hr><hr></p>'
2022-01-04 20:14:19 +00:00
assert dangerous_markup(content, allow_local_network_access)
2020-11-11 09:42:48 +00:00
2020-07-10 14:15:01 +00:00
content = '<p>This message contans a ' + \
'<a href="https://validsite/index.html">valid link.</a></p>'
2022-01-04 20:14:19 +00:00
assert not dangerous_markup(content, allow_local_network_access)
2020-11-11 09:42:48 +00:00
2020-07-10 14:15:01 +00:00
content = '<p>This message contans a ' + \
'<a href="https://validsite/iframe.html">' + \
'valid link having invalid but harmless name.</a></p>'
2022-01-04 20:14:19 +00:00
assert not dangerous_markup(content, allow_local_network_access)
2020-07-10 14:15:01 +00:00
2020-11-11 09:42:48 +00:00
content = '<p>This message which <a href="127.0.0.1:8736">' + \
'tries to access the local network</a></p>'
2022-01-04 20:14:19 +00:00
assert dangerous_markup(content, allow_local_network_access)
2020-11-11 09:42:48 +00:00
content = '<p>This message which <a href="http://192.168.5.10:7235">' + \
'tries to access the local network</a></p>'
2022-01-04 20:14:19 +00:00
assert dangerous_markup(content, allow_local_network_access)
2020-11-11 09:42:48 +00:00
content = '<p>127.0.0.1 This message which does not access ' + \
'the local network</a></p>'
2022-01-04 20:14:19 +00:00
assert not dangerous_markup(content, allow_local_network_access)
2020-11-11 09:42:48 +00:00
2020-07-10 14:15:01 +00:00
2021-12-29 21:55:09 +00:00
def _run_html_replace_quote_marks():
print('html_replace_quote_marks')
2022-01-04 20:14:19 +00:00
test_str = 'The "cat" "sat" on the mat'
result = html_replace_quote_marks(test_str)
2020-08-02 18:09:50 +00:00
assert result == 'The “cat” “sat” on the mat'
2020-08-02 17:01:12 +00:00
2022-01-04 20:14:19 +00:00
test_str = 'The cat sat on the mat'
result = html_replace_quote_marks(test_str)
2020-08-02 17:01:12 +00:00
assert result == 'The cat sat on the mat'
2022-01-04 20:14:19 +00:00
test_str = '"hello"'
result = html_replace_quote_marks(test_str)
2020-08-02 18:09:50 +00:00
assert result == '“hello”'
2020-08-02 17:01:12 +00:00
2022-01-04 20:14:19 +00:00
test_str = '"hello" <a href="somesite.html">&quot;test&quot; html</a>'
result = html_replace_quote_marks(test_str)
2020-08-02 19:16:22 +00:00
assert result == '“hello” <a href="somesite.html">“test” html</a>'
2020-08-02 17:17:51 +00:00
2020-08-02 17:01:12 +00:00
2021-12-29 21:55:09 +00:00
def _test_json_post_allows_comment():
2022-01-04 21:19:06 +00:00
print('test_json_post_allows_comments')
2021-12-25 22:09:19 +00:00
post_json_object = {
2020-08-21 18:32:16 +00:00
"id": "123"
}
2021-12-29 21:55:09 +00:00
assert json_post_allows_comments(post_json_object)
2021-12-25 22:09:19 +00:00
post_json_object = {
2020-08-21 18:32:16 +00:00
"id": "123",
"commentsEnabled": False
}
2021-12-29 21:55:09 +00:00
assert not json_post_allows_comments(post_json_object)
2021-12-25 22:09:19 +00:00
post_json_object = {
"id": "123",
"rejectReplies": False
}
2021-12-29 21:55:09 +00:00
assert json_post_allows_comments(post_json_object)
2021-12-25 22:09:19 +00:00
post_json_object = {
"id": "123",
"rejectReplies": True
}
2021-12-29 21:55:09 +00:00
assert not json_post_allows_comments(post_json_object)
2021-12-25 22:09:19 +00:00
post_json_object = {
2020-08-21 18:32:16 +00:00
"id": "123",
"commentsEnabled": True
}
2021-12-29 21:55:09 +00:00
assert json_post_allows_comments(post_json_object)
2021-12-25 22:09:19 +00:00
post_json_object = {
2020-08-21 18:32:16 +00:00
"id": "123",
"object": {
"commentsEnabled": True
}
}
2021-12-29 21:55:09 +00:00
assert json_post_allows_comments(post_json_object)
2021-12-25 22:09:19 +00:00
post_json_object = {
2020-08-21 18:32:16 +00:00
"id": "123",
"object": {
"commentsEnabled": False
}
}
2021-12-29 21:55:09 +00:00
assert not json_post_allows_comments(post_json_object)
2020-08-21 18:32:16 +00:00
2021-12-29 21:55:09 +00:00
def _test_remove_id_ending():
2022-01-04 21:19:06 +00:00
print('test_remove_id_ending')
2022-01-04 20:14:19 +00:00
test_str = 'https://activitypub.somedomain.net'
result_str = remove_id_ending(test_str)
assert result_str == 'https://activitypub.somedomain.net'
2020-08-23 11:13:35 +00:00
2022-01-04 20:14:19 +00:00
test_str = \
2020-08-23 11:13:35 +00:00
'https://activitypub.somedomain.net/users/foo/' + \
'statuses/34544814814/activity'
2022-01-04 20:14:19 +00:00
result_str = remove_id_ending(test_str)
assert result_str == \
2020-08-23 11:13:35 +00:00
'https://activitypub.somedomain.net/users/foo/statuses/34544814814'
2022-01-04 20:14:19 +00:00
test_str = \
2020-08-23 11:13:35 +00:00
'https://undo.somedomain.net/users/foo/statuses/34544814814/undo'
2022-01-04 20:14:19 +00:00
result_str = remove_id_ending(test_str)
assert result_str == \
2020-08-23 11:13:35 +00:00
'https://undo.somedomain.net/users/foo/statuses/34544814814'
2022-01-04 20:14:19 +00:00
test_str = \
2020-08-23 11:13:35 +00:00
'https://event.somedomain.net/users/foo/statuses/34544814814/event'
2022-01-04 20:14:19 +00:00
result_str = remove_id_ending(test_str)
assert result_str == \
2020-08-23 11:13:35 +00:00
'https://event.somedomain.net/users/foo/statuses/34544814814'
2021-12-29 21:55:09 +00:00
def _test_valid_content_warning():
2022-01-04 21:19:06 +00:00
print('test_valid_content_warning')
2022-01-04 20:14:19 +00:00
result_str = valid_content_warning('Valid content warning')
assert result_str == 'Valid content warning'
2020-08-25 19:35:55 +00:00
2022-01-04 20:14:19 +00:00
result_str = valid_content_warning('Invalid #content warning')
assert result_str == 'Invalid content warning'
2020-08-25 19:35:55 +00:00
2022-01-04 20:14:19 +00:00
result_str = \
2021-12-29 21:55:09 +00:00
valid_content_warning('Invalid <a href="somesite">content warning</a>')
2022-01-04 20:14:19 +00:00
assert result_str == 'Invalid content warning'
2020-08-25 19:35:55 +00:00
2022-05-24 09:50:42 +00:00
def _test_translation_labels() -> None:
print('test_translation_labels')
lang_json = load_json('translations/en.json')
assert lang_json
for _, _, files in os.walk('.'):
for source_file in files:
if not source_file.endswith('.py'):
continue
if source_file.startswith('.#'):
continue
if source_file.startswith('flycheck_'):
continue
source_str = ''
2022-06-09 14:46:30 +00:00
with open(source_file, 'r', encoding='utf-8') as file_source:
2022-05-24 09:50:42 +00:00
source_str = file_source.read()
if not source_str:
continue
if 'translate[' not in source_str:
continue
sections = source_str.split('translate[')
ctr = 0
for text in sections:
if ctr == 0:
ctr += 1
continue
if ']' not in text:
continue
label_str = text.split(']')[0]
if '"' not in label_str and "'" not in label_str:
continue
if '+' in label_str:
continue
label_str = label_str[1:]
label_str = label_str[:len(label_str)-1]
assert label_str
if not lang_json.get(label_str):
print("Translation for label '" + label_str + "' " +
"in module " + source_file + " was not found")
assert False
break
2021-12-29 21:55:09 +00:00
def _test_translations(base_dir: str) -> None:
2022-01-04 21:19:06 +00:00
print('test_translations')
2022-01-04 20:14:19 +00:00
languages_str = get_supported_languages(base_dir)
assert languages_str
2020-08-26 18:21:57 +00:00
# load all translations into a dict
2022-01-04 20:14:19 +00:00
lang_dict = {}
for lang in languages_str:
lang_json = load_json('translations/' + lang + '.json')
if not lang_json:
2020-08-29 11:14:19 +00:00
print('Missing language file ' +
'translations/' + lang + '.json')
2022-01-04 20:14:19 +00:00
assert lang_json
lang_dict[lang] = lang_json
2020-08-26 18:21:57 +00:00
# load english translations
2022-01-04 20:14:19 +00:00
translations_json = load_json('translations/en.json')
2020-08-26 18:21:57 +00:00
# test each english string exists in the other language files
2022-01-04 20:14:19 +00:00
for english_str, _ in translations_json.items():
for lang in languages_str:
lang_json = lang_dict[lang]
if not lang_json.get(english_str):
print(english_str + ' is missing from ' + lang + '.json')
assert lang_json.get(english_str)
2020-08-26 18:21:57 +00:00
2021-12-29 21:55:09 +00:00
def _test_constant_time_string():
2022-01-04 21:19:06 +00:00
print('test_constant_time_string_check')
2021-12-29 21:55:09 +00:00
assert constant_time_string_check('testing', 'testing')
assert not constant_time_string_check('testing', '1234')
assert not constant_time_string_check('testing', '1234567')
itterations = 256
start = time.time()
2021-12-29 21:55:09 +00:00
test_str = 'nnjfbefefbsnjsdnvbcueftqfeuqfbqefnjeniwufgy'
2022-01-04 20:14:19 +00:00
for _ in range(itterations):
2021-12-29 21:55:09 +00:00
constant_time_string_check(test_str, test_str)
end = time.time()
2022-01-04 20:14:19 +00:00
av_time1 = ((end - start) * 1000000 / itterations)
# change a single character and observe timing difference
start = time.time()
2022-01-04 20:14:19 +00:00
for _ in range(itterations):
2021-12-29 21:55:09 +00:00
constant_time_string_check(test_str, test_str)
end = time.time()
2022-01-04 20:14:19 +00:00
av_time2 = ((end - start) * 1000000 / itterations)
time_diff_microseconds = abs(av_time2 - av_time1)
# time difference should be less than 10uS
2022-01-04 20:14:19 +00:00
assert int(time_diff_microseconds) < 10
# change multiple characters and observe timing difference
start = time.time()
2021-12-29 21:55:09 +00:00
test_str2 = 'ano1befffbsn7sd3vbluef6qseuqfpqeznjgni9bfgi'
2022-01-04 20:14:19 +00:00
for _ in range(itterations):
2021-12-29 21:55:09 +00:00
constant_time_string_check(test_str, test_str2)
end = time.time()
2022-01-04 20:14:19 +00:00
av_time2 = ((end - start) * 1000000 / itterations)
time_diff_microseconds = abs(av_time2 - av_time1)
# time difference should be less than 10uS
2022-01-04 20:14:19 +00:00
assert int(time_diff_microseconds) < 10
2021-12-29 21:55:09 +00:00
def _test_replace_email_quote():
2022-01-04 21:19:06 +00:00
print('test_replace_email_quote')
2022-01-04 20:14:19 +00:00
test_str = '<p>This content has no quote.</p>'
assert html_replace_email_quote(test_str) == test_str
2022-01-04 20:14:19 +00:00
test_str = '<p>This content has no quote.</p>' + \
2020-09-14 09:41:44 +00:00
'<p>With multiple</p><p>lines</p>'
2022-01-04 20:14:19 +00:00
assert html_replace_email_quote(test_str) == test_str
2022-01-04 20:14:19 +00:00
test_str = '<p>&quot;This is a quoted paragraph.&quot;</p>'
assert html_replace_email_quote(test_str) == \
2020-09-14 11:30:56 +00:00
'<p><blockquote>This is a quoted paragraph.</blockquote></p>'
2022-01-04 20:14:19 +00:00
test_str = "<p><span class=\"h-card\">" + \
"<a href=\"https://somewebsite/@nickname\" " + \
"class=\"u-url mention\">@<span>nickname</span></a></span> " + \
"<br />&gt; This is a quote</p><p>Some other text.</p>"
2022-01-04 20:14:19 +00:00
expected_str = "<p><span class=\"h-card\">" + \
"<a href=\"https://somewebsite/@nickname\" " + \
"class=\"u-url mention\">@<span>nickname</span></a></span> " + \
"<br /><blockquote>This is a quote</blockquote></p>" + \
"<p>Some other text.</p>"
2022-01-04 20:14:19 +00:00
result_str = html_replace_email_quote(test_str)
if result_str != expected_str:
print('Result: ' + str(result_str))
print('Expect: ' + expected_str)
assert result_str == expected_str
2022-01-04 20:14:19 +00:00
test_str = "<p>Some text:</p><p>&gt; first line-&gt;second line</p>" + \
2020-09-14 10:25:12 +00:00
"<p>Some question?</p>"
2022-01-04 20:14:19 +00:00
expected_str = "<p>Some text:</p><p><blockquote>first line-<br>" + \
2020-09-14 10:25:12 +00:00
"second line</blockquote></p><p>Some question?</p>"
2022-01-04 20:14:19 +00:00
result_str = html_replace_email_quote(test_str)
if result_str != expected_str:
print('Result: ' + str(result_str))
print('Expect: ' + expected_str)
assert result_str == expected_str
2020-09-14 10:25:12 +00:00
2022-01-04 20:14:19 +00:00
test_str = "<p><span class=\"h-card\">" + \
2020-09-30 22:22:42 +00:00
"<a href=\"https://somedomain/@somenick\" " + \
"class=\"u-url mention\">@<span>somenick</span>" + \
"</a></span> </p><p>&gt; Text1.<br />&gt; <br />" + \
"&gt; Text2<br />&gt; <br />&gt; Text3<br />" + \
"&gt;<br />&gt; Text4<br />&gt; <br />&gt; " + \
"Text5<br />&gt; <br />&gt; Text6</p><p>Text7</p>"
2022-01-04 20:14:19 +00:00
expected_str = "<p><span class=\"h-card\">" + \
2020-09-30 22:55:53 +00:00
"<a href=\"https://somedomain/@somenick\" " + \
"class=\"u-url mention\">@<span>somenick</span></a>" + \
"</span> </p><p><blockquote> Text1.<br /><br />" + \
"Text2<br /><br />Text3<br />&gt;<br />Text4<br />" + \
"<br />Text5<br /><br />Text6</blockquote></p><p>Text7</p>"
2022-01-04 20:14:19 +00:00
result_str = html_replace_email_quote(test_str)
if result_str != expected_str:
print('Result: ' + str(result_str))
print('Expect: ' + expected_str)
assert result_str == expected_str
2020-09-30 22:22:42 +00:00
2021-12-29 21:55:09 +00:00
def _test_strip_html_tag():
2022-01-04 21:19:06 +00:00
print('test_remove_html_tag')
2022-01-04 20:14:19 +00:00
test_str = "<p><img width=\"864\" height=\"486\" " + \
2020-10-11 09:33:31 +00:00
"src=\"https://somesiteorother.com/image.jpg\"></p>"
2022-01-04 20:14:19 +00:00
result_str = remove_html_tag(test_str, 'width')
assert result_str == "<p><img height=\"486\" " + \
2020-10-11 09:33:31 +00:00
"src=\"https://somesiteorother.com/image.jpg\"></p>"
2021-12-29 21:55:09 +00:00
def _test_hashtag_rules():
2022-01-04 21:19:06 +00:00
print('test_hashtag_rule_tree')
2020-10-20 17:37:15 +00:00
operators = ('not', 'and', 'or', 'xor', 'from', 'contains')
2020-10-17 12:05:41 +00:00
2020-10-20 17:37:15 +00:00
url = 'testsite.com'
2020-10-18 15:10:36 +00:00
moderated = True
2022-01-04 20:14:19 +00:00
conditions_str = \
2020-10-18 15:10:36 +00:00
'contains "Cat" or contains "Corvid" or ' + \
'contains "Dormouse" or contains "Buzzard"'
2022-01-04 20:14:19 +00:00
tags_in_conditions = []
tree = hashtag_rule_tree(operators, conditions_str,
tags_in_conditions, moderated)
2020-10-18 15:10:36 +00:00
assert str(tree) == str(['or', ['contains', ['"Cat"']],
['contains', ['"Corvid"']],
['contains', ['"Dormouse"']],
['contains', ['"Buzzard"']]])
content = 'This is a test'
2020-10-17 17:36:10 +00:00
moderated = True
2022-01-04 20:14:19 +00:00
conditions_str = '#foo or #bar'
tags_in_conditions = []
tree = hashtag_rule_tree(operators, conditions_str,
tags_in_conditions, moderated)
2020-10-17 12:05:41 +00:00
assert str(tree) == str(['or', ['#foo'], ['#bar']])
2022-01-04 20:14:19 +00:00
assert str(tags_in_conditions) == str(['#foo', '#bar'])
2020-10-17 12:05:41 +00:00
hashtags = ['#foo']
2021-12-29 21:55:09 +00:00
assert hashtag_rule_resolve(tree, hashtags, moderated, content, url)
2020-10-17 12:05:41 +00:00
hashtags = ['#carrot', '#stick']
2021-12-29 21:55:09 +00:00
assert not hashtag_rule_resolve(tree, hashtags, moderated, content, url)
2020-10-20 17:37:15 +00:00
content = 'This is a test'
url = 'https://testsite.com/something'
moderated = True
2022-01-04 20:14:19 +00:00
conditions_str = '#foo and from "testsite.com"'
tags_in_conditions = []
tree = hashtag_rule_tree(operators, conditions_str,
tags_in_conditions, moderated)
2020-10-20 17:37:15 +00:00
assert str(tree) == str(['and', ['#foo'], ['from', ['"testsite.com"']]])
2022-01-04 20:14:19 +00:00
assert str(tags_in_conditions) == str(['#foo'])
2020-10-20 17:37:15 +00:00
hashtags = ['#foo']
2021-12-29 21:55:09 +00:00
assert hashtag_rule_resolve(tree, hashtags, moderated, content, url)
assert not hashtag_rule_resolve(tree, hashtags, moderated, content,
'othersite.net')
content = 'This is a test'
moderated = True
2022-01-04 20:14:19 +00:00
conditions_str = 'contains "is a" and #foo or #bar'
tags_in_conditions = []
tree = hashtag_rule_tree(operators, conditions_str,
tags_in_conditions, moderated)
assert str(tree) == \
str(['and', ['contains', ['"is a"']],
['or', ['#foo'], ['#bar']]])
2022-01-04 20:14:19 +00:00
assert str(tags_in_conditions) == str(['#foo', '#bar'])
hashtags = ['#foo']
2021-12-29 21:55:09 +00:00
assert hashtag_rule_resolve(tree, hashtags, moderated, content, url)
hashtags = ['#carrot', '#stick']
2021-12-29 21:55:09 +00:00
assert not hashtag_rule_resolve(tree, hashtags, moderated, content, url)
2020-10-17 17:36:10 +00:00
moderated = False
2022-01-04 20:14:19 +00:00
conditions_str = 'not moderated and #foo or #bar'
tags_in_conditions = []
tree = hashtag_rule_tree(operators, conditions_str,
tags_in_conditions, moderated)
2020-10-17 17:36:10 +00:00
assert str(tree) == \
str(['not', ['and', ['moderated'], ['or', ['#foo'], ['#bar']]]])
2022-01-04 20:14:19 +00:00
assert str(tags_in_conditions) == str(['#foo', '#bar'])
2020-10-17 17:36:10 +00:00
hashtags = ['#foo']
2021-12-29 21:55:09 +00:00
assert hashtag_rule_resolve(tree, hashtags, moderated, content, url)
2020-10-17 17:36:10 +00:00
hashtags = ['#carrot', '#stick']
2021-12-29 21:55:09 +00:00
assert hashtag_rule_resolve(tree, hashtags, moderated, content, url)
2020-10-17 17:36:10 +00:00
moderated = True
2022-01-04 20:14:19 +00:00
conditions_str = 'moderated and #foo or #bar'
tags_in_conditions = []
tree = hashtag_rule_tree(operators, conditions_str,
tags_in_conditions, moderated)
2020-10-17 17:36:10 +00:00
assert str(tree) == \
str(['and', ['moderated'], ['or', ['#foo'], ['#bar']]])
2022-01-04 20:14:19 +00:00
assert str(tags_in_conditions) == str(['#foo', '#bar'])
2020-10-17 17:36:10 +00:00
hashtags = ['#foo']
2021-12-29 21:55:09 +00:00
assert hashtag_rule_resolve(tree, hashtags, moderated, content, url)
2020-10-17 17:36:10 +00:00
hashtags = ['#carrot', '#stick']
2021-12-29 21:55:09 +00:00
assert not hashtag_rule_resolve(tree, hashtags, moderated, content, url)
2020-10-17 12:05:41 +00:00
2022-01-04 20:14:19 +00:00
conditions_str = 'x'
tags_in_conditions = []
tree = hashtag_rule_tree(operators, conditions_str,
tags_in_conditions, moderated)
2020-10-17 12:05:41 +00:00
assert tree is None
2022-06-08 20:20:27 +00:00
assert not tags_in_conditions
2020-10-17 12:05:41 +00:00
hashtags = ['#foo']
2021-12-29 21:55:09 +00:00
assert not hashtag_rule_resolve(tree, hashtags, moderated, content, url)
2020-10-17 12:05:41 +00:00
2022-01-04 20:14:19 +00:00
conditions_str = '#x'
tags_in_conditions = []
tree = hashtag_rule_tree(operators, conditions_str,
tags_in_conditions, moderated)
2020-10-17 12:05:41 +00:00
assert str(tree) == str(['#x'])
2022-01-04 20:14:19 +00:00
assert str(tags_in_conditions) == str(['#x'])
2020-10-17 12:05:41 +00:00
hashtags = ['#x']
2021-12-29 21:55:09 +00:00
assert hashtag_rule_resolve(tree, hashtags, moderated, content, url)
2020-10-17 12:05:41 +00:00
hashtags = ['#y', '#z']
2021-12-29 21:55:09 +00:00
assert not hashtag_rule_resolve(tree, hashtags, moderated, content, url)
2020-10-17 12:05:41 +00:00
2022-01-04 20:14:19 +00:00
conditions_str = 'not #b'
tags_in_conditions = []
tree = hashtag_rule_tree(operators, conditions_str,
tags_in_conditions, moderated)
2020-10-17 12:05:41 +00:00
assert str(tree) == str(['not', ['#b']])
2022-01-04 20:14:19 +00:00
assert str(tags_in_conditions) == str(['#b'])
2020-10-17 12:05:41 +00:00
hashtags = ['#y', '#z']
2021-12-29 21:55:09 +00:00
assert hashtag_rule_resolve(tree, hashtags, moderated, content, url)
2020-10-17 12:05:41 +00:00
hashtags = ['#a', '#b', '#c']
2021-12-29 21:55:09 +00:00
assert not hashtag_rule_resolve(tree, hashtags, moderated, content, url)
2020-10-17 12:05:41 +00:00
2022-01-04 20:14:19 +00:00
conditions_str = '#foo or #bar and #a'
tags_in_conditions = []
tree = hashtag_rule_tree(operators, conditions_str,
tags_in_conditions, moderated)
2020-10-17 12:05:41 +00:00
assert str(tree) == str(['and', ['or', ['#foo'], ['#bar']], ['#a']])
2022-01-04 20:14:19 +00:00
assert str(tags_in_conditions) == str(['#foo', '#bar', '#a'])
2020-10-18 15:10:36 +00:00
hashtags = ['#foo', '#bar', '#a']
2021-12-29 21:55:09 +00:00
assert hashtag_rule_resolve(tree, hashtags, moderated, content, url)
2020-10-17 12:05:41 +00:00
hashtags = ['#bar', '#a']
2021-12-29 21:55:09 +00:00
assert hashtag_rule_resolve(tree, hashtags, moderated, content, url)
2020-10-17 12:05:41 +00:00
hashtags = ['#foo', '#a']
2021-12-29 21:55:09 +00:00
assert hashtag_rule_resolve(tree, hashtags, moderated, content, url)
2020-10-17 12:05:41 +00:00
hashtags = ['#x', '#a']
2021-12-29 21:55:09 +00:00
assert not hashtag_rule_resolve(tree, hashtags, moderated, content, url)
2020-10-17 12:05:41 +00:00
2021-12-29 21:55:09 +00:00
def _test_newswire_tags():
2022-01-04 21:19:06 +00:00
print('test_newswire_tags')
2022-01-04 20:14:19 +00:00
rss_description = '<img src="https://somesite/someimage.jpg" ' + \
2020-10-25 10:08:02 +00:00
'class="misc-stuff" alt="#ExcitingHashtag" ' + \
'srcset="https://somesite/someimage.jpg" ' + \
'sizes="(max-width: 864px) 100vw, 864px" />' + \
'Compelling description with #ExcitingHashtag, which is ' + \
'being posted in #BoringForum'
2022-01-04 20:14:19 +00:00
tags = get_newswire_tags(rss_description, 10)
2020-10-25 10:06:54 +00:00
assert len(tags) == 2
assert '#BoringForum' in tags
assert '#ExcitingHashtag' in tags
2021-12-29 21:55:09 +00:00
def _test_first_paragraph_from_string():
2022-01-04 21:19:06 +00:00
print('test_first_paragraph_from_string')
2022-01-04 20:14:19 +00:00
test_str = \
2020-11-08 11:24:43 +00:00
'<p><a href="https://somesite.com/somepath">This is a test</a></p>' + \
'<p>This is another paragraph</p>'
2022-01-04 20:14:19 +00:00
result_str = first_paragraph_from_string(test_str)
if result_str != 'This is a test':
print(result_str)
assert result_str == 'This is a test'
2020-11-08 11:24:43 +00:00
2022-01-04 20:14:19 +00:00
test_str = 'Testing without html'
result_str = first_paragraph_from_string(test_str)
assert result_str == test_str
2020-11-08 11:24:43 +00:00
def _test_parse_newswire_feed_date():
2022-01-04 21:19:06 +00:00
print('test_parse_feed_date')
2020-12-09 10:38:09 +00:00
unique_string_identifier = 'some string abcd'
2022-01-04 20:14:19 +00:00
pub_date = "2020-12-14T00:08:06+00:00"
published_date = parse_feed_date(pub_date, unique_string_identifier)
2022-01-04 20:14:19 +00:00
assert published_date == "2020-12-14 00:08:06+00:00"
2020-12-14 15:17:30 +00:00
2022-01-04 20:14:19 +00:00
pub_date = "Tue, 08 Dec 2020 06:24:38 -0600"
published_date = parse_feed_date(pub_date, unique_string_identifier)
2022-01-04 20:14:19 +00:00
assert published_date == "2020-12-08 12:24:38+00:00"
2020-12-09 10:38:09 +00:00
2022-01-04 20:14:19 +00:00
pub_date = "2020-08-27T16:12:34+00:00"
published_date = parse_feed_date(pub_date, unique_string_identifier)
2022-01-04 20:14:19 +00:00
assert published_date == "2020-08-27 16:12:34+00:00"
2020-11-22 19:01:18 +00:00
2022-01-04 20:14:19 +00:00
pub_date = "Sun, 22 Nov 2020 19:51:33 +0100"
published_date = parse_feed_date(pub_date, unique_string_identifier)
2022-01-04 20:14:19 +00:00
assert published_date == "2020-11-22 18:51:33+00:00"
2020-11-22 18:43:01 +00:00
pub_date = "Sun, 22 Nov 2020 00:00:00 +0000"
published_date = parse_feed_date(pub_date, unique_string_identifier)
assert published_date != "2020-11-22 00:00:00+00:00"
assert "2020-11-22 00:" in published_date
2020-11-22 18:43:01 +00:00
2021-12-29 21:55:09 +00:00
def _test_valid_nick():
2022-01-04 21:19:06 +00:00
print('test_valid_nickname')
2020-11-24 10:53:10 +00:00
domain = 'somedomain.net'
nickname = 'myvalidnick'
2021-12-28 14:41:10 +00:00
assert valid_nickname(domain, nickname)
2020-11-24 10:53:10 +00:00
nickname = 'my.invalid.nick'
2021-12-28 14:41:10 +00:00
assert not valid_nickname(domain, nickname)
2020-11-24 10:53:10 +00:00
nickname = 'myinvalidnick?'
2021-12-28 14:41:10 +00:00
assert not valid_nickname(domain, nickname)
2020-11-24 10:53:10 +00:00
nickname = 'my invalid nick?'
2021-12-28 14:41:10 +00:00
assert not valid_nickname(domain, nickname)
2020-11-24 10:53:10 +00:00
2021-12-29 21:55:09 +00:00
def _test_guess_tag_category() -> None:
2022-01-04 21:19:06 +00:00
print('test_guess_hashtag_category')
2022-01-04 20:14:19 +00:00
hashtag_categories = {
2020-12-05 11:11:32 +00:00
"foo": ["swan", "goose"],
2021-07-13 08:43:07 +00:00
"bar": ["cats", "mouse"]
2020-12-05 11:11:32 +00:00
}
2022-01-04 20:14:19 +00:00
guess = guess_hashtag_category("unspecifiedgoose", hashtag_categories)
2020-12-05 11:11:32 +00:00
assert guess == "foo"
2022-01-04 20:14:19 +00:00
guess = guess_hashtag_category("mastocats", hashtag_categories)
2020-12-05 11:11:32 +00:00
assert guess == "bar"
2021-12-29 21:55:09 +00:00
def _test_mentioned_people(base_dir: str) -> None:
2022-01-04 21:19:06 +00:00
print('test_get_mentioned_people')
2020-12-13 19:05:26 +00:00
content = "@dragon@cave.site @bat@cave.site This is a test."
2022-01-04 21:19:06 +00:00
actors = \
get_mentioned_people(base_dir, 'https', content, 'mydomain', False)
2020-12-13 19:05:26 +00:00
assert actors
assert len(actors) == 2
assert actors[0] == "https://cave.site/users/dragon"
assert actors[1] == "https://cave.site/users/bat"
2021-12-29 21:55:09 +00:00
def _test_reply_to_public_post(base_dir: str) -> None:
2021-12-25 23:03:28 +00:00
system_language = 'en'
languages_understood = [system_language]
2020-12-13 19:53:31 +00:00
nickname = 'test7492362'
domain = 'other.site'
port = 443
2021-12-25 17:09:22 +00:00
http_prefix = 'https'
2021-12-26 19:47:06 +00:00
post_id = \
http_prefix + '://rat.site/users/ninjarodent/statuses/63746173435'
2021-05-09 19:11:05 +00:00
content = "@ninjarodent@rat.site This is a test."
2022-01-04 20:14:19 +00:00
save_to_file = False
2021-12-25 20:39:35 +00:00
client_to_server = False
2022-01-04 20:14:19 +00:00
comments_enabled = True
attach_image_filename = None
media_type = None
image_description = 'Some description'
2021-05-09 19:29:53 +00:00
city = 'London, England'
2022-01-04 20:14:19 +00:00
test_in_reply_to = post_id
test_in_reply_to_atom_uri = None
test_subject = None
test_schedule_post = False
test_event_date = None
test_event_time = None
2022-05-23 12:14:36 +00:00
test_event_end_time = None
2022-01-04 20:14:19 +00:00
test_location = None
test_is_article = False
conversation_id = None
2021-12-25 18:20:56 +00:00
low_bandwidth = True
2021-12-25 17:13:38 +00:00
content_license_url = 'https://creativecommons.org/licenses/by/4.0'
2022-07-18 16:18:04 +00:00
translate = {}
2020-12-13 19:53:31 +00:00
reply = \
2021-12-28 19:33:29 +00:00
create_public_post(base_dir, nickname, domain, port, http_prefix,
2022-05-31 16:20:16 +00:00
content, save_to_file,
2022-01-04 20:14:19 +00:00
client_to_server, comments_enabled,
attach_image_filename, media_type,
image_description, city, test_in_reply_to,
test_in_reply_to_atom_uri,
test_subject, test_schedule_post,
2022-05-23 12:14:36 +00:00
test_event_date, test_event_time,
test_event_end_time, test_location,
2022-01-04 20:14:19 +00:00
test_is_article, system_language, conversation_id,
low_bandwidth, content_license_url,
2022-07-18 16:18:04 +00:00
languages_understood, translate)
2020-12-13 20:07:45 +00:00
# print(str(reply))
2022-05-25 12:57:31 +00:00
expected_str = \
2020-12-13 19:53:31 +00:00
'<p><span class=\"h-card\">' + \
2022-09-03 17:09:00 +00:00
'<a href=\"https://rat.site/users/ninjarodent\" tabindex="10" ' + \
2020-12-13 19:53:31 +00:00
'class=\"u-url mention\">@<span>ninjarodent</span>' + \
'</a></span> This is a test.</p>'
2022-05-25 12:57:31 +00:00
if reply['object']['content'] != expected_str:
print(expected_str + '\n')
print(reply['object']['content'])
assert reply['object']['content'] == expected_str
2021-12-25 23:03:28 +00:00
reply['object']['contentMap'][system_language] = reply['object']['content']
2020-12-13 19:53:31 +00:00
assert reply['object']['tag'][0]['type'] == 'Mention'
assert reply['object']['tag'][0]['name'] == '@ninjarodent@rat.site'
2022-09-03 17:09:00 +00:00
assert reply['object']['tag'][0]['href'] == \
'https://rat.site/users/ninjarodent'
2020-12-13 19:53:31 +00:00
assert len(reply['object']['to']) == 1
assert reply['object']['to'][0].endswith('#Public')
assert len(reply['object']['cc']) >= 1
assert reply['object']['cc'][0].endswith(nickname + '/followers')
assert len(reply['object']['tag']) == 1
if len(reply['object']['cc']) != 2:
print('reply["object"]["cc"]: ' + str(reply['object']['cc']))
2020-12-13 19:53:31 +00:00
assert len(reply['object']['cc']) == 2
assert reply['object']['cc'][1] == \
2021-12-25 17:09:22 +00:00
http_prefix + '://rat.site/users/ninjarodent'
assert len(reply['to']) == 1
assert reply['to'][0].endswith('#Public')
assert len(reply['cc']) >= 1
assert reply['cc'][0].endswith(nickname + '/followers')
if len(reply['cc']) != 2:
print('reply["cc"]: ' + str(reply['cc']))
assert len(reply['cc']) == 2
2021-12-25 17:09:22 +00:00
assert reply['cc'][1] == http_prefix + '://rat.site/users/ninjarodent'
2020-12-13 19:53:31 +00:00
2022-01-04 20:14:19 +00:00
def _get_function_call_args(name: str, lines: [], start_line_ctr: int) -> []:
"""Returns the arguments of a function call given lines
of source code and a starting line number
2020-12-22 21:27:46 +00:00
"""
2022-01-04 20:14:19 +00:00
args_str = lines[start_line_ctr].split(name + '(')[1]
if ')' in args_str:
args_str = args_str.split(')')[0].replace(' ', '').split(',')
return args_str
for line_ctr in range(start_line_ctr + 1, len(lines)):
if ')' not in lines[line_ctr]:
args_str += lines[line_ctr]
continue
2022-01-04 20:14:19 +00:00
args_str += lines[line_ctr].split(')')[0]
break
2022-06-21 11:58:50 +00:00
return remove_eol(args_str).replace(' ', '').split(',')
2022-01-04 20:14:19 +00:00
def get_function_calls(name: str, lines: [], start_line_ctr: int,
function_properties: {}) -> []:
2020-12-22 22:55:45 +00:00
"""Returns the functions called by the given one,
Starting with the given source code at the given line
"""
2022-01-04 20:14:19 +00:00
calls_functions = []
function_content_str = ''
for line_ctr in range(start_line_ctr + 1, len(lines)):
line_str = lines[line_ctr].strip()
if line_str.startswith('def '):
2020-12-22 22:55:45 +00:00
break
2022-01-04 20:14:19 +00:00
if line_str.startswith('class '):
2020-12-22 22:55:45 +00:00
break
2022-01-04 20:14:19 +00:00
function_content_str += lines[line_ctr]
for func_name, _ in function_properties.items():
if func_name + '(' in function_content_str:
calls_functions.append(func_name)
return calls_functions
2020-12-22 22:55:45 +00:00
2022-08-12 11:22:19 +00:00
def _function_args_match(call_args: [], func_args: []) -> bool:
"""Do the function arguments match the function call arguments?
"""
2022-01-04 20:14:19 +00:00
if len(call_args) == len(func_args):
return True
2022-08-12 11:22:19 +00:00
# count non-optional arguments in function call
2022-01-04 20:14:19 +00:00
call_args_ctr = 0
for arg1 in call_args:
if arg1 == 'self':
continue
2022-01-04 20:14:19 +00:00
if '=' not in arg1 or arg1.startswith("'"):
call_args_ctr += 1
2022-08-12 11:22:19 +00:00
# count non-optional arguments in function def
2022-01-04 20:14:19 +00:00
func_args_ctr = 0
for arg2 in func_args:
if arg2 == 'self':
continue
2022-01-04 20:14:19 +00:00
if '=' not in arg2 or arg2.startswith("'"):
func_args_ctr += 1
2022-01-04 20:14:19 +00:00
return call_args_ctr >= func_args_ctr
2022-01-04 20:14:19 +00:00
def _module_in_groups(mod_name: str, include_groups: [],
mod_groups: {}) -> bool:
2021-06-26 13:46:22 +00:00
"""Is the given module within the included groups list?
"""
2022-01-04 20:14:19 +00:00
for group_name in include_groups:
if mod_name in mod_groups[group_name]:
2021-06-26 13:46:22 +00:00
return True
return False
2022-01-04 20:14:19 +00:00
def _diagram_groups(include_groups: [],
exclude_extra_modules: [],
modules: {}, mod_groups: {},
max_module_calls: int) -> None:
2021-06-26 13:46:22 +00:00
"""Draws a dot diagram containing only the given module groups
"""
2022-01-04 20:14:19 +00:00
call_graph_str = 'digraph EpicyonGroups {\n\n'
call_graph_str += \
' graph [fontsize=10 fontname="Verdana" compound=true];\n'
call_graph_str += ' node [fontsize=10 fontname="Verdana"];\n\n'
exclude_modules_from_diagram = [
2021-06-26 15:57:25 +00:00
'setup', 'tests', '__init__', 'pyjsonld'
2021-06-26 14:21:24 +00:00
]
2022-01-04 20:14:19 +00:00
exclude_modules_from_diagram += exclude_extra_modules
2021-06-26 13:46:22 +00:00
# colors of modules nodes
2022-01-04 20:14:19 +00:00
for mod_name, mod_properties in modules.items():
if mod_name in exclude_modules_from_diagram:
2021-06-26 13:46:22 +00:00
continue
2022-01-04 20:14:19 +00:00
if not _module_in_groups(mod_name, include_groups, mod_groups):
2021-06-26 13:46:22 +00:00
continue
2022-01-04 20:14:19 +00:00
if not mod_properties.get('calls'):
call_graph_str += ' "' + mod_name + \
2021-06-26 13:46:22 +00:00
'" [fillcolor=yellow style=filled];\n'
continue
2022-01-04 20:14:19 +00:00
if len(mod_properties['calls']) <= int(max_module_calls / 8):
call_graph_str += ' "' + mod_name + \
2021-06-26 13:46:22 +00:00
'" [fillcolor=green style=filled];\n'
2022-01-04 20:14:19 +00:00
elif len(mod_properties['calls']) < int(max_module_calls / 4):
call_graph_str += ' "' + mod_name + \
2021-06-26 13:46:22 +00:00
'" [fillcolor=orange style=filled];\n'
else:
2022-01-04 20:14:19 +00:00
call_graph_str += ' "' + mod_name + \
2021-06-26 13:46:22 +00:00
'" [fillcolor=red style=filled];\n'
2022-01-04 20:14:19 +00:00
call_graph_str += '\n'
2021-06-26 13:46:22 +00:00
# connections between modules
2022-01-04 20:14:19 +00:00
for mod_name, mod_properties in modules.items():
if mod_name in exclude_modules_from_diagram:
2021-06-26 13:46:22 +00:00
continue
2022-01-04 20:14:19 +00:00
if not _module_in_groups(mod_name, include_groups, mod_groups):
2021-06-26 13:46:22 +00:00
continue
2022-01-04 20:14:19 +00:00
if not mod_properties.get('calls'):
2021-06-26 13:46:22 +00:00
continue
2022-01-04 20:14:19 +00:00
for mod_call in mod_properties['calls']:
if mod_call in exclude_modules_from_diagram:
2021-06-26 13:46:22 +00:00
continue
2022-01-04 20:14:19 +00:00
if not _module_in_groups(mod_call, include_groups, mod_groups):
2021-06-26 13:46:22 +00:00
continue
2022-01-04 20:14:19 +00:00
call_graph_str += ' "' + mod_name + '" -> "' + mod_call + '";\n'
2021-06-26 13:46:22 +00:00
# module groups/clusters
2022-01-04 20:14:19 +00:00
cluster_ctr = 1
for group_name, group_modules in mod_groups.items():
if group_name not in include_groups:
2021-06-26 13:46:22 +00:00
continue
2022-01-04 20:14:19 +00:00
call_graph_str += '\n'
call_graph_str += \
' subgraph cluster_' + str(cluster_ctr) + ' {\n'
call_graph_str += ' node [style=filled];\n'
for mod_name in group_modules:
if mod_name not in exclude_modules_from_diagram:
call_graph_str += ' ' + mod_name + ';\n'
call_graph_str += ' label = "' + group_name + '";\n'
call_graph_str += ' color = blue;\n'
call_graph_str += ' }\n'
cluster_ctr += 1
call_graph_str += '\n}\n'
2021-06-26 13:46:22 +00:00
filename = 'epicyon_groups'
2022-01-04 20:14:19 +00:00
for group_name in include_groups:
filename += '_' + group_name.replace(' ', '-')
2021-06-26 13:46:22 +00:00
filename += '.dot'
2022-06-09 14:46:30 +00:00
with open(filename, 'w+', encoding='utf-8') as fp_graph:
2022-01-04 20:14:19 +00:00
fp_graph.write(call_graph_str)
2021-06-26 13:46:22 +00:00
print('Graph saved to ' + filename)
print('Plot using: ' +
'sfdp -x -Goverlap=false -Goverlap_scaling=2 ' +
'-Gsep=+100 -Tx11 epicyon_modules.dot')
2021-12-30 12:23:55 +00:00
def _test_post_variable_names():
2022-01-04 21:19:06 +00:00
print('test_post_variable_names')
2021-12-30 12:23:55 +00:00
2022-01-04 20:14:19 +00:00
for _, _, files in os.walk('.'):
2021-12-30 12:23:55 +00:00
for source_file in files:
if not source_file.endswith('.py'):
continue
if source_file.startswith('.#'):
continue
if source_file.startswith('flycheck_'):
continue
source_str = ''
2022-06-09 14:46:30 +00:00
with open(source_file, 'r', encoding='utf-8') as file_source:
2021-12-30 12:23:55 +00:00
source_str = file_source.read()
if not source_str:
continue
if ' name="' not in source_str:
continue
names_list = source_str.split(' name="')
for index in range(1, len(names_list)):
if '"' not in names_list[index]:
continue
name_var = names_list[index].split('"')[0]
if not name_var:
continue
if ' ' in name_var:
continue
if '_' in name_var:
print(name_var + ' is not camel case POST variable in ' +
source_file)
2021-12-30 13:56:38 +00:00
assert False
break
def _test_config_param_names():
2022-01-04 21:19:06 +00:00
print('test_config_param_names')
2021-12-30 13:56:38 +00:00
fnames = ('get_config_param', 'set_config_param')
2022-01-04 20:14:19 +00:00
for _, _, files in os.walk('.'):
2021-12-30 13:56:38 +00:00
for source_file in files:
if not source_file.endswith('.py'):
continue
if source_file.startswith('.#'):
continue
if source_file.startswith('flycheck_'):
continue
source_str = ''
2022-06-09 14:46:30 +00:00
with open(source_file, 'r', encoding='utf-8') as file_source:
2021-12-30 13:56:38 +00:00
source_str = file_source.read()
if not source_str:
continue
for fname in fnames:
if fname + '(' not in source_str:
continue
names_list = source_str.split(fname + '(')
for index in range(1, len(names_list)):
param_var_name = None
if '"' in names_list[index]:
param_var_name = names_list[index].split('"')[1]
elif "'" in names_list[index]:
param_var_name = names_list[index].split("'")[1]
if not param_var_name:
continue
if ' ' in param_var_name:
continue
if '.' in param_var_name:
continue
if '/' in param_var_name:
continue
if '__' in param_var_name:
continue
if 'POST' in param_var_name:
continue
if param_var_name.isdigit():
continue
if '_' in param_var_name:
print(fname + ' in ' + source_file +
' should have camel case variable ' +
param_var_name)
assert False
break
2021-12-30 14:03:49 +00:00
def _test_source_contains_no_tabs():
2022-01-04 21:19:06 +00:00
print('test_source_contains_no_tabs')
2021-12-30 14:03:49 +00:00
2022-01-04 20:14:19 +00:00
for _, _, files in os.walk('.'):
2021-12-30 14:03:49 +00:00
for source_file in files:
if not source_file.endswith('.py'):
continue
if source_file.startswith('.#'):
continue
if source_file.startswith('flycheck_'):
continue
source_str = ''
2022-06-09 14:46:30 +00:00
with open(source_file, 'r', encoding='utf-8') as file_source:
2021-12-30 14:03:49 +00:00
source_str = file_source.read()
if not source_str:
continue
if '\t' in source_str:
print(source_file + ' contains tabs')
assert False
2021-12-30 14:04:25 +00:00
break
2021-12-30 14:03:49 +00:00
2021-12-30 13:56:38 +00:00
def _test_checkbox_names():
2022-01-04 21:19:06 +00:00
print('test_checkbox_names')
2021-12-30 13:56:38 +00:00
fnames = ['edit_text_field', 'edit_check_box', 'edit_text_area']
for _, _, files in os.walk('.'):
2021-12-30 13:56:38 +00:00
for source_file in files:
if not source_file.endswith('.py'):
continue
if source_file.startswith('.#'):
continue
if source_file.startswith('flycheck_'):
continue
source_str = ''
2022-06-09 14:46:30 +00:00
with open(source_file, 'r', encoding='utf-8') as file_source:
2021-12-30 13:56:38 +00:00
source_str = file_source.read()
if not source_str:
continue
for fname in fnames:
if fname + '(' not in source_str:
continue
names_list = source_str.split(fname + '(')
for index in range(1, len(names_list)):
if ')' not in names_list[index]:
continue
allparams = names_list[index].split(')')[0]
if ',' not in allparams:
continue
allparams_list = allparams.split(',')
if len(allparams_list) < 2:
continue
param_var_name = allparams_list[1].strip()
param_var_name = param_var_name.replace('"', '')
param_var_name = param_var_name.replace("'", '')
if ' ' in param_var_name:
continue
if '/' in param_var_name:
continue
if '_' in param_var_name:
print(fname + ' in ' + source_file +
' should have camel case variable ' +
param_var_name)
assert False
break
def _test_post_field_names(source_file: str, fieldnames: []):
2022-01-04 21:19:06 +00:00
print('test_post_field_Names')
2021-12-30 13:56:38 +00:00
fnames = []
for field in fieldnames:
fnames.append(field + '.get')
2021-12-30 13:56:38 +00:00
source_str = ''
2022-06-09 14:46:30 +00:00
with open(source_file, 'r', encoding='utf-8') as file_source:
2021-12-30 13:56:38 +00:00
source_str = file_source.read()
if not source_str:
return
for fname in fnames:
if fname + '(' not in source_str:
continue
names_list = source_str.split(fname + '(')
for index in range(1, len(names_list)):
if ')' not in names_list[index]:
continue
param_var_name = names_list[index].split(')')[0].strip()
2022-01-01 17:29:48 +00:00
if '"' not in param_var_name and \
"'" not in param_var_name:
continue
quote_str = '"'
if "'" in param_var_name:
quote_str = "'"
2021-12-30 13:56:38 +00:00
orig_param_var_name = fname + '(' + param_var_name + ')'
param_var_name = param_var_name.split(quote_str)[1]
2021-12-30 13:56:38 +00:00
if ' ' in param_var_name:
continue
if '/' in param_var_name:
continue
if '_' in param_var_name:
print(orig_param_var_name + ' in ' + source_file +
' should be camel case')
assert False
fnames = []
for field in fieldnames:
fnames.append(field + '[')
2022-01-01 17:29:48 +00:00
for fname in fnames:
if fname in source_str:
names_list = source_str.split(fname)
for index in range(1, len(names_list)):
if ']' not in names_list[index]:
continue
param_var_name = names_list[index].split(']')[0].strip()
if '"' not in param_var_name and \
"'" not in param_var_name:
continue
quote_str = '"'
if "'" in param_var_name:
quote_str = "'"
2022-01-01 17:29:48 +00:00
orig_param_var_name = fname.strip() + param_var_name + ']'
param_var_name = param_var_name.split(quote_str)[1]
if ' ' in param_var_name:
continue
if '/' in param_var_name:
continue
2022-01-01 17:29:48 +00:00
if '_' in param_var_name:
print(orig_param_var_name + ' in ' + source_file +
' should be camel case')
assert False
2021-12-30 12:23:55 +00:00
def _test_thread_functions():
print('test_thread_functions')
modules = {}
threads_called_in_modules = []
# get the source for each module
2022-07-22 09:58:42 +00:00
# Allow recursive walk
for _, _, files in os.walk('.'):
for source_file in files:
if not source_file.endswith('.py'):
continue
if source_file.startswith('.#'):
continue
if source_file.startswith('flycheck_'):
continue
mod_name = source_file.replace('.py', '')
if mod_name == 'threads':
# don't test the threads module itself
continue
modules[mod_name] = {
'functions': []
}
source_str = ''
2022-06-09 14:46:30 +00:00
with open(source_file, 'r', encoding='utf-8') as fp_src:
source_str = fp_src.read()
modules[mod_name]['source'] = source_str
if 'thread_with_trace(' in source_str:
threads_called_in_modules.append(mod_name)
2022-06-09 14:46:30 +00:00
with open(source_file, 'r', encoding='utf-8') as fp_src:
lines = fp_src.readlines()
modules[mod_name]['lines'] = lines
for mod_name in threads_called_in_modules:
thread_sections = \
modules[mod_name]['source'].split('thread_with_trace(')
ctr = 0
for thread_str in thread_sections:
if ctr == 0 or ',' not in thread_str:
ctr += 1
continue
thread_function_args = thread_str.split(',')
first_parameter = thread_function_args[0]
if 'target=' not in first_parameter:
ctr += 1
continue
thread_function_name = first_parameter.split('target=')[1].strip()
if not thread_function_name:
ctr += 1
continue
if thread_function_name.startswith('self.'):
thread_function_name = thread_function_name.split('self.')[1]
# is this function declared at the top of the module
# or defined within the module?
import_str = ' import ' + thread_function_name
def_str = 'def ' + thread_function_name + '('
if import_str not in modules[mod_name]['source']:
if def_str not in modules[mod_name]['source']:
print(mod_name + ' ' + first_parameter)
print(import_str + ' not in ' + mod_name)
2022-06-08 20:20:27 +00:00
assert False
if def_str in modules[mod_name]['source']:
defininition_module = mod_name
else:
# which module is the thread function defined within?
test_str = modules[mod_name]['source'].split(import_str)[0]
defininition_module = test_str.split('from ')[-1]
print('Thread function ' + thread_function_name +
' defined in ' + defininition_module)
# check the function arguments
second_parameter = thread_function_args[1]
if 'args=' not in second_parameter:
print('No args parameter in ' + thread_function_name +
' module ' + mod_name)
ctr += 1
continue
arg_ctr = 0
calling_function_args_list = []
for func_arg in thread_function_args:
if arg_ctr == 0:
arg_ctr += 1
continue
last_arg = False
2022-04-03 12:43:20 +00:00
if '(' in func_arg and '()' not in func_arg:
func_arg = func_arg.split('(')[1]
2022-04-03 12:43:20 +00:00
if func_arg.endswith(')') and '()' not in func_arg:
func_arg = func_arg.split(')')[0]
last_arg = True
func_arg = func_arg.strip()
calling_function_args_list.append(func_arg)
if last_arg:
break
arg_ctr += 1
# print(mod_name + ' ' + thread_function_name + ' ' +
# str(calling_function_args_list))
# get the function definition arguments
test_str = \
modules[defininition_module]['source'].split(def_str)[1]
test_str = test_str.split(')')[0]
definition_function_args_list = test_str.split(',')
if len(definition_function_args_list) > 0:
if definition_function_args_list[0] == 'self':
definition_function_args_list = \
definition_function_args_list[1:]
if len(definition_function_args_list) != \
len(calling_function_args_list):
print('Thread function ' + thread_function_name +
' has ' + str(len(definition_function_args_list)) +
' arguments, but ' +
str(len(calling_function_args_list)) +
' were given in module ' + mod_name)
print(str(definition_function_args_list))
print(str(calling_function_args_list))
2022-06-08 20:20:27 +00:00
assert False
ctr += 1
2021-12-29 21:55:09 +00:00
def _test_functions():
2022-01-04 21:19:06 +00:00
print('test_functions')
function = {}
2022-01-04 20:14:19 +00:00
function_properties = {}
2020-12-22 19:28:34 +00:00
modules = {}
2022-01-04 20:14:19 +00:00
mod_groups = {}
method_loc = []
2022-01-04 20:14:19 +00:00
for _, _, files in os.walk('.'):
for source_file in files:
2022-08-12 11:13:37 +00:00
# is this really a source file?
2022-01-04 20:14:19 +00:00
if not source_file.endswith('.py'):
continue
2022-01-04 20:14:19 +00:00
if source_file.startswith('.#'):
continue
2022-01-04 20:14:19 +00:00
if source_file.startswith('flycheck_'):
2021-12-30 12:23:55 +00:00
continue
2022-08-12 11:13:37 +00:00
# get the module name
2022-01-04 20:14:19 +00:00
mod_name = source_file.replace('.py', '')
modules[mod_name] = {
2020-12-22 19:28:34 +00:00
'functions': []
}
2022-08-12 11:13:37 +00:00
# load the module source
2022-01-04 20:14:19 +00:00
source_str = ''
2022-06-09 14:46:30 +00:00
with open(source_file, 'r', encoding='utf-8') as fp_src:
2022-01-04 20:14:19 +00:00
source_str = fp_src.read()
modules[mod_name]['source'] = source_str
2022-08-12 11:13:37 +00:00
# go through the source line by line
2022-06-09 14:46:30 +00:00
with open(source_file, 'r', encoding='utf-8') as fp_src:
2022-01-04 20:14:19 +00:00
lines = fp_src.readlines()
modules[mod_name]['lines'] = lines
line_count = 0
prev_line = 'start'
method_name = ''
for line in lines:
2022-08-12 11:13:37 +00:00
# what group is this module in?
2021-06-15 15:08:12 +00:00
if '__module_group__' in line:
if '=' in line:
2022-01-04 20:14:19 +00:00
group_name = line.split('=')[1].strip()
group_name = group_name.replace('"', '')
group_name = group_name.replace("'", '')
modules[mod_name]['group'] = group_name
if not mod_groups.get(group_name):
mod_groups[group_name] = [mod_name]
2021-06-15 15:08:12 +00:00
else:
2022-01-04 20:14:19 +00:00
if mod_name not in mod_groups[group_name]:
mod_groups[group_name].append(mod_name)
2022-08-12 11:13:37 +00:00
# reading function lines
2020-12-23 12:48:50 +00:00
if not line.strip().startswith('def '):
2022-01-04 20:14:19 +00:00
if line_count > 0:
line_count += 1
2021-06-26 21:29:49 +00:00
# add LOC count for this function
2022-01-04 20:14:19 +00:00
if len(prev_line.strip()) == 0 and \
2021-06-26 19:01:48 +00:00
len(line.strip()) == 0 and \
2022-01-04 20:14:19 +00:00
line_count > 2:
line_count -= 2
if line_count > 80:
loc_str = str(line_count) + ';' + method_name
if line_count < 1000:
loc_str = '0' + loc_str
if line_count < 100:
loc_str = '0' + loc_str
if line_count < 10:
loc_str = '0' + loc_str
if loc_str not in method_loc:
method_loc.append(loc_str)
line_count = 0
prev_line = line
continue
2022-08-12 11:13:37 +00:00
# reading function def
2022-01-04 20:14:19 +00:00
prev_line = line
line_count = 1
method_name = line.split('def ', 1)[1].split('(')[0]
2022-08-12 11:13:37 +00:00
# get list of arguments with spaces removed
2022-01-04 20:14:19 +00:00
method_args = \
source_str.split('def ' + method_name + '(')[1]
method_args = method_args.split(')')[0]
method_args = method_args.replace(' ', '').split(',')
if function.get(mod_name):
function[mod_name].append(method_name)
else:
2022-01-04 20:14:19 +00:00
function[mod_name] = [method_name]
if method_name not in modules[mod_name]['functions']:
modules[mod_name]['functions'].append(method_name)
2022-08-12 11:13:37 +00:00
# create an entry for this function
2022-01-04 20:14:19 +00:00
function_properties[method_name] = {
"args": method_args,
"module": mod_name,
"calledInModule": []
}
2021-06-26 21:29:49 +00:00
# LOC count for the last function
2022-01-04 20:14:19 +00:00
if line_count > 2:
line_count -= 2
if line_count > 80:
loc_str = str(line_count) + ';' + method_name
if line_count < 1000:
loc_str = '0' + loc_str
if line_count < 100:
loc_str = '0' + loc_str
if line_count < 10:
loc_str = '0' + loc_str
if loc_str not in method_loc:
method_loc.append(loc_str)
break
2021-06-26 19:01:48 +00:00
print('LOC counts:')
2022-01-04 20:14:19 +00:00
method_loc.sort()
for loc_str in method_loc:
print(loc_str.split(';')[0] + ' ' + loc_str.split(';')[1])
2021-06-26 19:01:48 +00:00
2022-01-04 20:14:19 +00:00
exclude_func_args = [
'pyjsonld'
]
2022-01-04 20:14:19 +00:00
exclude_funcs = [
2020-12-23 12:48:50 +00:00
'link',
'set',
'get'
]
2021-12-29 10:39:46 +00:00
bad_function_names = []
2022-01-04 20:14:19 +00:00
for name, properties in function_properties.items():
2021-12-29 10:39:46 +00:00
if '_' not in name:
if name.lower() != name:
bad_function_names.append(name)
else:
if name.startswith('_'):
name2 = name[1:]
if '_' not in name2:
if name2.lower() != name2:
bad_function_names.append(name)
if bad_function_names:
bad_function_names2 = \
sorted(bad_function_names, key=len, reverse=True)
function_names_str = ''
function_names_sh = ''
ctr = 0
for name in bad_function_names2:
if ctr > 0:
function_names_str += '\n' + name
else:
function_names_str = name
snake_case_name = convert_to_snake_case(name)
function_names_sh += \
'sed -i "s|' + name + '|' + snake_case_name + '|g" *.py\n'
ctr += 1
print(function_names_str + '\n')
2022-06-09 14:46:30 +00:00
with open('scripts/bad_function_names.sh', 'w+',
encoding='utf-8') as fn_file:
2021-12-29 10:39:46 +00:00
fn_file.write(function_names_sh)
assert False
# which modules is each function used within?
2022-01-04 20:14:19 +00:00
for mod_name, _ in modules.items():
print('Module: ' + mod_name + '')
for name, properties in function_properties.items():
line_ctr = 0
for line in modules[mod_name]['lines']:
line_str = line.strip()
if line_str.startswith('def '):
line_ctr += 1
2020-12-23 12:48:50 +00:00
continue
2022-01-04 20:14:19 +00:00
if line_str.startswith('class '):
line_ctr += 1
2020-12-22 19:28:34 +00:00
continue
2022-08-12 11:13:37 +00:00
# detect a call to this function
2020-12-22 19:28:34 +00:00
if name + '(' in line:
2022-01-04 20:14:19 +00:00
mod_list = \
function_properties[name]['calledInModule']
if mod_name not in mod_list:
mod_list.append(mod_name)
if mod_name in exclude_func_args:
line_ctr += 1
continue
2022-01-04 20:14:19 +00:00
if name in exclude_funcs:
line_ctr += 1
continue
2022-08-12 11:13:37 +00:00
# get the function call arguments
2022-01-04 20:14:19 +00:00
call_args = \
2021-12-29 21:55:09 +00:00
_get_function_call_args(name,
2022-01-04 20:14:19 +00:00
modules[mod_name]['lines'],
line_ctr)
2022-08-12 11:13:37 +00:00
# get the function def arguments
2022-01-04 20:14:19 +00:00
func_args = function_properties[name]['args']
2022-08-12 11:13:37 +00:00
# match the call arguments to the definition arguments
2022-01-04 20:14:19 +00:00
if not _function_args_match(call_args, func_args):
print('Call to function ' + name +
' does not match its arguments')
print('def args: ' +
2022-01-04 20:14:19 +00:00
str(len(function_properties[name]['args'])) +
'\n' + str(function_properties[name]['args']))
print('Call args: ' + str(len(call_args)) + '\n' +
str(call_args))
print('module ' + mod_name + ' line ' + str(line_ctr))
assert False
2022-01-04 20:14:19 +00:00
line_ctr += 1
# don't check these functions, because they are procedurally called
exclusions = [
2020-12-23 12:48:50 +00:00
'do_GET',
'do_POST',
'do_HEAD',
2022-02-23 09:51:00 +00:00
'do_PROPFIND',
'do_PUT',
'do_REPORT',
'do_DELETE',
2020-12-23 12:48:50 +00:00
'__run',
2021-12-29 21:55:09 +00:00
'_send_to_named_addresses',
2020-12-23 12:48:50 +00:00
'globaltrace',
'localtrace',
'kill',
'clone',
'unregister_rdf_parser',
'set_document_loader',
2020-12-23 12:48:50 +00:00
'has_property',
'has_value',
'add_value',
'get_values',
'remove_property',
'remove_value',
'normalize',
'get_document_loader',
2021-12-28 20:32:11 +00:00
'run_inbox_queue_watchdog',
'run_inbox_queue',
'run_import_following',
'run_import_following_watchdog',
2021-12-29 21:55:09 +00:00
'run_post_schedule',
'run_post_schedule_watchdog',
'str2bool',
2021-12-29 21:55:09 +00:00
'run_newswire_daemon',
'run_newswire_watchdog',
'run_federated_shares_watchdog',
'run_federated_shares_daemon',
'fitness_thread',
'thread_send_post',
'send_to_followers',
2021-12-28 19:33:29 +00:00
'expire_cache',
2021-12-29 21:55:09 +00:00
'get_mutuals_of_person',
'run_posts_queue',
'run_shares_expire',
'run_posts_watchdog',
'run_shares_expire_watchdog',
'get_this_weeks_events',
'get_availability',
'_test_threads_function',
'create_server_group',
'create_server_alice',
'create_server_bob',
'create_server_eve',
'e2e_eremove_device',
'setOrganizationScheme',
2021-03-04 14:36:24 +00:00
'fill_headers',
2022-03-31 12:00:36 +00:00
'_nothing',
'check_for_changed_actor'
]
2022-01-04 20:14:19 +00:00
exclude_imports = [
2020-12-23 12:48:50 +00:00
'link',
'start'
2020-12-22 13:57:24 +00:00
]
2022-01-04 20:14:19 +00:00
exclude_local = [
'pyjsonld',
'daemon',
'tests'
]
2022-01-04 20:14:19 +00:00
exclude_mods = [
2020-12-23 12:48:50 +00:00
'pyjsonld'
]
# check that functions are called somewhere
2022-01-04 20:14:19 +00:00
for name, properties in function_properties.items():
2020-12-23 12:48:50 +00:00
if name.startswith('__'):
if name.endswith('__'):
continue
if name in exclusions:
continue
2022-01-04 20:14:19 +00:00
if properties['module'] in exclude_mods:
2020-12-23 12:48:50 +00:00
continue
2022-01-04 20:14:19 +00:00
is_local_function = False
if not properties['calledInModule']:
print('function ' + name +
' in module ' + properties['module'] +
' is not called anywhere')
assert properties['calledInModule']
if len(properties['calledInModule']) == 1:
2022-01-04 20:14:19 +00:00
mod_name = properties['calledInModule'][0]
if mod_name not in exclude_local and \
mod_name == properties['module']:
is_local_function = True
if not name.startswith('_'):
print('Local function ' + name +
2022-01-04 20:14:19 +00:00
' in ' + mod_name + '.py does not begin with _')
assert False
2022-01-04 20:14:19 +00:00
if name not in exclude_imports:
for mod_name in properties['calledInModule']:
if mod_name == properties['module']:
2020-12-22 13:57:24 +00:00
continue
2022-01-04 20:14:19 +00:00
import_str = 'from ' + properties['module'] + ' import ' + name
if import_str not in modules[mod_name]['source']:
print(import_str + ' not found in ' + mod_name + '.py')
2020-12-22 13:57:24 +00:00
assert False
2022-01-04 20:14:19 +00:00
if not is_local_function:
if name.startswith('_'):
2022-01-04 20:14:19 +00:00
exclude_public = [
'pyjsonld',
'daemon',
'tests'
]
2022-01-04 20:14:19 +00:00
mod_name = properties['module']
if mod_name not in exclude_public:
print('Public function ' + name + ' in ' +
2022-01-04 20:14:19 +00:00
mod_name + '.py begins with _')
assert False
2020-12-22 13:04:49 +00:00
print('Function: ' + name + '')
2020-12-23 16:19:18 +00:00
print('Constructing function call graph')
2022-01-04 20:14:19 +00:00
module_colors = (
2021-12-29 21:55:09 +00:00
'red', 'green', 'yellow', 'orange', 'purple', 'cyan',
'darkgoldenrod3', 'darkolivegreen1', 'darkorange1',
'darkorchid1', 'darkseagreen', 'darkslategray4',
'deeppink1', 'deepskyblue1', 'dimgrey', 'gold1',
'goldenrod', 'burlywood2', 'bisque1', 'brown1',
'chartreuse2', 'cornsilk', 'darksalmon'
)
2022-01-04 20:14:19 +00:00
max_module_calls = 1
max_function_calls = 1
color_ctr = 0
for mod_name, _ in modules.items():
line_ctr = 0
modules[mod_name]['color'] = module_colors[color_ctr]
color_ctr += 1
if color_ctr >= len(module_colors):
color_ctr = 0
for line in modules[mod_name]['lines']:
2020-12-23 12:48:50 +00:00
if line.strip().startswith('def '):
2020-12-22 22:55:45 +00:00
name = line.split('def ')[1].split('(')[0]
2022-01-04 20:14:19 +00:00
calls_list = \
get_function_calls(name, modules[mod_name]['lines'],
line_ctr, function_properties)
function_properties[name]['calls'] = calls_list.copy()
if len(calls_list) > max_function_calls:
max_function_calls = len(calls_list)
2020-12-23 16:19:18 +00:00
# keep track of which module calls which other module
2022-01-04 20:14:19 +00:00
for fnc in calls_list:
mod_call = function_properties[fnc]['module']
if mod_call != mod_name:
if modules[mod_name].get('calls'):
if mod_call not in modules[mod_name]['calls']:
modules[mod_name]['calls'].append(mod_call)
if len(modules[mod_name]['calls']) > \
max_module_calls:
max_module_calls = \
len(modules[mod_name]['calls'])
2020-12-23 16:19:18 +00:00
else:
2022-01-04 20:14:19 +00:00
modules[mod_name]['calls'] = [mod_call]
line_ctr += 1
2021-06-26 13:46:22 +00:00
2021-12-29 21:55:09 +00:00
_diagram_groups(['Commandline Interface', 'ActivityPub'], ['utils'],
2022-01-04 20:14:19 +00:00
modules, mod_groups, max_module_calls)
2021-12-29 21:55:09 +00:00
_diagram_groups(['Commandline Interface', 'Core'], ['utils'],
2022-01-04 20:14:19 +00:00
modules, mod_groups, max_module_calls)
2021-12-29 21:55:09 +00:00
_diagram_groups(['Timeline', 'Core'], ['utils'],
2022-01-04 20:14:19 +00:00
modules, mod_groups, max_module_calls)
2021-12-29 21:55:09 +00:00
_diagram_groups(['Web Interface', 'Core'], ['utils'],
2022-01-04 20:14:19 +00:00
modules, mod_groups, max_module_calls)
2021-12-29 21:55:09 +00:00
_diagram_groups(['Web Interface Columns', 'Core'], ['utils'],
2022-01-04 20:14:19 +00:00
modules, mod_groups, max_module_calls)
2021-12-29 21:55:09 +00:00
_diagram_groups(['Core'], [],
2022-01-04 20:14:19 +00:00
modules, mod_groups, max_module_calls)
2021-12-29 21:55:09 +00:00
_diagram_groups(['ActivityPub'], [],
2022-01-04 20:14:19 +00:00
modules, mod_groups, max_module_calls)
2021-12-29 21:55:09 +00:00
_diagram_groups(['ActivityPub', 'Core'], ['utils'],
2022-01-04 20:14:19 +00:00
modules, mod_groups, max_module_calls)
2021-12-29 21:55:09 +00:00
_diagram_groups(['ActivityPub', 'Security'], ['utils'],
2022-01-04 20:14:19 +00:00
modules, mod_groups, max_module_calls)
2021-12-29 21:55:09 +00:00
_diagram_groups(['Core', 'Security'], ['utils'],
2022-01-04 20:14:19 +00:00
modules, mod_groups, max_module_calls)
2021-12-29 21:55:09 +00:00
_diagram_groups(['Timeline', 'Security'], ['utils'],
2022-01-04 20:14:19 +00:00
modules, mod_groups, max_module_calls)
2021-12-29 21:55:09 +00:00
_diagram_groups(['Web Interface', 'Accessibility'],
['utils', 'webapp_utils'],
2022-01-04 20:14:19 +00:00
modules, mod_groups, max_module_calls)
2021-12-29 21:55:09 +00:00
_diagram_groups(['Core', 'Accessibility'], ['utils'],
2022-01-04 20:14:19 +00:00
modules, mod_groups, max_module_calls)
2021-12-29 21:55:09 +00:00
def _test_links_within_post(base_dir: str) -> None:
2022-01-04 21:19:06 +00:00
print('test_links_within_post')
2021-12-25 23:03:28 +00:00
system_language = 'en'
languages_understood = [system_language]
2021-01-02 10:37:19 +00:00
nickname = 'test27636'
domain = 'rando.site'
port = 443
2021-12-25 17:09:22 +00:00
http_prefix = 'https'
2021-01-02 10:37:19 +00:00
content = 'This is a test post with links.\n\n' + \
2021-09-10 16:14:50 +00:00
'ftp://ftp.ncdc.noaa.gov/pub/data/ghcn/v4/\n\nhttps://libreserver.org'
2022-01-04 20:14:19 +00:00
save_to_file = False
2021-12-25 20:39:35 +00:00
client_to_server = False
2022-01-04 20:14:19 +00:00
comments_enabled = True
attach_image_filename = None
media_type = None
image_description = None
2021-05-09 19:29:53 +00:00
city = 'London, England'
2022-01-04 20:14:19 +00:00
test_in_reply_to = None
test_in_reply_to_atom_uri = None
test_subject = None
test_schedule_post = False
test_event_date = None
test_event_time = None
2022-05-23 12:14:36 +00:00
test_event_end_time = None
2022-01-04 20:14:19 +00:00
test_location = None
test_is_article = False
conversation_id = None
2021-12-25 18:20:56 +00:00
low_bandwidth = True
2021-12-25 17:13:38 +00:00
content_license_url = 'https://creativecommons.org/licenses/by/4.0'
2022-07-18 16:18:04 +00:00
translate = {}
2021-05-09 19:11:05 +00:00
2021-12-25 22:09:19 +00:00
post_json_object = \
2021-12-28 19:33:29 +00:00
create_public_post(base_dir, nickname, domain, port, http_prefix,
2022-05-31 16:20:16 +00:00
content, save_to_file,
2022-01-04 20:14:19 +00:00
client_to_server, comments_enabled,
attach_image_filename, media_type,
image_description, city,
test_in_reply_to, test_in_reply_to_atom_uri,
test_subject, test_schedule_post,
2022-05-23 12:14:36 +00:00
test_event_date, test_event_time,
test_event_end_time, test_location,
2022-01-04 20:14:19 +00:00
test_is_article, system_language, conversation_id,
low_bandwidth, content_license_url,
2022-07-18 16:18:04 +00:00
languages_understood, translate)
2021-07-01 20:41:17 +00:00
2022-05-25 12:57:31 +00:00
expected_str = \
2021-01-02 10:37:19 +00:00
'<p>This is a test post with links.<br><br>' + \
'<a href="ftp://ftp.ncdc.noaa.gov/pub/data/ghcn/v4/" ' + \
2022-05-25 12:57:31 +00:00
'tabindex="10" ' + \
2021-01-02 10:37:19 +00:00
'rel="nofollow noopener noreferrer" target="_blank">' + \
'<span class="invisible">ftp://</span>' + \
'<span class="ellipsis">' + \
'ftp.ncdc.noaa.gov/pub/data/ghcn/v4/</span>' + \
2022-05-25 12:57:31 +00:00
'</a><br><br><a href="https://libreserver.org" tabindex="10" ' + \
2021-01-02 10:37:19 +00:00
'rel="nofollow noopener noreferrer" target="_blank">' + \
'<span class="invisible">https://</span>' + \
2021-09-10 16:14:50 +00:00
'<span class="ellipsis">libreserver.org</span></a></p>'
2022-05-25 12:57:31 +00:00
if post_json_object['object']['content'] != expected_str:
print(expected_str + '\n')
print(post_json_object['object']['content'])
assert post_json_object['object']['content'] == expected_str
2021-12-25 22:09:19 +00:00
assert post_json_object['object']['content'] == \
2021-12-25 23:03:28 +00:00
post_json_object['object']['contentMap'][system_language]
2021-01-02 10:37:19 +00:00
2021-01-05 23:09:04 +00:00
content = "<p>Some text</p><p>Other text</p><p>More text</p>" + \
"<pre><code>Errno::EOHNOES (No such file or rodent @ " + \
"ik_right - /tmp/blah.png)<br></code></pre><p>" + \
"(<a href=\"https://welllookeyhere.maam/error.txt\" " + \
"rel=\"nofollow noopener noreferrer\" target=\"_blank\">" + \
"wuh</a>)</p><p>Oh yeah like for sure</p>" + \
"<p>Ground sloth tin opener</p>" + \
"<p><a href=\"https://whocodedthis.huh/tags/" + \
"taggedthing\" class=\"mention hashtag\" rel=\"tag\" " + \
"target=\"_blank\">#<span>taggedthing</span></a></p>"
2021-12-25 22:09:19 +00:00
post_json_object = \
2021-12-28 19:33:29 +00:00
create_public_post(base_dir, nickname, domain, port, http_prefix,
content,
2022-05-31 16:20:16 +00:00
False,
2021-12-28 19:33:29 +00:00
False, True,
None, None,
False, None,
2022-01-04 20:14:19 +00:00
test_in_reply_to, test_in_reply_to_atom_uri,
test_subject, test_schedule_post,
2022-05-23 12:14:36 +00:00
test_event_date, test_event_time,
test_event_end_time, test_location,
2022-01-04 20:14:19 +00:00
test_is_article, system_language, conversation_id,
low_bandwidth, content_license_url,
2022-07-18 16:18:04 +00:00
languages_understood, translate)
2021-12-25 22:09:19 +00:00
assert post_json_object['object']['content'] == content
2021-12-25 23:03:28 +00:00
assert post_json_object['object']['contentMap'][system_language] == content
2021-01-05 23:09:04 +00:00
2021-01-02 10:37:19 +00:00
2021-12-29 21:55:09 +00:00
def _test_mastoapi():
2022-01-04 21:19:06 +00:00
print('test_masto_api')
2021-01-22 13:32:37 +00:00
nickname = 'ThisIsATestNickname'
2022-01-04 20:14:19 +00:00
masto_id = get_masto_api_v1id_from_nickname(nickname)
assert masto_id
nickname2 = get_nickname_from_masto_api_v1id(masto_id)
2021-01-22 13:32:37 +00:00
if nickname2 != nickname:
print(nickname + ' != ' + nickname2)
assert nickname2 == nickname
2021-12-29 21:55:09 +00:00
def _test_domain_handling():
2022-01-04 21:19:06 +00:00
print('test_domain_handling')
2022-01-04 20:14:19 +00:00
test_domain = 'localhost'
assert decoded_host(test_domain) == test_domain
test_domain = '127.0.0.1:60'
assert decoded_host(test_domain) == test_domain
test_domain = '192.168.5.153'
assert decoded_host(test_domain) == test_domain
test_domain = 'xn--espaa-rta.icom.museum'
assert decoded_host(test_domain) == "españa.icom.museum"
2021-12-29 21:55:09 +00:00
def _test_prepare_html_post_nick():
2022-01-04 21:19:06 +00:00
print('test_prepare_html_post_nickname')
2022-01-04 20:14:19 +00:00
post_html = '<a class="imageAnchor" href="/users/bob?replyfollowers='
post_html += '<a class="imageAnchor" href="/users/bob?repeatprivate='
result = prepare_html_post_nickname('alice', post_html)
assert result == post_html.replace('/bob?', '/alice?')
2021-02-02 21:08:33 +00:00
2022-01-04 20:14:19 +00:00
post_html = '<a class="imageAnchor" href="/users/bob?replyfollowers='
post_html += '<a class="imageAnchor" href="/users/bob;repeatprivate='
expected_html = '<a class="imageAnchor" href="/users/alice?replyfollowers='
expected_html += '<a class="imageAnchor" href="/users/bob;repeatprivate='
result = prepare_html_post_nickname('alice', post_html)
assert result == expected_html
2021-02-02 21:08:33 +00:00
2021-12-29 21:55:09 +00:00
def _test_valid_hash_tag():
2022-01-04 21:19:06 +00:00
print('test_valid_hash_tag')
2022-03-29 20:14:21 +00:00
assert valid_hash_tag('blobcat_thisisfine')
2021-12-29 21:55:09 +00:00
assert valid_hash_tag('ThisIsValid')
2022-03-02 19:05:26 +00:00
assert valid_hash_tag('this_is_valid')
2021-12-29 21:55:09 +00:00
assert valid_hash_tag('ThisIsValid12345')
assert valid_hash_tag('ThisIsVälid')
assert valid_hash_tag('यहमान्यहै')
2022-03-02 10:10:43 +00:00
assert valid_hash_tag('한국어')
2022-03-03 11:52:55 +00:00
assert valid_hash_tag('테스트')
assert valid_hash_tag('테_스트')
2022-05-20 10:17:53 +00:00
assert valid_hash_tag('c99')
2022-03-02 19:05:26 +00:00
assert not valid_hash_tag('this-is-invalid')
2021-12-29 21:55:09 +00:00
assert not valid_hash_tag('ThisIsNotValid!')
assert not valid_hash_tag('#ThisIsAlsoNotValid')
assert not valid_hash_tag('#यहमान्यहै')
assert not valid_hash_tag('ThisIsAlso&NotValid')
assert not valid_hash_tag('ThisIsAlsoNotValid"')
assert not valid_hash_tag('This Is Also Not Valid"')
assert not valid_hash_tag('This=IsAlsoNotValid"')
2022-05-20 10:17:53 +00:00
assert not valid_hash_tag('12345')
2021-12-29 21:55:09 +00:00
def _test_markdown_to_html():
2022-01-04 21:19:06 +00:00
print('test_markdown_to_html')
2021-02-24 20:37:59 +00:00
markdown = 'This is just plain text'
2021-12-29 21:55:09 +00:00
assert markdown_to_html(markdown) == markdown
2021-02-24 20:37:59 +00:00
2021-02-26 23:00:06 +00:00
markdown = 'This is a quotation:\n' + \
'> Some quote or other'
2022-06-27 12:38:55 +00:00
expected = \
'This is a quotation:<br>\n' + \
2021-02-26 23:00:06 +00:00
'<blockquote><i>Some quote or other</i></blockquote>'
2022-06-27 12:38:55 +00:00
result = markdown_to_html(markdown)
if result != expected:
print(result)
assert result == expected
2021-02-26 23:00:06 +00:00
2021-02-26 23:13:33 +00:00
markdown = 'This is a multi-line quotation:\n' + \
'> The first line\n' + \
'> The second line'
2021-12-29 21:55:09 +00:00
assert markdown_to_html(markdown) == \
2022-06-27 12:38:55 +00:00
'This is a multi-line quotation:<br>\n' + \
2021-02-26 23:13:33 +00:00
'<blockquote><i>The first line The second line</i></blockquote>'
2022-06-27 10:38:31 +00:00
markdown = 'This is a list of points:\n' + \
' * Point 1\n' + \
' * Point 2\n\n' + \
'And some other text.'
result = markdown_to_html(markdown)
expected = \
2022-06-27 16:21:48 +00:00
'This is a list of points:<br>\n<ul class="md_list">' + \
2022-06-28 09:18:42 +00:00
'\n<li>Point 1</li>\n' + \
'<li>Point 2</li>\n<li></li>\n</ul><br>\n' + \
2022-06-27 12:38:55 +00:00
'And some other text.<br>\n'
if result != expected:
print(result)
assert result == expected
markdown = 'This is a list of points:\n' + \
' * **Point 1**\n' + \
' * *Point 2*\n\n' + \
'And some other text.'
result = markdown_to_html(markdown)
expected = \
2022-06-28 09:18:42 +00:00
'This is a list of points:<br>\n<ul class="md_list">\n' + \
'<li><b>Point 1</b></li>\n' + \
'<li><i>Point 2</i></li>\n<li></li>\n</ul><br>\n' + \
2022-06-27 12:38:55 +00:00
'And some other text.<br>\n'
2022-06-27 10:38:31 +00:00
if result != expected:
print(result)
assert result == expected
markdown = 'This is a code section:\n' + \
'``` json\n' + \
'10 PRINT "YOLO"\n' + \
'20 GOTO 10\n' + \
'```\n\n' + \
'And some other text.'
result = markdown_to_html(markdown)
expected = \
2022-06-27 12:38:55 +00:00
'This is a code section:<br>\n' + \
'<code>\n' + \
2022-06-27 16:21:48 +00:00
'10 PRINT "YOLO"\n' + \
'20 GOTO 10\n' + \
2022-06-27 12:38:55 +00:00
'</code>\n' + \
2022-06-27 16:21:48 +00:00
'<br>\n' + \
'And some other text.<br>\n'
2022-06-27 10:38:31 +00:00
if result != expected:
print(result)
assert result == expected
2021-02-25 10:54:38 +00:00
markdown = 'This is **bold**'
2021-12-29 21:55:09 +00:00
assert markdown_to_html(markdown) == 'This is <b>bold</b>'
2021-02-25 10:54:38 +00:00
markdown = 'This is *italic*'
2021-12-29 21:55:09 +00:00
assert markdown_to_html(markdown) == 'This is <i>italic</i>'
2021-02-25 10:54:38 +00:00
markdown = 'This is _underlined_'
2022-06-27 16:21:48 +00:00
assert markdown_to_html(markdown) == 'This is <u>underlined</u>'
2021-02-25 10:54:38 +00:00
markdown = 'This is **just** plain text'
2021-12-29 21:55:09 +00:00
assert markdown_to_html(markdown) == 'This is <b>just</b> plain text'
2021-02-25 10:54:38 +00:00
2021-02-24 20:37:59 +00:00
markdown = '# Title1\n### Title3\n## Title2\n'
2022-06-27 12:38:55 +00:00
expected = '<h1>Title1</h1>\n<h3>Title3</h3>\n<h2>Title2</h2>\n'
result = markdown_to_html(markdown)
if result != expected:
print(result)
assert result == expected
2021-02-24 20:37:59 +00:00
markdown = \
2021-02-25 10:54:38 +00:00
'This is [a link](https://something.somewhere) to something.\n' + \
2021-02-26 15:20:43 +00:00
'And [something else](https://cat.pic).\n' + \
'Or ![pounce](/cat.jpg).'
expected = \
2021-02-24 20:37:59 +00:00
'This is <a href="https://something.somewhere" ' + \
'target="_blank" rel="nofollow noopener noreferrer">' + \
2022-06-27 12:38:55 +00:00
'a link</a> to something.<br>\n' + \
2021-02-24 20:37:59 +00:00
'And <a href="https://cat.pic" ' + \
'target="_blank" rel="nofollow noopener noreferrer">' + \
2022-06-27 12:38:55 +00:00
'something else</a>.<br>\n' + \
2021-02-26 15:20:43 +00:00
'Or <img class="markdownImage" src="/cat.jpg" alt="pounce" />.'
result = markdown_to_html(markdown)
if result != expected:
print(result)
assert result == expected
2021-02-24 20:37:59 +00:00
2021-12-29 21:55:09 +00:00
def _test_extract_text_fields_from_post():
2022-01-04 21:19:06 +00:00
print('test_extract_text_fields_in_post')
2022-07-10 21:15:06 +00:00
boundary = '--LYNX'
form_data = '--LYNX\r\nContent-Disposition: form-data; ' + \
'name="fieldName"\r\nContent-Type: text/plain; ' + \
'charset=utf-8\r\n\r\nThis is a lynx test\r\n' + \
'--LYNX\r\nContent-Disposition: ' + \
'form-data; name="submitYes"\r\nContent-Type: text/plain; ' + \
'charset=utf-8\r\n\r\nBUTTON\r\n--LYNX--\r\n'
debug = True
fields = extract_text_fields_in_post(None, boundary, debug, form_data)
print('fields: ' + str(fields))
assert fields
assert fields['fieldName'] == 'This is a lynx test'
assert fields['submitYes'] == 'BUTTON'
boundary = '-----------------------------116202748023898664511855843036'
2022-01-04 20:14:19 +00:00
form_data = '-----------------------------116202748023898664511855' + \
'843036\r\nContent-Disposition: form-data; name="submitPost"' + \
'\r\n\r\nSubmit\r\n-----------------------------116202748023' + \
'898664511855843036\r\nContent-Disposition: form-data; name=' + \
'"subject"\r\n\r\n\r\n-----------------------------116202748' + \
'023898664511855843036\r\nContent-Disposition: form-data; na' + \
'me="message"\r\n\r\nThis is a ; test\r\n-------------------' + \
'----------116202748023898664511855843036\r\nContent-Disposi' + \
'tion: form-data; name="commentsEnabled"\r\n\r\non\r\n------' + \
'-----------------------116202748023898664511855843036\r\nCo' + \
'ntent-Disposition: form-data; name="eventDate"\r\n\r\n\r\n' + \
'-----------------------------116202748023898664511855843036' + \
'\r\nContent-Disposition: form-data; name="eventTime"\r\n\r' + \
'\n\r\n-----------------------------116202748023898664511855' + \
'843036\r\nContent-Disposition: form-data; name="location"' + \
'\r\n\r\n\r\n-----------------------------116202748023898664' + \
'511855843036\r\nContent-Disposition: form-data; name=' + \
'"imageDescription"\r\n\r\n\r\n-----------------------------' + \
'116202748023898664511855843036\r\nContent-Disposition: ' + \
'form-data; name="attachpic"; filename=""\r\nContent-Type: ' + \
'application/octet-stream\r\n\r\n\r\n----------------------' + \
'-------116202748023898664511855843036--\r\n'
debug = False
2022-01-04 20:14:19 +00:00
fields = extract_text_fields_in_post(None, boundary, debug, form_data)
assert fields['submitPost'] == 'Submit'
assert fields['subject'] == ''
assert fields['commentsEnabled'] == 'on'
assert fields['eventDate'] == ''
assert fields['eventTime'] == ''
assert fields['location'] == ''
assert fields['imageDescription'] == ''
assert fields['message'] == 'This is a ; test'
2021-12-29 21:55:09 +00:00
def _test_speaker_replace_link():
print('testSpeakerReplaceLinks')
text = 'The Tor Project: For Snowflake volunteers: If you use ' + \
'Firefox, Brave, or Chrome, our Snowflake extension turns ' + \
'your browser into a proxy that connects Tor users in ' + \
'censored regions to the Tor network. Note: you should ' + \
'not run more than one snowflake in the same ' + \
'network.https://support.torproject.org/censorship/' + \
'how-to-help-running-snowflake/'
2022-01-04 20:14:19 +00:00
detected_links = []
result = \
speaker_replace_links(text, {'Linked': 'Web link'}, detected_links)
assert len(detected_links) == 1
assert detected_links[0] == \
'https://support.torproject.org/censorship/' + \
'how-to-help-running-snowflake/'
assert 'Web link support.torproject.org' in result
2021-12-29 21:55:09 +00:00
def _test_camel_case_split():
2022-01-04 21:19:06 +00:00
print('test_camel_case_split')
2022-01-04 20:14:19 +00:00
test_str = 'ThisIsCamelCase'
assert camel_case_split(test_str) == 'This Is Camel Case'
2022-01-04 20:14:19 +00:00
test_str = 'Notcamelcase test'
assert camel_case_split(test_str) == 'Notcamelcase test'
2021-12-29 21:55:09 +00:00
def _test_emoji_images():
2022-01-04 21:19:06 +00:00
print('test_emoji_images')
2022-01-04 20:14:19 +00:00
emoji_filename = 'emoji/default_emoji.json'
assert os.path.isfile(emoji_filename)
emoji_json = load_json(emoji_filename)
assert emoji_json
for emoji_name, emoji_image in emoji_json.items():
emoji_image_filename = 'emoji/' + emoji_image + '.png'
if not os.path.isfile(emoji_image_filename):
print('Missing emoji image ' + emoji_name + ' ' +
emoji_image + '.png')
assert os.path.isfile(emoji_image_filename)
2021-03-08 18:47:55 +00:00
2021-12-29 21:55:09 +00:00
def _test_extract_pgp_public_key():
print('testExtractPGPPublicKey')
2022-01-04 20:14:19 +00:00
pub_key = \
2021-03-11 17:15:32 +00:00
'-----BEGIN PGP PUBLIC KEY BLOCK-----\n\n' + \
'mDMEWZBueBYJKwYBBAHaRw8BAQdAKx1t6wL0RTuU6/' + \
'IBjngMbVJJ3Wg/3UW73/PV\n' + \
'I47xKTS0IUJvYiBNb3R0cmFtIDxib2JAZnJlZWRvb' + \
'WJvbmUubmV0PoiQBBMWCAA4\n' + \
'FiEEmruCwAq/OfgmgEh9zCU2GR+nwz8FAlmQbngCG' + \
'wMFCwkIBwMFFQoJCAsFFgID\n' + \
'AQACHgECF4AACgkQzCU2GR+nwz/9sAD/YgsHnVszH' + \
'Nz1zlVc5EgY1ByDupiJpHj0\n' + \
'XsLYk3AbNRgBALn45RqgD4eWHpmOriH09H5Rc5V9i' + \
'N4+OiGUn2AzJ6oHuDgEWZBu\n' + \
'eBIKKwYBBAGXVQEFAQEHQPRBG2ZQJce475S3e0Dxe' + \
'b0Fz5WdEu2q3GYLo4QG+4Ry\n' + \
'AwEIB4h4BBgWCAAgFiEEmruCwAq/OfgmgEh9zCU2G' + \
'R+nwz8FAlmQbngCGwwACgkQ\n' + \
'zCU2GR+nwz+OswD+JOoyBku9FzuWoVoOevU2HH+bP' + \
'OMDgY2OLnST9ZSyHkMBAMcK\n' + \
'fnaZ2Wi050483Sj2RmQRpb99Dod7rVZTDtCqXk0J\n' + \
'=gv5G\n' + \
'-----END PGP PUBLIC KEY BLOCK-----'
2022-01-04 20:14:19 +00:00
test_str = "Some introduction\n\n" + pub_key + "\n\nSome message."
assert contains_pgp_public_key(test_str)
2021-12-26 19:15:36 +00:00
assert not contains_pgp_public_key('String without a pgp key')
2022-01-04 20:14:19 +00:00
result = extract_pgp_public_key(test_str)
assert result
2022-01-04 20:14:19 +00:00
assert result == pub_key
2021-12-29 21:55:09 +00:00
def test_update_actor(base_dir: str):
2021-03-17 20:18:00 +00:00
print('Testing update of actor properties')
2022-01-04 20:14:19 +00:00
global TEST_SERVER_ALICE_RUNNING
TEST_SERVER_ALICE_RUNNING = False
2021-03-17 20:18:00 +00:00
2021-12-25 17:09:22 +00:00
http_prefix = 'http'
2021-12-25 21:09:22 +00:00
proxy_type = None
2021-12-25 23:45:30 +00:00
federation_list = []
2021-03-17 20:18:00 +00:00
2021-12-25 16:17:53 +00:00
if os.path.isdir(base_dir + '/.tests'):
shutil.rmtree(base_dir + '/.tests',
2021-10-29 18:48:15 +00:00
ignore_errors=False, onerror=None)
2021-12-25 16:17:53 +00:00
os.mkdir(base_dir + '/.tests')
2021-03-17 20:18:00 +00:00
# create the server
2022-01-04 20:14:19 +00:00
alice_dir = base_dir + '/.tests/alice'
alice_domain = '127.0.0.11'
alice_port = 61792
alice_send_threads = []
bob_address = '127.0.0.84:6384'
global THR_ALICE
if THR_ALICE:
while THR_ALICE.is_alive():
THR_ALICE.stop()
2021-03-17 20:18:00 +00:00
time.sleep(1)
2022-01-04 20:14:19 +00:00
THR_ALICE.kill()
2021-03-17 20:18:00 +00:00
2022-01-04 20:14:19 +00:00
THR_ALICE = \
2021-12-29 21:55:09 +00:00
thread_with_trace(target=create_server_alice,
2022-01-04 20:14:19 +00:00
args=(alice_dir, alice_domain,
alice_port, bob_address,
2021-12-28 21:36:27 +00:00
federation_list, False, False,
2022-01-04 20:14:19 +00:00
alice_send_threads),
2021-12-28 21:36:27 +00:00
daemon=True)
2021-03-17 20:18:00 +00:00
2022-01-04 20:14:19 +00:00
THR_ALICE.start()
assert THR_ALICE.is_alive() is True
2021-03-17 20:18:00 +00:00
# wait for server to be running
ctr = 0
2022-01-04 20:14:19 +00:00
while not TEST_SERVER_ALICE_RUNNING:
2021-03-17 20:18:00 +00:00
time.sleep(1)
ctr += 1
if ctr > 60:
break
2022-01-04 20:14:19 +00:00
print('Alice online: ' + str(TEST_SERVER_ALICE_RUNNING))
2021-03-17 20:18:00 +00:00
print('\n\n*******************************************************')
print('Alice updates her PGP key')
2022-01-04 20:14:19 +00:00
session_alice = create_session(proxy_type)
2021-12-25 22:28:18 +00:00
cached_webfingers = {}
2021-12-25 22:17:49 +00:00
person_cache = {}
2021-03-17 20:18:00 +00:00
password = 'alicepass'
2022-01-04 20:14:19 +00:00
outbox_path = alice_dir + '/accounts/alice@' + alice_domain + '/outbox'
actor_filename = \
alice_dir + '/accounts/' + 'alice@' + alice_domain + '.json'
assert os.path.isfile(actor_filename)
assert len([name for name in os.listdir(outbox_path)
if os.path.isfile(os.path.join(outbox_path, name))]) == 0
pub_key = \
2021-03-17 20:18:00 +00:00
'-----BEGIN PGP PUBLIC KEY BLOCK-----\n\n' + \
'mDMEWZBueBYJKwYBBAHaRw8BAQdAKx1t6wL0RTuU6/' + \
'IBjngMbVJJ3Wg/3UW73/PV\n' + \
'I47xKTS0IUJvYiBNb3R0cmFtIDxib2JAZnJlZWRvb' + \
'WJvbmUubmV0PoiQBBMWCAA4\n' + \
'FiEEmruCwAq/OfgmgEh9zCU2GR+nwz8FAlmQbngCG' + \
'wMFCwkIBwMFFQoJCAsFFgID\n' + \
'AQACHgECF4AACgkQzCU2GR+nwz/9sAD/YgsHnVszH' + \
'Nz1zlVc5EgY1ByDupiJpHj0\n' + \
'XsLYk3AbNRgBALn45RqgD4eWHpmOriH09H5Rc5V9i' + \
'N4+OiGUn2AzJ6oHuDgEWZBu\n' + \
'eBIKKwYBBAGXVQEFAQEHQPRBG2ZQJce475S3e0Dxe' + \
'b0Fz5WdEu2q3GYLo4QG+4Ry\n' + \
'AwEIB4h4BBgWCAAgFiEEmruCwAq/OfgmgEh9zCU2G' + \
'R+nwz8FAlmQbngCGwwACgkQ\n' + \
'zCU2GR+nwz+OswD+JOoyBku9FzuWoVoOevU2HH+bP' + \
'OMDgY2OLnST9ZSyHkMBAMcK\n' + \
'fnaZ2Wi050483Sj2RmQRpb99Dod7rVZTDtCqXk0J\n' + \
'=gv5G\n' + \
'-----END PGP PUBLIC KEY BLOCK-----'
2021-12-25 23:03:28 +00:00
signing_priv_key_pem = None
2022-01-04 20:14:19 +00:00
actor_update = \
pgp_public_key_upload(alice_dir, session_alice,
2021-12-29 21:55:09 +00:00
'alice', password,
2022-01-04 20:14:19 +00:00
alice_domain, alice_port,
2021-12-29 21:55:09 +00:00
http_prefix,
cached_webfingers, person_cache,
2022-01-04 20:14:19 +00:00
True, pub_key, signing_priv_key_pem)
print('actor update result: ' + str(actor_update))
assert actor_update
2021-03-17 20:18:00 +00:00
# load alice actor
2022-01-04 20:14:19 +00:00
print('Loading actor: ' + actor_filename)
actor_json = load_json(actor_filename)
2021-12-26 10:29:52 +00:00
assert actor_json
if len(actor_json['attachment']) == 0:
print("actor_json['attachment'] has no contents")
assert len(actor_json['attachment']) > 0
2022-01-03 10:27:55 +00:00
property_found = False
2021-12-26 10:32:45 +00:00
for property_value in actor_json['attachment']:
if property_value['name'] == 'PGP':
2021-03-17 20:18:00 +00:00
print('PGP property set within attachment')
2022-01-04 20:14:19 +00:00
assert pub_key in property_value['value']
2022-01-03 10:27:55 +00:00
property_found = True
assert property_found
2021-03-17 20:18:00 +00:00
# stop the server
2022-01-04 20:14:19 +00:00
THR_ALICE.kill()
THR_ALICE.join()
assert THR_ALICE.is_alive() is False
2021-03-17 20:18:00 +00:00
2021-12-25 16:17:53 +00:00
os.chdir(base_dir)
if os.path.isdir(base_dir + '/.tests'):
shutil.rmtree(base_dir + '/.tests', ignore_errors=False, onerror=None)
2021-03-17 20:18:00 +00:00
2021-12-29 21:55:09 +00:00
def _test_remove_interactions() -> None:
2022-01-04 21:19:06 +00:00
print('test_remove_post_interactions')
2021-12-25 22:09:19 +00:00
post_json_object = {
2021-04-30 11:45:46 +00:00
"type": "Create",
"object": {
"to": ["#Public"],
"likes": {
"items": ["a", "b", "c"]
},
"replies": {
"replyStuff": ["a", "b", "c"]
},
"shares": {
"sharesStuff": ["a", "b", "c"]
},
"bookmarks": {
"bookmarksStuff": ["a", "b", "c"]
},
"ignores": {
"ignoresStuff": ["a", "b", "c"]
}
}
}
2021-12-28 19:33:29 +00:00
remove_post_interactions(post_json_object, True)
2021-12-25 22:09:19 +00:00
assert post_json_object['object']['likes']['items'] == []
assert post_json_object['object']['replies'] == {}
assert post_json_object['object']['shares'] == {}
assert post_json_object['object']['bookmarks'] == {}
assert post_json_object['object']['ignores'] == {}
post_json_object['object']['to'] = ["some private address"]
2021-12-28 19:33:29 +00:00
assert not remove_post_interactions(post_json_object, False)
2021-04-30 11:45:46 +00:00
2021-12-29 21:55:09 +00:00
def _test_spoofed_geolocation() -> None:
2022-01-04 21:19:06 +00:00
print('test_spoof_geolocation')
2022-01-04 20:14:19 +00:00
nogo_line = \
'NEW YORK, USA: 73.951W,40.879, 73.974W,40.83, ' + \
'74.029W,40.756, 74.038W,40.713, 74.056W,40.713, ' + \
'74.127W,40.647, 74.038W,40.629, 73.995W,40.667, ' + \
'74.014W,40.676, 73.994W,40.702, 73.967W,40.699, ' + \
'73.958W,40.729, 73.956W,40.745, 73.918W,40.781, ' + \
'73.937W,40.793, 73.946W,40.782, 73.977W,40.738, ' + \
'73.98W,40.713, 74.012W,40.705, 74.006W,40.752, ' + \
'73.955W,40.824'
2022-01-04 20:14:19 +00:00
polygon = parse_nogo_string(nogo_line)
assert len(polygon) > 0
assert polygon[0][1] == -73.951
assert polygon[0][0] == 40.879
2022-01-04 20:14:19 +00:00
cities_list = [
2021-05-18 16:58:44 +00:00
'NEW YORK, USA:40.7127281:W74.0060152:784',
'LOS ANGELES, USA:34.0536909:W118.242766:1214',
2021-06-11 09:58:21 +00:00
'SAN FRANCISCO, USA:37.74594738515095:W122.44299445520019:121',
2021-05-18 16:58:44 +00:00
'HOUSTON, USA:29.6072:W95.1586:1553',
2021-06-11 09:58:21 +00:00
'MANCHESTER, ENGLAND:53.4794892:W2.2451148:1276',
2021-05-18 17:46:40 +00:00
'BERLIN, GERMANY:52.5170365:13.3888599:891',
2021-06-08 13:45:43 +00:00
'ANKARA, TURKEY:39.93:32.85:24521',
2021-06-11 11:42:07 +00:00
'LONDON, ENGLAND:51.5073219:W0.1276474:1738',
'SEATTLE, USA:47.59840153253106:W122.31143714060059:217'
2021-05-09 19:11:05 +00:00
]
2022-01-04 20:14:19 +00:00
test_square = [
[[0.03, 0.01], [0.02, 10], [10.01, 10.02], [10.03, 0.02]]
]
2022-01-04 20:14:19 +00:00
assert point_in_nogo(test_square, 5, 5)
assert point_in_nogo(test_square, 2, 3)
assert not point_in_nogo(test_square, 20, 5)
assert not point_in_nogo(test_square, 11, 6)
assert not point_in_nogo(test_square, 5, -5)
assert not point_in_nogo(test_square, 5, 11)
assert not point_in_nogo(test_square, -5, -5)
assert not point_in_nogo(test_square, -5, 5)
nogo_list = []
2021-12-26 13:17:46 +00:00
curr_time = datetime.datetime.utcnow()
2022-01-04 20:14:19 +00:00
decoy_seed = 7634681
city_radius = 0.1
2021-12-29 21:55:09 +00:00
coords = spoof_geolocation('', 'los angeles', curr_time,
2022-01-04 20:14:19 +00:00
decoy_seed, cities_list, nogo_list)
assert coords[0] >= 34.0536909 - city_radius
assert coords[0] <= 34.0536909 + city_radius
assert coords[1] >= 118.242766 - city_radius
assert coords[1] <= 118.242766 + city_radius
2021-05-09 19:11:05 +00:00
assert coords[2] == 'N'
assert coords[3] == 'W'
2021-05-11 12:36:35 +00:00
assert len(coords[4]) > 4
assert len(coords[5]) > 4
assert coords[6] > 0
2022-01-04 20:14:19 +00:00
nogo_list = []
2021-12-29 21:55:09 +00:00
coords = spoof_geolocation('', 'unknown', curr_time,
2022-01-04 20:14:19 +00:00
decoy_seed, cities_list, nogo_list)
assert coords[0] >= 51.8744 - city_radius
assert coords[0] <= 51.8744 + city_radius
assert coords[1] >= 0.368333 - city_radius
assert coords[1] <= 0.368333 + city_radius
2021-05-09 19:29:53 +00:00
assert coords[2] == 'N'
assert coords[3] == 'W'
2021-05-11 12:36:35 +00:00
assert len(coords[4]) == 0
assert len(coords[5]) == 0
assert coords[6] == 0
2022-01-04 20:14:19 +00:00
kml_str = '<?xml version="1.0" encoding="UTF-8"?>\n'
kml_str += '<kml xmlns="http://www.opengis.net/kml/2.2">\n'
kml_str += '<Document>\n'
nogo_line2 = \
'NEW YORK, USA: 74.115W,40.663, 74.065W,40.602, ' + \
'74.118W,40.555, 74.047W,40.516, 73.882W,40.547, ' + \
'73.909W,40.618, 73.978W,40.579, 74.009W,40.602, ' + \
'74.033W,40.61, 74.039W,40.623, 74.032W,40.641, ' + \
'73.996W,40.665'
2022-01-04 20:14:19 +00:00
polygon2 = parse_nogo_string(nogo_line2)
nogo_list = [polygon, polygon2]
2021-05-10 13:43:38 +00:00
for i in range(1000):
2022-01-04 20:14:19 +00:00
day_number = randint(10, 30)
2021-05-10 13:43:38 +00:00
hour = randint(1, 23)
2022-01-04 20:14:19 +00:00
hour_str = str(hour)
2021-05-10 13:43:38 +00:00
if hour < 10:
2022-01-04 20:14:19 +00:00
hour_str = '0' + hour_str
date_time_str = "2021-05-" + str(day_number) + " " + hour_str + ":14"
curr_time = datetime.datetime.strptime(date_time_str, "%Y-%m-%d %H:%M")
2021-12-29 21:55:09 +00:00
coords = spoof_geolocation('', 'new york, usa', curr_time,
2022-01-04 20:14:19 +00:00
decoy_seed, cities_list, nogo_list)
2021-05-18 16:58:44 +00:00
longitude = coords[1]
if coords[3] == 'W':
longitude = -coords[1]
2022-01-04 20:14:19 +00:00
kml_str += '<Placemark id="' + str(i) + '">\n'
kml_str += ' <name>' + str(i) + '</name>\n'
kml_str += ' <Point>\n'
kml_str += ' <coordinates>' + str(longitude) + ',' + \
2021-05-10 13:43:38 +00:00
str(coords[0]) + ',0</coordinates>\n'
2022-01-04 20:14:19 +00:00
kml_str += ' </Point>\n'
kml_str += '</Placemark>\n'
2021-06-08 13:45:43 +00:00
2022-01-04 20:14:19 +00:00
nogo_line = \
2021-06-08 13:45:43 +00:00
'LONDON, ENGLAND: 0.23888E,51.459, 0.1216E,51.5, ' + \
'0.016E,51.479, 0.097W,51.502, 0.126W,51.482, ' + \
'0.196W,51.457, 0.292W,51.465, 0.309W,51.49, ' + \
'0.226W,51.495, 0.198W,51.47, 0.174W,51.488, ' + \
'0.136W,51.489, 0.1189W,51.515, 0.038E,51.513, ' + \
'0.0692E,51.51, 0.12833E,51.526, 0.3289E,51.475'
2022-01-04 20:14:19 +00:00
polygon = parse_nogo_string(nogo_line)
nogo_line2 = \
2021-06-08 13:45:43 +00:00
'LONDON, ENGLAND: 0.054W,51.535, 0.044W,51.53, ' + \
'0.008W,51.55, 0.0429W,51.57, 0.038W,51.6, ' + \
'0.0209W,51.603, 0.032W,51.613, 0.00191E,51.66, ' + \
'0.024W,51.666, 0.0313W,51.659, 0.0639W,51.579, ' + \
'0.059W,51.568, 0.0329W,51.552'
2022-01-04 20:14:19 +00:00
polygon2 = parse_nogo_string(nogo_line2)
nogo_list = [polygon, polygon2]
2021-06-08 13:45:43 +00:00
for i in range(1000):
2022-01-04 20:14:19 +00:00
day_number = randint(10, 30)
2021-06-08 13:45:43 +00:00
hour = randint(1, 23)
2022-01-04 20:14:19 +00:00
hour_str = str(hour)
2021-06-08 13:45:43 +00:00
if hour < 10:
2022-01-04 20:14:19 +00:00
hour_str = '0' + hour_str
date_time_str = "2021-05-" + str(day_number) + " " + hour_str + ":14"
curr_time = datetime.datetime.strptime(date_time_str, "%Y-%m-%d %H:%M")
2021-12-29 21:55:09 +00:00
coords = spoof_geolocation('', 'london, england', curr_time,
2022-01-04 20:14:19 +00:00
decoy_seed, cities_list, nogo_list)
2021-06-08 13:45:43 +00:00
longitude = coords[1]
if coords[3] == 'W':
longitude = -coords[1]
2022-01-04 20:14:19 +00:00
kml_str += '<Placemark id="' + str(i) + '">\n'
kml_str += ' <name>' + str(i) + '</name>\n'
kml_str += ' <Point>\n'
kml_str += ' <coordinates>' + str(longitude) + ',' + \
2021-06-08 13:45:43 +00:00
str(coords[0]) + ',0</coordinates>\n'
2022-01-04 20:14:19 +00:00
kml_str += ' </Point>\n'
kml_str += '</Placemark>\n'
2021-06-11 09:58:21 +00:00
2022-01-04 20:14:19 +00:00
nogo_line = \
2021-06-11 11:42:07 +00:00
'SAN FRANCISCO, USA: 121.988W,37.408, 121.924W,37.452, ' + \
'121.951W,37.498, 121.992W,37.505, 122.056W,37.54, ' + \
'122.077W,37.578, 122.098W,37.618, 122.131W,37.637, ' + \
'122.189W,37.706, 122.227W,37.775, 122.279W,37.798, ' + \
'122.315W,37.802, 122.291W,37.832, 122.309W,37.902, ' + \
'122.382W,37.915, 122.368W,37.927, 122.514W,37.882, ' + \
'122.473W,37.83, 122.481W,37.788, 122.394W,37.796, ' + \
'122.384W,37.729, 122.4W,37.688, 122.382W,37.654, ' + \
'122.406W,37.637, 122.392W,37.612, 122.356W,37.586, ' + \
'122.332W,37.586, 122.275W,37.529, 122.228W,37.488, ' + \
'122.181W,37.482, 122.134W,37.48, 122.128W,37.471, ' + \
'122.122W,37.448, 122.095W,37.428, 122.07W,37.413, ' + \
'122.036W,37.402, 122.035W,37.421'
2022-01-04 20:14:19 +00:00
polygon = parse_nogo_string(nogo_line)
nogo_line2 = \
2021-06-11 11:42:07 +00:00
'SAN FRANCISCO, USA: 122.446W,37.794, 122.511W,37.778, ' + \
'122.51W,37.771, 122.454W,37.775, 122.452W,37.766, ' + \
'122.510W,37.763, 122.506W,37.735, 122.498W,37.733, ' + \
'122.496W,37.729, 122.491W,37.729, 122.475W,37.73, ' + \
'122.474W,37.72, 122.484W,37.72, 122.485W,37.703, ' + \
'122.495W,37.702, 122.493W,37.679, 122.486W,37.667, ' + \
'122.492W,37.664, 122.493W,37.629, 122.456W,37.625, ' + \
'122.450W,37.617, 122.455W,37.621, 122.41W,37.586, ' + \
'122.383W,37.561, 122.335W,37.509, 122.655W,37.48, ' + \
'122.67W,37.9, 122.272W,37.93, 122.294W,37.801, ' + \
'122.448W,37.804'
2022-01-04 20:14:19 +00:00
polygon2 = parse_nogo_string(nogo_line2)
nogo_list = [polygon, polygon2]
2021-06-11 09:58:21 +00:00
for i in range(1000):
2022-01-04 20:14:19 +00:00
day_number = randint(10, 30)
2021-06-11 09:58:21 +00:00
hour = randint(1, 23)
2022-01-04 20:14:19 +00:00
hour_str = str(hour)
2021-06-11 09:58:21 +00:00
if hour < 10:
2022-01-04 20:14:19 +00:00
hour_str = '0' + hour_str
date_time_str = "2021-05-" + str(day_number) + " " + hour_str + ":14"
curr_time = datetime.datetime.strptime(date_time_str, "%Y-%m-%d %H:%M")
2021-12-29 21:55:09 +00:00
coords = spoof_geolocation('', 'SAN FRANCISCO, USA', curr_time,
2022-01-04 20:14:19 +00:00
decoy_seed, cities_list, nogo_list)
2021-06-11 09:58:21 +00:00
longitude = coords[1]
if coords[3] == 'W':
longitude = -coords[1]
2022-01-04 20:14:19 +00:00
kml_str += '<Placemark id="' + str(i) + '">\n'
kml_str += ' <name>' + str(i) + '</name>\n'
kml_str += ' <Point>\n'
kml_str += ' <coordinates>' + str(longitude) + ',' + \
2021-06-11 09:58:21 +00:00
str(coords[0]) + ',0</coordinates>\n'
2022-01-04 20:14:19 +00:00
kml_str += ' </Point>\n'
kml_str += '</Placemark>\n'
2021-06-11 09:58:21 +00:00
2022-01-04 20:14:19 +00:00
nogo_line = \
2021-06-11 11:42:07 +00:00
'SEATTLE, USA: 122.247W,47.918, 122.39W,47.802, ' + \
'122.389W,47.769, 122.377W,47.758, 122.371W,47.726, ' + \
'122.379W,47.706, 122.4W,47.696, 122.405W,47.673, ' + \
'122.416W,47.65, 122.414W,47.642, 122.391W,47.632, ' + \
'122.373W,47.633, 122.336W,47.602, 122.288W,47.501, ' + \
'122.299W,47.503, 122.386W,47.592, 122.412W,47.574, ' + \
'122.394W,47.549, 122.388W,47.507, 122.35W,47.481, ' + \
'122.365W,47.459, 122.33W,47.406, 122.323W,47.392, ' + \
'122.321W,47.346, 122.441W,47.302, 122.696W,47.085, ' + \
'122.926W,47.066, 122.929W,48.383'
2022-01-04 20:14:19 +00:00
polygon = parse_nogo_string(nogo_line)
nogo_line2 = \
2021-06-11 11:42:07 +00:00
'SEATTLE, USA: 122.267W,47.758, 122.29W,47.471, ' + \
'122.272W,47.693, 122.256W,47.672, 122.278W,47.652, ' + \
'122.29W,47.583, 122.262W,47.548, 122.265W,47.52, ' + \
'122.218W,47.498, 122.194W,47.501, 122.193W,47.55, ' + \
'122.173W,47.58, 122.22W,47.617, 122.238W,47.617, ' + \
'122.239W,47.637, 122.2W,47.644, 122.207W,47.703, ' + \
'122.22W,47.705, 122.231W,47.699, 122.255W,47.751'
2022-01-04 20:14:19 +00:00
polygon2 = parse_nogo_string(nogo_line2)
nogo_line3 = \
2021-06-11 11:42:07 +00:00
'SEATTLE, USA: 122.347W,47.675, 122.344W,47.681, ' + \
'122.337W,47.685, 122.324W,47.679, 122.331W,47.677, ' + \
'122.34W,47.669, 122.34W,47.664, 122.348W,47.665'
2022-01-04 20:14:19 +00:00
polygon3 = parse_nogo_string(nogo_line3)
nogo_line4 = \
2021-06-11 11:42:07 +00:00
'SEATTLE, USA: 122.423W,47.669, 122.345W,47.641, ' + \
'122.34W,47.625, 122.327W,47.626, 122.274W,47.64, ' + \
'122.268W,47.654, 122.327W,47.654, 122.336W,47.647, ' + \
'122.429W,47.684'
2022-01-04 20:14:19 +00:00
polygon4 = parse_nogo_string(nogo_line4)
nogo_list = [polygon, polygon2, polygon3, polygon4]
2021-06-11 11:42:07 +00:00
for i in range(1000):
2022-01-04 20:14:19 +00:00
day_number = randint(10, 30)
2021-06-11 11:42:07 +00:00
hour = randint(1, 23)
2022-01-04 20:14:19 +00:00
hour_str = str(hour)
2021-06-11 11:42:07 +00:00
if hour < 10:
2022-01-04 20:14:19 +00:00
hour_str = '0' + hour_str
date_time_str = "2021-05-" + str(day_number) + " " + hour_str + ":14"
curr_time = datetime.datetime.strptime(date_time_str, "%Y-%m-%d %H:%M")
2021-12-29 21:55:09 +00:00
coords = spoof_geolocation('', 'SEATTLE, USA', curr_time,
2022-01-04 20:14:19 +00:00
decoy_seed, cities_list, nogo_list)
2021-06-11 11:42:07 +00:00
longitude = coords[1]
if coords[3] == 'W':
longitude = -coords[1]
2022-01-04 20:14:19 +00:00
kml_str += '<Placemark id="' + str(i) + '">\n'
kml_str += ' <name>' + str(i) + '</name>\n'
kml_str += ' <Point>\n'
kml_str += ' <coordinates>' + str(longitude) + ',' + \
2021-06-11 11:42:07 +00:00
str(coords[0]) + ',0</coordinates>\n'
2022-01-04 20:14:19 +00:00
kml_str += ' </Point>\n'
kml_str += '</Placemark>\n'
2021-06-11 11:42:07 +00:00
2022-01-04 20:14:19 +00:00
kml_str += '</Document>\n'
kml_str += '</kml>'
2022-06-09 14:46:30 +00:00
with open('unittest_decoy.kml', 'w+', encoding='utf-8') as kmlfile:
2022-01-04 20:14:19 +00:00
kmlfile.write(kml_str)
2021-05-09 19:11:05 +00:00
2021-12-29 21:55:09 +00:00
def _test_skills() -> None:
2022-01-04 21:19:06 +00:00
print('test_skills')
2021-12-26 10:29:52 +00:00
actor_json = {
2021-05-16 15:10:39 +00:00
'hasOccupation': [
{
'@type': 'Occupation',
'name': "Sysop",
2021-05-17 10:27:14 +00:00
"occupationLocation": {
"@type": "City",
"name": "Fediverse"
2021-05-17 09:12:10 +00:00
},
2021-05-16 15:10:39 +00:00
'skills': []
}
]
2021-05-13 14:13:27 +00:00
}
2022-01-04 20:14:19 +00:00
skills_dict = {
2021-05-13 14:13:27 +00:00
'bakery': 40,
'gardening': 70
}
2022-01-04 20:14:19 +00:00
set_skills_from_dict(actor_json, skills_dict)
2021-12-28 20:32:11 +00:00
assert actor_has_skill(actor_json, 'bakery')
assert actor_has_skill(actor_json, 'gardening')
assert actor_skill_value(actor_json, 'bakery') == 40
assert actor_skill_value(actor_json, 'gardening') == 70
2021-05-13 14:13:27 +00:00
2021-12-29 21:55:09 +00:00
def _test_roles() -> None:
2022-01-04 21:19:06 +00:00
print('test_roles')
2021-12-26 10:29:52 +00:00
actor_json = {
2021-05-16 15:10:39 +00:00
'hasOccupation': [
{
'@type': 'Occupation',
'name': "Sysop",
2021-05-17 10:27:14 +00:00
'occupationLocation': {
'@type': 'City',
'name': 'Fediverse'
2021-05-17 09:12:10 +00:00
},
2021-05-16 15:10:39 +00:00
'skills': []
}
]
}
2022-01-04 20:14:19 +00:00
test_roles_list = ["admin", "moderator"]
2022-09-02 18:06:13 +00:00
actor_roles_from_list(actor_json, test_roles_list)
2021-12-29 21:55:09 +00:00
assert actor_has_role(actor_json, "admin")
assert actor_has_role(actor_json, "moderator")
assert not actor_has_role(actor_json, "editor")
assert not actor_has_role(actor_json, "counselor")
assert not actor_has_role(actor_json, "artist")
2021-12-29 21:55:09 +00:00
def _test_useragent_domain() -> None:
2022-01-04 21:19:06 +00:00
print('test_user_agent_domain')
2022-01-04 20:14:19 +00:00
user_agent = \
2021-06-20 15:45:29 +00:00
'http.rb/4.4.1 (Mastodon/9.10.11; +https://mastodon.something/)'
2022-02-03 12:30:57 +00:00
agent_domain = user_agent_domain(user_agent, False)
if agent_domain != 'mastodon.something':
print(agent_domain)
assert agent_domain == 'mastodon.something'
2022-01-04 20:14:19 +00:00
user_agent = \
2021-06-20 15:45:29 +00:00
'Mozilla/70.0 (X11; Linux x86_64; rv:1.0) Gecko/20450101 Firefox/1.0'
2022-01-04 20:14:19 +00:00
assert user_agent_domain(user_agent, False) is None
2021-06-20 15:45:29 +00:00
2021-12-29 21:55:09 +00:00
def _test_switch_word(base_dir: str) -> None:
2022-01-04 21:19:06 +00:00
print('test_switch_words')
2021-07-06 16:29:03 +00:00
rules = [
"rock -> hamster",
"orange -> lemon"
]
nickname = 'testuser'
domain = 'testdomain.com'
content = 'This is a test'
2021-12-29 21:55:09 +00:00
result = switch_words(base_dir, nickname, domain, content, rules)
2021-07-06 16:29:03 +00:00
assert result == content
content = 'This is orange test'
2021-12-29 21:55:09 +00:00
result = switch_words(base_dir, nickname, domain, content, rules)
2021-07-06 16:29:03 +00:00
assert result == 'This is lemon test'
content = 'This is a test rock'
2021-12-29 21:55:09 +00:00
result = switch_words(base_dir, nickname, domain, content, rules)
2021-07-06 16:29:03 +00:00
assert result == 'This is a test hamster'
2021-12-29 21:55:09 +00:00
def _test_word_lengths_limit() -> None:
2022-01-04 21:19:06 +00:00
print('test_limit_word_lengths')
2022-01-04 20:14:19 +00:00
max_word_length = 13
text = "This is a test"
2022-01-04 20:14:19 +00:00
result = limit_word_lengths(text, max_word_length)
assert result == text
text = "This is an exceptionallylongword test"
2022-01-04 20:14:19 +00:00
result = limit_word_lengths(text, max_word_length)
assert result == "This is an exceptionally test"
2021-12-29 21:55:09 +00:00
def _test_limit_repeted_words() -> None:
2022-01-04 21:19:06 +00:00
print('test_limit_repeated_words')
text = \
"This is a preamble.\n\n" + \
"Same Same Same Same Same Same Same Same Same Same " + \
"Same Same Same Same Same Same Same Same Same Same " + \
"Same Same Same Same Same Same Same Same Same Same " + \
"Same Same Same Same Same Same Same Same Same Same " + \
"Same Same Same Same Same Same Same Same Same Same " + \
"Same Same Same Same Same Same Same Same Same Same " + \
"Same Same Same Same Same Same Same Same Same Same\n\n" + \
"Some other text."
expected = \
"This is a preamble.\n\n" + \
"Same Same Same Same Same Same\n\n" + \
"Some other text."
2021-12-29 21:55:09 +00:00
result = limit_repeated_words(text, 6)
assert result == expected
text = \
"This is other preamble.\n\n" + \
"Same Same Same Same Same Same Same Same Same Same " + \
"Same Same Same Same Same Same Same Same Same Same " + \
"Same Same Same Same Same Same Same Same Same Same " + \
"Same Same Same Same Same Same Same Same Same Same " + \
"Same Same Same Same Same Same Same Same Same Same " + \
"Same Same Same Same Same Same Same Same Same Same " + \
"Same Same Same Same Same Same Same Same Same Same " + \
"Same Same Same Same Same Same Same Same Same Same " + \
"Same Same Same Same Same Same Same Same Same Same"
expected = \
"This is other preamble.\n\n" + \
"Same Same Same Same Same Same"
2021-12-29 21:55:09 +00:00
result = limit_repeated_words(text, 6)
assert result == expected
2021-12-29 21:55:09 +00:00
def _test_set_actor_language():
2022-01-04 21:19:06 +00:00
print('test_set_actor_languages')
2021-12-26 10:29:52 +00:00
actor_json = {
2021-07-19 10:07:29 +00:00
"attachment": []
}
set_actor_languages(actor_json, 'es, fr, en')
2021-12-26 10:29:52 +00:00
assert len(actor_json['attachment']) == 1
assert actor_json['attachment'][0]['name'] == 'Languages'
assert actor_json['attachment'][0]['type'] == 'PropertyValue'
assert isinstance(actor_json['attachment'][0]['value'], str)
assert ',' in actor_json['attachment'][0]['value']
2021-12-26 10:35:37 +00:00
lang_list = get_actor_languages_list(actor_json)
assert 'en' in lang_list
assert 'fr' in lang_list
assert 'es' in lang_list
2022-01-04 20:14:19 +00:00
languages_str = get_actor_languages(actor_json)
assert languages_str == 'en / es / fr'
2021-07-19 10:07:29 +00:00
2021-12-29 21:55:09 +00:00
def _test_get_links_from_content():
2022-01-04 21:19:06 +00:00
print('test_get_links_from_content')
2021-07-20 10:45:04 +00:00
content = 'This text has no links'
2021-12-29 21:55:09 +00:00
links = get_links_from_content(content)
2021-07-20 10:45:04 +00:00
assert not links
link1 = 'https://somewebsite.net'
link2 = 'http://somewhere.or.other'
content = \
2021-07-20 18:02:42 +00:00
'This is <a href="' + link1 + '">@linked</a>. ' + \
2021-07-20 10:45:04 +00:00
'And <a href="' + link2 + '">another</a>.'
2021-12-29 21:55:09 +00:00
links = get_links_from_content(content)
assert len(links.items()) == 2
2021-07-20 18:02:42 +00:00
assert links.get('@linked')
assert links['@linked'] == link1
assert links.get('another')
assert links['another'] == link2
2021-07-20 10:45:04 +00:00
2022-01-04 20:14:19 +00:00
content_plain = '<p>' + remove_html(content) + '</p>'
assert '>@linked</a>' not in content_plain
content = add_links_to_content(content_plain, links)
2021-07-20 18:02:42 +00:00
assert '>@linked</a>' in content
2021-07-20 10:45:04 +00:00
2021-12-29 21:55:09 +00:00
def _test_authorized_shared_items():
2022-01-04 21:19:06 +00:00
print('test_authorize_shared_items')
2021-12-25 18:05:01 +00:00
shared_items_fed_domains = \
['dog.domain', 'cat.domain', 'birb.domain']
2022-01-04 20:14:19 +00:00
tokens_json = \
2021-12-29 21:55:09 +00:00
generate_shared_item_federation_tokens(shared_items_fed_domains, None)
2022-01-04 20:14:19 +00:00
tokens_json = \
2021-12-29 21:55:09 +00:00
create_shared_item_federation_token(None, 'cat.domain',
2022-01-04 20:14:19 +00:00
False, tokens_json)
assert tokens_json
assert not tokens_json.get('dog.domain')
assert tokens_json.get('cat.domain')
assert not tokens_json.get('birb.domain')
assert len(tokens_json['dog.domain']) == 0
assert len(tokens_json['cat.domain']) >= 64
assert len(tokens_json['birb.domain']) == 0
2021-12-28 21:36:27 +00:00
assert not authorize_shared_items(shared_items_fed_domains, None,
'birb.domain',
'cat.domain', 'M' * 86,
2022-01-04 20:14:19 +00:00
False, tokens_json)
2021-12-28 21:36:27 +00:00
assert authorize_shared_items(shared_items_fed_domains, None,
'birb.domain',
2022-01-04 20:14:19 +00:00
'cat.domain', tokens_json['cat.domain'],
False, tokens_json)
tokens_json = \
2021-12-29 21:55:09 +00:00
update_shared_item_federation_token(None,
'dog.domain', 'testToken',
2022-01-04 20:14:19 +00:00
True, tokens_json)
assert tokens_json['dog.domain'] == 'testToken'
# the shared item federation list changes
2021-12-25 18:05:01 +00:00
shared_items_federated_domains = \
['possum.domain', 'cat.domain', 'birb.domain']
2022-01-04 20:14:19 +00:00
tokens_json = \
merge_shared_item_tokens(None, '',
shared_items_federated_domains,
tokens_json)
assert 'dog.domain' not in tokens_json
assert 'cat.domain' in tokens_json
assert len(tokens_json['cat.domain']) >= 64
assert 'birb.domain' in tokens_json
assert 'possum.domain' in tokens_json
assert len(tokens_json['birb.domain']) == 0
assert len(tokens_json['possum.domain']) == 0
2021-12-29 21:55:09 +00:00
def _test_date_conversions() -> None:
2022-01-04 21:19:06 +00:00
print('test_date_conversions')
2022-01-04 20:14:19 +00:00
date_str = "2021-05-16T14:37:41Z"
date_sec = date_string_to_seconds(date_str)
date_str2 = date_seconds_to_string(date_sec)
assert date_str == date_str2
2021-07-28 09:35:21 +00:00
2021-12-29 21:55:09 +00:00
def _test_valid_password():
2022-01-04 21:19:06 +00:00
print('test_valid_password')
2021-12-26 18:05:54 +00:00
assert not valid_password('123')
assert not valid_password('')
assert valid_password('パスワード12345')
assert valid_password('测试密码12345')
assert valid_password('A!bc:defg1/234?56')
2021-07-29 14:15:44 +00:00
2021-12-29 21:55:09 +00:00
def _test_get_price_from_string() -> None:
2022-01-04 21:19:06 +00:00
print('test_get_price_from_string')
2021-12-29 21:55:09 +00:00
price, curr = get_price_from_string("5.23")
2021-08-07 17:03:41 +00:00
assert price == "5.23"
assert curr == "EUR"
2021-12-29 21:55:09 +00:00
price, curr = get_price_from_string("£7.36")
2021-08-07 17:03:41 +00:00
assert price == "7.36"
assert curr == "GBP"
2021-12-29 21:55:09 +00:00
price, curr = get_price_from_string("$10.63")
2021-08-07 17:03:41 +00:00
assert price == "10.63"
assert curr == "USD"
2021-12-29 21:55:09 +00:00
def _translate_ontology(base_dir: str) -> None:
2022-01-04 20:14:19 +00:00
print('test_translate_ontology')
2021-10-06 09:31:58 +00:00
return
2022-01-04 20:14:19 +00:00
ontology_types = get_category_types(base_dir)
2021-08-08 11:16:18 +00:00
url = 'https://translate.astian.org'
2022-01-04 20:14:19 +00:00
api_key = None
lt_lang_list = libretranslate_languages(url, api_key)
2021-09-15 10:44:44 +00:00
2022-01-04 20:14:19 +00:00
languages_str = get_supported_languages(base_dir)
assert languages_str
2021-08-08 11:16:18 +00:00
2022-01-04 20:14:19 +00:00
for otype in ontology_types:
2021-08-08 11:16:18 +00:00
changed = False
2022-01-04 20:14:19 +00:00
filename = base_dir + '/ontology/' + otype + 'Types.json'
2021-08-08 11:16:18 +00:00
if not os.path.isfile(filename):
continue
2022-01-04 20:14:19 +00:00
ontology_json = load_json(filename)
if not ontology_json:
2021-08-08 11:16:18 +00:00
continue
index = -1
2022-01-04 20:14:19 +00:00
for item in ontology_json['@graph']:
2021-08-08 11:16:18 +00:00
index += 1
if "rdfs:label" not in item:
continue
2022-01-04 20:14:19 +00:00
english_str = None
languages_found = []
2021-08-08 11:16:18 +00:00
for label in item["rdfs:label"]:
if '@language' not in label:
continue
2022-01-04 20:14:19 +00:00
languages_found.append(label['@language'])
2021-08-08 11:16:18 +00:00
if '@value' not in label:
continue
if label['@language'] == 'en':
2022-01-04 20:14:19 +00:00
english_str = label['@value']
if not english_str:
2021-08-08 11:16:18 +00:00
continue
2022-01-04 20:14:19 +00:00
for lang in languages_str:
if lang not in languages_found:
translated_str = None
if url and lang in lt_lang_list:
translated_str = \
libretranslate(url, english_str,
'en', lang, api_key)
if not translated_str:
translated_str = english_str
2021-08-08 11:16:18 +00:00
else:
2022-01-04 20:14:19 +00:00
translated_str = translated_str.replace('<p>', '')
translated_str = translated_str.replace('</p>', '')
ontology_json['@graph'][index]["rdfs:label"].append({
"@value": translated_str,
2021-08-08 11:16:18 +00:00
"@language": lang
})
changed = True
if not changed:
continue
2022-01-04 20:14:19 +00:00
save_json(ontology_json, filename + '.new')
2021-08-08 11:16:18 +00:00
2021-12-29 21:55:09 +00:00
def _test_can_replyto(base_dir: str) -> None:
2022-01-04 21:19:06 +00:00
print('test_can_reply_to')
2021-12-25 23:03:28 +00:00
system_language = 'en'
languages_understood = [system_language]
2021-09-08 20:12:03 +00:00
nickname = 'test27637'
domain = 'rando.site'
port = 443
2021-12-25 17:09:22 +00:00
http_prefix = 'https'
2021-09-08 20:12:03 +00:00
content = 'This is a test post with links.\n\n' + \
2021-09-10 16:14:50 +00:00
'ftp://ftp.ncdc.noaa.gov/pub/data/ghcn/v4/\n\nhttps://libreserver.org'
2022-01-04 20:14:19 +00:00
save_to_file = False
2021-12-25 20:39:35 +00:00
client_to_server = False
2022-01-04 20:14:19 +00:00
comments_enabled = True
attach_image_filename = None
media_type = None
image_description = None
2021-09-08 20:12:03 +00:00
city = 'London, England'
2022-01-04 20:14:19 +00:00
test_in_reply_to = None
test_in_reply_to_atom_uri = None
test_subject = None
test_schedule_post = False
test_event_date = None
test_event_time = None
2022-05-23 12:14:36 +00:00
test_event_end_time = None
2022-01-04 20:14:19 +00:00
test_location = None
test_is_article = False
conversation_id = None
2021-12-25 18:20:56 +00:00
low_bandwidth = True
2021-12-25 17:13:38 +00:00
content_license_url = 'https://creativecommons.org/licenses/by/4.0'
2022-07-18 16:18:04 +00:00
translate = {}
2021-09-08 20:12:03 +00:00
2021-12-25 22:09:19 +00:00
post_json_object = \
2021-12-28 19:33:29 +00:00
create_public_post(base_dir, nickname, domain, port, http_prefix,
2022-05-31 16:20:16 +00:00
content, save_to_file,
2022-01-04 20:14:19 +00:00
client_to_server, comments_enabled,
attach_image_filename, media_type,
image_description, city,
test_in_reply_to, test_in_reply_to_atom_uri,
test_subject, test_schedule_post,
2022-05-23 12:14:36 +00:00
test_event_date, test_event_time,
test_event_end_time, test_location,
2022-01-04 20:14:19 +00:00
test_is_article, system_language, conversation_id,
low_bandwidth, content_license_url,
2022-07-18 16:18:04 +00:00
languages_understood, translate)
2021-09-08 20:12:03 +00:00
# set the date on the post
2022-01-04 20:14:19 +00:00
curr_date_str = "2021-09-08T20:45:00Z"
post_json_object['published'] = curr_date_str
post_json_object['object']['published'] = curr_date_str
2021-09-08 20:12:03 +00:00
2021-09-08 20:13:41 +00:00
# test a post within the reply interval
2022-01-04 20:14:19 +00:00
post_url = post_json_object['object']['id']
reply_interval_hours = 2
curr_date_str = "2021-09-08T21:32:10Z"
2021-12-28 12:15:46 +00:00
assert can_reply_to(base_dir, nickname, domain,
2022-01-04 20:14:19 +00:00
post_url, reply_interval_hours,
curr_date_str,
2021-12-28 12:15:46 +00:00
post_json_object)
2021-09-08 20:13:41 +00:00
# test a post outside of the reply interval
2022-01-04 20:14:19 +00:00
curr_date_str = "2021-09-09T09:24:47Z"
2021-12-28 12:15:46 +00:00
assert not can_reply_to(base_dir, nickname, domain,
2022-01-04 20:14:19 +00:00
post_url, reply_interval_hours,
curr_date_str,
2021-12-28 12:15:46 +00:00
post_json_object)
2021-09-08 20:12:03 +00:00
2021-12-29 21:55:09 +00:00
def _test_seconds_between_publish() -> None:
2022-01-04 21:19:06 +00:00
print('test_seconds_between_published')
2021-10-14 15:12:35 +00:00
published1 = "2021-10-14T09:39:27Z"
published2 = "2021-10-14T09:41:28Z"
2022-01-04 20:14:19 +00:00
seconds_elapsed = seconds_between_published(published1, published2)
assert seconds_elapsed == 121
2021-10-14 15:12:35 +00:00
# invalid date
published2 = "2021-10-14N09:41:28Z"
2022-01-04 20:14:19 +00:00
seconds_elapsed = seconds_between_published(published1, published2)
assert seconds_elapsed == -1
2021-10-14 15:12:35 +00:00
2021-12-29 21:55:09 +00:00
def _test_word_similarity() -> None:
2022-01-04 21:19:06 +00:00
print('test_words_similarity')
2022-01-04 20:14:19 +00:00
min_words = 10
2021-10-14 15:12:35 +00:00
content1 = "This is the same"
content2 = "This is the same"
2022-01-04 20:14:19 +00:00
assert words_similarity(content1, content2, min_words) == 100
2021-10-14 15:12:35 +00:00
content1 = "This is our world now... " + \
"the world of the electron and the switch, the beauty of the baud"
content2 = "This is our world now. " + \
"The world of the electron and the webkit, the beauty of the baud"
2022-01-04 20:14:19 +00:00
similarity = words_similarity(content1, content2, min_words)
2021-10-14 15:12:35 +00:00
assert similarity > 70
content1 = "<p>We&apos;re growing! </p><p>A new denizen " + \
"is frequenting HackBucket. You probably know him already " + \
2021-10-18 16:33:42 +00:00
"from her epic typos - but let&apos;s not spoil too much " + \
"\ud83d\udd2e</p>"
content2 = "<p>We&apos;re growing! </p><p>A new denizen " + \
"is frequenting HackBucket. You probably know them already " + \
"from their epic typos - but let&apos;s not spoil too much " + \
"\ud83d\udd2e</p>"
2022-01-04 20:14:19 +00:00
similarity = words_similarity(content1, content2, min_words)
2021-10-18 16:33:42 +00:00
assert similarity > 80
2021-10-14 15:12:35 +00:00
2021-12-29 21:55:09 +00:00
def _test_add_cw_lists(base_dir: str) -> None:
2022-01-04 21:19:06 +00:00
print('test_add_CW_from_lists')
2021-10-21 13:08:21 +00:00
translate = {}
system_language = "en"
2021-12-28 21:55:38 +00:00
cw_lists = load_cw_lists(base_dir, True)
2021-12-25 23:26:38 +00:00
assert cw_lists
2021-10-21 13:08:21 +00:00
2021-12-25 22:09:19 +00:00
post_json_object = {
2021-10-21 13:08:21 +00:00
"object": {
"sensitive": False,
"summary": None,
"content": ""
}
}
add_cw_from_lists(post_json_object, cw_lists, translate, 'Murdoch press',
system_language)
2021-12-25 22:09:19 +00:00
assert post_json_object['object']['sensitive'] is False
assert post_json_object['object']['summary'] is None
2021-10-21 13:08:21 +00:00
2021-12-25 22:09:19 +00:00
post_json_object = {
2021-10-21 13:08:21 +00:00
"object": {
"sensitive": False,
"summary": None,
"contentMap": {
"en": "Blah blah news.co.uk blah blah"
}
2021-10-21 13:08:21 +00:00
}
}
add_cw_from_lists(post_json_object, cw_lists, translate, 'Murdoch press',
system_language)
2021-12-25 22:09:19 +00:00
assert post_json_object['object']['sensitive'] is True
assert post_json_object['object']['summary'] == "Murdoch Press"
2021-10-21 13:08:21 +00:00
2021-12-25 22:09:19 +00:00
post_json_object = {
2021-10-21 13:08:21 +00:00
"object": {
"sensitive": True,
"summary": "Existing CW",
"content": "Blah blah news.co.uk blah blah"
}
}
add_cw_from_lists(post_json_object, cw_lists, translate, 'Murdoch press',
system_language)
2021-12-25 22:09:19 +00:00
assert post_json_object['object']['sensitive'] is True
assert post_json_object['object']['summary'] == \
"Murdoch Press / Existing CW"
2021-10-21 13:08:21 +00:00
2021-12-29 21:55:09 +00:00
def _test_valid_emoji_content() -> None:
2022-01-04 21:19:06 +00:00
print('test_valid_emoji_content')
2021-12-29 21:55:09 +00:00
assert not valid_emoji_content(None)
assert not valid_emoji_content(' ')
assert not valid_emoji_content('j')
assert not valid_emoji_content('😀😀😀')
assert valid_emoji_content('😀')
assert valid_emoji_content('😄')
2021-11-10 13:10:02 +00:00
2022-01-04 20:14:19 +00:00
def _test_httpsig_base_new(with_digest: bool, base_dir: str,
algorithm: str, digest_algorithm: str) -> None:
2022-01-04 21:19:06 +00:00
print('test_httpsig_new(' + str(with_digest) + ')')
debug = True
2021-12-25 16:17:53 +00:00
path = base_dir + '/.testHttpsigBaseNew'
if os.path.isdir(path):
shutil.rmtree(path, ignore_errors=False, onerror=None)
os.mkdir(path)
os.chdir(path)
2021-12-26 15:32:00 +00:00
content_type = 'application/activity+json'
nickname = 'socrates'
2022-01-04 20:14:19 +00:00
host_domain = 'someother.instance'
domain = 'argumentative.social'
2021-12-25 17:09:22 +00:00
http_prefix = 'https'
port = 5576
password = 'SuperSecretPassword'
2022-01-04 20:14:19 +00:00
private_key_pem, public_key_pem, _, _ = \
2021-12-29 21:55:09 +00:00
create_person(path, nickname, domain, port, http_prefix,
False, False, password)
2022-01-04 20:14:19 +00:00
assert private_key_pem
if with_digest:
message_body_json = {
"a key": "a value",
"another key": "A string",
"yet another key": "Another string"
}
2022-01-04 20:14:19 +00:00
message_body_json_str = json.dumps(message_body_json)
else:
2022-01-04 20:14:19 +00:00
message_body_json_str = ''
2022-01-04 20:14:19 +00:00
headers_domain = get_full_domain(host_domain, port)
2022-01-04 20:14:19 +00:00
date_str = strftime("%a, %d %b %Y %H:%M:%S %Z", gmtime())
boxpath = '/inbox'
2022-01-04 20:14:19 +00:00
if not with_digest:
headers = {
2022-01-04 20:14:19 +00:00
'host': headers_domain,
'date': date_str,
2021-12-26 15:32:00 +00:00
'accept': content_type
}
2022-01-04 20:14:19 +00:00
signature_index_header, signature_header = \
sign_post_headers_new(date_str, private_key_pem, nickname,
2021-12-29 21:55:09 +00:00
domain, port,
2022-01-04 20:14:19 +00:00
host_domain, port,
boxpath, http_prefix, message_body_json_str,
algorithm, digest_algorithm, debug)
else:
2022-01-04 20:14:19 +00:00
digest_prefix = get_digest_prefix(digest_algorithm)
body_digest = \
message_content_digest(message_body_json_str, digest_algorithm)
content_length = len(message_body_json_str)
headers = {
2022-01-04 20:14:19 +00:00
'host': headers_domain,
'date': date_str,
'digest': f'{digest_prefix}={body_digest}',
2021-12-26 15:32:00 +00:00
'content-type': content_type,
2022-01-04 20:14:19 +00:00
'content-length': str(content_length)
}
2022-01-04 20:14:19 +00:00
assert get_digest_algorithm_from_headers(headers) == digest_algorithm
signature_index_header, signature_header = \
sign_post_headers_new(date_str, private_key_pem, nickname,
domain, port, host_domain, port,
boxpath, http_prefix, message_body_json_str,
algorithm, digest_algorithm, debug)
headers['signature'] = signature_header
headers['signature-input'] = signature_index_header
print('headers: ' + str(headers))
2022-01-04 20:14:19 +00:00
getreq_method = not with_digest
debug = True
2022-01-04 20:14:19 +00:00
assert verify_post_headers(http_prefix, public_key_pem, headers,
boxpath, getreq_method, None,
message_body_json_str, debug)
debug = False
2022-01-04 20:14:19 +00:00
if with_digest:
# everything correct except for content-length
2022-01-04 20:14:19 +00:00
headers['content-length'] = str(content_length + 2)
assert verify_post_headers(http_prefix, public_key_pem, headers,
boxpath, getreq_method, None,
message_body_json_str, debug) is False
assert verify_post_headers(http_prefix, public_key_pem, headers,
'/parambulator' + boxpath, getreq_method, None,
message_body_json_str, debug) is False
assert verify_post_headers(http_prefix, public_key_pem, headers,
boxpath, not getreq_method, None,
message_body_json_str, debug) is False
if not with_digest:
# fake domain
headers = {
'host': 'bogon.domain',
2022-01-04 20:14:19 +00:00
'date': date_str,
2021-12-26 15:32:00 +00:00
'content-type': content_type
}
else:
# correct domain but fake message
2022-01-04 20:14:19 +00:00
message_body_json_str = \
'{"a key": "a value", "another key": "Fake GNUs", ' + \
'"yet another key": "More Fake GNUs"}'
2022-01-04 20:14:19 +00:00
content_length = len(message_body_json_str)
digest_prefix = get_digest_prefix(digest_algorithm)
body_digest = \
message_content_digest(message_body_json_str, digest_algorithm)
headers = {
'host': domain,
2022-01-04 20:14:19 +00:00
'date': date_str,
'digest': f'{digest_prefix}={body_digest}',
2021-12-26 15:32:00 +00:00
'content-type': content_type,
2022-01-04 20:14:19 +00:00
'content-length': str(content_length)
}
2022-01-04 20:14:19 +00:00
assert get_digest_algorithm_from_headers(headers) == digest_algorithm
headers['signature'] = signature_header
headers['signature-input'] = signature_index_header
pprint(headers)
2022-01-04 20:14:19 +00:00
assert verify_post_headers(http_prefix, public_key_pem, headers,
boxpath, not getreq_method, None,
message_body_json_str, False) is False
2021-12-25 16:17:53 +00:00
os.chdir(base_dir)
shutil.rmtree(path, ignore_errors=False, onerror=None)
2021-12-29 21:55:09 +00:00
def _test_get_actor_from_in_reply_to() -> None:
print('test_get_actor_from_in_reply_to')
2022-01-04 20:14:19 +00:00
in_reply_to = \
'https://fosstodon.org/users/bashrc/statuses/107400700612621140'
2022-01-04 20:14:19 +00:00
reply_actor = get_actor_from_in_reply_to(in_reply_to)
assert reply_actor == 'https://fosstodon.org/users/bashrc'
2022-01-04 20:14:19 +00:00
in_reply_to = 'https://fosstodon.org/activity/107400700612621140'
reply_actor = get_actor_from_in_reply_to(in_reply_to)
assert reply_actor is None
2022-02-12 20:37:15 +00:00
def _test_xml_podcast_dict(base_dir: str) -> None:
print('test_xml_podcast_dict')
xml_str = \
'<?xml version="1.0" encoding="UTF-8" ?>\n' + \
'<rss version="2.0" xmlns:podcast="' + \
'https://podcastindex.org/namespace/1.0">\n' + \
'<podcast:episode>5</podcast:episode>\n' + \
'<podcast:chapters ' + \
'url="https://whoframed.rodger/ep1_chapters.json" ' + \
'type="application/json"/>\n' + \
'<podcast:funding ' + \
'url="https://whoframed.rodger/donate">' + \
'Support the show</podcast:funding>\n' + \
'<podcast:images ' + \
'srcset="https://whoframed.rodger/images/ep1/' + \
'pci_avatar-massive.jpg 1500w, ' + \
'https://whoframed.rodger/images/ep1/pci_avatar-middle.jpg 600w, ' + \
'https://whoframed.rodger/images/ep1/pci_avatar-small.jpg 300w, ' + \
'https://whoframed.rodger/images/ep1/' + \
'pci_avatar-microfiche.jpg 50w" />\n' + \
'<podcast:location geo="geo:57.4272,34.63763" osm="R472152">' + \
'Nowheresville</podcast:location>\n' + \
'<podcast:locked owner="podcastowner@whoframed.rodger">yes' + \
'</podcast:locked>\n' + \
'<podcast:person group="visuals" role="cover art designer" ' + \
'href="https://whoframed.rodger/artist/rodgetrabbit">' + \
'Rodger Rabbit</podcast:person>\n' + \
'<podcast:person href="https://whoframed.rodger" ' + \
'img="http://whoframed.rodger/images/rr.jpg">Rodger Rabbit' + \
'</podcast:person>\n' + \
'<podcast:person href="https://whoframed.rodger" ' + \
'img="http://whoframed.rodger/images/jr.jpg">' + \
'Jessica Rabbit</podcast:person>\n' + \
'<podcast:person role="guest" ' + \
'href="https://whoframed.rodger/blog/bettyboop/" ' + \
'img="http://whoframed.rodger/images/bb.jpg">' + \
'Betty Boop</podcast:person>\n' + \
'<podcast:person role="guest" ' + \
'href="https://goodto.talk/bobhoskins/" ' + \
'img="https://goodto.talk/images/bhosk.jpg">' + \
'Bob Hoskins</podcast:person>\n' + \
'<podcast:season name="Podcasting 2.0">1</podcast:season>\n' + \
'<podcast:soundbite startTime="15.27" duration="8.0" />\n' + \
'<podcast:soundbite startTime="21.34" duration="32.0" />\n' + \
'<podcast:transcript ' + \
'url="https://whoframed.rodger/ep1/transcript.txt" ' + \
'type="text/plain" />\n' + \
'<podcast:transcript ' + \
'url="https://whoframed.rodger/ep2/transcript.txt" ' + \
'type="text/plain" />\n' + \
'<podcast:transcript ' + \
'url="https://whoframed.rodger/ep3/transcript.txt" ' + \
'type="text/plain" />\n' + \
'<podcast:value type="donate" method="keysend" ' + \
'suggested="2.95">\n' + \
' <podcast:valueRecipient name="hosting company" ' + \
'type="node" address="someaddress1" split="1" />\n' + \
' <podcast:valueRecipient name="podcaster" type="node" ' + \
'address="someaddress2" split="99" />\n' + \
'</podcast:value>\n' + \
'</rss>'
2022-02-12 20:37:15 +00:00
podcast_properties = xml_podcast_to_dict(base_dir, xml_str, xml_str)
assert podcast_properties
2022-04-29 13:54:13 +00:00
pprint(podcast_properties)
assert podcast_properties.get('valueRecipients')
assert podcast_properties.get('persons')
assert podcast_properties.get('soundbites')
assert podcast_properties.get('locations')
assert podcast_properties.get('transcripts')
assert podcast_properties.get('episode')
assert podcast_properties.get('funding')
assert int(podcast_properties['episode']) == 5
assert podcast_properties['funding']['text'] == "Support the show"
2022-01-11 18:25:13 +00:00
assert podcast_properties['funding']['url'] == \
"https://whoframed.rodger/donate"
assert len(podcast_properties['transcripts']) == 3
assert len(podcast_properties['valueRecipients']) == 2
assert len(podcast_properties['persons']) == 5
assert len(podcast_properties['locations']) == 1
def _test_link_from_rss_item() -> None:
2022-01-12 17:23:13 +00:00
print('test_get_link_from_rssitem')
rss_item = \
'<link>' + \
'https://anchor.fm/creativecommons/episodes/' + \
'Hessel-van-Oorschot-of-Tribe-of-Noise--Free-Music-Archive-e1crvce' + \
'</link>\n' + \
'<pubDate>Wed, 12 Jan 2022 14:28:46 GMT</pubDate>\n' + \
2022-01-12 17:23:13 +00:00
'<enclosure url="https://anchor.fm/s/4d70d828/podcast/' + \
'play/46054222/https%3A%2F%2Fd3ctxlq1ktw2nl.cloudfront.net' + \
'%2Fstaging%2F2022-0-12%2F7352f28c-a928-ea7a-65ae-' + \
'ccb5edffbac1.mp3" length="67247880" type="audio/mpeg"/>\n' + \
'<podcast:alternateEnclosure type="audio/mpeg" ' + \
'length="27800000" bitrate="128000" default="true" ' + \
'title="Standard">\n' + \
'<podcast:source uri="https://whoframed.rodger/rabbit.mp3" />\n' + \
'<podcast:source uri="http://randomaddress.onion/rabbit.mp3" />\n' + \
'<podcast:source uri="http://randomaddress.i2p/rabbit.mp3" />\n' + \
'</podcast:alternateEnclosure>\n' + \
'<podcast:alternateEnclosure type="audio/opus" ' + \
'length="19200000" bitrate="128000" ' + \
'title="High Quality">\n' + \
'<podcast:source uri="https://whoframed.rodger/rabbit.opus" />\n' + \
'<podcast:source uri="http://randomaddress.onion/rabbit.opus" />\n' + \
'<podcast:source uri="http://randomaddress.i2p/rabbit.opus" />\n' + \
'</podcast:alternateEnclosure>\n'
link, mime_type = get_link_from_rss_item(rss_item, None, None)
2022-01-12 17:23:13 +00:00
assert link
assert link.endswith('1.mp3')
2022-01-12 18:35:15 +00:00
assert mime_type
assert mime_type == 'audio/mpeg'
2022-01-12 17:23:13 +00:00
2022-04-22 14:27:25 +00:00
link, mime_type = get_link_from_rss_item(rss_item, ['audio/mp3'], None)
assert link
assert link.endswith('1.mp3')
assert mime_type
assert mime_type == 'audio/mpeg'
link, mime_type = get_link_from_rss_item(rss_item, ['audio/mpeg'], None)
assert link
assert link == 'https://whoframed.rodger/rabbit.mp3'
assert mime_type
assert mime_type == 'audio/mpeg'
link, mime_type = get_link_from_rss_item(rss_item, ['audio/opus'], None)
assert mime_type
if mime_type != 'audio/opus':
print('mime_type: ' + mime_type)
assert mime_type == 'audio/opus'
assert link
assert link == 'https://whoframed.rodger/rabbit.opus'
link, mime_type = get_link_from_rss_item(rss_item, ['audio/opus'], 'tor')
assert mime_type
if mime_type != 'audio/opus':
print('mime_type: ' + mime_type)
assert mime_type == 'audio/opus'
assert link
assert link == 'http://randomaddress.onion/rabbit.opus'
2022-01-12 17:23:13 +00:00
rss_item = \
'<link>' + \
'https://anchor.fm/creativecommons/episodes/' + \
'Hessel-van-Oorschot-of-Tribe-of-Noise--Free-Music-Archive-e1crvce' + \
'</link>' + \
'<pubDate>Wed, 12 Jan 2022 14:28:46 GMT</pubDate>'
link, mime_type = get_link_from_rss_item(rss_item, None, None)
2022-01-12 17:23:13 +00:00
assert link
assert link.startswith('https://anchor.fm')
2022-01-12 18:35:15 +00:00
assert not mime_type
2022-01-12 17:23:13 +00:00
2022-01-13 22:37:53 +00:00
rss_item = \
'<link href="' + \
'https://test.link/creativecommons/episodes/' + \
'Hessel-van-Oorschot-of-Tribe-of-Noise--Free-Music-Archive-e1crvce' + \
'"/>' + \
'<pubDate>Wed, 12 Jan 2022 14:28:46 GMT</pubDate>'
link, mime_type = get_link_from_rss_item(rss_item, None, None)
2022-01-13 22:37:53 +00:00
assert link
assert link.startswith('https://test.link/creativecommons')
2022-01-12 17:23:13 +00:00
2022-01-14 10:20:37 +00:00
def _test_safe_webtext() -> None:
print('test_safe_webtext')
web_text = '<p>Some text including a link https://some.site/some-path</p>'
expected_text = 'Some text including a link ' + \
'<a href="https://some.site/some-path"'
safe_text = safe_web_text(web_text)
if expected_text not in safe_text:
print('Original html: ' + web_text)
print('Expected html: ' + expected_text)
print('Actual html: ' + safe_text)
assert expected_text in safe_text
assert '<p>' not in safe_text
assert '</p>' not in safe_text
web_text = 'Some text with <script>some script</script>'
expected_text = 'Some text with some script'
safe_text = safe_web_text(web_text)
if expected_text != safe_text:
print('Original html: ' + web_text)
print('Expected html: ' + expected_text)
print('Actual html: ' + safe_text)
assert expected_text == safe_text
2022-02-25 21:00:53 +00:00
def _test_published_to_local_timezone() -> None:
print('published_to_local_timezone')
published_str = '2022-02-25T20:15:00Z'
timezone = 'Europe/Berlin'
published = \
datetime.datetime.strptime(published_str, "%Y-%m-%dT%H:%M:%SZ")
datetime_object = \
convert_published_to_local_timezone(published, timezone)
local_time_str = datetime_object.strftime("%a %b %d, %H:%M")
assert local_time_str == 'Fri Feb 25, 21:15'
timezone = 'Asia/Seoul'
published = \
datetime.datetime.strptime(published_str, "%Y-%m-%dT%H:%M:%SZ")
datetime_object = \
convert_published_to_local_timezone(published, timezone)
local_time_str = datetime_object.strftime("%a %b %d, %H:%M")
assert local_time_str == 'Sat Feb 26, 05:15'
2022-03-24 13:14:41 +00:00
def _test_bold_reading() -> None:
print('bold_reading')
text = "This is a test of emboldening."
text_bold = bold_reading_string(text)
expected = \
"<b>Th</b>is <b>i</b>s a <b>te</b>st <b>o</b>f " + \
"<b>embold</b>ening."
if text_bold != expected:
print(text_bold)
assert text_bold == expected
text = "<p>This is a test of emboldening with paragraph.<p>"
text_bold = bold_reading_string(text)
expected = \
"<p><b>Th</b>is <b>i</b>s a <b>te</b>st <b>o</b>f " + \
"<b>embold</b>ening <b>wi</b>th <b>parag</b>raph.</p>"
2022-03-24 13:14:41 +00:00
if text_bold != expected:
print(text_bold)
assert text_bold == expected
text = \
"<p>This is a test of emboldening</p>" + \
"<p>With more than one paragraph.<p>"
text_bold = bold_reading_string(text)
expected = \
"<p><b>Th</b>is <b>i</b>s a <b>te</b>st <b>o</b>f " + \
"<b>embold</b>ening</p><p><b>Wi</b>th <b>mo</b>re " + \
"<b>th</b>an <b>on</b>e <b>parag</b>raph.</p>"
2022-03-24 13:14:41 +00:00
if text_bold != expected:
print(text_bold)
assert text_bold == expected
2022-03-24 14:08:07 +00:00
text = '<p>This is a test <a class="some class" ' + \
'href="some_url"><label>with markup containing spaces</label></a><p>'
2022-03-24 14:08:07 +00:00
text_bold = bold_reading_string(text)
expected = \
'<p><b>Th</b>is <b>i</b>s a <b>te</b>st ' + \
'<a class="some class" href="some_url"><label>with ' + \
'<b>mar</b>kup <b>conta</b>ining spaces</label></a></p>'
2022-03-24 14:08:07 +00:00
if text_bold != expected:
print(text_bold)
assert text_bold == expected
text = "There&apos;s the quoted text here"
2022-03-24 15:15:53 +00:00
text_bold = bold_reading_string(text)
expected = \
"<b>Ther</b>e's <b>th</b>e <b>quo</b>ted <b>te</b>xt <b>he</b>re"
2022-03-24 15:15:53 +00:00
if text_bold != expected:
print(text_bold)
assert text_bold == expected
text = '<p><span class=\"h-card\"><a ' + \
'href=\"https://something.social/@someone\" ' + \
'class=\"u-url mention\">@<span>Someone or other' + \
'</span></a></span> some text</p>'
text_bold = bold_reading_string(text)
expected = \
'<p><span class="h-card">' + \
'<a href="https://something.social/@someone" ' + \
'class="u-url mention">@<span>Someone <b>o</b>r other' + \
'</span></a></span> <b>so</b>me <b>te</b>xt</p>'
if text_bold != expected:
print(text_bold)
assert text_bold == expected
2022-03-24 13:14:41 +00:00
2022-04-10 19:19:40 +00:00
def _test_diff_content() -> None:
print('diff_content')
prev_content = \
2022-04-11 12:13:04 +00:00
'Some text before.\n' + \
'Starting sentence. This is some content.\nThis is another line.'
2022-04-10 19:19:40 +00:00
content = \
'Some text before.\nThis is some more content.\nThis is another line.'
result = content_diff(content, prev_content)
2022-04-11 12:13:04 +00:00
expected = \
'<p><label class="diff_remove">' + \
'- Starting sentence</label><br><label class="diff_add">' + \
'+ This is some more content</label><br>' + \
'<label class="diff_remove">- This is some content</label></p>'
2022-04-10 19:19:40 +00:00
assert result == expected
2022-04-11 12:13:04 +00:00
content = \
'Some text before.\nThis is content.\nThis line.'
result = content_diff(content, prev_content)
expected = \
'<p><label class="diff_remove">- Starting sentence</label><br>' + \
'<label class="diff_add">+ This is content</label><br>' + \
'<label class="diff_remove">- This is some content</label><br>' + \
'<label class="diff_add">+ This line</label><br>' + \
'<label class="diff_remove">- This is another line</label></p>'
assert result == expected
system_language = "en"
2022-04-11 12:13:04 +00:00
translate = {
"SHOW EDITS": "SHOW EDITS"
}
timezone = 'Europe/Berlin'
content1 = \
"<p>This is some content.</p>" + \
"<p>Some other content.</p>"
content2 = \
"<p>This is some previous content.</p>" + \
"<p>Some other previous content.</p>"
content3 = \
"<p>This is some more previous content.</p>" + \
"<p>Some other previous content.</p>"
post_json_object = {
"object": {
"content": content1,
"published": "2020-12-14T00:08:06Z"
}
}
edits_json = {
"2020-12-14T00:05:19Z": {
"object": {
"content": content3,
"published": "2020-12-14T00:05:19Z"
}
},
"2020-12-14T00:07:34Z": {
"object": {
"contentMap": {
"en": content2
},
2022-04-11 12:13:04 +00:00
"published": "2020-12-14T00:07:34Z"
}
}
}
html_str = \
create_edits_html(edits_json, post_json_object, translate,
timezone, system_language)
2022-04-11 12:13:04 +00:00
assert html_str
expected = \
2022-06-10 19:55:01 +00:00
'<details><summary class="cw" tabindex="10">SHOW EDITS</summary>' + \
2022-04-11 12:13:04 +00:00
'<p><b>Mon Dec 14, 01:07</b></p><p><label class="diff_add">' + \
'+ This is some content</label><br><label class="diff_remove">' + \
'- This is some previous content</label><br>' + \
'<label class="diff_add">+ Some other content</label><br>' + \
'<label class="diff_remove">- Some other previous content' + \
'</label></p><p><b>Mon Dec 14, 01:05</b></p><p>' + \
'<label class="diff_add">+ This is some previous content' + \
'</label><br><label class="diff_remove">' + \
'- This is some more previous content</label></p></details>'
assert html_str == expected
2022-04-10 19:19:40 +00:00
2022-05-18 16:06:26 +00:00
def _test_color_contrast_value(base_dir: str) -> None:
print('test_color_contrast_value')
minimum_color_contrast = 4.5
background = 'black'
foreground = 'white'
contrast = color_contrast(background, foreground)
assert contrast
assert contrast > 20
assert contrast < 22
foreground = 'grey'
contrast = color_contrast(background, foreground)
assert contrast
assert contrast > 5
assert contrast < 6
themes = get_themes_list(base_dir)
for theme_name in themes:
theme_filename = base_dir + '/theme/' + theme_name + '/theme.json'
if not os.path.isfile(theme_filename):
continue
theme_json = load_json(theme_filename)
if not theme_json:
continue
if not theme_json.get('main-fg-color'):
continue
if not theme_json.get('main-bg-color'):
continue
foreground = theme_json['main-fg-color']
background = theme_json['main-bg-color']
contrast = color_contrast(background, foreground)
if contrast is None:
continue
if contrast < minimum_color_contrast:
print('Theme ' + theme_name + ' has not enough color contrast ' +
str(contrast) + ' < ' + str(minimum_color_contrast))
assert contrast >= minimum_color_contrast
print('Color contrast is ok for all themes')
2022-06-21 11:58:50 +00:00
def _test_remove_end_of_line():
print('remove_end_of_line')
text = 'some text\r\n'
expected = 'some text'
assert remove_eol(text) == expected
text = 'some text'
assert remove_eol(text) == expected
2022-07-05 11:37:35 +00:00
def _test_dogwhistles():
print('dogwhistles')
dogwhistles = {
"X-hamstered": "hamsterism",
"gerbil": "rodent",
2022-07-05 19:35:38 +00:00
"*snake": "slither",
"start*end": "something"
2022-07-05 11:37:35 +00:00
}
content = 'This text does not contain any dogwhistles'
2022-07-05 12:30:21 +00:00
assert not detect_dogwhistles(content, dogwhistles)
2022-07-05 11:37:35 +00:00
content = 'A gerbil named joe'
2022-07-05 12:30:21 +00:00
assert detect_dogwhistles(content, dogwhistles)
2022-07-05 16:21:48 +00:00
content = 'A rattlesnake.'
assert detect_dogwhistles(content, dogwhistles)
2022-07-05 19:35:38 +00:00
content = 'A startthingend.'
assert detect_dogwhistles(content, dogwhistles)
2022-07-05 11:37:35 +00:00
content = 'This content is unhamstered and yhamstered.'
2022-07-05 12:30:21 +00:00
result = detect_dogwhistles(content, dogwhistles)
2022-07-05 11:37:35 +00:00
assert result
assert result.get('hamstered')
assert result['hamstered']['count'] == 2
assert result['hamstered']['category'] == "hamsterism"
2022-07-09 10:37:33 +00:00
def _test_text_standardize():
print('text_standardize')
expected = 'This is a test'
result = standardize_text(expected)
if result != expected:
print(result)
assert result == expected
text = '𝔗𝔥𝔦𝔰 𝔦𝔰 𝔞 𝔱𝔢𝔰𝔱'
result = standardize_text(text)
if result != expected:
print(result)
assert result == expected
text = '𝕿𝖍𝖎𝖘 𝖎𝖘 𝖆 𝖙𝖊𝖘𝖙'
result = standardize_text(text)
if result != expected:
print(result)
assert result == expected
text = '𝓣𝓱𝓲𝓼 𝓲𝓼 𝓪 𝓽𝓮𝓼𝓽'
result = standardize_text(text)
if result != expected:
print(result)
assert result == expected
text = '𝒯𝒽𝒾𝓈 𝒾𝓈 𝒶 𝓉𝑒𝓈𝓉'
result = standardize_text(text)
if result != expected:
print(result)
assert result == expected
text = '𝕋𝕙𝕚𝕤 𝕚𝕤 𝕒 𝕥𝕖𝕤𝕥'
result = standardize_text(text)
if result != expected:
print(result)
assert result == expected
text = ' '
result = standardize_text(text)
if result != expected:
print(result)
assert result == expected
def _test_combine_lines():
print('combine_lines')
text = 'This is a test'
expected = text
result = combine_textarea_lines(text)
if result != expected:
print('expected: ' + expected)
print('result: ' + result)
assert result == expected
text = 'First line.\n\nSecond line.'
2022-07-11 15:13:39 +00:00
expected = 'First line.</p><p>Second line.'
result = combine_textarea_lines(text)
if result != expected:
print('expected: ' + expected)
print('result: ' + result)
assert result == expected
text = 'First\nline.\n\nSecond\nline.'
2022-07-11 15:13:39 +00:00
expected = 'First line.</p><p>Second line.'
result = combine_textarea_lines(text)
if result != expected:
print('expected: ' + expected)
print('result: ' + result)
assert result == expected
# with extra space
text = 'First\nline.\n\nSecond \nline.'
2022-07-11 15:13:39 +00:00
expected = 'First line.</p><p>Second line.'
result = combine_textarea_lines(text)
if result != expected:
print('expected: ' + expected)
print('result: ' + result)
assert result == expected
2022-07-11 15:52:50 +00:00
text = 'Introduction blurb.\n\n* List item 1\n' + \
'* List item 2\n* List item 3\n\nFinal blurb.'
expected = 'Introduction blurb.</p><p>* List item 1\n' + \
'* List item 2\n* List item 3</p><p>Final blurb.'
result = combine_textarea_lines(text)
if result != expected:
print('expected: ' + expected)
print('result: ' + result)
assert result == expected
def _test_hashtag_maps():
print('hashtag_maps')
content = \
"<p>This is a test, with a couple of links and a " + \
"<a href=\"https://epicyon.libreserver.org/tags/Hashtag\" " + \
"class=\"mention hashtag\" rel=\"tag\" tabindex=\"10\">#" + \
"<span>Hashtag</span></a><br><br>" + \
"<a href=\"https://" + \
"www.openstreetmap.org/#map=19/52.90860/-3.59917\" " + \
"tabindex=\"10\" rel=\"nofollow noopener noreferrer\" " + \
"target=\"_blank\"><span class=\"invisible\">https://" + \
"</span><span class=\"ellipsis\">" + \
"www.openstreetmap.org/#map=19/52.90860/-</span><span " + \
"class=\"invisible\">3.59917</span></a><br><br>" + \
"<a href=\"https://" + \
"www.google.com/maps/@52.217291,-3.0811865,20.04z\" " + \
"tabindex=\"10\" rel=\"nofollow noopener noreferrer\" " + \
"target=\"_blank\"><span class=\"invisible\">" + \
"https://</span><span class=\"ellipsis\">" + \
"www.google.com/maps/@52.217291,-3.081186</span>" + \
"<span class=\"invisible\">5,20.04z</span></a><br><br>" + \
"<a href=\"https://" + \
"epicyon.libreserver.org/tags/AnotherHashtag\" " + \
"class=\"mention hashtag\" rel=\"tag\" tabindex=\"10\">#" + \
"<span>AnotherHashtag</span></a></p>"
map_links = get_map_links_from_post_content(content)
link = "www.google.com/maps/@52.217291,-3.0811865,20.04z"
assert link in map_links
2022-08-22 20:44:14 +00:00
zoom, latitude, longitude = geocoords_from_map_link(link)
2022-08-22 20:49:27 +00:00
assert zoom == 20
2022-08-22 20:44:14 +00:00
assert latitude
2022-08-22 20:49:27 +00:00
assert int(latitude * 1000) == 52217
2022-08-22 20:44:14 +00:00
assert longitude
2022-08-22 20:49:27 +00:00
assert int(longitude * 1000) == -3081
link = "www.openstreetmap.org/#map=19/52.90860/-3.59917"
assert link in map_links
2022-08-22 20:44:14 +00:00
zoom, latitude, longitude = geocoords_from_map_link(link)
2022-08-22 20:49:27 +00:00
assert zoom == 19
2022-08-22 20:44:14 +00:00
assert latitude
2022-08-22 20:49:27 +00:00
assert int(latitude * 1000) == 52908
2022-08-22 20:44:14 +00:00
assert longitude
2022-08-22 20:49:27 +00:00
assert int(longitude * 1000) == -3599
assert len(map_links) == 2
2022-09-25 17:26:11 +00:00
def _test_uninvert():
print('test_uninvert')
2022-09-26 09:37:44 +00:00
text = 'ʇsƎʇ ɐ sı sıɥ⊥'
expected = "This is a tEst"
2022-09-25 17:26:11 +00:00
result = remove_inverted_text(text, 'en')
if result != expected:
print('text: ' + text)
print('expected: ' + expected)
print('result: ' + result)
assert result == expected
2022-10-05 17:55:24 +00:00
text = '🅻🅴🆅🅸🅰🆃🅰🆁 abc'
expected = "LEVIATAR abc"
result = remove_square_capitals(text, 'en')
if result != expected:
print('expected: ' + expected)
print('result: ' + result)
print('text: ' + text)
assert result == expected
2022-09-25 19:47:15 +00:00
text = '<p>Some ordinary text</p><p>ʇsǝʇ ɐ sı sıɥʇ</p>'
expected = "<p>Some ordinary text</p><p>this is a test</p>"
result = remove_inverted_text(text, 'en')
if result != expected:
print('text: ' + text)
print('expected: ' + expected)
print('result: ' + result)
assert result == expected
2022-09-25 17:26:11 +00:00
2021-12-29 21:55:09 +00:00
def run_all_tests():
2022-03-29 21:10:09 +00:00
base_dir = os.getcwd()
2019-06-30 21:20:02 +00:00
print('Running tests...')
2021-12-29 21:55:09 +00:00
update_default_themes_list(os.getcwd())
2021-12-30 14:03:49 +00:00
_test_source_contains_no_tabs()
2021-12-29 21:55:09 +00:00
_translate_ontology(base_dir)
_test_get_price_from_string()
2021-12-30 12:23:55 +00:00
_test_post_variable_names()
2021-12-30 13:56:38 +00:00
_test_config_param_names()
_test_post_field_names('daemon.py', ['fields', 'actor_json'])
_test_post_field_names('theme.py', ['config_json'])
2022-01-02 19:57:50 +00:00
_test_post_field_names('inbox.py',
['queue_json', 'post_json_object',
'message_json', 'liked_post_json'])
2021-12-30 13:56:38 +00:00
_test_checkbox_names()
_test_thread_functions()
2021-12-29 21:55:09 +00:00
_test_functions()
2022-09-25 17:26:11 +00:00
_test_uninvert()
_test_hashtag_maps()
_test_combine_lines()
2022-07-09 10:37:33 +00:00
_test_text_standardize()
2022-07-05 11:37:35 +00:00
_test_dogwhistles()
2022-06-21 11:58:50 +00:00
_test_remove_end_of_line()
2022-05-24 09:50:42 +00:00
_test_translation_labels()
2022-05-18 16:06:26 +00:00
_test_color_contrast_value(base_dir)
2022-04-10 19:19:40 +00:00
_test_diff_content()
2022-03-24 13:14:41 +00:00
_test_bold_reading()
2022-02-25 21:00:53 +00:00
_test_published_to_local_timezone()
2022-01-14 10:20:37 +00:00
_test_safe_webtext()
_test_link_from_rss_item()
2022-02-12 20:37:15 +00:00
_test_xml_podcast_dict(base_dir)
2021-12-29 21:55:09 +00:00
_test_get_actor_from_in_reply_to()
_test_valid_emoji_content()
_test_add_cw_lists(base_dir)
_test_word_similarity()
_test_seconds_between_publish()
_test_sign_and_verify()
_test_danger_svg(base_dir)
_test_can_replyto(base_dir)
_test_date_conversions()
_test_authorized_shared_items()
_test_valid_password()
_test_get_links_from_content()
_test_set_actor_language()
_test_limit_repeted_words()
_test_word_lengths_limit()
_test_switch_word(base_dir)
_test_useragent_domain()
_test_roles()
_test_skills()
_test_spoofed_geolocation()
_test_remove_interactions()
_test_extract_pgp_public_key()
_test_emoji_images()
_test_camel_case_split()
_test_speaker_replace_link()
_test_extract_text_fields_from_post()
_test_markdown_to_html()
_test_valid_hash_tag()
_test_prepare_html_post_nick()
_test_domain_handling()
_test_mastoapi()
_test_links_within_post(base_dir)
_test_reply_to_public_post(base_dir)
_test_mentioned_people(base_dir)
_test_guess_tag_category()
_test_valid_nick()
_test_parse_newswire_feed_date()
2021-12-29 21:55:09 +00:00
_test_first_paragraph_from_string()
_test_newswire_tags()
_test_hashtag_rules()
_test_strip_html_tag()
_test_replace_email_quote()
_test_constant_time_string()
_test_translations(base_dir)
_test_valid_content_warning()
_test_remove_id_ending()
_test_json_post_allows_comment()
_run_html_replace_quote_marks()
_test_danger_css(base_dir)
_test_danger_markup()
_test_strip_html()
_test_site_active()
_test_jsonld()
_test_remove_txt_formatting()
_test_web_links()
_test_recent_posts_cache()
_test_theme()
_test_save_load_json()
_test_json_string()
_test_get_status_number()
_test_addemoji(base_dir)
_test_actor_parsing()
_test_httpsig(base_dir)
_test_http_signed_get(base_dir)
_test_http_sig_new('rsa-sha256', 'rsa-sha256')
_test_httpsig_base_new(True, base_dir, 'rsa-sha256', 'rsa-sha256')
_test_httpsig_base_new(False, base_dir, 'rsa-sha256', 'rsa-sha256')
_test_cache()
_test_threads()
_test_create_person_account(base_dir)
_test_authentication(base_dir)
_test_followers_of_person(base_dir)
_test_followers_on_domain(base_dir)
_test_follows(base_dir)
_test_group_followers(base_dir)
2022-02-26 23:34:16 +00:00
time.sleep(2)
2020-03-22 21:16:02 +00:00
print('Tests succeeded\n')