Skip to content

Commit

Permalink
Add logoutQuietly function
Browse files Browse the repository at this point in the history
  • Loading branch information
Jasper Aikema committed Jul 9, 2024
1 parent fe43336 commit e3b5c1e
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
24 changes: 24 additions & 0 deletions contents/salt.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,27 @@ def authenticate(self):
return None
else:
raise Exception(f"Unexpected failure interacting with salt-api {response.text()}")

def logoutQuietly(self, authToken):
"""
Remove or invalidate sessions
:header authToken: The token of the session
"""

headers = {
"X-Auth-Token": authToken,
}

url = f"{self.endpoint}/logout"

logger.info("Logging out with salt-api endpoint: [%s]", url)

try:
requests.post(url,
headers=headers)
except requests.exceptions.ConnectionError as e:
logger.warning("Encountered exception (%s) while trying to logout. Ignoring...", e)
pass

except InterruptedError:
logger.warning("Interrupted while trying to logout.")
2 changes: 1 addition & 1 deletion contents/tests/test_authenticate.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import sys,os,time
import sys, os, time
import unittest
import json
from unittest import mock
Expand Down
53 changes: 53 additions & 0 deletions contents/tests/test_logout.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import sys, os
import unittest
from requests.exceptions import ConnectionError
from unittest import mock

sys.path.append(os.getcwd())
from contents.salt import SaltApiNodeStepPlugin


class TestSaltApiNodeStepPlugin(unittest.TestCase):

def setUp(self):

self.PARAM_ENDPOINT = "https://localhost"
self.PARAM_EAUTH = "pam"
self.PARAM_MINION_NAME = "minion"
self.PARAM_FUNCTION = "some.function"
self.PARAM_USER = "user"
self.PARAM_PASSWORD = "password&!@$*"
self.AUTH_TOKEN = "123qwe"
self.OUTPUT_JID = "20130213093536481553"
self.HOST_RESPONSE = "\"some response\""

self.plugin = SaltApiNodeStepPlugin(self.PARAM_ENDPOINT, self.PARAM_USER, self.PARAM_PASSWORD)

@mock.patch('requests.post')
def test_logout(self, mock_post):
mock_post.return_value.status_code = 200
mock_post.return_value.json.return_value = {}

result = self.plugin.logoutQuietly(self.AUTH_TOKEN)

self.assertIsNone(result)
mock_post.assert_called_once_with(
self.PARAM_ENDPOINT+'/logout',
headers={"X-Auth-Token": self.AUTH_TOKEN}
)

@mock.patch('requests.post')
def test_logout_throws_IOException_remains_quiet(self, mock_post):
mock_post.side_effect = ConnectionError('test ConnectionError')

result = self.plugin.logoutQuietly(self.AUTH_TOKEN)

self.assertIsNone(result)

@mock.patch('requests.post')
def test_logout_throws_interrupted_exception_remains_quiet(self, mock_post):
mock_post.side_effect = InterruptedError('test InterruptedError')

result = self.plugin.logoutQuietly(self.AUTH_TOKEN)

self.assertIsNone(result)

0 comments on commit e3b5c1e

Please sign in to comment.