def test_bugs_fails_on_network_error(db): with bugs_session() as bugs: def dud_post(url, auth, json): class _PostReturn: status_code = 400 return _PostReturn() new_config = config.copy() new_config["BUG_TOOL_ENABLED"] = True with patch("couchers.servicers.bugs.config", new_config): with patch("couchers.servicers.bugs.requests.post", dud_post): with pytest.raises(grpc.RpcError) as e: res = bugs.ReportBug( bugs_pb2.ReportBugReq( subject="subject", description="description", results="results", frontend_version="frontend_version", user_agent="user_agent", page="page", user_id=99, )) assert e.value.code() == grpc.StatusCode.INTERNAL
def testconfig(): prevconfig = config.copy() config.clear() config.update(prevconfig) config["DEV"] = True config["VERSION"] = "testing_version" config["BASE_URL"] = "http://*****:*****@couchers.org.invalid" config["REPORTS_EMAIL_RECIPIENT"] = "*****@*****.**" config["SMTP_HOST"] = "localhost" config["SMTP_PORT"] = 587 config["SMTP_USERNAME"] = "******" config["SMTP_PASSWORD"] = "******" config["ENABLE_MEDIA"] = True config["MEDIA_SERVER_SECRET_KEY"] = bytes.fromhex( "91e29bbacc74fa7e23c5d5f34cca5015cb896e338a620003de94a502a461f4bc") config[ "MEDIA_SERVER_BEARER_TOKEN"] = "c02d383897d3b82774ced09c9e17802164c37e7e105d8927553697bf4550e91e" config["MEDIA_SERVER_BASE_URL"] = "http://127.0.0.1:5000" config["BUG_TOOL_ENABLED"] = False config["BUG_TOOL_GITHUB_REPO"] = "user/repo" config["BUG_TOOL_GITHUB_USERNAME"] = "******" config["BUG_TOOL_GITHUB_TOKEN"] = "token" yield None config.clear() config.update(prevconfig)
def test_email_prefix_config(db, monkeypatch): user, token = generate_user() user.new_email = f"{random_hex(12)}@couchers.org.invalid" with patch("couchers.email.queue_email") as mock: send_email_changed_notification_email(user) assert mock.call_count == 1 (sender_name, sender_email, _, subject, _, _), _ = mock.call_args assert sender_name == "Couchers.org" assert sender_email == "*****@*****.**" assert subject == "[TEST] Couchers.org email change requested" new_config = config.copy() new_config["NOTIFICATION_EMAIL_SENDER"] = "TestCo" new_config["NOTIFICATION_EMAIL_ADDRESS"] = "*****@*****.**" new_config["NOTIFICATION_EMAIL_PREFIX"] = "" monkeypatch.setattr(couchers.email, "config", new_config) with patch("couchers.email.queue_email") as mock: send_email_changed_notification_email(user) assert mock.call_count == 1 (sender_name, sender_email, _, subject, _, _), _ = mock.call_args assert sender_name == "TestCo" assert sender_email == "*****@*****.**" assert subject == "Couchers.org email change requested"
def test_process_add_users_to_email_list(db): new_config = config.copy() new_config["MAILCHIMP_ENABLED"] = True new_config["MAILCHIMP_API_KEY"] = "dummy_api_key" new_config["MAILCHIMP_DC"] = "dc99" new_config["MAILCHIMP_LIST_ID"] = "dummy_list_id" with patch("couchers.config.config", new_config): with patch("couchers.jobs.handlers.requests.post") as mock: process_add_users_to_email_list(empty_pb2.Empty()) mock.assert_not_called() generate_user(added_to_mailing_list=False, email="*****@*****.**", name="Tester1") generate_user(added_to_mailing_list=True, email="*****@*****.**", name="Tester2") generate_user(added_to_mailing_list=False, email="*****@*****.**", name="Tester3 von test") with patch("couchers.jobs.handlers.requests.post") as mock: ret = mock.return_value ret.status_code = 200 process_add_users_to_email_list(empty_pb2.Empty()) mock.assert_called_once_with( "https://dc99.api.mailchimp.com/3.0/lists/dummy_list_id", auth=("apikey", "dummy_api_key"), json={ "members": [ { "email_address": "*****@*****.**", "status_if_new": "subscribed", "status": "subscribed", "merge_fields": { "FNAME": "Tester1", }, }, { "email_address": "*****@*****.**", "status_if_new": "subscribed", "status": "subscribed", "merge_fields": { "FNAME": "Tester3 von test", }, }, ] }, ) with patch("couchers.jobs.handlers.requests.post") as mock: process_add_users_to_email_list(empty_pb2.Empty()) mock.assert_not_called()
def test_bugs_with_user(db): user, token = generate_user(username="******") with bugs_session() as bugs: def dud_post(url, auth, json): assert url == "https://api.github.com/repos/org/repo/issues" assert auth == ("user", "token") assert json == { "title": "subject", "body": ("Subject: subject\nDescription:\ndescription\n\nResults:\nresults\n\nBackend version: " + config["VERSION"] + "\nFrontend version: frontend_version\nUser Agent: user_agent\nPage: page\nUser (spoofable): testing_user (1)" ), "labels": ["bug tool"], } class _PostReturn: status_code = 201 def json(self): return {"number": 11} return _PostReturn() new_config = config.copy() new_config["BUG_TOOL_ENABLED"] = True with patch("couchers.servicers.bugs.config", new_config): with patch("couchers.servicers.bugs.requests.post", dud_post): res = bugs.ReportBug( bugs_pb2.ReportBugReq( subject="subject", description="description", results="results", frontend_version="frontend_version", user_agent="user_agent", page="page", user_id=user.id, )) assert res.bug_id == "#11" assert res.bug_url == "https://github.com/org/repo/issues/11"
def test_send_sms(db, monkeypatch): new_config = config.copy() new_config["ENABLE_SMS"] = True new_config["SMS_SENDER_ID"] = "CouchersOrg" monkeypatch.setattr(couchers.phone.sms, "config", new_config) msg_id = random_hex() with patch("couchers.phone.sms.boto3") as mock: sns = Mock() sns.publish.return_value = {"MessageId": msg_id} mock.client.return_value = sns assert couchers.phone.sms.send_sms("+46701740605", "Testing SMS message") == "success" mock.client.assert_called_once_with("sns") sns.publish.assert_called_once_with( PhoneNumber="+46701740605", Message="Testing SMS message", MessageAttributes={ "AWS.SNS.SMS.SMSType": { "DataType": "String", "StringValue": "Transactional" }, "AWS.SNS.SMS.SenderID": { "DataType": "String", "StringValue": "CouchersOrg" }, }, ) with session_scope() as session: sms = session.execute(select(SMS)).scalar_one() assert sms.message_id == msg_id assert sms.sms_sender_id == "CouchersOrg" assert sms.number == "+46701740605" assert sms.message == "Testing SMS message"