def test_login_with_specific_login_domain_for_multiple_ov(
            self, get_authentication_mode, get_oneview_multiple_ips,
            oneview_client_mockup):

        get_authentication_mode.return_value = 'session'
        tokens_ov = collections.OrderedDict({
            '10.0.0.1': 'abc',
            '10.0.0.2': 'def',
            '10.0.0.3': 'ghi'
        })
        list_ips = list(tokens_ov.keys())

        client_session.init_map_clients()

        get_oneview_multiple_ips.return_value = list_ips

        client_session.login('SOME_DOMAIN\\user', 'password123')

        oneview_client_mockup.assert_any_call({
            'ip': '10.0.0.1',
            'credentials': {
                'userName': '******',
                'password': '******',
                'authLoginDomain': 'SOME_DOMAIN'
            },
            'api_version': 600
        })
    def test_map_token_redfish_for_multiple_ov(self, get_authentication_mode,
                                               get_oneview_multiple_ips,
                                               oneview_client_mockup,
                                               uuid_mock):
        get_authentication_mode.return_value = 'session'
        mocked_rf_token = "abc"
        session_id = '123456'
        list_ips = ['10.0.0.1', '10.0.0.2', '10.0.0.3']
        conn_1 = mock.MagicMock()

        connection_list = [conn_1, mock.MagicMock(), mock.MagicMock()]

        connections_ov = collections.OrderedDict({
            'client_ov_by_ip': {
                list_ips[0]: connection_list[0],
                list_ips[1]: connection_list[1],
                list_ips[2]: connection_list[2]
            },
            'session_id': session_id
        })

        uuid_mock.uuid4.return_value = session_id

        iter_conns_ov = iter(connection_list)

        client_session.init_map_clients()

        get_oneview_multiple_ips.return_value = list_ips

        def function_returning_token(ov_config):
            return next(iter_conns_ov)

        oneview_client_mockup.side_effect = function_returning_token
        conn_1.connection.get_session_id.return_value = mocked_rf_token

        # Check if redfish token return is one of the OneView's token
        rf_token, _ = client_session.login('user', 'password')
        oneview_client_mockup.assert_any_call({
            'ip': '10.0.0.1',
            'credentials': {
                'userName': '******',
                'password': '******'
            },
            'api_version': 600
        })
        self.assertEqual(rf_token, mocked_rf_token)

        # Check if cached connection map has the Redfish token return on login
        map_clients = client_session._get_map_clients()
        self.assertTrue(rf_token in map_clients)

        # Check if cached connection map has the correct
        # OneViewIp/OneViewConnection tuples
        for ov_ip, ov_conn in map_clients[rf_token].items():
            self.assertEqual(connections_ov[ov_ip], ov_conn)
Example #3
0
def post_session():
    """Posts Session

        The response to the POST request to create a session includes:

            - An X-Auth-Token header that contains a session auth token that
            the client can use an subsequent requests.
            - A Location header that contains a link to the newly created
            session resource.
            - The JSON response body that contains a full representation of
            the newly created session object.

        Exception:
            HPOneViewException: Invalid username or password.
            return abort(401)

    """

    try:
        try:
            body = request.get_json()
            username = body["UserName"]
            password = body["Password"]
        except Exception:
            error_message = "Invalid JSON key. The JSON request body " \
                            "must have the keys UserName and Password"
            abort(status.HTTP_400_BAD_REQUEST, error_message)

        token, session_id = client_session.login(username, password)

        sess = Session(session_id)

        if subscription.add_subscription_from_file():
            scmb.init_event_service(token)

        return ResponseBuilder.success_201(
            sess, {
                "Location": sess.redfish["@odata.id"],
                "X-Auth-Token": token
            })

    except HPOneViewException as e:
        logging.exception('Unauthorized error: {}'.format(e))
        abort(status.HTTP_401_UNAUTHORIZED)