|
| 1 | +import unittest |
| 2 | +import sys |
| 3 | +import os |
| 4 | +import logging |
| 5 | +from unittest.mock import patch |
| 6 | +import urllib.error |
| 7 | +import socket |
| 8 | + |
| 9 | +sys.path.append(os.path.join(os.path.dirname(os.path.dirname(__file__)))) |
| 10 | +from pysteamsignin.steamsignin import SteamSignIn |
| 11 | + |
| 12 | + |
| 13 | +def valid_results(): |
| 14 | + steam_id = "12345678901234567" |
| 15 | + claimed = f"https://steamcommunity.com/openid/id/{steam_id}" |
| 16 | + |
| 17 | + return { |
| 18 | + "openid.assoc_handle": "assoc", |
| 19 | + "openid.signed": "claimed_id,identity", |
| 20 | + "openid.sig": "sig", |
| 21 | + "openid.ns": "http://specs.openid.net/auth/2.0", |
| 22 | + "openid.claimed_id": claimed, |
| 23 | + "openid.identity": claimed, |
| 24 | + } |
| 25 | + |
| 26 | + |
| 27 | +class FakeSteamResponse: |
| 28 | + def __init__(self, body): |
| 29 | + self._body = body.encode("utf-8") |
| 30 | + |
| 31 | + def read(self): |
| 32 | + return self._body |
| 33 | + |
| 34 | + def __enter__(self): |
| 35 | + return self |
| 36 | + |
| 37 | + def __exit__(self, exc_type, exc, tb): |
| 38 | + pass |
| 39 | + |
| 40 | + |
| 41 | +class TestSteamSignIn(unittest.TestCase): |
| 42 | + |
| 43 | + @patch("urllib.request.urlopen") |
| 44 | + def test_valid_openid_response_returns_steamid_string(self, mock_urlopen): |
| 45 | + mock_urlopen.return_value = FakeSteamResponse("is_valid:true\n") |
| 46 | + |
| 47 | + signin = SteamSignIn() |
| 48 | + result = signin.ValidateResults(valid_results()) |
| 49 | + |
| 50 | + self.assertEqual(result, "12345678901234567") |
| 51 | + |
| 52 | + @patch("urllib.request.urlopen") |
| 53 | + def test_steam_is_valid_false_denies_login(self, mock_urlopen): |
| 54 | + mock_urlopen.return_value = FakeSteamResponse("is_valid:false\n") |
| 55 | + |
| 56 | + signin = SteamSignIn() |
| 57 | + result = signin.ValidateResults(valid_results()) |
| 58 | + |
| 59 | + self.assertFalse(result) |
| 60 | + |
| 61 | + @patch("urllib.request.urlopen", side_effect = socket.timeout("timed out")) |
| 62 | + def test_openid_timeout_fails_closed_and_logs_warning(self, mock_urlopen): |
| 63 | + with self.assertLogs("pysteamsignin.steamsignin", level = logging.WARNING) as logs: |
| 64 | + signin = SteamSignIn() |
| 65 | + result = signin.ValidateResults(valid_results()) |
| 66 | + |
| 67 | + self.assertFalse(result) |
| 68 | + self.assertIn("Steam OpenID verification failed", logs.output[0]) |
| 69 | + |
| 70 | + @patch("urllib.request.urlopen", side_effect = urllib.error.URLError("connection failed")) |
| 71 | + def test_network_error_fails_closed_and_logs_warning(self, mock_urlopen): |
| 72 | + with self.assertLogs("pysteamsignin.steamsignin", level = logging.WARNING) as logs: |
| 73 | + signin = SteamSignIn() |
| 74 | + result = signin.ValidateResults(valid_results()) |
| 75 | + |
| 76 | + self.assertFalse(result) |
| 77 | + self.assertIn("Steam OpenID verification failed", logs.output[0]) |
| 78 | + |
| 79 | + @patch("urllib.request.urlopen") |
| 80 | + def test_claimed_id_identity_mismatch_fails_validation(self, mock_urlopen): |
| 81 | + mock_urlopen.return_value = FakeSteamResponse("is_valid:true\n") |
| 82 | + |
| 83 | + results = valid_results() |
| 84 | + results["openid.identity"] = "https://steamcommunity.com/openid/id/DIFFERENT" |
| 85 | + |
| 86 | + signin = SteamSignIn() |
| 87 | + result = signin.ValidateResults(results) |
| 88 | + |
| 89 | + self.assertFalse(result) |
| 90 | + |
| 91 | + |
| 92 | +if __name__ == "__main__": |
| 93 | + unittest.main() |
0 commit comments