# retoor import http.server import json import threading import time from contextlib import contextmanager from devplacepy.database import get_setting, set_setting from tests.conftest import BASE_URL, CACHE_VERSION_PROPAGATION_SECONDS from tests.e2e.components.title import _create_post GIF_BYTES = ( b"GIF89a\x01\x00\x01\x00\x80\x00\x00\x00\x00\x00\xff\xff\xff" b"\x21\xf9\x04\x01\x00\x00\x00\x00\x2c\x00\x00\x00\x00\x01\x00" b"\x01\x00\x00\x02\x02\x44\x01\x00\x3b" ) SETTING_KEYS = ("giphy_api_key", "giphy_base_url", "giphy_rating") TITLES = {"gif1": "happy dance", "gif2": "sad cat", "gif3": "dance party"} PICKER = "dp-gif-picker .gif-toggle-btn" PANEL = "dp-gif-picker .gif-picker-wrapper" ITEM = "dp-gif-picker .gif-picker-item" def _gif_item(port, gif_id): media = f"http://127.0.0.1:{port}/media/{gif_id}.gif" return { "id": gif_id, "title": TITLES[gif_id], "url": f"https://giphy.test/gifs/{gif_id}", "images": { "fixed_width": {"url": media, "width": "200", "height": "150", "size": "10"}, "original": {"url": media, "width": "480", "height": "360", "size": "10"}, }, } def _fake_giphy(): holder = {} class Handler(http.server.BaseHTTPRequestHandler): def _send(self, status, body, content_type): self.send_response(status) self.send_header("Content-Type", content_type) self.send_header("Content-Length", str(len(body))) self.end_headers() self.wfile.write(body) def do_GET(self): port = holder["server"].server_port path, _, query = self.path.partition("?") params = dict(pair.split("=", 1) for pair in query.split("&") if "=" in pair) if path.startswith("/media/"): return self._send(200, GIF_BYTES, "image/gif") ids = list(TITLES) if path.endswith("/search"): needle = params.get("q", "").replace("+", " ").lower() ids = [i for i in ids if needle in TITLES[i]] if path.endswith("/search") or path.endswith("/trending"): body = {"data": [_gif_item(port, i) for i in ids]} else: body = {"data": _gif_item(port, path.rsplit("/", 1)[-1])} self._send(200, json.dumps(body).encode(), "application/json") def log_message(self, *args): return server = http.server.ThreadingHTTPServer(("127.0.0.1", 0), Handler) holder["server"] = server threading.Thread(target=server.serve_forever, daemon=True).start() return server @contextmanager def _giphy(key="e2e-giphy-key"): server = _fake_giphy() originals = {name: get_setting(name, "") for name in SETTING_KEYS} try: set_setting("giphy_api_key", key) set_setting("giphy_base_url", f"http://127.0.0.1:{server.server_port}/v1/gifs") set_setting("giphy_rating", "pg") time.sleep(CACHE_VERSION_PROPAGATION_SECONDS) yield server finally: for name, value in originals.items(): set_setting(name, value) server.shutdown() server.server_close() time.sleep(CACHE_VERSION_PROPAGATION_SECONDS) def _stub_attach(page): uploaded = page.request.post( f"{BASE_URL}/uploads/upload", multipart={"file": {"name": "party.gif", "mimeType": "image/gif", "buffer": GIF_BYTES}}, headers={"Accept": "application/json"}, ) assert uploaded.ok, uploaded.text() record = uploaded.json() calls = [] def fulfil(route): calls.append(route.request.post_data_json) route.fulfill(status=201, content_type="application/json", body=json.dumps(record)) page.route("**/giphy/attach", fulfil) return record, calls def _open_post_with_comment_form(page): _create_post(page, "A post that will receive a GIF comment.", "Giphy host post") page.locator(".comment-form textarea").first.wait_for(state="visible") def test_picker_is_hidden_when_giphy_is_not_configured(alice): page, _ = alice with _giphy(key=""): _open_post_with_comment_form(page) page.locator("dp-gif-picker").first.wait_for(state="attached") page.wait_for_function("() => document.querySelector('dp-gif-picker').hidden === true") assert not page.locator(PICKER).first.is_visible() def test_trending_search_and_dismissal(alice): page, _ = alice with _giphy(): _open_post_with_comment_form(page) page.locator(PICKER).first.click() page.locator(PANEL).first.wait_for(state="visible") page.wait_for_function("() => document.querySelectorAll('.gif-picker-item').length === 3") assert "Powered by GIPHY" in page.locator(".gif-picker-credit").first.text_content() page.locator(".gif-picker-search").first.fill("dance") page.wait_for_function("() => document.querySelectorAll('.gif-picker-item').length === 2") page.keyboard.press("Escape") page.locator(PANEL).first.wait_for(state="hidden") page.locator(PICKER).first.click() page.locator(PANEL).first.wait_for(state="visible") page.locator(".page-title, h1").first.click() page.locator(PANEL).first.wait_for(state="hidden") def test_empty_search_says_so(alice): page, _ = alice with _giphy(): _open_post_with_comment_form(page) page.locator(PICKER).first.click() page.locator(".gif-picker-search").first.fill("zzzz-no-such-gif") status = page.locator(".gif-picker-status").first status.wait_for(state="visible") page.wait_for_function("() => document.querySelector('.gif-picker-status').textContent === 'No GIFs found'") assert page.locator(ITEM).count() == 0 def test_picked_gif_becomes_an_attachment_not_markup(alice): page, _ = alice with _giphy(): _open_post_with_comment_form(page) record, calls = _stub_attach(page) form = page.locator(".comment-form").first form.locator("textarea").fill("nice one") page.locator(PICKER).first.click() page.locator(ITEM).first.wait_for(state="visible") page.locator(ITEM).first.click() page.locator(PANEL).first.wait_for(state="hidden") assert calls == [{"gif_id": "gif1", "title": "happy dance"}] assert form.locator("input[name=attachment_uids]").input_value() == record["uid"] assert form.locator("textarea").input_value() == "nice one" form.locator(".comment-form-submit").click() page.wait_for_load_state("domcontentloaded") page.locator(".comment img[src$='.gif']").first.wait_for(state="visible") assert page.locator(".comment img[src$='.gif']").count() == 1 def test_a_second_pick_adds_a_second_attachment(alice): page, _ = alice with _giphy(): _open_post_with_comment_form(page) record, _ = _stub_attach(page) form = page.locator(".comment-form").first for _round in range(2): page.locator(PICKER).first.click() page.locator(ITEM).first.wait_for(state="visible") page.locator(ITEM).first.click() page.locator(PANEL).first.wait_for(state="hidden") assert form.locator("input[name=attachment_uids]").input_value() == record["uid"] def test_composer_has_the_picker_beside_the_attachment_button(alice): page, _ = alice with _giphy(): page.goto(f"{BASE_URL}/feed", wait_until="domcontentloaded") page.locator(".feed-fab").first.wait_for(state="visible", timeout=10000) page.locator(".feed-fab").first.click() actions = page.locator("#create-post-modal .composer-attachments-actions") actions.wait_for(state="visible") actions.locator(".gif-toggle-btn").wait_for(state="visible") assert actions.locator("dp-upload").count() == 1 record, _ = _stub_attach(page) actions.locator(".gif-toggle-btn").click() page.locator(ITEM).first.wait_for(state="visible") page.locator(ITEM).first.click() page.wait_for_function( "(uid) => document.querySelector('#create-post-modal input[name=attachment_uids]').value === uid", arg=record["uid"], ) def test_message_input_has_the_picker(alice, bob): page, _ = alice _, bob_user = bob with _giphy(): page.goto(f"{BASE_URL}/messages?search={bob_user['username']}", wait_until="domcontentloaded") area = page.locator(".messages-input-area") area.wait_for(state="visible") area.locator(".composer-more-btn").click() area.locator(".gif-toggle-btn").wait_for(state="visible") record, _ = _stub_attach(page) area.locator(".gif-toggle-btn").click() page.locator(ITEM).first.wait_for(state="visible") page.locator(ITEM).first.click() page.wait_for_function( "(uid) => document.querySelector('.messages-input-area input[name=attachment_uids]').value === uid", arg=record["uid"], ) def test_panel_fits_a_small_phone(alice): page, _ = alice page.set_viewport_size({"width": 360, "height": 640}) with _giphy(): _open_post_with_comment_form(page) page.locator(PICKER).first.click() panel = page.locator(PANEL).first panel.wait_for(state="visible") page.locator(ITEM).first.wait_for(state="visible") box = panel.bounding_box() assert box["x"] >= 0 assert box["x"] + box["width"] <= 360 assert box["y"] >= 0 assert box["y"] + box["height"] <= 640 def test_message_panel_fits_a_small_phone(alice, bob): page, _ = alice _, bob_user = bob page.set_viewport_size({"width": 360, "height": 640}) with _giphy(): page.goto(f"{BASE_URL}/messages?search={bob_user['username']}", wait_until="domcontentloaded") area = page.locator(".messages-input-area") area.wait_for(state="visible") area.locator(".composer-more-btn").click() area.locator(".gif-toggle-btn").click() panel = page.locator(PANEL).first panel.wait_for(state="visible") page.locator(ITEM).first.wait_for(state="visible") box = panel.bounding_box() assert box["x"] >= 0 assert box["x"] + box["width"] <= 360 assert box["y"] >= 0