2018-02-11 16:04:40 +01:00
|
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
2020-05-02 01:15:50 +02:00
|
|
|
|
# Copyright 2018-2020 Mike Fährmann
|
2018-02-11 16:04:40 +01:00
|
|
|
|
#
|
|
|
|
|
# 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
|
|
|
|
|
# published by the Free Software Foundation.
|
|
|
|
|
|
2020-05-02 01:15:50 +02:00
|
|
|
|
import os
|
|
|
|
|
import sys
|
2018-02-11 16:04:40 +01:00
|
|
|
|
import unittest
|
|
|
|
|
|
2020-05-02 01:15:50 +02:00
|
|
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
|
from gallery_dl import oauth, text # noqa E402
|
2018-02-11 16:04:40 +01:00
|
|
|
|
|
2020-05-10 00:33:00 +02:00
|
|
|
|
TESTSERVER = "http://term.ie/oauth/example"
|
2018-06-28 14:32:02 +02:00
|
|
|
|
TESTSERVER = "http://term.ie/oauth/example"
|
2018-02-11 16:04:40 +01:00
|
|
|
|
CONSUMER_KEY = "key"
|
|
|
|
|
CONSUMER_SECRET = "secret"
|
|
|
|
|
REQUEST_TOKEN = "requestkey"
|
|
|
|
|
REQUEST_TOKEN_SECRET = "requestsecret"
|
|
|
|
|
ACCESS_TOKEN = "accesskey"
|
|
|
|
|
ACCESS_TOKEN_SECRET = "accesssecret"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestOAuthSession(unittest.TestCase):
|
|
|
|
|
|
|
|
|
|
def test_concat(self):
|
2018-05-10 18:26:10 +02:00
|
|
|
|
concat = oauth.concat
|
2018-02-11 16:04:40 +01:00
|
|
|
|
|
|
|
|
|
self.assertEqual(concat(), "")
|
|
|
|
|
self.assertEqual(concat("str"), "str")
|
|
|
|
|
self.assertEqual(concat("str1", "str2"), "str1&str2")
|
|
|
|
|
|
|
|
|
|
self.assertEqual(concat("&", "?/"), "%26&%3F%2F")
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
concat("GET", "http://example.org/", "foo=bar&baz=a"),
|
|
|
|
|
"GET&http%3A%2F%2Fexample.org%2F&foo%3Dbar%26baz%3Da"
|
|
|
|
|
)
|
|
|
|
|
|
2018-05-10 18:26:10 +02:00
|
|
|
|
def test_nonce(self, size=16):
|
|
|
|
|
nonce_values = set(oauth.nonce(size) for _ in range(size))
|
2018-02-11 16:04:40 +01:00
|
|
|
|
|
|
|
|
|
# uniqueness
|
2018-05-10 18:26:10 +02:00
|
|
|
|
self.assertEqual(len(nonce_values), size)
|
2018-02-11 16:04:40 +01:00
|
|
|
|
|
|
|
|
|
# length
|
|
|
|
|
for nonce in nonce_values:
|
2018-05-10 18:26:10 +02:00
|
|
|
|
self.assertEqual(len(nonce), size)
|
2018-02-11 16:04:40 +01:00
|
|
|
|
|
|
|
|
|
def test_quote(self):
|
2018-05-10 18:26:10 +02:00
|
|
|
|
quote = oauth.quote
|
2018-02-11 16:04:40 +01:00
|
|
|
|
|
|
|
|
|
reserved = ",;:!\"§$%&/(){}[]=?`´+*'äöü"
|
|
|
|
|
unreserved = ("ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
|
"abcdefghijklmnopqrstuvwxyz"
|
|
|
|
|
"0123456789-._~")
|
|
|
|
|
|
|
|
|
|
for char in unreserved:
|
|
|
|
|
self.assertEqual(quote(char), char)
|
|
|
|
|
|
|
|
|
|
for char in reserved:
|
|
|
|
|
quoted = quote(char)
|
|
|
|
|
quoted_hex = quoted.replace("%", "")
|
|
|
|
|
self.assertTrue(quoted.startswith("%"))
|
|
|
|
|
self.assertTrue(len(quoted) >= 3)
|
|
|
|
|
self.assertEqual(quoted_hex.upper(), quoted_hex)
|
|
|
|
|
|
|
|
|
|
def test_request_token(self):
|
|
|
|
|
response = self._oauth_request(
|
2018-06-28 14:32:02 +02:00
|
|
|
|
"/request_token.php", {})
|
2018-02-11 16:04:40 +01:00
|
|
|
|
expected = "oauth_token=requestkey&oauth_token_secret=requestsecret"
|
|
|
|
|
self.assertEqual(response, expected, msg=response)
|
|
|
|
|
|
|
|
|
|
data = text.parse_query(response)
|
|
|
|
|
self.assertTrue(data["oauth_token"], REQUEST_TOKEN)
|
|
|
|
|
self.assertTrue(data["oauth_token_secret"], REQUEST_TOKEN_SECRET)
|
|
|
|
|
|
|
|
|
|
def test_access_token(self):
|
|
|
|
|
response = self._oauth_request(
|
2018-06-28 14:32:02 +02:00
|
|
|
|
"/access_token.php", {}, REQUEST_TOKEN, REQUEST_TOKEN_SECRET)
|
2018-02-11 16:04:40 +01:00
|
|
|
|
expected = "oauth_token=accesskey&oauth_token_secret=accesssecret"
|
|
|
|
|
self.assertEqual(response, expected, msg=response)
|
|
|
|
|
|
|
|
|
|
data = text.parse_query(response)
|
|
|
|
|
self.assertTrue(data["oauth_token"], ACCESS_TOKEN)
|
|
|
|
|
self.assertTrue(data["oauth_token_secret"], ACCESS_TOKEN_SECRET)
|
|
|
|
|
|
|
|
|
|
def test_authenticated_call(self):
|
2018-05-10 18:26:10 +02:00
|
|
|
|
params = {"method": "foo", "a": "äöüß/?&#", "äöüß/?&#": "a"}
|
2018-02-11 16:04:40 +01:00
|
|
|
|
response = self._oauth_request(
|
2018-06-28 14:32:02 +02:00
|
|
|
|
"/echo_api.php", params, ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
|
2018-02-11 16:04:40 +01:00
|
|
|
|
|
|
|
|
|
self.assertEqual(text.parse_query(response), params)
|
|
|
|
|
|
|
|
|
|
def _oauth_request(self, endpoint, params=None,
|
|
|
|
|
oauth_token=None, oauth_token_secret=None):
|
2018-05-10 18:26:10 +02:00
|
|
|
|
session = oauth.OAuth1Session(
|
2018-02-11 16:04:40 +01:00
|
|
|
|
CONSUMER_KEY, CONSUMER_SECRET,
|
|
|
|
|
oauth_token, oauth_token_secret,
|
|
|
|
|
)
|
2020-05-10 00:33:00 +02:00
|
|
|
|
try:
|
|
|
|
|
return session.get(TESTSERVER + endpoint, params=params).text
|
|
|
|
|
except OSError:
|
|
|
|
|
raise unittest.SkipTest()
|
2018-02-11 16:04:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
unittest.main(warnings="ignore")
|