File tree Expand file tree Collapse file tree 4 files changed +64
-1
lines changed Expand file tree Collapse file tree 4 files changed +64
-1
lines changed Original file line number Diff line number Diff line change 11fastapi[all]
22pytest
33pip-tools
4+ pytest-httpx
Original file line number Diff line number Diff line change @@ -38,7 +38,9 @@ httpcore==0.16.1
3838httptools == 0.5.0
3939 # via uvicorn
4040httpx == 0.23.1
41- # via fastapi
41+ # via
42+ # fastapi
43+ # pytest-httpx
4244idna == 3.4
4345 # via
4446 # anyio
@@ -69,6 +71,10 @@ pydantic==1.10.2
6971pyparsing == 3.0.9
7072 # via packaging
7173pytest == 7.2.0
74+ # via
75+ # -r requirements.in
76+ # pytest-httpx
77+ pytest-httpx == 0.21.2
7278 # via -r requirements.in
7379python-dotenv == 0.21.0
7480 # via uvicorn
Original file line number Diff line number Diff line change 1+ import httpx
2+ from httpx ._models import Response
3+
4+ from config import get_settings
5+
6+ settings = get_settings ()
7+
8+
9+ class CampfireBot :
10+ def send_message (self , message : str ) -> Response :
11+ """ https://github.com/basecamp/bc3-api/blob/master/sections/chatbots.md#create-a-line """
12+
13+ url = f'https://3.basecampapi.com/{ settings .BASECAMP_ACCOUNT_ID } /integrations/{ settings .BASECAMP_CHATBOT_KEY } /buckets/1/chats/2/lines.json'
14+
15+ response = httpx .post (url , data = {'content' : message })
16+ response .raise_for_status ()
17+
18+ return response
Original file line number Diff line number Diff line change 1+ import pytest
2+
3+ from basecamp .campfire_bot import CampfireBot
4+ from httpx ._exceptions import HTTPStatusError
5+
6+
7+ @pytest .fixture
8+ def success_response (httpx_mock ):
9+ httpx_mock .add_response (
10+ method = 'POST' ,
11+ status_code = 200 ,
12+ )
13+
14+
15+ @pytest .fixture
16+ def bad_response (httpx_mock ):
17+ httpx_mock .add_response (
18+ method = 'POST' ,
19+ status_code = 400 ,
20+ )
21+
22+
23+ @pytest .fixture
24+ def bot ():
25+ return CampfireBot ()
26+
27+
28+ @pytest .mark .usefixtures ('success_response' )
29+ def test_good_response (bot ):
30+ result = bot .send_message ('Hello World!' )
31+
32+ assert result .status_code == 200
33+
34+
35+ @pytest .mark .usefixtures ('bad_response' )
36+ def test_bad_response (bot ):
37+ with pytest .raises (HTTPStatusError ):
38+ bot .send_message ('Hello World!' )
You can’t perform that action at this time.
0 commit comments