def test_datetime(self, mock_url_read, mock_write_json, mock_read_json):
        """ Test the last activity date. """
        mock_url_read.side_effect = [
            '{"access_token": "ey_xx", "refresh_token": "new_refresh_token"}',
            '{"value": [{"completedDateTime": null, "createdDateTime":"2018-02-28T13:01:08.8386828Z",'
            '"assignments": {"ecf0xx": {"assignedDateTime": "2018-02-28T13:01:08.8386828Z"}}}]}']
        mock_read_json.return_value = {'refresh_token': 'refresh_token_content_xx'}
        planner = SharepointPlanner(url='/home', client_id='client_id_xx',
                                    client_secret='client_secret_k=',
                                    refresh_token_location='file_location_of_token.json')

        last_activity_date = planner.datetime('plan_id_xx')

        mock_write_json.assert_called_once_with(
            {'refresh_token': 'new_refresh_token'}, 'file_location_of_token.json')
        self.assertEqual(
            last_activity_date,
            datetime.datetime(2018, 2, 28, 13, 1, 8, tzinfo=tzutc()).astimezone(tzlocal()).replace(tzinfo=None))
        self.assertEqual(mock_url_read.call_args_list[0],
                         call(url='https://login.microsoftonline.com/common/oauth2/token',
                              post_body=bytes(parse.urlencode({
                                  "grant_type": "refresh_token",
                                  "client_id": 'client_id_xx',
                                  "client_secret": 'client_secret_k=',
                                  "resource": "https://graph.microsoft.com",
                                  "refresh_token": 'refresh_token_content_xx'
                              }), 'ascii')))
        self.assertEqual(mock_url_read.call_args_list[1],
                         call(url='https://graph.microsoft.com/v1.0/planner/plans/plan_id_xx/tasks'))
    def test_datetime_http_error(self, mock_url_read, mock_error, mock_write_json, mock_read_json):
        """ Test that the last activity date is min date, when an http error occurs. """
        file_object = MagicMock()
        file_object.read = MagicMock(return_value=b'additional reason')
        mock_url_read.side_effect = [urllib.error.HTTPError(None, None, None, None, file_object)]
        mock_read_json.return_value = {'refresh_token': 'refresh_token_content_xx'}
        planner = SharepointPlanner(url='/home', client_id='client_id_xx',
                                    client_secret='client_secret_k=',
                                    refresh_token_location='file_location_of_token.json')

        last_activity_date = planner.datetime('plan_id_xx')

        mock_write_json.assert_not_called()
        mock_url_read.assert_called_once_with(
            url='https://login.microsoftonline.com/common/oauth2/token',
            post_body=bytes(parse.urlencode({
                "grant_type": "refresh_token",
                "client_id": 'client_id_xx',
                "client_secret": 'client_secret_k=',
                "resource": "https://graph.microsoft.com",
                "refresh_token": 'refresh_token_content_xx'
            }), 'ascii')
        )
        self.assertEqual(last_activity_date, datetime.datetime.min)
        self.assertEqual(
            'Error retrieving access token. Reason: %s. Additional information: %s', mock_error.call_args_list[0][0][0])
        self.assertIsInstance(mock_error.call_args_list[0][0][1], urllib.error.HTTPError)
        self.assertEqual('additional reason', mock_error.call_args_list[0][0][2])
    def test_datetime(self, mock_url_read, mock_write_json, mock_read_json):
        """ Test the last activity date. """
        mock_url_read.side_effect = [
            '{"access_token": "ey_xx", "refresh_token": "new_refresh_token"}',
            '{"value": [{"completedDateTime": null, "createdDateTime":"2018-02-28T13:01:08.8386828Z",'
            '"bucketId": "1a", "assignments": {"ecf0xx": {"assignedDateTime": "2018-02-28T13:01:08.8386828Z"}}}]}']
        mock_read_json.return_value = {'refresh_token': 'refresh_token_content_xx'}
        planner = SharepointPlanner(url='/home', client_id='client_id_xx',
                                    client_secret='client_secret_k=',
                                    refresh_token_location='file_location_of_token.json')

        last_activity_date = planner.datetime('plan_id_xx')

        mock_write_json.assert_called_once_with(
            {'refresh_token': 'new_refresh_token'}, 'file_location_of_token.json')
        self.assertEqual(
            last_activity_date,
            datetime.datetime(2018, 2, 28, 13, 1, 8, tzinfo=tzutc()).astimezone(tzlocal()).replace(tzinfo=None))
        self.assertEqual(mock_url_read.call_args_list[0],
                         call(url='https://login.microsoftonline.com/common/oauth2/token',
                              post_body=bytes(parse.urlencode({
                                  "grant_type": "refresh_token",
                                  "client_id": 'client_id_xx',
                                  "client_secret": 'client_secret_k=',
                                  "resource": "https://graph.microsoft.com",
                                  "refresh_token": 'refresh_token_content_xx'
                              }), 'ascii')))
        self.assertEqual(mock_url_read.call_args_list[1],
                         call(url='https://graph.microsoft.com/v1.0/planner/plans/plan_id_xx/tasks'))
    def test_datetime_http_error(self, mock_url_read, mock_error, mock_write_json, mock_read_json):
        """ Test that the last activity date is min date, when an http error occurs. """
        file_object = MagicMock()
        file_object.read = MagicMock(return_value=b'additional reason')
        mock_url_read.side_effect = [urllib.error.HTTPError(None, None, None, None, file_object)]
        mock_read_json.return_value = {'refresh_token': 'refresh_token_content_xx'}
        planner = SharepointPlanner(url='/home', client_id='client_id_xx',
                                    client_secret='client_secret_k=',
                                    refresh_token_location='file_location_of_token.json')

        last_activity_date = planner.datetime('plan_id_xx')

        mock_write_json.assert_not_called()
        mock_url_read.assert_called_once_with(
            url='https://login.microsoftonline.com/common/oauth2/token',
            post_body=bytes(parse.urlencode({
                "grant_type": "refresh_token",
                "client_id": 'client_id_xx',
                "client_secret": 'client_secret_k=',
                "resource": "https://graph.microsoft.com",
                "refresh_token": 'refresh_token_content_xx'
            }), 'ascii')
        )
        self.assertEqual(last_activity_date, datetime.datetime.min)
        self.assertEqual(
            'Error retrieving access token. Reason: %s. Additional information: %s', mock_error.call_args_list[0][0][0])
        self.assertIsInstance(mock_error.call_args_list[0][0][1], urllib.error.HTTPError)
        self.assertEqual('additional reason', mock_error.call_args_list[0][0][2])
    def test_init_json_error(self, mock_url_read, mock_error, mock_write_json, mock_read_json):
        """ Test that the last activity date is min date, when invalid json retrieved. """
        mock_url_read.return_value = 'non-json'
        mock_read_json.return_value = {'refresh_token': 'refresh_token_content_xx'}
        planner = SharepointPlanner(url='/home', client_id='client_id_xx',
                                    client_secret='client_secret_k=',
                                    refresh_token_location='file_location_of_token.json')

        last_activity_date = planner.datetime('plan_id_xx')

        mock_write_json.assert_not_called()
        self.assertEqual(last_activity_date, datetime.datetime.min)
        self.assertEqual('Invalid json retrieved for access token. Reason: %s.', mock_error.call_args_list[0][0][0])
        self.assertIsInstance(mock_error.call_args_list[0][0][1], json.decoder.JSONDecodeError)
    def test_datetime_http_error_tasks(self, mock_url_read, mock_write_json, mock_read_json):
        """ Test that the last activity date is min date, when an http error occurs during tasks retrieval. """
        mock_url_read.side_effect = [
            '{"access_token": "ey_xx", "refresh_token": "new_refresh_token"}',
            urllib.error.HTTPError(None, None, None, None, None)]
        mock_read_json.return_value = {'refresh_token': 'refresh_token_content_xx'}
        planner = SharepointPlanner(url='/home', client_id='client_id_xx',
                                    client_secret='client_secret_k=',
                                    refresh_token_location='file_location_of_token.json')

        last_activity_date = planner.datetime('plan_id_xx')

        mock_write_json.assert_called_once()
        self.assertEqual(last_activity_date, datetime.datetime.min)
    def test_init_json_error(self, mock_url_read, mock_error, mock_write_json, mock_read_json):
        """ Test that the last activity date is min date, when invalid json retrieved. """
        mock_url_read.return_value = 'non-json'
        mock_read_json.return_value = {'refresh_token': 'refresh_token_content_xx'}
        planner = SharepointPlanner(url='/home', client_id='client_id_xx',
                                    client_secret='client_secret_k=',
                                    refresh_token_location='file_location_of_token.json')

        last_activity_date = planner.datetime('plan_id_xx')

        mock_write_json.assert_not_called()
        self.assertEqual(last_activity_date, datetime.datetime.min)
        self.assertEqual('Invalid json retrieved for access token. Reason: %s.', mock_error.call_args_list[0][0][0])
        self.assertIsInstance(mock_error.call_args_list[0][0][1], json.decoder.JSONDecodeError)
    def test_datetime_http_error_tasks(self, mock_url_read, mock_write_json, mock_read_json):
        """ Test that the last activity date is min date, when an http error occurs during tasks retrieval. """
        mock_url_read.side_effect = [
            '{"access_token": "ey_xx", "refresh_token": "new_refresh_token"}',
            urllib.error.HTTPError(None, None, None, None, None)]
        mock_read_json.return_value = {'refresh_token': 'refresh_token_content_xx'}
        planner = SharepointPlanner(url='/home', client_id='client_id_xx',
                                    client_secret='client_secret_k=',
                                    refresh_token_location='file_location_of_token.json')

        last_activity_date = planner.datetime('plan_id_xx')

        mock_write_json.assert_called_once()
        self.assertEqual(last_activity_date, datetime.datetime.min)
    def test_datetime_json_key_error_tasks(self, mock_url_read, mock_error, mock_write_json, mock_read_json):
        """ Test that the last activity date is min date, when invalid json retrieved during tasks retrieval. """
        mock_url_read.side_effect = [
            '{"access_token": "ey_xx", "refresh_token": "new_refresh_token"}',
            '{}']
        mock_read_json.return_value = {'refresh_token': 'refresh_token_content_xx'}
        planner = SharepointPlanner(url='/home', client_id='client_id_xx',
                                    client_secret='client_secret_k=',
                                    refresh_token_location='file_location_of_token.json')

        last_activity_date = planner.datetime('plan_id_xx')

        mock_write_json.assert_called_once()
        self.assertEqual(last_activity_date, datetime.datetime.min)
        self.assertEqual('Invalid json retrieved for tasks. Reason: %s.', mock_error.call_args_list[0][0][0])
        self.assertIsInstance(mock_error.call_args_list[0][0][1], KeyError)
    def test_datetime_json_key_error_tasks(self, mock_url_read, mock_error, mock_write_json, mock_read_json):
        """ Test that the last activity date is min date, when invalid json retrieved during tasks retrieval. """
        mock_url_read.side_effect = [
            '{"access_token": "ey_xx", "refresh_token": "new_refresh_token"}',
            '{}']
        mock_read_json.return_value = {'refresh_token': 'refresh_token_content_xx'}
        planner = SharepointPlanner(url='/home', client_id='client_id_xx',
                                    client_secret='client_secret_k=',
                                    refresh_token_location='file_location_of_token.json')

        last_activity_date = planner.datetime('plan_id_xx')

        mock_write_json.assert_called_once()
        self.assertEqual(last_activity_date, datetime.datetime.min)
        self.assertEqual('Invalid json retrieved for tasks. Reason: %s.', mock_error.call_args_list[0][0][0])
        self.assertIsInstance(mock_error.call_args_list[0][0][1], KeyError)
Exemple #11
0
    def test_datetime_multiple_plans(self, mock_url_read, mock_write_json,
                                     mock_read_json):
        """ Test the last activity date is the last from all listed plans. """
        mock_url_read.side_effect = [
            '{"access_token": "ey_xx", "refresh_token": "new_refresh_token"}',
            '{"value": [{"completedDateTime": null, "createdDateTime":"2018-02-28T11:01:08.8386828Z",'
            '"bucketId": "1a", "assignments": {"ecf0xx": {"assignedDateTime": "2018-02-28T13:01:08.8386828Z"}}}]}',
            '{"value": [{"completedDateTime": null, "createdDateTime":"2018-03-28T11:01:08.88Z",'
            '"bucketId": "2b", "assignments": {}}]}'
        ]
        mock_read_json.return_value = {
            'refresh_token': 'refresh_token_content_xx'
        }
        planner = SharepointPlanner(
            url='/home',
            client_id='client_id_xx',
            client_secret='client_secret_k=',
            refresh_token_location='file_location_of_token.json')

        last_activity_date = planner.datetime('plan_id_1', 'plan_id_2')

        mock_write_json.assert_called_once_with(
            {'refresh_token': 'new_refresh_token'},
            'file_location_of_token.json')
        self.assertEqual(
            last_activity_date,
            datetime.datetime(2018, 3, 28, 11, 1, 8,
                              tzinfo=tzutc()).astimezone(
                                  tzlocal()).replace(tzinfo=None))
        self.assertEqual(
            mock_url_read.call_args_list[1],
            call(
                url=
                'https://graph.microsoft.com/v1.0/planner/plans/plan_id_1/tasks'
            ))
        self.assertEqual(
            mock_url_read.call_args_list[2],
            call(
                url=
                'https://graph.microsoft.com/v1.0/planner/plans/plan_id_2/tasks'
            ))
    def test_datetime_multiple_plans(self, mock_url_read, mock_write_json, mock_read_json):
        """ Test the last activity date is the last from all listed plans. """
        mock_url_read.side_effect = [
            '{"access_token": "ey_xx", "refresh_token": "new_refresh_token"}',
            '{"value": [{"completedDateTime": null, "createdDateTime":"2018-02-28T11:01:08.8386828Z",'
            '"bucketId": "1a", "assignments": {"ecf0xx": {"assignedDateTime": "2018-02-28T13:01:08.8386828Z"}}}]}',
            '{"value": [{"completedDateTime": null, "createdDateTime":"2018-03-28T11:01:08.88Z",'
            '"bucketId": "2b", "assignments": {}}]}']
        mock_read_json.return_value = {'refresh_token': 'refresh_token_content_xx'}
        planner = SharepointPlanner(url='/home', client_id='client_id_xx',
                                    client_secret='client_secret_k=',
                                    refresh_token_location='file_location_of_token.json')

        last_activity_date = planner.datetime('plan_id_1', 'plan_id_2')

        mock_write_json.assert_called_once_with(
            {'refresh_token': 'new_refresh_token'}, 'file_location_of_token.json')
        self.assertEqual(
            last_activity_date,
            datetime.datetime(2018, 3, 28, 11, 1, 8, tzinfo=tzutc()).astimezone(tzlocal()).replace(tzinfo=None))
        self.assertEqual(mock_url_read.call_args_list[1],
                         call(url='https://graph.microsoft.com/v1.0/planner/plans/plan_id_1/tasks'))
        self.assertEqual(mock_url_read.call_args_list[2],
                         call(url='https://graph.microsoft.com/v1.0/planner/plans/plan_id_2/tasks'))