question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

unit test for webhook

See original GitHub issue

Hi @gonchik

I noticed you have #806 merged. Thank you.

As we just invoke the original rest api #806, the unit tests would be too simple.

Do we need to add this kind of code style of unit test for this feature? If yes, I’d like to summit another PR. If no, could you please show some better code style of unit test?

Here is the snippet. It seems stupid, but it does work.

from atlassian import Bitbucket
from unittest.mock import patch
from unittest import TestCase

class TestWebhook(TestCase):

    def setUp(self):
        self.fake_data = {
            "project_key": "fake_project",
            "repository_slug": "fake_repo",
            "name": "fake_name",
            "events": ["repo:refs_changed", "pr:merged", "pr:opened"],
            "webhook_url": "https://example.com",
            "active": True,
            "secret": "fake_secret",
        }

    def test_create_webhook(self):
        return_value = {
            "id": 10,
            "name": "fake_name",
            "createdDate": 1513106011000,
            "updatedDate": 1513106011000,
            "events": [
                "repo:refs_changed",
                "pr:merged",
                "pr:opened"
            ],
            "configuration": {
                "secret": "fake_secret"
            },
            "url": "https://example.com",
            "active": True
        }
        with patch.object(Bitbucket, "post", return_value=return_value):
            bitbucket = Bitbucket(url="https://bitbucket.example.com", username="username", password="password")
            rst = bitbucket.create_webhook(self.fake_data["project_key"], self.fake_data["repository_slug"],
                                           self.fake_data["name"], self.fake_data["events"],
                                           self.fake_data["webhook_url"], self.fake_data["active"])
            self.assertEqual(rst["name"], self.fake_data["name"])
            self.assertEqual(rst["events"], self.fake_data["events"])
            self.assertEqual(rst["configuration"]["secret"], self.fake_data["secret"])
            self.assertEqual(rst["url"], self.fake_data["webhook_url"])
            self.assertEqual(rst["active"], self.fake_data["active"])

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
zhan9sancommented, Aug 9, 2021

Hi @Spacetown

Thanks for your advice.

I re-considered this approach of test and found it is really amazing.

I narrowly considered the test is unit test, but it’s more like an integration test here. Session.request is mocked in the furthest edge parts we cannot control. That’s a good point.

The rest tests will be added these days.

Besides, this inspires me to add tests to Artifactory Project.

0reactions
Spacetowncommented, Aug 9, 2021

@zhan9san That was the reason why I introduced this mookup. Ther where a couble of errors in refactroing the BB implementation. For implementing the OO interface I searched for a way to test all functions in the interface and implemnted this mookup. It’s a little bit of work to implement the responses but than you can test a specific endpoint in detail.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to test webhooks and response statuses using MailSlurp
For automated testing we can use MailSlurp @mailslurp/test-webhooks library to setup real http endpoints that can respond with a set of responses we...
Read more >
Testing Webhook Receivers | Svix Resoures
Testing webhook receivers is similar to testing an API endpoint, though the difference here ... Unit tests are used to test the smallest...
Read more >
How to create a GitHub Action to run Unit Tests & perform ...
First, we use actions/checkout, this will check out the code and allow our Action to run the NPM commands successfully. We then provide...
Read more >
How to write Unit test cases for Web hooks in c#? - Stack ...
I want to mock the web hook request (Post request) in unit test cases and pass the request headers and body as the...
Read more >
Testing Webhooks > Stripe Level 2 - SymfonyCasts
The first strategy - and the one we use here on KnpU - is to create an automated test that sends a fake...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found