@@ -113,3 +113,68 @@ def test_oauth(self, helpers):
113113 status = 400 ,
114114 )
115115 cli .revoke_access_token (token = "token" )
116+
117+ def test_subscribe_push_notification (self ):
118+ HUB_URL = "https://pubsubhubbub.appspot.com/subscribe"
119+ cli = Client (client_id = "id" , client_secret = "secret" )
120+
121+ # subscribe returns True on 202 Accepted
122+ with responses .RequestsMock () as m :
123+ m .add (method = "POST" , url = HUB_URL , status = 202 )
124+ result = cli .subscribe_push_notification (
125+ channel_id = "UCxxxxxx" ,
126+ callback_url = "https://example.com/webhook" ,
127+ )
128+ assert result is True
129+ # verify hub.mode and hub.topic were sent correctly
130+ assert m .calls [0 ].request .body is not None
131+ assert "hub.mode=subscribe" in m .calls [0 ].request .body
132+ assert "UCxxxxxx" in m .calls [0 ].request .body
133+
134+ # unsubscribe returns True on 202 Accepted
135+ with responses .RequestsMock () as m :
136+ m .add (method = "POST" , url = HUB_URL , status = 202 )
137+ result = cli .subscribe_push_notification (
138+ channel_id = "UCxxxxxx" ,
139+ callback_url = "https://example.com/webhook" ,
140+ mode = "unsubscribe" ,
141+ )
142+ assert result is True
143+ assert "hub.mode=unsubscribe" in m .calls [0 ].request .body
144+
145+ # sync verify returns True on 204 No Content
146+ with responses .RequestsMock () as m :
147+ m .add (method = "POST" , url = HUB_URL , status = 204 )
148+ result = cli .subscribe_push_notification (
149+ channel_id = "UCxxxxxx" ,
150+ callback_url = "https://example.com/webhook" ,
151+ verify = "sync" ,
152+ )
153+ assert result is True
154+
155+ # optional params: lease_seconds and secret are included in request body
156+ with responses .RequestsMock () as m :
157+ m .add (method = "POST" , url = HUB_URL , status = 202 )
158+ cli .subscribe_push_notification (
159+ channel_id = "UCxxxxxx" ,
160+ callback_url = "https://example.com/webhook" ,
161+ lease_seconds = 432000 ,
162+ secret = "mysecret" ,
163+ )
164+ body = m .calls [0 ].request .body
165+ assert "hub.lease_seconds=432000" in body
166+ assert "hub.secret=mysecret" in body
167+
168+ # hub error raises PyYouTubeException
169+ with pytest .raises (PyYouTubeException ):
170+ with responses .RequestsMock () as m :
171+ m .add (
172+ method = "POST" ,
173+ url = HUB_URL ,
174+ json = {"error" : {"code" : 400 , "message" : "bad request" }},
175+ status = 400 ,
176+ )
177+ cli .subscribe_push_notification (
178+ channel_id = "UCxxxxxx" ,
179+ callback_url = "https://example.com/webhook" ,
180+ )
0 commit comments