Skip to content

Commit

Permalink
implements api tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andreformento committed Jun 27, 2021
1 parent 35875e3 commit a3f9d4c
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 3 deletions.
10 changes: 8 additions & 2 deletions api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,15 @@ def configure_api_app(
api_root_path=None,
):
if not isinstance(gazettes, GazetteAccessInterface):
raise Exception("Only GazetteAccessInterface object are accepted")
raise Exception(
"Only GazetteAccessInterface object are accepted for gazettes parameter"
)
if api_root_path is not None and type(api_root_path) != str:
raise Exception("Invalid api_root_path")
if not isinstance(suggestion_service, SuggestionServiceInterface):
raise Exception(
"Only SuggestionServiceInterface object are accepted for suggestion_service parameter"
)
app.gazettes = gazettes
app.root_path = api_root_path
app.suggestion_service = suggestion_service
app.root_path = api_root_path
93 changes: 92 additions & 1 deletion tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from api import app, configure_api_app
from gazettes import GazetteAccessInterface, GazetteRequest
from suggestions import SuggestionServiceInterface
from suggestions import Suggestion, SuggestionServiceInterface


@GazetteAccessInterface.register
Expand Down Expand Up @@ -569,3 +569,94 @@ def test_city_endpoint_should_return_city_info_returned_by_gazettes_interface(se
}
},
)


class ApiSuggestionsEndpointTests(TestCase):
def setUp(self):
self.suggestion_service = MockSuggestionService()
configure_api_app(MockGazetteAccessInterface(), self.suggestion_service)
self.client = TestClient(app)

def test_suggestion_endpoint_should_send_email(self):
self.suggestion_service.add_suggestion = MagicMock(return_value=True)

response = self.client.post(
"/suggestions",
json={
"email_address": "[email protected]",
"name": "My Name",
"content": "Suggestion content",
},
)
assert response.status_code == 200
assert response.json() == {"status": "sent"}

def test_api_should_fail_when_try_to_set_any_object_as_suggestions_service_interface(
self,
):
with self.assertRaises(Exception):
configure_api_app(MockGazetteAccessInterface(), MagicMock())

def test_suggestion_endpoint_should_fail_send_email(self):
self.suggestion_service.add_suggestion = MagicMock(return_value=False)

response = self.client.post(
"/suggestions",
json={
"email_address": "[email protected]",
"name": "My Name",
"content": "Suggestion content",
},
)
assert response.status_code == 400
assert response.json() == {"status": "Problem on sent message"}

def test_suggestion_endpoint_should_reject_when_email_address_is_not_present(self):
response = self.client.post(
"/suggestions", json={"name": "My Name", "content": "Suggestion content",},
)
assert response.status_code == 422
assert response.json() == {
"detail": [
{
"loc": ["body", "email_address"],
"msg": "field required",
"type": "value_error.missing",
}
]
}

def test_suggestion_endpoint_should_reject_when_name_is_not_present(self):
response = self.client.post(
"/suggestions",
json={
"email_address": "[email protected]",
"content": "Suggestion content",
},
)
assert response.status_code == 422
assert response.json() == {
"detail": [
{
"loc": ["body", "name"],
"msg": "field required",
"type": "value_error.missing",
}
]
}

def test_suggestion_endpoint_should_reject_when_content_is_not_present(self):
response = self.client.post(
"/suggestions",
json={"email_address": "[email protected]", "name": "My Name",},
)
assert response.status_code == 422
assert response.json() == {
"detail": [
{
"loc": ["body", "content"],
"msg": "field required",
"type": "value_error.missing",
}
]
}

0 comments on commit a3f9d4c

Please sign in to comment.