From 8895db264ee131cb7dc825bebfdb971803cfa328 Mon Sep 17 00:00:00 2001
From: Jordan Borean <jborean93@gmail.com>
Date: Sat, 30 Nov 2019 21:07:39 +1000
Subject: [PATCH] Attempt to get tests working again on pywin32

---
 smbprotocol/session.py     |  2 +-
 tests/test_smbclient_os.py | 23 ++++++++++++++++-------
 2 files changed, 17 insertions(+), 8 deletions(-)

diff --git a/smbprotocol/session.py b/smbprotocol/session.py
index 72ed797..c5e7246 100644
--- a/smbprotocol/session.py
+++ b/smbprotocol/session.py
@@ -289,7 +289,7 @@ class Session(object):
         server_mechs = list(
             token['innerContextToken']['negTokenInit']['mechTypes']
         )
-        if MechTypes.MS_KRB5 in server_mechs and MechTypes.KRB5:
+        if MechTypes.MS_KRB5 in server_mechs and MechTypes.KRB5 in server_mechs:
             log.debug("Both MS_KRB5 and KRB5 received in the initial SPNGEO "
                       "token, removing MS_KRB5 to avoid duplication of work")
             server_mechs.remove(MechTypes.MS_KRB5)
diff --git a/tests/test_smbclient_os.py b/tests/test_smbclient_os.py
index fccb9dd..1ffb40a 100644
--- a/tests/test_smbclient_os.py
+++ b/tests/test_smbclient_os.py
@@ -39,6 +39,12 @@ from smbprotocol.reparse_point import (
     ReparseTags,
 )
 
+HAS_SSPI = False
+try:
+    import sspi
+except ImportError:
+    pass
+
 
 @pytest.mark.parametrize('path', [
     '\\\\only_server',
@@ -53,10 +59,12 @@ def test_open_bad_path(path):
 def test_reset_connection(smb_share):
     smbclient.reset_connection_cache()
 
-    # Once we've reset the connection it should fail because we didn't set any credentials
-    expected = 'Failed to authenticate with server'
-    with pytest.raises(SMBAuthenticationError, match=expected):
-        smbclient.stat(smb_share)
+    # Once we've reset the connection it should fail because we didn't set any credentials.
+    # Won't work if pywin32 is installed as implicit auth is available.
+    if not HAS_SSPI:
+        expected = 'Failed to authenticate with server'
+        with pytest.raises(SMBAuthenticationError, match=expected):
+            smbclient.stat(smb_share)
 
 
 def test_delete_session(smb_share):
@@ -64,9 +72,10 @@ def test_delete_session(smb_share):
     smbclient.delete_session(server)
 
     # Once we've closed the connection it should fail because we didn't set any credentials
-    expected = 'Failed to authenticate with server'
-    with pytest.raises(SMBAuthenticationError, match=expected):
-        smbclient.stat(smb_share)
+    if not HAS_SSPI:
+        expected = 'Failed to authenticate with server'
+        with pytest.raises(SMBAuthenticationError, match=expected):
+            smbclient.stat(smb_share)
 
 
 def test_copy_across_paths_raises(smb_share):
-- 
GitLab