Skip to content

Commit

Permalink
Update test
Browse files Browse the repository at this point in the history
Signed-off-by: Mehmet Baran Geylani <[email protected]>
  • Loading branch information
brngylni committed Jul 24, 2024
1 parent ae9508f commit cd87148
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 115 deletions.
3 changes: 2 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ def create_service(client):
service, created = get_or_create(db.session, Service, name="GitHub Demo", type="GitHub", desc="description", user_id=user.id)
service.token = "43b901cba25c458a9175ef119a9603bb"
db.session.commit()
db.session.refresh(service)

yield
yield service

with client.application.app_context():
# Teardown code to remove the object from the database
Expand Down
203 changes: 101 additions & 102 deletions tests/test_message/event_sample.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import hmac
import hashlib
import json


request = {
"ref": "refs/heads/main",
"before": "034c31e8e46431711bed9eada103aa552aaee6dd",
Expand All @@ -6,77 +11,77 @@
"id": 807808293,
"node_id": "R_kgDOMCYtJQ",
"name": "demo_repo",
"full_name": "brngylni/demo_repo",
"full_name": "username/demo_repo",
"private": True,
"owner": {
"name": "brngylni",
"email": "66281850+brngylni@users.noreply.github.com",
"login": "brngylni",
"name": "username",
"email": "66281850+username@users.noreply.github.com",
"login": "username",
"id": 66281850,
"node_id": "MDQ6VXNlcjY2MjgxODUw",
"avatar_url": "https://avatars.githubusercontent.com/u/66281850?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/brngylni",
"html_url": "https://github.com/brngylni",
"followers_url": "https://api.github.com/users/brngylni/followers",
"following_url": "https://api.github.com/users/brngylni/following{/other_user}",
"gists_url": "https://api.github.com/users/brngylni/gists{/gist_id}",
"starred_url": "https://api.github.com/users/brngylni/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/brngylni/subscriptions",
"organizations_url": "https://api.github.com/users/brngylni/orgs",
"repos_url": "https://api.github.com/users/brngylni/repos",
"events_url": "https://api.github.com/users/brngylni/events{/privacy}",
"received_events_url": "https://api.github.com/users/brngylni/received_events",
"url": "https://api.github.com/users/username",
"html_url": "https://github.com/username",
"followers_url": "https://api.github.com/users/username/followers",
"following_url": "https://api.github.com/users/username/following{/other_user}",
"gists_url": "https://api.github.com/users/username/gists{/gist_id}",
"starred_url": "https://api.github.com/users/username/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/username/subscriptions",
"organizations_url": "https://api.github.com/users/username/orgs",
"repos_url": "https://api.github.com/users/username/repos",
"events_url": "https://api.github.com/users/username/events{/privacy}",
"received_events_url": "https://api.github.com/users/username/received_events",
"type": "User",
"site_admin": False
},
"html_url": "https://github.com/brngylni/demo_repo",
"html_url": "https://github.com/username/demo_repo",
"description": None,
"fork": False,
"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",
"url": "https://github.com/username/demo_repo",
"forks_url": "https://api.github.com/repos/username/demo_repo/forks",
"keys_url": "https://api.github.com/repos/username/demo_repo/keys{/key_id}",
"collaborators_url": "https://api.github.com/repos/username/demo_repo/collaborators{/collaborator}",
"teams_url": "https://api.github.com/repos/username/demo_repo/teams",
"hooks_url": "https://api.github.com/repos/username/demo_repo/hooks",
"issue_events_url": "https://api.github.com/repos/username/demo_repo/issues/events{/number}",
"events_url": "https://api.github.com/repos/username/demo_repo/events",
"assignees_url": "https://api.github.com/repos/username/demo_repo/assignees{/user}",
"branches_url": "https://api.github.com/repos/username/demo_repo/branches{/branch}",
"tags_url": "https://api.github.com/repos/username/demo_repo/tags",
"blobs_url": "https://api.github.com/repos/username/demo_repo/git/blobs{/sha}",
"git_tags_url": "https://api.github.com/repos/username/demo_repo/git/tags{/sha}",
"git_refs_url": "https://api.github.com/repos/username/demo_repo/git/refs{/sha}",
"trees_url": "https://api.github.com/repos/username/demo_repo/git/trees{/sha}",
"statuses_url": "https://api.github.com/repos/username/demo_repo/statuses/{sha}",
"languages_url": "https://api.github.com/repos/username/demo_repo/languages",
"stargazers_url": "https://api.github.com/repos/username/demo_repo/stargazers",
"contributors_url": "https://api.github.com/repos/username/demo_repo/contributors",
"subscribers_url": "https://api.github.com/repos/username/demo_repo/subscribers",
"subscription_url": "https://api.github.com/repos/username/demo_repo/subscription",
"commits_url": "https://api.github.com/repos/username/demo_repo/commits{/sha}",
"git_commits_url": "https://api.github.com/repos/username/demo_repo/git/commits{/sha}",
"comments_url": "https://api.github.com/repos/username/demo_repo/comments{/number}",
"issue_comment_url": "https://api.github.com/repos/username/demo_repo/issues/comments{/number}",
"contents_url": "https://api.github.com/repos/username/demo_repo/contents/{+path}",
"compare_url": "https://api.github.com/repos/username/demo_repo/compare/{base}...{head}",
"merges_url": "https://api.github.com/repos/username/demo_repo/merges",
"archive_url": "https://api.github.com/repos/username/demo_repo/{archive_format}{/ref}",
"downloads_url": "https://api.github.com/repos/username/demo_repo/downloads",
"issues_url": "https://api.github.com/repos/username/demo_repo/issues{/number}",
"pulls_url": "https://api.github.com/repos/username/demo_repo/pulls{/number}",
"milestones_url": "https://api.github.com/repos/username/demo_repo/milestones{/number}",
"notifications_url": "https://api.github.com/repos/username/demo_repo/notifications{?since,all,participating}",
"labels_url": "https://api.github.com/repos/username/demo_repo/labels{/name}",
"releases_url": "https://api.github.com/repos/username/demo_repo/releases{/id}",
"deployments_url": "https://api.github.com/repos/username/demo_repo/deployments",
"created_at": 1717013663,
"updated_at": "2024-07-22T21:14:26Z",
"pushed_at": 1721682985,
"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",
"git_url": "git://github.com/username/demo_repo.git",
"ssh_url": "[email protected]:username/demo_repo.git",
"clone_url": "https://github.com/username/demo_repo.git",
"svn_url": "https://github.com/username/demo_repo",
"homepage": None,
"size": 34,
"stargazers_count": 0,
Expand Down Expand Up @@ -107,51 +112,51 @@
"master_branch": "main"
},
"pusher": {
"name": "brngylni",
"email": "66281850+brngylni@users.noreply.github.com"
"name": "username",
"email": "66281850+username@users.noreply.github.com"
},
"sender": {
"login": "brngylni",
"login": "username",
"id": 66281850,
"node_id": "MDQ6VXNlcjY2MjgxODUw",
"avatar_url": "https://avatars.githubusercontent.com/u/66281850?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/brngylni",
"html_url": "https://github.com/brngylni",
"followers_url": "https://api.github.com/users/brngylni/followers",
"following_url": "https://api.github.com/users/brngylni/following{/other_user}",
"gists_url": "https://api.github.com/users/brngylni/gists{/gist_id}",
"starred_url": "https://api.github.com/users/brngylni/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/brngylni/subscriptions",
"organizations_url": "https://api.github.com/users/brngylni/orgs",
"repos_url": "https://api.github.com/users/brngylni/repos",
"events_url": "https://api.github.com/users/brngylni/events{/privacy}",
"received_events_url": "https://api.github.com/users/brngylni/received_events",
"url": "https://api.github.com/users/username",
"html_url": "https://github.com/username",
"followers_url": "https://api.github.com/users/username/followers",
"following_url": "https://api.github.com/users/username/following{/other_user}",
"gists_url": "https://api.github.com/users/username/gists{/gist_id}",
"starred_url": "https://api.github.com/users/username/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/username/subscriptions",
"organizations_url": "https://api.github.com/users/username/orgs",
"repos_url": "https://api.github.com/users/username/repos",
"events_url": "https://api.github.com/users/username/events{/privacy}",
"received_events_url": "https://api.github.com/users/username/received_events",
"type": "User",
"site_admin": False
},
"created": False,
"deleted": False,
"forced": False,
"base_ref": None,
"compare": "https://github.com/brngylni/demo_repo/compare/034c31e8e464...443f68ce112a",
"compare": "https://github.com/username/demo_repo/compare/034c31e8e464...443f68ce112a",
"commits": [
{
"id": "443f68ce112ab055bbf48666762a626baa4729f6",
"tree_id": "6c91650b5abfaa946051d8c51b6e18eafddbf159",
"distinct": True,
"message": "demo",
"timestamp": "2024-07-23T00:16:24+03:00",
"url": "https://github.com/brngylni/demo_repo/commit/443f68ce112ab055bbf48666762a626baa4729f6",
"url": "https://github.com/username/demo_repo/commit/443f68ce112ab055bbf48666762a626baa4729f6",
"author": {
"name": "Mehmet Baran Geylani",
"email": "barankara5@gmail.com",
"username": "brngylni"
"name": "Name Surname",
"email": "example@example.com",
"username": "username"
},
"committer": {
"name": "Mehmet Baran Geylani",
"email": "barankara5@gmail.com",
"username": "brngylni"
"name": "Name Surname",
"email": "example@example.com",
"username": "username"
},
"added": [],
"removed": [],
Expand All @@ -166,16 +171,16 @@
"distinct": True,
"message": "demo",
"timestamp": "2024-07-23T00:16:24+03:00",
"url": "https://github.com/brngylni/demo_repo/commit/443f68ce112ab055bbf48666762a626baa4729f6",
"url": "https://github.com/username/demo_repo/commit/443f68ce112ab055bbf48666762a626baa4729f6",
"author": {
"name": "Mehmet Baran Geylani",
"email": "barankara5@gmail.com",
"username": "brngylni"
"name": "Name Surname",
"email": "example@example.com",
"username": "username"
},
"committer": {
"name": "Mehmet Baran Geylani",
"email": "barankara5@gmail.com",
"username": "brngylni"
"name": "Name Surname",
"email": "example@example.com",
"username": "username"
},
"added": [],
"removed": [],
Expand All @@ -184,20 +189,14 @@
]
}
}

headers ={
"Host": "2e3d-31-223-50-82.ngrok-free.app",
"User-Agent": "GitHub-Hookshot/4e5b6c2",
"Content-Length": 7226,
"Accept": "*/*",
"Content-Type": "application/json",
"X-Forwarded-For": "140.82.115.123",
"X-Forwarded-Host": "2e3d-31-223-50-82.ngrok-free.app",
"X-Forwarded-Proto": "https",
"X-Github-Delivery": "2a49023a-201c-11ef-94cd-dbd2347128d4",
"X-Github-Event": "push",
"X-Github-Hook-Id": 481337580,
"X-Github-Hook-Installation-Target-Id": 807808293,
"X-Github-Hook-Installation-Target-Type": "repository",
"Accept-Encoding": "gzip"
token = "68078698f22148389daf33d32a339e14"
hashed_value = hmac.new(token.encode('utf-8'), msg=json.dumps(request).encode("utf-8"), digestmod=hashlib.sha256)
headers = {
"X-Hub-Signature-256": f"sha256={hashed_value.hexdigest()}",
"X-Github-Event": "push",
"X-Github-Delivery": "f1064eb2-4995-11ef-82e4-18ae0022c13c",
"X-Github-Hook-Id": "491622597",
"X-Github-Hook-Installation-Target-Id": "807808293",
"X-Github-Hook-Installation-Target-Type": "repository",
"X-Hub-Signature": "sha1=0e44dae9a9c979dc05d1f5846b06fe578e581533",
}
32 changes: 20 additions & 12 deletions tests/test_message/test_message_create.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,50 @@
import pytest
from .event_sample import request, headers


@pytest.mark.parametrize(
"data, headers, id, code",
[
def generate_test_params(service):
return [
pytest.param(
request,
headers,
1,
service.uuid,
200,
id="MESSAGE Endpoint - 200 Success",
),
pytest.param(
request,
{**headers, "X-Hub-Signature-256": ""},
1,
service.uuid,
400,
id="MESSAGE Endpoint - 400 Bad Request"
),
pytest.param(
None,
headers,
1,
service.uuid,
415,
id="MESSAGE Endpoint - 415 Unsupported Media Type"
),
pytest.param(
request,
headers,
2,
"non-existent-uuid", # For the 404 test case
404,
id="MESSAGE Endpoint - Not Found"
)
]
)

@pytest.mark.usefixtures("create_service")
def test_message_create(client, data, headers, id, code):
response = client.post(f"/message/{id}", json=data, headers=headers)
assert response.status_code == code
def test_message_create(client, create_service):
test_params = generate_test_params(create_service)

@pytest.mark.parametrize(
"data, headers, id, code",
test_params
)
def inner_test(client, data, headers, id, code):
response = client.post(f"/message/{id}", json=data, headers=headers)
assert response.status_code == code

# Call the inner test function with each set of parameters
for param in test_params:
inner_test(client, *param.values)

0 comments on commit cd87148

Please sign in to comment.