summaryrefslogtreecommitdiffstats
path: root/test/test_oauth.py
diff options
context:
space:
mode:
Diffstat (limited to 'test/test_oauth.py')
-rw-r--r--test/test_oauth.py104
1 files changed, 104 insertions, 0 deletions
diff --git a/test/test_oauth.py b/test/test_oauth.py
new file mode 100644
index 0000000..2ce5b43
--- /dev/null
+++ b/test/test_oauth.py
@@ -0,0 +1,104 @@
+#!/usr/bin/env python3
+# -*- coding: utf-8 -*-
+
+# Copyright 2018 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
+# published by the Free Software Foundation.
+
+import unittest
+
+from gallery_dl import oauth, text
+
+TESTSERVER = "http://term.ie/oauth/example"
+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):
+ concat = oauth.concat
+
+ 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"
+ )
+
+ def test_nonce(self, size=16):
+ nonce_values = set(oauth.nonce(size) for _ in range(size))
+
+ # uniqueness
+ self.assertEqual(len(nonce_values), size)
+
+ # length
+ for nonce in nonce_values:
+ self.assertEqual(len(nonce), size)
+
+ def test_quote(self):
+ quote = oauth.quote
+
+ 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(
+ "/request_token.php", {})
+ 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(
+ "/access_token.php", {}, REQUEST_TOKEN, REQUEST_TOKEN_SECRET)
+ 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):
+ params = {"method": "foo", "a": "äöüß/?&#", "äöüß/?&#": "a"}
+ response = self._oauth_request(
+ "/echo_api.php", params, ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
+
+ self.assertEqual(text.parse_query(response), params)
+
+ def _oauth_request(self, endpoint, params=None,
+ oauth_token=None, oauth_token_secret=None):
+ session = oauth.OAuth1Session(
+ CONSUMER_KEY, CONSUMER_SECRET,
+ oauth_token, oauth_token_secret,
+ )
+ url = TESTSERVER + endpoint
+ return session.get(url, params=params).text
+
+
+if __name__ == "__main__":
+ unittest.main(warnings="ignore")