| 1 |
import pytest |
| 2 |
import responses |
| 3 |
|
| 4 |
from censys.search.v2 import CensysCerts, CensysHosts |
| 5 |
from censys.search.v2.api import CensysSearchAPIv2 |
| 6 |
from tests.utils import V2_URL, CensysTestCase |
| 7 |
|
| 8 |
TEST_COMMENT = "**This is a comment.**" |
| 9 |
GET_COMMENTS_RESPONSE = { |
| 10 |
"code": 200, |
| 11 |
"status": "OK", |
| 12 |
"result": { |
| 13 |
"ip": "8.8.8.8", |
| 14 |
"comments": [ |
| 15 |
{ |
| 16 |
"id": "comment-id", |
| 17 |
"ip": "1.1.1.1", |
| 18 |
"author_id": "string", |
| 19 |
"contents": TEST_COMMENT, |
| 20 |
"created_at": "2016-01-01T00:00:00Z", |
| 21 |
} |
| 22 |
], |
| 23 |
}, |
| 24 |
} |
| 25 |
ADD_COMMENTS_RESPONSE = { |
| 26 |
"code": 200, |
| 27 |
"status": "OK", |
| 28 |
"result": { |
| 29 |
"id": "comment-id", |
| 30 |
"ip": "1.1.1.1", |
| 31 |
"author_id": "string", |
| 32 |
"contents": TEST_COMMENT, |
| 33 |
"created_at": "2016-01-01T00:00:00Z", |
| 34 |
}, |
| 35 |
} |
| 36 |
GET_COMMENT_RESPONSE = { |
| 37 |
"code": 200, |
| 38 |
"status": "OK", |
| 39 |
"result": { |
| 40 |
"id": "comment-id", |
| 41 |
"ip": "1.1.1.1", |
| 42 |
"author_id": "string", |
| 43 |
"contents": "**This is a comment.**", |
| 44 |
"created_at": "2016-01-01T00:00:00Z", |
| 45 |
}, |
| 46 |
} |
| 47 |
|
| 48 |
INDEX_PARAMS = [ |
| 49 |
{"index": "hosts", "index_cls": CensysHosts, "document_id": "1.0.0.0"}, |
| 50 |
{ |
| 51 |
"index": "certificates", |
| 52 |
"index_cls": CensysCerts, |
| 53 |
"document_id": "fb444eb8e68437bae06232b9f5091bccff62a768ca09e92eb5c9c2cf9d17c426", |
| 54 |
}, |
| 55 |
] |
| 56 |
|
| 57 |
|
| 58 |
class CensysCommentsTests(CensysTestCase): |
| 59 |
index: str |
| 60 |
index_cls: CensysSearchAPIv2 |
| 61 |
document_id: str |
| 62 |
api: CensysSearchAPIv2 |
| 63 |
|
| 64 |
@pytest.fixture(autouse=True, params=INDEX_PARAMS, ids=["hosts", "certificates"]) |
| 65 |
def _index_config(self, request): |
| 66 |
self.index = request.param["index"] |
| 67 |
self.index_cls = request.param["index_cls"] |
| 68 |
self.document_id = request.param["document_id"] |
| 69 |
self.setUpApi(self.index_cls(self.api_id, self.api_secret)) |
| 70 |
|
| 71 |
def test_get_comments(self): |
| 72 |
self.responses.add( |
| 73 |
responses.GET, |
| 74 |
f"{V2_URL}/{self.index}/{self.document_id}/comments", |
| 75 |
status=200, |
| 76 |
json=GET_COMMENTS_RESPONSE, |
| 77 |
) |
| 78 |
results = self.api.get_comments(self.document_id) |
| 79 |
assert results == GET_COMMENTS_RESPONSE["result"]["comments"] |
| 80 |
|
| 81 |
def test_add_comment(self): |
| 82 |
self.responses.add( |
| 83 |
responses.POST, |
| 84 |
f"{V2_URL}/{self.index}/{self.document_id}/comments", |
| 85 |
status=200, |
| 86 |
json=ADD_COMMENTS_RESPONSE, |
| 87 |
match=[responses.json_params_matcher({"contents": TEST_COMMENT})], |
| 88 |
) |
| 89 |
results = self.api.add_comment(self.document_id, TEST_COMMENT) |
| 90 |
assert results == ADD_COMMENTS_RESPONSE["result"] |
| 91 |
|
| 92 |
def test_get_comment(self): |
| 93 |
self.responses.add( |
| 94 |
responses.GET, |
| 95 |
f"{V2_URL}/{self.index}/{self.document_id}/comments/comment-id", |
| 96 |
status=200, |
| 97 |
json=GET_COMMENT_RESPONSE, |
| 98 |
) |
| 99 |
results = self.api.get_comment(self.document_id, "comment-id") |
| 100 |
assert results == GET_COMMENT_RESPONSE["result"] |
| 101 |
|
| 102 |
def test_delete_comment(self): |
| 103 |
self.responses.add( |
| 104 |
responses.DELETE, |
| 105 |
f"{V2_URL}/{self.index}/{self.document_id}/comments/comment-id", |
| 106 |
status=209, |
| 107 |
) |
| 108 |
self.api.delete_comment(self.document_id, "comment-id") |
| 109 |
|
| 110 |
def test_update_comment(self): |
| 111 |
self.responses.add( |
| 112 |
responses.PUT, |
| 113 |
f"{V2_URL}/{self.index}/{self.document_id}/comments/comment-id", |
| 114 |
status=200, |
| 115 |
json={"code": 200, "status": "OK"}, |
| 116 |
match=[responses.json_params_matcher({"contents": TEST_COMMENT})], |
| 117 |
) |
| 118 |
results = self.api.update_comment(self.document_id, "comment-id", TEST_COMMENT) |
| 119 |
assert results == {"code": 200, "status": "OK"} |