diff --git a/test/test_oauth.py b/test/test_oauth.py index 7455928d..0082419d 100644 --- a/test/test_oauth.py +++ b/test/test_oauth.py @@ -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")