Help with a mock unit test, how to test class attributes value after method under test runs?

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:


in the method call in the test function.

I am a noob at mocking so I may have misconfigured the test in, So I’m asking for some help here. Thanks!


import sys

# 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()    

def test_regenRToken(self):



import sys

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, 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)  )


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       

        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']