-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from hypothesis/add-functests-to-slack-annotations
Add functests to slack annotations
- Loading branch information
Showing
6 changed files
with
180 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
lint,functests: pytest-httpx | ||
lint,functests: freezegun |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,14 @@ | ||
import json | ||
from datetime import UTC, datetime, timedelta | ||
from subprocess import run | ||
|
||
import httpx | ||
import pytest | ||
from freezegun import freeze_time | ||
|
||
from src.slack_annotations.cli import cli | ||
from src.slack_annotations.core import SEARCH_HOURS | ||
|
||
|
||
def test_help(): | ||
"""Test the slack-annotations --help command.""" | ||
|
@@ -9,3 +18,164 @@ def test_help(): | |
def test_version(): | ||
"""Test the slack-annotations --version command.""" | ||
run(["slack-annotations", "--version"], check=True) | ||
|
||
|
||
@freeze_time("2024-12-01T01:00:00+00:00") | ||
def test_cli_default(search_annotations, slack_annotations, httpx_mock, capsys): | ||
"""Test the slack-annotations with default options.""" | ||
search_after = (datetime.now(UTC) - timedelta(hours=SEARCH_HOURS)).isoformat() | ||
params = { | ||
"sort": "created", | ||
"order": "asc", | ||
"search_after": search_after, | ||
} | ||
httpx_mock.add_response( | ||
url=httpx.URL("https://hypothes.is/api/search", params=params), | ||
content=json.dumps(search_annotations), | ||
) | ||
|
||
cli([]) | ||
|
||
assert capsys.readouterr().out.strip() == json.dumps(slack_annotations) | ||
|
||
|
||
@freeze_time("2024-12-01T01:00:00+00:00") | ||
def test_cli_with_search_after_from_cache_file( | ||
search_annotations, slack_annotations, httpx_mock, tmp_path, capsys | ||
): | ||
"""Test the slack-annotations with a search_after from the cache file.""" | ||
search_after = "2024-12-01T00:30:00+00:00" | ||
params = { | ||
"sort": "created", | ||
"order": "asc", | ||
"search_after": search_after, | ||
} | ||
httpx_mock.add_response( | ||
url=httpx.URL("https://hypothes.is/api/search", params=params), | ||
content=json.dumps(search_annotations), | ||
) | ||
cache_path = tmp_path / "cache.json" | ||
cache_path.write_text(json.dumps({"search_after": search_after})) | ||
|
||
cli(["--cache-path", str(cache_path)]) | ||
|
||
assert capsys.readouterr().out.strip() == json.dumps(slack_annotations) | ||
assert json.loads(cache_path.read_text()) == { | ||
"search_after": "2024-12-03T18:40:42.325652+00:00" | ||
} | ||
|
||
|
||
@freeze_time("2024-12-01T01:00:00+00:00") | ||
def test_cli_token(search_annotations, slack_annotations, httpx_mock, capsys): | ||
"""Test the slack-annotations with a token.""" | ||
search_after = (datetime.now(UTC) - timedelta(hours=SEARCH_HOURS)).isoformat() | ||
params = { | ||
"sort": "created", | ||
"order": "asc", | ||
"search_after": search_after, | ||
} | ||
token = "test-token" | ||
httpx_mock.add_response( | ||
url=httpx.URL("https://hypothes.is/api/search", params=params), | ||
content=json.dumps(search_annotations), | ||
match_headers={"Authorization": f"Bearer {token}"}, | ||
) | ||
|
||
cli(["--token", token]) | ||
|
||
assert capsys.readouterr().out.strip() == json.dumps(slack_annotations) | ||
|
||
|
||
@pytest.fixture | ||
def slack_annotations(): | ||
return { | ||
"text": "2 new annotations", | ||
"blocks": [ | ||
{ | ||
"type": "section", | ||
"text": { | ||
"type": "mrkdwn", | ||
"text": "`test_user_1` (md............................) annotated <https://example.com/|Annotating the law | Hypothes.is>:", | ||
}, | ||
"fields": [ | ||
{"type": "mrkdwn", "text": "*Quote:*"}, | ||
{ | ||
"type": "mrkdwn", | ||
"text": "*Annotation* (<https://hyp.is/test_annotation_id_1/example.com/|in-context link>):", | ||
}, | ||
{"type": "plain_text", "text": "(None)"}, | ||
{"type": "plain_text", "text": "test_user_1 reply"}, | ||
], | ||
}, | ||
{"type": "divider"}, | ||
{ | ||
"type": "section", | ||
"text": { | ||
"type": "mrkdwn", | ||
"text": "`test_user_2` (Test User 2) annotated <https://web.hypothes.is/blog/step-by-step-guide-to-using-hypothesis-for-collaborative-projects/|Annotating the law | Hypothes.is>:", | ||
}, | ||
"fields": [ | ||
{"type": "mrkdwn", "text": "*Quote:*"}, | ||
{ | ||
"type": "mrkdwn", | ||
"text": "*Annotation* (<https://hyp.is/test_annotation_id_2/web.hypothes.is/blog/step-by-step-guide-to-using-hypothesis-for-collaborative-projects/|in-context link>):", | ||
}, | ||
{ | ||
"type": "plain_text", | ||
"text": "The ability to collaborate effectively is invaluable in today\u2019s interconnected world. Whether in academic or professional settings, tools that streamline communication, enhance content sharing and promote open dialogue are important. Hypothesis is a standout tool in this regard, especially renowned for its capabilities in group annotations and as an online learning tool.", | ||
}, | ||
{"type": "plain_text", "text": "A useful tool!"}, | ||
], | ||
}, | ||
{"type": "divider"}, | ||
{ | ||
"type": "context", | ||
"elements": [ | ||
{ | ||
"type": "mrkdwn", | ||
"text": "These annotations are posted to Slack by a <https://github.com/hypothesis/slack-annotations/|GitHub Actions workflow>", | ||
} | ||
], | ||
}, | ||
], | ||
} | ||
|
||
|
||
@pytest.fixture | ||
def search_annotations(): | ||
return { | ||
"rows": [ | ||
{ | ||
"created": "2024-12-02T18:34:42.333087+00:00", | ||
"user": "acct:[email protected]", | ||
"uri": "https://example.com/", | ||
"text": "test_user_1 reply", | ||
"document": {"title": ["Annotating the law | Hypothes.is"]}, | ||
"links": { | ||
"incontext": "https://hyp.is/test_annotation_id_1/example.com/" | ||
}, | ||
"user_info": {"display_name": "md............................"}, | ||
}, | ||
{ | ||
"created": "2024-12-03T18:40:42.325652+00:00", | ||
"user": "acct:[email protected]", | ||
"uri": "https://web.hypothes.is/blog/step-by-step-guide-to-using-hypothesis-for-collaborative-projects/", | ||
"text": "A useful tool!", | ||
"target": [ | ||
{ | ||
"source": "https://web.hypothes.is/blog/step-by-step-guide-to-using-hypothesis-for-collaborative-projects/", | ||
"selector": [ | ||
{ | ||
"exact": "The ability to collaborate effectively is invaluable in today’s interconnected world. Whether in academic or professional settings, tools that streamline communication, enhance content sharing and promote open dialogue are important. Hypothesis is a standout tool in this regard, especially renowned for its capabilities in group annotations and as an online learning tool." | ||
}, | ||
], | ||
} | ||
], | ||
"document": {"title": ["Annotating the law | Hypothes.is"]}, | ||
"links": { | ||
"incontext": "https://hyp.is/test_annotation_id_2/web.hypothes.is/blog/step-by-step-guide-to-using-hypothesis-for-collaborative-projects/" | ||
}, | ||
"user_info": {"display_name": "Test User 2"}, | ||
}, | ||
] | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters