2121from readthedocs .integrations .models import Integration
2222from readthedocs .oauth .models import RemoteOrganization , RemoteRepository
2323from readthedocs .projects .models import Feature , Project
24+ from readthedocs .restapi .views .integrations import GitHubWebhookView
2425from readthedocs .restapi .views .task_views import get_status_data
2526
2627super_auth = base64 .b64encode (b'super:test' ).decode ('utf-8' )
@@ -632,18 +633,21 @@ class IntegrationsTests(TestCase):
632633
633634 def setUp (self ):
634635 self .project = get (Project )
635- self .version = get (Version , verbose_name = 'master' , project = self .project )
636+ self .version = get (Version , verbose_name = 'master' , active = True , project = self .project )
637+ self .version_tag = get (Version , verbose_name = 'v1.0' , active = True , project = self .project )
636638
637- def test_github_webhook (self , trigger_build ):
639+ def test_github_webhook_for_branches (self , trigger_build ):
638640 """GitHub webhook API."""
639641 client = APIClient ()
642+
640643 client .post (
641644 '/api/v2/webhook/github/{0}/' .format (self .project .slug ),
642645 {'ref' : 'master' },
643646 format = 'json' ,
644647 )
645648 trigger_build .assert_has_calls (
646- [mock .call (force = True , version = mock .ANY , project = self .project )])
649+ [mock .call (force = True , version = self .version , project = self .project )])
650+
647651 client .post (
648652 '/api/v2/webhook/github/{0}/' .format (self .project .slug ),
649653 {'ref' : 'non-existent' },
@@ -652,6 +656,52 @@ def test_github_webhook(self, trigger_build):
652656 trigger_build .assert_has_calls (
653657 [mock .call (force = True , version = mock .ANY , project = self .project )])
654658
659+ client .post (
660+ '/api/v2/webhook/github/{0}/' .format (self .project .slug ),
661+ {'ref' : 'refs/heads/master' },
662+ format = 'json' ,
663+ )
664+ trigger_build .assert_has_calls (
665+ [mock .call (force = True , version = self .version , project = self .project )])
666+
667+ def test_github_webhook_for_tags (self , trigger_build ):
668+ """GitHub webhook API."""
669+ client = APIClient ()
670+
671+ client .post (
672+ '/api/v2/webhook/github/{0}/' .format (self .project .slug ),
673+ {'ref' : 'v1.0' },
674+ format = 'json' ,
675+ )
676+ trigger_build .assert_has_calls (
677+ [mock .call (force = True , version = self .version_tag , project = self .project )])
678+
679+ client .post (
680+ '/api/v2/webhook/github/{0}/' .format (self .project .slug ),
681+ {'ref' : 'refs/heads/non-existent' },
682+ format = 'json' ,
683+ )
684+ trigger_build .assert_has_calls (
685+ [mock .call (force = True , version = mock .ANY , project = self .project )])
686+
687+ client .post (
688+ '/api/v2/webhook/github/{0}/' .format (self .project .slug ),
689+ {'ref' : 'refs/tags/v1.0' },
690+ format = 'json' ,
691+ )
692+ trigger_build .assert_has_calls (
693+ [mock .call (force = True , version = self .version_tag , project = self .project )])
694+
695+ def test_github_parse_ref (self , trigger_build ):
696+ wh = GitHubWebhookView ()
697+
698+ self .assertEqual (wh ._normalize_ref ('refs/heads/master' ), 'master' )
699+ self .assertEqual (wh ._normalize_ref ('refs/heads/v0.1' ), 'v0.1' )
700+ self .assertEqual (wh ._normalize_ref ('refs/tags/v0.1' ), 'v0.1' )
701+ self .assertEqual (wh ._normalize_ref ('refs/tags/tag' ), 'tag' )
702+ self .assertEqual (wh ._normalize_ref ('refs/heads/stable/2018' ), 'stable/2018' )
703+ self .assertEqual (wh ._normalize_ref ('refs/tags/tag/v0.1' ), 'tag/v0.1' )
704+
655705 def test_github_invalid_webhook (self , trigger_build ):
656706 """GitHub webhook unhandled event."""
657707 client = APIClient ()
0 commit comments