mirror of
https://github.com/mikf/gallery-dl.git
synced 2024-11-21 18:22:30 +01:00
extend OAuth tests
This commit is contained in:
parent
0d142e403c
commit
83e7a25b6b
@ -1,7 +1,7 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Copyright 2018-2020 Mike Fährmann
|
||||
# Copyright 2018-2023 Mike Fährmann
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License version 2 as
|
||||
@ -10,6 +10,7 @@
|
||||
import os
|
||||
import sys
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
from gallery_dl import oauth, text # noqa E402
|
||||
@ -66,6 +67,53 @@ class TestOAuthSession(unittest.TestCase):
|
||||
self.assertTrue(len(quoted) >= 3)
|
||||
self.assertEqual(quoted_hex.upper(), quoted_hex)
|
||||
|
||||
def test_generate_signature(self):
|
||||
client = oauth.OAuth1Client(
|
||||
CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
|
||||
|
||||
request = MockRequest()
|
||||
params = []
|
||||
self.assertEqual(
|
||||
client.generate_signature(request, params),
|
||||
"Wt2xo49dM5pkL4gsnCakNdHaVUo%3D")
|
||||
|
||||
request = MockRequest("https://example.org/")
|
||||
params = [("hello", "world"), ("foo", "bar")]
|
||||
self.assertEqual(
|
||||
client.generate_signature(request, params),
|
||||
"ay2269%2F8uKpZqKJR1doTtpv%2Bzn0%3D")
|
||||
|
||||
request = MockRequest("https://example.org/index.html"
|
||||
"?hello=world&foo=bar", method="POST")
|
||||
params = [("oauth_signature_method", "HMAC-SHA1")]
|
||||
self.assertEqual(
|
||||
client.generate_signature(request, params),
|
||||
"yVZWb1ts4smdMmXxMlhaXrkoOng%3D")
|
||||
|
||||
def test_dunder_call(self):
|
||||
client = oauth.OAuth1Client(
|
||||
CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
|
||||
request = MockRequest("https://example.org/")
|
||||
|
||||
with patch("time.time") as tmock, \
|
||||
patch("gallery_dl.oauth.nonce") as nmock:
|
||||
tmock.return_value = 123456789.123
|
||||
nmock.return_value = "abcdefghijklmno"
|
||||
|
||||
client(request)
|
||||
|
||||
self.assertEqual(
|
||||
request.headers["Authorization"],
|
||||
"""OAuth \
|
||||
oauth_consumer_key="key",\
|
||||
oauth_nonce="abcdefghijklmno",\
|
||||
oauth_signature_method="HMAC-SHA1",\
|
||||
oauth_timestamp="123456789",\
|
||||
oauth_version="1.0",\
|
||||
oauth_token="accesskey",\
|
||||
oauth_signature="DjtTk5j5P3BDZFnstZ%2FtEYcwD6c%3D"\
|
||||
""")
|
||||
|
||||
def test_request_token(self):
|
||||
response = self._oauth_request(
|
||||
"/request_token.php", {})
|
||||
@ -110,5 +158,13 @@ class TestOAuthSession(unittest.TestCase):
|
||||
raise unittest.SkipTest()
|
||||
|
||||
|
||||
class MockRequest():
|
||||
|
||||
def __init__(self, url="", method="GET"):
|
||||
self.url = url
|
||||
self.method = method
|
||||
self.headers = {}
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main(warnings="ignore")
|
||||
|
Loading…
Reference in New Issue
Block a user