-
Notifications
You must be signed in to change notification settings - Fork 965
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: working POST API for Project Observations (#15228)
- Loading branch information
1 parent
bb5251c
commit 68c1db9
Showing
23 changed files
with
648 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import pytest | ||
|
||
from pyramid.httpexceptions import HTTPAccepted, HTTPBadRequest | ||
|
||
from tests.common.db.packaging import ProjectFactory | ||
from warehouse.api.echo import api_echo, api_projects_observations | ||
|
||
|
||
class TestAPI: | ||
def test_echo(self, pyramid_request, pyramid_user): | ||
assert api_echo(pyramid_request) == {"username": pyramid_user.username} | ||
|
||
|
||
class TestAPIProjectObservations: | ||
def test_missing_fields(self, pyramid_request): | ||
project = ProjectFactory.create() | ||
pyramid_request.json_body = {} | ||
|
||
with pytest.raises(HTTPBadRequest) as exc: | ||
api_projects_observations(project, pyramid_request) | ||
|
||
assert exc.value.json == { | ||
"error": "missing required fields", | ||
"missing": ["kind", "summary"], | ||
} | ||
|
||
def test_invalid_kind(self, pyramid_request): | ||
project = ProjectFactory.create() | ||
pyramid_request.json_body = {"kind": "invalid", "summary": "test"} | ||
|
||
with pytest.raises(HTTPBadRequest) as exc: | ||
api_projects_observations(project, pyramid_request) | ||
|
||
assert exc.value.json == { | ||
"error": "invalid kind", | ||
"kind": "invalid", | ||
"project": project.name, | ||
} | ||
|
||
def test_malware_missing_inspector_url(self, pyramid_request): | ||
project = ProjectFactory.create() | ||
pyramid_request.json_body = {"kind": "is_malware", "summary": "test"} | ||
|
||
with pytest.raises(HTTPBadRequest) as exc: | ||
api_projects_observations(project, pyramid_request) | ||
|
||
assert exc.value.json == { | ||
"error": "missing required fields", | ||
"missing": ["inspector_url"], | ||
"project": project.name, | ||
} | ||
|
||
def test_malware_invalid_inspector_url(self, pyramid_request): | ||
project = ProjectFactory.create() | ||
pyramid_request.json_body = { | ||
"kind": "is_malware", | ||
"summary": "test", | ||
"inspector_url": "invalid", | ||
} | ||
|
||
with pytest.raises(HTTPBadRequest) as exc: | ||
api_projects_observations(project, pyramid_request) | ||
|
||
assert exc.value.json == { | ||
"error": "invalid inspector_url", | ||
"inspector_url": "invalid", | ||
"project": project.name, | ||
} | ||
|
||
def test_valid_malware_observation(self, db_request, pyramid_user): | ||
project = ProjectFactory.create() | ||
db_request.json_body = { | ||
"kind": "is_malware", | ||
"summary": "test", | ||
"inspector_url": "https://inspector.pypi.io/...", | ||
} | ||
|
||
response = api_projects_observations(project, db_request) | ||
|
||
assert isinstance(response, HTTPAccepted) | ||
assert response.json_body == { | ||
"project": project.name, | ||
"thanks": "for the observation", | ||
} | ||
|
||
def test_valid_spam_observation(self, db_request, pyramid_user): | ||
project = ProjectFactory.create() | ||
db_request.json_body = { | ||
"kind": "is_spam", | ||
"summary": "test", | ||
} | ||
|
||
response = api_projects_observations(project, db_request) | ||
|
||
assert isinstance(response, HTTPAccepted) | ||
assert response.json_body == { | ||
"project": project.name, | ||
"thanks": "for the observation", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import pretend | ||
import pytest | ||
import requests | ||
import responses | ||
|
||
from warehouse.observations.models import ObservationKind | ||
from warehouse.observations.tasks import ( | ||
execute_observation_report, | ||
report_observation_to_helpscout, | ||
) | ||
|
||
from ...common.db.accounts import UserFactory | ||
from ...common.db.packaging import ProjectFactory | ||
|
||
|
||
def test_execute_observation_report(app_config): | ||
_delay = pretend.call_recorder(lambda x: None) | ||
app_config.task = lambda x: pretend.stub(delay=_delay) | ||
observation = pretend.stub(id=pretend.stub()) | ||
session = pretend.stub(info={"warehouse.observations.new": {observation}}) | ||
|
||
execute_observation_report(app_config, session) | ||
|
||
assert _delay.calls == [pretend.call(observation.id)] | ||
|
||
|
||
@responses.activate | ||
@pytest.mark.parametrize( | ||
"kind", [ObservationKind.IsMalware, ObservationKind.SomethingElse] | ||
) | ||
@pytest.mark.parametrize("payload", [{}, {"foo": "bar"}]) | ||
def test_report_observation_to_helpscout(kind, payload, db_request, monkeypatch): | ||
db_request.registry.settings = {"helpscout.app_secret": "fake-sekret"} | ||
# Mock out the authentication to HelpScout | ||
monkeypatch.setattr( | ||
"warehouse.observations.tasks._authenticate_helpscout", | ||
lambda x: "SOME_TOKEN", | ||
) | ||
|
||
# Create an Observation | ||
user = UserFactory.create() | ||
db_request.user = user | ||
project = ProjectFactory.create() | ||
observation = project.record_observation( | ||
request=db_request, | ||
kind=kind, | ||
summary="Project Observation", | ||
payload=payload, | ||
actor=user, | ||
) | ||
# Need to flush the session to ensure the Observation has an ID | ||
db_request.db.flush() | ||
|
||
db_request.http = requests.Session() | ||
|
||
responses.add( | ||
responses.POST, | ||
"https://api.helpscout.net/v2/conversations", | ||
json={"id": 123}, | ||
) | ||
|
||
report_observation_to_helpscout(None, db_request, observation.id) | ||
|
||
assert len(responses.calls) == 1 | ||
assert ( | ||
responses.calls[0].request.url == "https://api.helpscout.net/v2/conversations" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.