# coding: utf-8

"""
    OneRoster OpenAPI (JSON) Definition

    The OneRoster service binding is available in WSDL/XSD and REST/JSON. The model is based upon the IMS PSM modelling approach.  # noqa: E501

    OpenAPI spec version: 1.1
    Contact: lmattson@imsglobal.org
    Generated by: https://github.com/swagger-api/swagger-codegen.git
"""

from __future__ import absolute_import

import unittest

import requests
from faker import Faker
from mockito import when, verify, mock
from requests import Response, HTTPError

from oneroster_client.api.users_management_api import UsersManagementApi  # noqa: E501
from test.factories.oneroster_user import OneRosterSingleUserFactory

fake = Faker()


class TestUsersManagementApi(unittest.TestCase):
    """UsersManagementApi unit test stubs"""

    def setUp(self):
        self.api = UsersManagementApi()  # noqa: E501
        self.default_url = '/'
        self.default_headers = {'Accept': 'application/json',
                                'User-Agent': 'Swagger-Codegen/1.0.0/python',
                                'Authorization': f'Bearer '}

    def tearDown(self):
        pass

    def test_get_user(self):
        """Test case for get_user

        The REST read request message for the getUser() API call.  # noqa: E501
        """
        expected_response = self.api.api_client.sanitize_for_serialization(OneRosterSingleUserFactory())
        sourced_id = fake.uuid4()
        response = mock(Response)
        when(response).json().thenReturn(expected_response)
        when(requests).request(...).thenReturn(response)
        when(response).raise_for_status()

        self.api.get_user(sourced_id)

        verify(requests).request('GET', f'{self.default_url}/users/{sourced_id}',
                                 params=[],
                                 json=None,
                                 headers=self.default_headers,
                                 timeout=None)

    def test_get_user_raises_value_error_for_invalid_datetime(self):
        """Test case for get_user

        The REST read request message for the getUser() API call.  # noqa: E501
        """
        expected_response = self.api.api_client.sanitize_for_serialization(OneRosterSingleUserFactory())
        invalid_date = fake.sentence()
        expected_response['user']['dateLastModified'] = invalid_date
        sourced_id = fake.uuid4()
        response = mock(Response)
        when(response).json().thenReturn(expected_response)
        when(requests).request(...).thenReturn(response)
        when(response).raise_for_status()

        with self.assertRaises(ValueError) as context:
            self.api.get_user(sourced_id)

        assert str(context.exception) == f"Failed to parse `{invalid_date}` as datetime object"

        verify(requests).request('GET', f'{self.default_url}/users/{sourced_id}',
                                 params=[],
                                 json=None,
                                 headers=self.default_headers,
                                 timeout=None)

    def test_get_user_raises_404(self):
        http_error = HTTPError()
        http_error.response = mock(Response)
        http_error.response.status_code = 404
        sourced_id = fake.uuid4()
        when(requests).request(...).thenReturn(http_error.response)
        when(http_error.response).raise_for_status(...).thenRaise(http_error)

        with self.assertRaises(HTTPError) as context:
            self.api.get_user(sourced_id)

        verify(requests).request('GET', f'{self.default_url}/users/{sourced_id}',
                                 params=[],
                                 json=None,
                                 headers=self.default_headers,
                                 timeout=None)
        assert context.exception.response.status_code == 404
        assert context.exception.dependency_name == 'OneRoster'

    def test_get_user_raises_connection_error_with_dependency_name(self):
        connection_error = requests.ConnectionError()
        connection_error.response = mock(Response)
        connection_error.response.status_code = 503
        when(requests).request(...).thenReturn(connection_error.response)
        when(connection_error.response).raise_for_status(...).thenRaise(connection_error)

        with self.assertRaises(requests.ConnectionError) as context:
            self.api.get_user(fake.uuid4())

        assert context.exception.response.status_code == 503
        assert context.exception.dependency_name == 'OneRoster'

    def test_get_users(self):
        """Test case for get_users

        The REST read request message for the getUsers() API call.  # noqa: E501
        """
        pass


if __name__ == '__main__':
    unittest.main()
