Hi, I’ve inherited the code below. I need to write a mock test for method: __regenRToken
This is my test code so far. When I run it says that the method is called. But I cannot quite figure out how to test for the new value assigned to:
self.__clsConfig.Config['r_token']
self.R_TOKEN
in the method call in the test function.
I am a noob at mocking so I may have misconfigured the test in test.py, So I’m asking for some help here. Thanks!
–test.py
import sys
sys.path.append('src/tokens')
sys.path.append('src/configuration')
# Third-party imports...
import json
import unittest
from unittest import mock
#software under test
from t import tokens
from c import Configuration
class TestTokens(unittest.TestCase):
def setUp(self):
resp = '{"r_token": "XYZ"}'
js = json.loads(resp)
jo = json.dumps(js)
self.MockConfgClass = mock.patch('configuration.Configuration', auto_spec=True).start()
self.t = tokens(self.MockConfgClass)
self.t._tokens__jloads = mock.Mock(return_value=js)
self.t._tokens__bldApiHdr = mock.Mock(return_value='{"token":"XYZ"}')
self.t._tokens__post = mock.Mock(return_value=resp)
self.t._tokens__jdumps = mock.Mock(return_value=jo)
self.t._tokens__regenRToken = mock.Mock()
self.addCleanup(mock.patch.stopall)
def test_regenRToken(self):
self.t._tokens__regenRToken(self)
self.t._tokens__regenRToken.assert_called()
TestTokens(‘test_regenRToken’).run()
–t.py
import sys
sys.path.append('src/configuration')
sys.path.append('src/common')
import configuration as cfg
from common import Utils
import requests
import json
class tokens:
def __init__(self, config):
self.__API_RTOKEN = '/i/v1/rToken/'
self.__clsConfig = config
self.R_TOKEN = config.ALN_R_TOKEN
def __post(self, url, hdr_data):
return requests.post(url, data=hdr_data)
def __put(self, name, value):
self.__clsConfig._Configuration__s_client.put(Name=name, Value=value)
def __jloads(self, txt):
return json.loads(txt)
def __jdumps(self, jobj):
return json.dumps(jobj)
def __bldApiHdr(self, id):
return Utils.buildAPIHeader(id, self.__clsConfig.Config)
def __regenRToken(self):
api_hdr = self.__bldApiHdr(1)
response = self.__post('{base_url}{api}'.format(base_url=self.__clsConfig.URL, api=self.__API_RTOKEN), hdr_data=api_hdr)
jresp = self.__jloads(response.text)
self.__clsConfig.Config['r_token'] = jresp['r_token']
self.R_TOKEN = jresp['r_token']
self.__put(name=self.__clsConfig.APP_PS_CONFIG_NAME, value=self.__jdumps(self.__clsConfig.Config) )
–c.py
import boto3
import os
import json
class Configuration:
def __init__(self, profile, region='us-east-1', cfg_to_file=False):
self.APP_PS_CONFIG_NAME = '/alnmgr/config'
self.__s_client = self.__getClient("ssm", profile, region)
__Cfg = self.__getPValue(self.__s_client)
self.Config = json.loads(__Cfg)
self.ALN_URL = self.Config['url']
self.ALN_USER = self.Config['user']
self.ALN_USERID = self.Config['user_id']
self.ALN_R_TOKEN = self.Config['r_token']
self.ALN_R_TOKEN_NAME = self.Config['r_token_name']
self.APP_REGION = region if self.Config['region'] != region else region
self.APP_WORKING_PATH = self.Config['working_path']
self.APP_ARCHIVE_FOLDER = self.Config['archive_folder']
self.APP_API_TKN_PRE_EXP_TIME = self.Config['apitoken_pre_expiry_time']
self.APP_PROFILE = profile
if(cfg_to_file==True):
with open("CFG.json", "w") as file:
json.dump(__Cfg, file)
def __getClient(self, service, profile, region):
os.environ['AWS_PROFILE'] = profile
os.environ['AWS_DEFAULT_REGION'] = region
return boto3.client(service)
def __getPValue(self, doDecryption=True):
parameter = self.__s_client.get_parameter(Name=self.APP_PS_CONFIG_NAME, WithDecryption=doDecryption)
return parameter['Parameter']['Value']