-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add tests for message endpoint #40
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if it is sending any messages to the staging Fedora Messaging deployment or if it is not doing anything at all in the backend. Anyway, please make the suggested changes and then we can have another round of reviews.
get_or_create(db.session, Service, name="Github Demo", type="Github", desc="description", user_id=user.id) | ||
|
||
service, created = get_or_create(db.session, Service, name="Github Demo", type="Github", desc="description", user_id=user.id) | ||
service.token = "43b901cba25c458a9175ef119a9603bb" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it your actual GitHub token or the one generated by our service?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was generated by our service and then I got it from there.
tests/conftest.py
Outdated
@@ -43,8 +43,8 @@ def create_service(client): | |||
with client.application.app_context(): | |||
# Setup code to create the object in the database | |||
user, created = get_or_create(db.session, User, username="mehmet") # Adjust fields as necessary | |||
get_or_create(db.session, Service, name="Github Demo", type="Github", desc="description", user_id=user.id) | |||
|
|||
service, created = get_or_create(db.session, Service, name="Github Demo", type="Github", desc="description", user_id=user.id) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GitHub is a proper noun.
service, created = get_or_create(db.session, Service, name="Github Demo", type="Github", desc="description", user_id=user.id) | |
service, created = get_or_create(db.session, Service, name="GitHub Demo", type="GitHub", desc="description", user_id=user.id) |
tests/test_message/event_sample.py
Outdated
@@ -0,0 +1,204 @@ | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove the empty line from the top of the file.
tests/test_message/event_sample.py
Outdated
"deleted": False, | ||
"forced": False, | ||
"base_ref": None, | ||
"compare": "https://github.com/brngylni/demo_repo/compare/034c31e8e464...443f68ce112a", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this be emulated using VCR.py?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I checked the documentation but it seems like there's a request, response structure when recording requests whereas we receive the request from GitHub to a webhook. So I'm not sure if we can make use of this. What do you think?
import vcr
import urllib.request
with vcr.use_cassette('fixtures/vcr_cassettes/synopsis.yaml'):
response = urllib.request.urlopen('http://www.iana.org/domains/reserved').read()
assert b'Example domains' in response
https://vcrpy.readthedocs.io/en/latest/usage.html#record-modes
What I've done is use a secret key generated by our service and get a sample push event payload.
tests/test_message/event_sample.py
Outdated
"distinct": True, | ||
"message": "demo", | ||
"timestamp": "2024-07-23T00:16:24+03:00", | ||
"url": "https://github.com/brngylni/demo_repo/commit/443f68ce112ab055bbf48666762a626baa4729f6", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this be emulated using VCR.py?
tests/test_message/event_sample.py
Outdated
"type": "User", | ||
"site_admin": False | ||
}, | ||
"html_url": "https://github.com/brngylni/demo_repo", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a way to emulate this without using a secondary external repository?
I want our test cases to be self-sufficient and not dependent on external factors.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean a seperate repository under fedora infra?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@brngylni I mean by not having an external repository at all.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought having a real request might be helpful in terms of testing the hash and etc. That's why I used a real payload. I can edit the payload if you'd like to.
tests/test_message/event_sample.py
Outdated
"name": "demo_repo", | ||
"full_name": "brngylni/demo_repo", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a way to emulate this without using a secondary external repository?
I want our test cases to be self-sufficient and not dependent on external factors.
tests/test_message/event_sample.py
Outdated
"url": "https://github.com/brngylni/demo_repo", | ||
"forks_url": "https://api.github.com/repos/brngylni/demo_repo/forks", | ||
"keys_url": "https://api.github.com/repos/brngylni/demo_repo/keys{/key_id}", | ||
"collaborators_url": "https://api.github.com/repos/brngylni/demo_repo/collaborators{/collaborator}", | ||
"teams_url": "https://api.github.com/repos/brngylni/demo_repo/teams", | ||
"hooks_url": "https://api.github.com/repos/brngylni/demo_repo/hooks", | ||
"issue_events_url": "https://api.github.com/repos/brngylni/demo_repo/issues/events{/number}", | ||
"events_url": "https://api.github.com/repos/brngylni/demo_repo/events", | ||
"assignees_url": "https://api.github.com/repos/brngylni/demo_repo/assignees{/user}", | ||
"branches_url": "https://api.github.com/repos/brngylni/demo_repo/branches{/branch}", | ||
"tags_url": "https://api.github.com/repos/brngylni/demo_repo/tags", | ||
"blobs_url": "https://api.github.com/repos/brngylni/demo_repo/git/blobs{/sha}", | ||
"git_tags_url": "https://api.github.com/repos/brngylni/demo_repo/git/tags{/sha}", | ||
"git_refs_url": "https://api.github.com/repos/brngylni/demo_repo/git/refs{/sha}", | ||
"trees_url": "https://api.github.com/repos/brngylni/demo_repo/git/trees{/sha}", | ||
"statuses_url": "https://api.github.com/repos/brngylni/demo_repo/statuses/{sha}", | ||
"languages_url": "https://api.github.com/repos/brngylni/demo_repo/languages", | ||
"stargazers_url": "https://api.github.com/repos/brngylni/demo_repo/stargazers", | ||
"contributors_url": "https://api.github.com/repos/brngylni/demo_repo/contributors", | ||
"subscribers_url": "https://api.github.com/repos/brngylni/demo_repo/subscribers", | ||
"subscription_url": "https://api.github.com/repos/brngylni/demo_repo/subscription", | ||
"commits_url": "https://api.github.com/repos/brngylni/demo_repo/commits{/sha}", | ||
"git_commits_url": "https://api.github.com/repos/brngylni/demo_repo/git/commits{/sha}", | ||
"comments_url": "https://api.github.com/repos/brngylni/demo_repo/comments{/number}", | ||
"issue_comment_url": "https://api.github.com/repos/brngylni/demo_repo/issues/comments{/number}", | ||
"contents_url": "https://api.github.com/repos/brngylni/demo_repo/contents/{+path}", | ||
"compare_url": "https://api.github.com/repos/brngylni/demo_repo/compare/{base}...{head}", | ||
"merges_url": "https://api.github.com/repos/brngylni/demo_repo/merges", | ||
"archive_url": "https://api.github.com/repos/brngylni/demo_repo/{archive_format}{/ref}", | ||
"downloads_url": "https://api.github.com/repos/brngylni/demo_repo/downloads", | ||
"issues_url": "https://api.github.com/repos/brngylni/demo_repo/issues{/number}", | ||
"pulls_url": "https://api.github.com/repos/brngylni/demo_repo/pulls{/number}", | ||
"milestones_url": "https://api.github.com/repos/brngylni/demo_repo/milestones{/number}", | ||
"notifications_url": "https://api.github.com/repos/brngylni/demo_repo/notifications{?since,all,participating}", | ||
"labels_url": "https://api.github.com/repos/brngylni/demo_repo/labels{/name}", | ||
"releases_url": "https://api.github.com/repos/brngylni/demo_repo/releases{/id}", | ||
"deployments_url": "https://api.github.com/repos/brngylni/demo_repo/deployments", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a way to emulate this without using a secondary external repository?
I want our test cases to be self-sufficient and not dependent on external factors.
tests/test_message/event_sample.py
Outdated
"git_url": "git://github.com/brngylni/demo_repo.git", | ||
"ssh_url": "[email protected]:brngylni/demo_repo.git", | ||
"clone_url": "https://github.com/brngylni/demo_repo.git", | ||
"svn_url": "https://github.com/brngylni/demo_repo", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a way to emulate this without using a secondary external repository?
I want our test cases to be self-sufficient and not dependent on external factors.
@pytest.mark.parametrize( | ||
"data, headers, code", | ||
[ | ||
pytest.param( | ||
request, | ||
headers, | ||
200, | ||
id="MESSAGE Endpoint - 200 Success", | ||
), | ||
pytest.param( | ||
request, | ||
{**headers, "X-Hub-Signature-256": ""}, | ||
400, | ||
id="MESSAGE Endpoint - 400 Bad Request" | ||
), | ||
pytest.param( | ||
None, | ||
headers, | ||
415, | ||
id="MESSAGE Endpoint - 415 Unsupported Media Type" | ||
) | ||
] | ||
) | ||
|
||
@pytest.mark.usefixtures("create_service") | ||
def test_message_create(client, data, headers, code): | ||
response = client.post("/message/1", json=data, headers=headers) | ||
assert response.status_code == code | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"data, headers, code", | ||
[ | ||
pytest.param( | ||
request, | ||
headers, | ||
404, | ||
id="MESSAGE Endpoint - Not Found", | ||
) | ||
] | ||
) | ||
|
||
@pytest.mark.usefixtures("create_service") | ||
def test_not_found(client, data, headers, code): | ||
response = client.post("/message/2", json=data, headers=headers) | ||
assert response.status_code == code |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suspect that these can be combined to be four parameters under one testing function. Simply parameterize the URL path too (as in f"/message/{id}"
, where the id
is provided from the parameters) and you should be fine.
cd87148
to
2664602
Compare
Signed-off-by: Mehmet Baran Geylani <[email protected]>
4c9ce68
to
25c3a24
Compare
This PR adds the tests for message endpoint