def test_token_auth_session_need_renewal_memory_token(
            self, mocked_decode_token, moccked_post):
        """
        Test token_auth_session when memory token about to expire and need to be refreshed
        """
        new_token = json.loads(mocked_token_create_response()).get(
            'entry')[0].get('content').get('token')

        auth = {'token_auth': {'initial_token': "asdfg"}}
        status = CheckData()
        # load a token in memory for validation
        status.data['http://testhost:8089token'] = 'memorytokenpresent'
        persistence_check_name = "splunk_metric"
        config = FakeInstanceConfig()

        helper = SplunkHelper(config)
        helper.requests_session.headers.update(
            {'Authorization': "Bearer memorytokenpresent"})
        helper._current_time = mock.MagicMock()
        helper._current_time.return_value = datetime.datetime(
            2020, 06, 5, 15, 44, 51)
        helper.token_auth_session(auth, config.base_url, status,
                                  persistence_check_name)

        # Header should be updated with the new token
        expected_header = helper.requests_session.headers.get("Authorization")
        self.assertEqual(expected_header, "Bearer {}".format(new_token))
        # persistence data will have new token as well
        self.assertEqual(status.data.get('http://testhost:8089token'),
                         new_token)
        status.data.clear()
    def test_token_auth_session_invalid_memory_token(self,
                                                     mocked_decode_token):
        """
        Test token_auth_session to throw TokenExpiredException when memory token is expired
        """
        auth = {'token_auth': {'initial_token': "asdfg"}}
        status = CheckData()
        # load a token in memory for validation
        status.data['http://testhost:8089token'] = 'memorytokenpresent'
        persistence_check_name = "splunk_metric"
        config = FakeInstanceConfig()

        helper = SplunkHelper(config)
        helper.requests_session.headers.update(
            {'Authorization': "Bearer memorytokenpresent"})
        helper._current_time = mock.MagicMock()
        helper._current_time.return_value = datetime.datetime(
            2020, 06, 16, 15, 44, 51)
        check = False
        try:
            helper.token_auth_session(auth, config.base_url, status,
                                      persistence_check_name)
        except TokenExpiredException:
            check = True
        msg = "Current in use authentication token is expired. Please provide a valid token in the YAML " \
              "and restart the Agent"
        self.assertTrue(check, msg)
    def test_token_auth_session(self, mocked_decode_token):
        """
        Test token_auth_session when memory token is valid and doesn't need renewal
        """
        auth = {'token_auth': {'initial_token': "asdfg"}}
        # load a token in memory for validation
        status = CheckData()
        status.data['http://testhost:8089token'] = 'memorytokenpresent'
        persistence_check_name = "splunk_metric"
        config = FakeInstanceConfig()

        helper = SplunkHelper(config)
        # update headers with memory token
        helper.requests_session.headers.update(
            {'Authorization': "Bearer memorytokenpresent"})
        helper._current_time = mock.MagicMock()
        helper._current_time.return_value = datetime.datetime(
            2020, 05, 14, 15, 44, 51)
        helper.token_auth_session(auth, config.base_url, status,
                                  persistence_check_name)

        # Header should be still with the memory token
        expected_header = helper.requests_session.headers.get("Authorization")
        self.assertEqual(expected_header,
                         "Bearer {}".format("memorytokenpresent"))
 def test_decode_token_util_when_exp_set_never(self, mocked_decode_token):
     """
     Should return 999 days if first initial token expiration set to never
     """
     helper = SplunkHelper(FakeInstanceConfig())
     helper._current_time = mock.MagicMock()
     helper._current_time.return_value = datetime.datetime(
         2020, 05, 14, 15, 44, 51)
     days = helper._decode_token_util("test", True)
     self.assertEqual(days, 999)
 def test_decode_token_util(self, mocked_decode_token):
     """
     Test token decoding utility
     """
     helper = SplunkHelper(FakeInstanceConfig())
     helper._current_time = mock.MagicMock()
     helper._current_time.return_value = datetime.datetime(
         2020, 05, 14, 15, 44, 51)
     days = helper._decode_token_util("test", False)
     self.assertEqual(days, 27)
 def test_need_renewal_false(self, mocked_decode_token):
     """
     Test need renewal method when is_initial_token is false and should return False
     """
     helper = SplunkHelper(FakeInstanceConfig())
     helper._current_time = mock.MagicMock()
     helper._current_time.return_value = datetime.datetime(
         2020, 05, 14, 15, 44, 51)
     valid = helper.need_renewal("test")
     # Need renewal should return True since flag is true
     self.assertFalse(valid)
 def test_is_token_expired_false(self, mocked_decode_token):
     """
     Test token validation method for invalid token
     """
     helper = SplunkHelper(FakeInstanceConfig())
     helper._current_time = mock.MagicMock()
     helper._current_time.return_value = datetime.datetime(
         2020, 05, 14, 15, 44, 51)
     valid = helper.is_token_expired("test")
     # Token is not expired
     self.assertFalse(valid)
     self.assertEqual(valid, False)