From 000cc3d00bc0d97847afa08bb372c3b26a118ed5 Mon Sep 17 00:00:00 2001 From: Daniil Bondarenko Date: Wed, 16 Feb 2022 04:49:30 +0200 Subject: [PATCH] #36: Move event details into json files --- tests/conftest.py | 14 ++++- tests/test_alert.py | 4 +- tests/test_app.py | 129 +++---------------------------------------- tests/test_gitlab.py | 4 +- tests/test_ws.py | 4 +- 5 files changed, 27 insertions(+), 128 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 1cbb7b1..c6074c5 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,9 +1,13 @@ import sys +import unittest + import pytest +import os +import json # each test runs on cwd to its temp dir -@pytest.fixture(autouse=True) +@pytest.fixture def go_to_tmpdir(request): # Get the fixture dynamically by its name. tmpdir = request.getfixturevalue("tmpdir") @@ -12,3 +16,11 @@ def go_to_tmpdir(request): # Chdir only for the duration of the test. with tmpdir.as_cwd(): yield + + +class AbstractTest(unittest.TestCase): + + def body(self, json_file: str, folder="tests/resources") -> dict: + filename = os.path.join(os.path.realpath(folder), json_file) + with open(filename, "r") as read_file: + return json.load(read_file) diff --git a/tests/test_alert.py b/tests/test_alert.py index f135c42..6ddc173 100644 --- a/tests/test_alert.py +++ b/tests/test_alert.py @@ -1,7 +1,7 @@ from g2w import Alert -import unittest +from .conftest import AbstractTest -class AlertTest(unittest.TestCase): +class AlertTest(AbstractTest): def test_comment(self): self.assertGreater(Alert().desc('{"user":"Tom"}').find("user%22%3A%22Tom"), 0) diff --git a/tests/test_app.py b/tests/test_app.py index 79f3ac1..0ba708e 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -1,7 +1,8 @@ import os import pytest from fastapi.testclient import TestClient -import unittest +from .conftest import AbstractTest + from g2w.__main__ import app @@ -13,15 +14,7 @@ # - https://stackoverflow.com/a/52065289/6916890 -# @todo #/DEV Move push event details into a separate *.json file. -# For example, the `json.load` or `pkg_resources` from `setuptools` might -# be used: -# - https://stackoverflow.com/a/37151805/6916890 -# - https://stackoverflow.com/a/1396657/6916890 -# - https://setuptools.pypa.io/en/latest/pkg_resources.html - - -class ClientTest(unittest.TestCase): +class ClientTest(AbstractTest): @pytest.mark.skipif(os.getenv("WS_ADMIN_EMAIL") is None, reason="Environment variable 'WS_ADMIN_EMAIL' is absent") @pytest.mark.skipif(os.getenv("WS_URL_ALL_USERS") is None, reason="Environment variable 'WS_URL_ALL_USERS' is absent") @pytest.mark.skipif(os.getenv("WS_URL_POST_COMMENT") is None, reason="Environment variable 'WS_URL_POST_COMMENT' is absent") @@ -31,74 +24,7 @@ def test_e2e_push(self): # requests_mock.get('http://test.com', text='data') response = TestClient(app).post( "/gitlab/push/223728", - json={ - "object_kind": "push", - "event_name": "push", - "before": "95790bf891e76fee5e1747ab589903a6a1f80f22", - "after": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7", - "ref": "refs/heads/master", - "checkout_sha": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7", - "user_id": 4, - "user_name": "John Smith", - "user_username": "jsmith", - "user_email": "john@example.com", - "user_avatar": "https://s.gravatar.com/avatar/d4c74594d841139328695756648b6bd6?s=8://s.gravatar.com/avatar/d4c74594d841139328695756648b6bd6?s=80", - "project_id": 15, - "project": { - "id": 15, - "name": "Diaspora", - "description": "", - "web_url": "https://example.com/mike/diaspora", - "avatar_url": None, - "git_ssh_url": "git@example.com:mike/diaspora.git", - "git_http_url": "https://example.com/mike/diaspora.git", - "namespace": "Mike", - "visibility_level": 0, - "path_with_namespace": "mike/diaspora", - "default_branch": "master", - "homepage": "https://example.com/mike/diaspora", - "url": "git@example.com:mike/diaspora.git", - "ssh_url": "git@example.com:mike/diaspora.git", - "http_url": "https://example.com/mike/diaspora.git", - }, - "repository": { - "name": "Diaspora", - "url": "git@example.com:mike/diaspora.git", - "description": "", - "homepage": "https://example.com/mike/diaspora", - "git_http_url": "https://example.com/mike/diaspora.git", - "git_ssh_url": "git@example.com:mike/diaspora.git", - "visibility_level": 0, - }, - "commits": [ - { - "id": "b6568db1bc1dcd7f8b4d5a946b0b91f9dacd7327", - "message": "Update Catalan translation to e38cb41.\n\nSee https://gitlab.com/gitlab-org/gitlab for more information", - "title": "Update Catalan translation to e38cb41.", - "timestamp": "2011-12-12T14:27:31+02:00", - "url": "https://example.com/mike/diaspora/commit/b6568db1bc1dcd7f8b4d5a946b0b91f9dacd7327", - "author": {"name": "Jordi Mallach", "email": "jordi@softcatala.org"}, - "added": ["CHANGELOG"], - "modified": ["app/controller/application.rb"], - "removed": [], - }, - { - "id": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7", - "message": "#WS-6231285: fixed readme", - "title": "#WS-6231285: fixed readme", - "timestamp": "2012-01-03T23:36:29+02:00", - "url": "https://example.com/mike/diaspora/commit/da1560886d4f094c3e6c9ef40349f7d38b5d27d7", - "author": { - "name": "GitLab dev user", - "email": "gitlabdev@dv6700.(none)", - }, - "added": ["CHANGELOG"], - "modified": ["app/controller/application.rb"], - "removed": [], - }, - ], - "total_commits_count": 2, - }, + json=self.body("sample2_commits.json") ) self.assertEqual(response.status_code, 200) self.assertGreater(int(response.json()["comments"][0]["id"]), 0) @@ -109,49 +35,10 @@ def test_e2e_push(self): def test_e2e_alert(self): response = TestClient(app).post( "/grafana/alert/223728", - json={ - "receiver": "My Super Webhook", - "status": "firing", - "orgId": 1, - "alerts": [ - { - "status": "firing", - "labels": {"alertname": "High memory usage", "team": "blue", "zone": "us-1"}, - "annotations": {"description": "The system has high memory usage", "runbook_url": "https://myrunbook.com/runbook/1234", "summary": "This alert was triggered for zone us-1"}, - "startsAt": "2021-10-12T09:51:03.157076+02:00", - "endsAt": "0001-01-01T00:00:00Z", - "generatorURL": "https://play.grafana.org/alerting/1afz29v7z/edit", - "fingerprint": "c6eadffa33fcdf37", - "silenceURL": "https://play.grafana.org/alerting/silence/new?alertmanager=grafana&matchers=alertname%3DT2%2Cteam%3Dblue%2Czone%3Dus-1", - "dashboardURL": "", - "panelURL": "", - "valueString": "[ metric='' labels={} value=14151.331895396988 ]", - }, - { - "status": "firing", - "labels": {"alertname": "High CPU usage", "team": "blue", "zone": "eu-1"}, - "annotations": {"description": "The system has high CPU usage", "runbook_url": "https://myrunbook.com/runbook/1234", "summary": "This alert was triggered for zone eu-1"}, - "startsAt": "2021-10-12T09:56:03.157076+02:00", - "endsAt": "0001-01-01T00:00:00Z", - "generatorURL": "https://play.grafana.org/alerting/d1rdpdv7k/edit", - "fingerprint": "bc97ff14869b13e3", - "silenceURL": "https://play.grafana.org/alerting/silence/new?alertmanager=grafana&matchers=alertname%3DT1%2Cteam%3Dblue%2Czone%3Deu-1", - "dashboardURL": "", - "panelURL": "", - "valueString": "[ metric='' labels={} value=47043.702386305304 ]", - }, - ], - "groupLabels": {}, - "commonLabels": {"team": "blue"}, - "commonAnnotations": {}, - "externalURL": "https://play.grafana.org/", - "version": "1", - "groupKey": "{}:{}", - "truncatedAlerts": 0, - "title": "[FIRING:2] (blue)", - "state": "alerting", - "message": "**Firing**\n\nLabels:\n - alertname = T2\n - team = blue\n - zone = us-1\nAnnotations:\n - description = This is the alert rule checking the second system\n - runbook_url = https://myrunbook.com\n - summary = This is my summary\nSource: https://play.grafana.org/alerting/1afz29v7z/edit\nSilence: https://play.grafana.org/alerting/silence/new?alertmanager=grafana&matchers=alertname%3DT2%2Cteam%3Dblue%2Czone%3Dus-1\n\nLabels:\n - alertname = T1\n - team = blue\n - zone = eu-1\nAnnotations:\nSource: https://play.grafana.org/alerting/d1rdpdv7k/edit\nSilence: https://play.grafana.org/alerting/silence/new?alertmanager=grafana&matchers=alertname%3DT1%2Cteam%3Dblue%2Czone%3Deu-1\n", - }, + json=self.body("sample1_alert.json") ) self.assertEqual(response.status_code, 200) self.assertGreater(response.json()["created"]["id"], 0) + + + diff --git a/tests/test_gitlab.py b/tests/test_gitlab.py index 777df68..6d71d43 100644 --- a/tests/test_gitlab.py +++ b/tests/test_gitlab.py @@ -2,7 +2,7 @@ import unittest from g2w import Push -from g2w import commit_msg_pattern +from .conftest import AbstractTest given = pytest.mark.parametrize @@ -86,7 +86,7 @@ ) -class PushTest(unittest.TestCase): +class PushTest(AbstractTest): def test_ctor(self): self.assertEqual(fake_push_event.ref, "refs/heads/master") diff --git a/tests/test_ws.py b/tests/test_ws.py index 8396523..8f120de 100644 --- a/tests/test_ws.py +++ b/tests/test_ws.py @@ -1,7 +1,7 @@ import os import pytest -import unittest +from .conftest import AbstractTest from g2w import Ws from .test_gitlab import fake_push_event @@ -10,7 +10,7 @@ # Right now this @pytest.mark.skipif looks too verbose. -class UsersAndCommentTest(unittest.TestCase): +class UsersAndCommentTest(AbstractTest): @pytest.mark.skipif(os.getenv("WS_INT_TESTS_DISABLED") is not None, reason="Integration tests are disabled") @pytest.mark.skipif(os.getenv("WS_URL_ALL_USERS") is None, reason="Environment variable 'WS_URL_ALL_USERS' is absent") def test_users(self):