Skip to content

Commit 29cc903

Browse files
author
gabino
committed
Refactor test mocks to use respx
1 parent bae7c1e commit 29cc903

15 files changed

Lines changed: 788 additions & 98 deletions

quienesquien/client.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,13 @@ async def _fetch_auth_token(self) -> str:
6666
'Authorization': f'Bearer {self.secret_key}',
6767
'Accept-Encoding': 'identity',
6868
}
69-
70-
response = await self._make_request(
71-
'GET', auth_url, headers=headers, params=params
72-
)
73-
self._auth_token = response.text
69+
try:
70+
response = await self._make_request(
71+
'GET', auth_url, headers=headers, params=params
72+
)
73+
self._auth_token = response.text
74+
except httpx.HTTPStatusError as exc:
75+
raise QuienEsQuienError(exc.response) from exc
7476
return self._auth_token
7577

7678
async def search(
@@ -148,7 +150,7 @@ async def search(
148150
raise InvalidTokenError(exc.response)
149151
if exc.response.status_code == 403:
150152
raise InsufficientBalanceError(exc.response)
151-
raise QuienEsQuienError(exc.response)
153+
raise QuienEsQuienError(exc.response) from exc
152154

153155
response_data = response.json()
154156
if not response_data.get('success', False):
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
interactions:
2+
- request:
3+
body: ''
4+
headers:
5+
Authorization:
6+
- DUMMY_AUTHORIZATION
7+
accept:
8+
- '*/*'
9+
accept-encoding:
10+
- identity
11+
connection:
12+
- keep-alive
13+
host:
14+
- app.q-detect.com
15+
user-agent:
16+
- python-httpx/0.28.1
17+
method: GET
18+
uri: https://app.q-detect.com/api/token?client_id=DUMMY_CLIENT_ID
19+
response:
20+
body:
21+
string: DUMMY_RESPONSE_TOKEN
22+
headers:
23+
Access-Control-Allow-Origin:
24+
- '*'
25+
Cache-Control:
26+
- no-cache, private
27+
Connection:
28+
- keep-alive
29+
Content-Type:
30+
- text/html; charset=UTF-8
31+
Date:
32+
- Thu, 20 Feb 2025 15:49:53 GMT
33+
Server:
34+
- nginx/1.18.0 (Ubuntu)
35+
Set-Cookie: DUMMY_SET_COOKIE
36+
Transfer-Encoding:
37+
- chunked
38+
X-Content-Type-Options:
39+
- nosniff
40+
X-Frame-Options:
41+
- SAMEORIGIN
42+
X-XSS-Protection:
43+
- 1; mode=block
44+
status:
45+
code: 200
46+
message: OK
47+
- request:
48+
body: ''
49+
headers:
50+
Authorization:
51+
- DUMMY_AUTHORIZATION
52+
accept:
53+
- '*/*'
54+
accept-encoding:
55+
- gzip, deflate
56+
connection:
57+
- keep-alive
58+
host:
59+
- app.q-detect.com
60+
user-agent:
61+
- python-httpx/0.28.1
62+
method: GET
63+
uri: https://app.q-detect.com/api/find?client_id=DUMMY_CLIENT_ID&name=Pepito+Cuenca&percent=80&username=DUMMY_USERNAME
64+
response:
65+
body:
66+
string: "{\"success\":false,\"status\":\"El token proporcionado para realizar
67+
esta acci\xF3n es inv\xE1lido\"}"
68+
headers:
69+
Content-Length:
70+
- '91'
71+
Content-Type:
72+
- application/json
73+
Set-Cookie: DUMMY_SET_COOKIE
74+
status:
75+
code: 401
76+
message: Unauthorized
77+
version: 1
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
interactions:
2+
- request:
3+
body: ''
4+
headers:
5+
Authorization:
6+
- DUMMY_AUTHORIZATION
7+
accept:
8+
- '*/*'
9+
accept-encoding:
10+
- identity
11+
connection:
12+
- keep-alive
13+
host:
14+
- app.q-detect.com
15+
user-agent:
16+
- python-httpx/0.28.1
17+
method: GET
18+
uri: https://app.q-detect.com/api/token?client_id=DUMMY_CLIENT_ID
19+
response:
20+
body:
21+
string: DUMMY_RESPONSE_TOKEN
22+
headers:
23+
Access-Control-Allow-Origin:
24+
- '*'
25+
Cache-Control:
26+
- no-cache, private
27+
Connection:
28+
- keep-alive
29+
Content-Type:
30+
- text/html; charset=UTF-8
31+
Date:
32+
- Thu, 20 Feb 2025 15:49:53 GMT
33+
Server:
34+
- nginx/1.18.0 (Ubuntu)
35+
Set-Cookie: DUMMY_SET_COOKIE
36+
Transfer-Encoding:
37+
- chunked
38+
X-Content-Type-Options:
39+
- nosniff
40+
X-Frame-Options:
41+
- SAMEORIGIN
42+
X-XSS-Protection:
43+
- 1; mode=block
44+
status:
45+
code: 200
46+
message: OK
47+
- request:
48+
body: ''
49+
headers:
50+
Authorization:
51+
- DUMMY_AUTHORIZATION
52+
accept:
53+
- '*/*'
54+
accept-encoding:
55+
- gzip, deflate
56+
connection:
57+
- keep-alive
58+
host:
59+
- app.q-detect.com
60+
user-agent:
61+
- python-httpx/0.28.1
62+
method: GET
63+
uri: https://app.q-detect.com/api/find?client_id=DUMMY_CLIENT_ID&name=Pepito+Cuenca&percent=80&username=DUMMY_USERNAME
64+
response:
65+
body:
66+
string: "{\"success\":false,\"status\":\"No se puede realizar la b\xFAsqueda,
67+
saldo insuficiente\"}"
68+
headers:
69+
Content-Length:
70+
- '82'
71+
Content-Type:
72+
- application/json
73+
Set-Cookie: DUMMY_SET_COOKIE
74+
status:
75+
code: 403
76+
message: Forbidden
77+
version: 1
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
interactions:
2+
- request:
3+
body: ''
4+
headers:
5+
Authorization:
6+
- DUMMY_AUTHORIZATION
7+
accept:
8+
- '*/*'
9+
accept-encoding:
10+
- identity
11+
connection:
12+
- keep-alive
13+
host:
14+
- app.q-detect.com
15+
user-agent:
16+
- python-httpx/0.28.1
17+
method: GET
18+
uri: https://app.q-detect.com/api/token?client_id=DUMMY_CLIENT_ID
19+
response:
20+
body:
21+
string: DUMMY_RESPONSE_TOKEN
22+
headers:
23+
Access-Control-Allow-Origin:
24+
- '*'
25+
Cache-Control:
26+
- no-cache, private
27+
Connection:
28+
- keep-alive
29+
Content-Type:
30+
- text/html; charset=UTF-8
31+
Date:
32+
- Thu, 20 Feb 2025 15:54:50 GMT
33+
Server:
34+
- nginx/1.18.0 (Ubuntu)
35+
Set-Cookie: DUMMY_SET_COOKIE
36+
Transfer-Encoding:
37+
- chunked
38+
X-Content-Type-Options:
39+
- nosniff
40+
X-Frame-Options:
41+
- SAMEORIGIN
42+
X-XSS-Protection:
43+
- 1; mode=block
44+
status:
45+
code: 200
46+
message: OK
47+
- request:
48+
body: ''
49+
headers:
50+
Authorization:
51+
- DUMMY_AUTHORIZATION
52+
accept:
53+
- '*/*'
54+
accept-encoding:
55+
- gzip, deflate
56+
connection:
57+
- keep-alive
58+
host:
59+
- app.q-detect.com
60+
user-agent:
61+
- python-httpx/0.28.1
62+
method: GET
63+
uri: https://app.q-detect.com/api/find?client_id=DUMMY_CLIENT_ID&name=Pepito+Cuenca&percent=80&username=DUMMY_USERNAME
64+
response:
65+
body:
66+
string: '{"success":false,"status":"Internal server error"}'
67+
headers:
68+
Content-Length:
69+
- '50'
70+
Content-Type:
71+
- application/json
72+
Set-Cookie: DUMMY_SET_COOKIE
73+
status:
74+
code: 500
75+
message: Internal Server Error
76+
version: 1
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
interactions:
2+
- request:
3+
body: ''
4+
headers:
5+
Authorization:
6+
- DUMMY_AUTHORIZATION
7+
accept:
8+
- '*/*'
9+
accept-encoding:
10+
- identity
11+
connection:
12+
- keep-alive
13+
host:
14+
- app.q-detect.com
15+
user-agent:
16+
- python-httpx/0.28.1
17+
method: GET
18+
uri: https://app.q-detect.com/api/token?client_id=DUMMY_CLIENT_ID
19+
response:
20+
body:
21+
string: DUMMY_RESPONSE_TOKEN
22+
headers:
23+
Access-Control-Allow-Origin:
24+
- '*'
25+
Cache-Control:
26+
- no-cache, private
27+
Connection:
28+
- keep-alive
29+
Content-Type:
30+
- text/html; charset=UTF-8
31+
Date:
32+
- Thu, 20 Feb 2025 15:49:52 GMT
33+
Server:
34+
- nginx/1.18.0 (Ubuntu)
35+
Set-Cookie: DUMMY_SET_COOKIE
36+
Transfer-Encoding:
37+
- chunked
38+
X-Content-Type-Options:
39+
- nosniff
40+
X-Frame-Options:
41+
- SAMEORIGIN
42+
X-XSS-Protection:
43+
- 1; mode=block
44+
status:
45+
code: 200
46+
message: OK
47+
- request:
48+
body: ''
49+
headers:
50+
Authorization:
51+
- DUMMY_AUTHORIZATION
52+
accept:
53+
- '*/*'
54+
accept-encoding:
55+
- gzip, deflate
56+
connection:
57+
- keep-alive
58+
host:
59+
- app.q-detect.com
60+
user-agent:
61+
- python-httpx/0.28.1
62+
method: GET
63+
uri: https://app.q-detect.com/api/find?client_id=DUMMY_CLIENT_ID&name=Pepito+Cuenca&percent=80&username=DUMMY_USERNAME
64+
response:
65+
body:
66+
string: '{"success":false,"status":"Tu plan de consultas ha expirado, por favor
67+
actualiza tu plan para continuar usando la API"}'
68+
headers:
69+
Content-Length:
70+
- '119'
71+
Content-Type:
72+
- application/json
73+
Set-Cookie: DUMMY_SET_COOKIE
74+
status:
75+
code: 200
76+
message: OK
77+
version: 1

tests/cassettes/test_invalid_token.yaml

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,36 @@ interactions:
33
body: ''
44
headers:
55
Authorization:
6-
- DUMMY
6+
- DUMMY_AUTHORIZATION
7+
accept:
8+
- '*/*'
9+
accept-encoding:
10+
- identity
11+
connection:
12+
- keep-alive
13+
host:
14+
- app.q-detect.com
15+
user-agent:
16+
- python-httpx/0.28.1
17+
method: GET
18+
uri: https://app.q-detect.com/api/token?client_id=DUMMY_CLIENT_ID
19+
response:
20+
body:
21+
string: an_invalid_token
22+
headers:
23+
Content-Length:
24+
- '16'
25+
Content-Type:
26+
- text/plain; charset=utf-8
27+
Set-Cookie: DUMMY_SET_COOKIE
28+
status:
29+
code: 200
30+
message: OK
31+
- request:
32+
body: ''
33+
headers:
34+
Authorization:
35+
- DUMMY_AUTHORIZATION
736
accept:
837
- '*/*'
938
accept-encoding:
@@ -30,10 +59,10 @@ interactions:
3059
Content-Type:
3160
- application/json
3261
Date:
33-
- Wed, 19 Feb 2025 17:39:06 GMT
62+
- Thu, 20 Feb 2025 15:49:52 GMT
3463
Server:
3564
- nginx/1.18.0 (Ubuntu)
36-
Set-Cookie: DUMMY
65+
Set-Cookie: DUMMY_SET_COOKIE
3766
Transfer-Encoding:
3867
- chunked
3968
X-Content-Type-Options:

0 commit comments

Comments
 (0)