1
0
mirror of https://github.com/mikf/gallery-dl.git synced 2024-11-25 04:02:32 +01:00

extend OAuth tests

This commit is contained in:
Mike Fährmann 2023-03-02 16:55:01 +01:00
parent 0d142e403c
commit 83e7a25b6b
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88

View File

@ -1,7 +1,7 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# -*- coding: utf-8 -*- # -*- 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 # 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 # it under the terms of the GNU General Public License version 2 as
@ -10,6 +10,7 @@
import os import os
import sys import sys
import unittest import unittest
from unittest.mock import patch
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from gallery_dl import oauth, text # noqa E402 from gallery_dl import oauth, text # noqa E402
@ -66,6 +67,53 @@ class TestOAuthSession(unittest.TestCase):
self.assertTrue(len(quoted) >= 3) self.assertTrue(len(quoted) >= 3)
self.assertEqual(quoted_hex.upper(), quoted_hex) 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): def test_request_token(self):
response = self._oauth_request( response = self._oauth_request(
"/request_token.php", {}) "/request_token.php", {})
@ -110,5 +158,13 @@ class TestOAuthSession(unittest.TestCase):
raise unittest.SkipTest() raise unittest.SkipTest()
class MockRequest():
def __init__(self, url="", method="GET"):
self.url = url
self.method = method
self.headers = {}
if __name__ == "__main__": if __name__ == "__main__":
unittest.main(warnings="ignore") unittest.main(warnings="ignore")