Clean up utility functions

This commit is contained in:
Mads Marquart
2020-01-23 16:19:09 +01:00
parent 4015bed474
commit 16081fbb19
8 changed files with 143 additions and 143 deletions

View File

@@ -4,7 +4,7 @@ from fbchat._graphql import ConcatJSONDecoder, queries_to_json, response_to_json
@pytest.mark.parametrize(
"content,result",
"text,result",
[
("", []),
('{"a":"b"}', [{"a": "b"}]),
@@ -12,8 +12,8 @@ from fbchat._graphql import ConcatJSONDecoder, queries_to_json, response_to_json
(' \n{"a": "b" } \n { "b" \n\n : "c" }', [{"a": "b"}, {"b": "c"}]),
],
)
def test_concat_json_decoder(content, result):
assert result == json.loads(content, cls=ConcatJSONDecoder)
def test_concat_json_decoder(text, result):
assert result == json.loads(text, cls=ConcatJSONDecoder)
def test_queries_to_json():

73
tests/test_session.py Normal file
View File

@@ -0,0 +1,73 @@
import datetime
import pytest
from fbchat._session import (
base36encode,
prefix_url,
generate_message_id,
client_id_factory,
is_home,
get_error_data,
)
@pytest.mark.parametrize(
"number,expected",
[(1, "1"), (10, "a"), (123, "3f"), (1000, "rs"), (123456789, "21i3v9")],
)
def test_base36encode(number, expected):
assert base36encode(number) == expected
def test_prefix_url():
assert prefix_url("/") == "https://www.facebook.com/"
assert prefix_url("/abc") == "https://www.facebook.com/abc"
def test_generate_message_id():
# Returns random output, so hard to test more thoroughly
assert generate_message_id(datetime.datetime.utcnow(), "def")
def test_client_id_factory():
# Returns random output, so hard to test more thoroughly
assert client_id_factory()
def test_is_home():
assert not is_home("https://m.facebook.com/login/?...")
assert is_home("https://m.facebook.com/home.php?refsrc=...")
@pytest.mark.skip
def test_get_error_data():
html = """<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Log in to Facebook | Facebook</title>
<meta name="referrer" content="origin-when-crossorigin" id="meta_referrer" />
<style type="text/css">...</style>
<meta name="description" content="..." />
<link rel="canonical" href="https://www.facebook.com/login/" />
</head>
<body tabindex="0" class="b c d e f g">
<div class="h"><div id="viewport">...<div id="objects_container"><div class="g" id="root" role="main">
<table class="x" role="presentation"><tbody><tr><td class="y">
<div class="z ba bb" style="" id="login_error">
<div class="bc">
<span>The password you entered is incorrect. <a href="/recover/initiate/?ars=facebook_login_pw_error&amp;email=abc@mail.com&amp;__ccr=XXX" class="bd" aria-label="Have you forgotten your password?">Did you forget your password?</a></span>
</div>
</div>
...
</td></tr></tbody></table>
<div style="display:none"></div><span><img src="https://facebook.com/security/hsts-pixel.gif" width="0" height="0" style="display:none" /></span>
</div></div><div></div></div></div>
</body>
</html>
"""
url = "https://m.facebook.com/login/?email=abc@mail.com&li=XXX&e=1348092"
msg = "The password you entered is incorrect. Did you forget your password?"
assert (1348092, msg) == get_error_data(html)

View File

@@ -4,13 +4,9 @@ import datetime
from fbchat._util import (
strip_json_cruft,
parse_json,
str_base,
generate_message_id,
get_signature_id,
get_jsmods_require,
mimetype_to_key,
get_url_parameter,
prefix_url,
seconds_to_datetime,
millis_to_datetime,
datetime_to_seconds,
@@ -42,31 +38,6 @@ def test_parse_json_invalid():
parse_json("No JSON object here!")
@pytest.mark.parametrize(
"number,base,expected",
[
(123, 10, "123"),
(1, 36, "1"),
(10, 36, "a"),
(123, 36, "3f"),
(1000, 36, "rs"),
(123456789, 36, "21i3v9"),
],
)
def test_str_base(number, base, expected):
assert str_base(number, base) == expected
def test_generate_message_id():
# Returns random output, so hard to test more thoroughly
generate_message_id("abc")
def test_get_signature_id():
# Returns random output, so hard to test more thoroughly
get_signature_id()
def test_get_jsmods_require_get_image_url():
data = {
"__ar": 1,
@@ -118,15 +89,7 @@ def test_mimetype_to_key():
def test_get_url_parameter():
assert get_url_parameter("http://example.com?a=b&c=d", "c") == "d"
assert get_url_parameter("http://example.com?a=b&a=c", "a") == "b"
with pytest.raises(IndexError):
get_url_parameter("http://example.com", "a")
def test_prefix_url():
assert prefix_url("/") == "https://www.facebook.com/"
assert prefix_url("/abc") == "https://www.facebook.com/abc"
assert prefix_url("abc") == "abc"
assert prefix_url("https://m.facebook.com/abc") == "https://m.facebook.com/abc"
assert get_url_parameter("http://example.com", "a") is None
DT_0 = datetime.datetime(1970, 1, 1, 0, 0, tzinfo=datetime.timezone.utc)