Skip to content
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

Merged
merged 1 commit into from
Jul 25, 2024
Merged

Add tests for message endpoint #40

merged 1 commit into from
Jul 25, 2024

Conversation

brngylni
Copy link
Contributor

@brngylni brngylni commented Jul 22, 2024

This PR adds the tests for message endpoint

Copy link
Member

@gridhead gridhead left a 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"
Copy link
Member

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?

Copy link
Contributor Author

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.

@@ -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)
Copy link
Member

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.

Suggested change
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)

@@ -0,0 +1,204 @@

Copy link
Member

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.

"deleted": False,
"forced": False,
"base_ref": None,
"compare": "https://github.com/brngylni/demo_repo/compare/034c31e8e464...443f68ce112a",
Copy link
Member

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?

Ref. https://vcrpy.readthedocs.io/

Copy link
Contributor Author

@brngylni brngylni Jul 23, 2024

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.

"distinct": True,
"message": "demo",
"timestamp": "2024-07-23T00:16:24+03:00",
"url": "https://github.com/brngylni/demo_repo/commit/443f68ce112ab055bbf48666762a626baa4729f6",
Copy link
Member

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?

Ref. https://vcrpy.readthedocs.io/

"type": "User",
"site_admin": False
},
"html_url": "https://github.com/brngylni/demo_repo",
Copy link
Member

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.

Copy link
Contributor Author

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?

Copy link
Member

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.

Copy link
Contributor Author

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.

"name": "demo_repo",
"full_name": "brngylni/demo_repo",
Copy link
Member

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.

Comment on lines 37 to 72
"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",
Copy link
Member

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.

Comment on lines 77 to 79
"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",
Copy link
Member

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.

Comment on lines 5 to 42
@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
Copy link
Member

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.

@gridhead gridhead self-assigned this Jul 23, 2024
@brngylni brngylni requested a review from gridhead July 23, 2024 10:39
@brngylni brngylni force-pushed the test_message_endpoint branch 2 times, most recently from cd87148 to 2664602 Compare July 24, 2024 20:08
Signed-off-by: Mehmet Baran Geylani <[email protected]>
@brngylni brngylni requested a review from abompard July 25, 2024 09:08
@abompard abompard merged commit 9547fcb into main Jul 25, 2024
5 of 10 checks passed
@abompard abompard deleted the test_message_endpoint branch July 25, 2024 15:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants