Browse Source

ref: prevent test_push_event from reaching out to public github (#63117)

otherwise this fails with:

```
tests/sentry_plugins/github/endpoints/test_push_event.py:115: in test_user_email
    assert commit.author.email == "alberto@sentry.io"
E   AssertionError: assert 'baxterthehac...ly.github.com' == 'alberto@sentry.io'
E     - alberto@sentry.io
E     + baxterthehacker@users.noreply.github.com
```

cc @dashed 

<!-- Describe your PR here. -->
anthony sottile 1 year ago
parent
commit
ce5fd39892
2 changed files with 56 additions and 0 deletions
  1. 38 0
      fixtures/github.py
  2. 18 0
      tests/sentry_plugins/github/endpoints/test_push_event.py

+ 38 - 0
fixtures/github.py

@@ -2176,6 +2176,44 @@ PULL_REQUEST_CLOSED_EVENT_EXAMPLE = b"""{
   }
 }"""
 
+# https://api.github.com/users/baxterthehacker
+API_GITHUB_COM_USERS_BAXTERTHEHACKER = """\
+{
+  "login": "baxterthehacker",
+  "id": 6752317,
+  "node_id": "MDQ6VXNlcjY3NTIzMTc=",
+  "avatar_url": "https://avatars.githubusercontent.com/u/6752317?v=4",
+  "gravatar_id": "",
+  "url": "https://api.github.com/users/baxterthehacker",
+  "html_url": "https://github.com/baxterthehacker",
+  "followers_url": "https://api.github.com/users/baxterthehacker/followers",
+  "following_url": "https://api.github.com/users/baxterthehacker/following{/other_user}",
+  "gists_url": "https://api.github.com/users/baxterthehacker/gists{/gist_id}",
+  "starred_url": "https://api.github.com/users/baxterthehacker/starred{/owner}{/repo}",
+  "subscriptions_url": "https://api.github.com/users/baxterthehacker/subscriptions",
+  "organizations_url": "https://api.github.com/users/baxterthehacker/orgs",
+  "repos_url": "https://api.github.com/users/baxterthehacker/repos",
+  "events_url": "https://api.github.com/users/baxterthehacker/events{/privacy}",
+  "received_events_url": "https://api.github.com/users/baxterthehacker/received_events",
+  "type": "User",
+  "site_admin": false,
+  "name": null,
+  "company": null,
+  "blog": "",
+  "location": null,
+  "email": null,
+  "hireable": null,
+  "bio": null,
+  "twitter_username": null,
+  "public_repos": 6,
+  "public_gists": 0,
+  "followers": 9,
+  "following": 1,
+  "created_at": "2014-02-21T22:24:28Z",
+  "updated_at": "2023-12-22T12:36:56Z"
+}
+"""
+
 # Example taken from github with additional example commit added https://docs.github.com/en/rest/commits/commits#compare-two-commits
 COMPARE_COMMITS_EXAMPLE_WITH_INTERMEDIATE = """{
   "url": "https://api.github.com/repos/octocat/Hello-World/compare/master...topic",

+ 18 - 0
tests/sentry_plugins/github/endpoints/test_push_event.py

@@ -1,6 +1,10 @@
+import contextlib
 from datetime import datetime, timezone
 from uuid import uuid4
 
+import responses
+
+from fixtures.github import API_GITHUB_COM_USERS_BAXTERTHEHACKER
 from sentry.models.commit import Commit
 from sentry.models.commitauthor import CommitAuthor
 from sentry.models.options.organization_option import OrganizationOption
@@ -10,8 +14,20 @@ from sentry.testutils.silo import region_silo_test
 from sentry_plugins.github.testutils import PUSH_EVENT_EXAMPLE
 
 
+@contextlib.contextmanager
+def mock_baxter_response():
+    with responses.RequestsMock() as mck:
+        mck.add(
+            "GET",
+            "https://api.github.com/users/baxterthehacker",
+            body=API_GITHUB_COM_USERS_BAXTERTHEHACKER,
+        )
+        yield
+
+
 @region_silo_test
 class PushEventWebhookTest(APITestCase):
+    @mock_baxter_response()
     def test_simple(self):
         project = self.project  # force creation
 
@@ -67,6 +83,7 @@ class PushEventWebhookTest(APITestCase):
         assert commit.author.external_id is None
         assert commit.date_added == datetime(2015, 5, 5, 23, 40, 15, tzinfo=timezone.utc)
 
+    @mock_baxter_response()
     def test_user_email(self):
         project = self.project  # force creation
         user = self.create_user(email="alberto@sentry.io")
@@ -125,6 +142,7 @@ class PushEventWebhookTest(APITestCase):
         assert commit.author.external_id == "github:baxterthehacker"
         assert commit.date_added == datetime(2015, 5, 5, 23, 40, 15, tzinfo=timezone.utc)
 
+    @responses.activate
     def test_anonymous_lookup(self):
         project = self.project  # force creation