Fixed hide_response body (#370)#375
Conversation
c5a9ebb to
d3ae052
Compare
camilamaia
left a comment
There was a problem hiding this comment.
Hey, @hebertjulio, would you mind creating new tests for the changes you've made? I've just created a detailed doc of how to do it:
- https://github.com/scanapi/scanapi/wiki/First-Pull-Request#8-test-your-changes
- https://github.com/scanapi/scanapi/wiki/Writing-Tests
Please, let me know if you need any help!
|
Of course @camilamaia I can do it, but I don't know which test can be created, at the moment I am learning to create tests, and I have difficulties in defining the tests that need to be created. |
No problem, I can help you :) I would create:
This is the file https://github.com/scanapi/scanapi/blob/master/tests/unit/test_hide_utils.py where you should add the tests. Let me know how it goes! |
|
Hi @camilamaia, i don't understand why there are two gets method in line 8 of test_hide_utils.py file. is that correct? scanapi/tests/unit/test_hide_utils.py Lines 8 to 10 in 4ee9c9b |
|
@hebertjulio yes, this is mocking the Unit tests don't call external services, external APIs. Unit tests must test only the Ok, but what if the method I am testing calls an external API? What should I do? Here is where the Yes, let's say your method calls def my_method:
requests.get("http://test.com")with mock, we say:
requests_mock.get("http://test.com", text="data")
return requests.get("http://test.com")So, instead of the code try to real do a GET request to It is a bit complex in the beginning to understand https://requests-mock.readthedocs.io/en/latest/pytest.html#pytest |
|
@camilamaia creating the tests I found what can be a problem when the sensitive data of a body is overwritten, following the implemented logic, when I don't have the body attribute, I have to work with the content attribute, but this field cannot be overwritten as well as the body attribute. I believe that the overwritten has to occur in the printing of the report and not as an intermediate process. Lines 65 to 69 in 4ee9c9b |
|
@hebertjulio indeed. But in the earlier versions of requests, you can set the attribute http_msg._content = json.dumps(body).encode("utf-8")In the Python shell, I got an example to show how we can change the attribute in fact: >>> import json
>>> import requests
>>> http_msg = requests.get("https://demo.scanapi.dev/api/v1/snippets/2/")
>>> http_msg.content
b'{"url":"http://demo.scanapi.dev/api/v1/snippets/2/","id":2,"highlight":"http://demo.scanapi.dev/api/v1/snippets/2/highlight/","owner":"admin","title":"Print","code":"print(\xe2\x80\x9cabc\xe2\x80\x9d)","linenos":false,"language":"python","style":"vs"}'
>>> http_msg.text
'{"url":"http://demo.scanapi.dev/api/v1/snippets/2/","id":2,"highlight":"http://demo.scanapi.dev/api/v1/snippets/2/highlight/","owner":"admin","title":"Print","code":"print(“abc”)","linenos":false,"language":"python","style":"vs"}'
>>> body = http_msg.body if hasattr(http_msg, "body") else http_msg.content
>>> body = json.loads(body.decode("UTF-8"))
>>> body
{'url': 'http://demo.scanapi.dev/api/v1/snippets/2/', 'id': 2, 'highlight': 'http://demo.scanapi.dev/api/v1/snippets/2/highlight/', 'owner': 'admin', 'title': 'Print', 'code': 'print(“abc”)', 'linenos': False, 'language': 'python', 'style': 'vs'}
>>> body['title'] = "SENSITIVE_INFO"
>>> body
{'url': 'http://demo.scanapi.dev/api/v1/snippets/2/', 'id': 2, 'highlight': 'http://demo.scanapi.dev/api/v1/snippets/2/highlight/', 'owner': 'admin', 'title': 'SENSITIVE_INFO', 'code': 'print(“abc”)', 'linenos': False, 'language': 'python', 'style': 'vs'}
>>> http_msg._content = json.dumps(body).encode("utf-8")
>>>
>>> http_msg.content
b'{"url": "http://demo.scanapi.dev/api/v1/snippets/2/", "id": 2, "highlight": "http://demo.scanapi.dev/api/v1/snippets/2/highlight/", "owner": "admin", "title": "SENSITIVE_INFO", "code": "print(\\u201cabc\\u201d)", "linenos": false, "language": "python", "style": "vs"}'
>>> http_msg.text
'{"url": "http://demo.scanapi.dev/api/v1/snippets/2/", "id": 2, "highlight": "http://demo.scanapi.dev/api/v1/snippets/2/highlight/", "owner": "admin", "title": "SENSITIVE_INFO", "code": "print(\\u201cabc\\u201d)", "linenos": false, "language": "python", "style": "vs"}'Reference: https://stackoverflow.com/a/26342225/8298081 |
9ce3bb4 to
a30a68b
Compare
Closes #370
Closes #371