def test_max_login_failure_cert_login(monkeypatch):
    ah = account_handler.AccountHandler(Mock())
    config = {'parent_group': 'parent-group'}
    monkeypatch.setattr(account_handler, 'CONFIG_BROKER', config)

    mock_dict = Mock()
    mock_dict.return_value.exists.return_value = True
    mock_dict.return_value.safeDictionary.side_effect = {'cert': ''}
    monkeypatch.setattr(account_handler, 'RequestDictionary', mock_dict)

    mock_resp = Mock()
    mock_resp.return_value = mock_response(url='ticket=12345')
    monkeypatch.setattr('requests.get', mock_resp)

    config = {'full_url': 'full-url', 'max_cert_url': 'max-cert-url'}
    monkeypatch.setattr(account_handler, 'CONFIG_BROKER', config)

    max_dict = {'cas:serviceResponse': {}}
    monkeypatch.setattr(account_handler, 'get_max_dict',
                        Mock(return_value=max_dict))
    json_response = ah.max_login(Mock())
    error_message = (
        "The Max CAS endpoint was unable to locate your session using "
        "the ticket/service combination you provided.")

    # Did not get a successful response from MAX
    assert error_message == json.loads(
        json_response.get_data().decode("utf-8"))['message']
Example #2
0
def max_login_func(create_session_mock, max_dict_mock, monkeypatch, max_response):
    def json_return(): return {"ticket": "12345", "service": "https://some.url.gov"}
    request = type('Request', (object,),  {"is_json": True, "headers": {"Content-Type": "application/json"},
                                           "get_json": json_return})
    ah = account_handler.AccountHandler(request=request)
    monkeypatch.setattr(account_handler, 'CONFIG_BROKER', {"parent_group": "test"})
    max_dict_mock.return_value = max_response
    create_session_mock.return_value = True
    return ah
def test_max_login_success_cert_login(monkeypatch):
    ah = account_handler.AccountHandler(Mock())

    mock_dict = Mock()
    mock_dict.return_value.exists.return_value = True
    mock_dict.return_value.safeDictionary.side_effect = {'cert': ''}
    monkeypatch.setattr(account_handler, 'RequestDictionary', mock_dict)

    max_dict = {'cas:serviceResponse': {}}
    monkeypatch.setattr(account_handler, 'get_max_dict',
                        Mock(return_value=max_dict))

    config = {
        'parent_group': 'parent-group',
        'full_url': 'full-url',
        'max_cert_url': 'max-cert-url'
    }
    monkeypatch.setattr(account_handler, 'CONFIG_BROKER', config)

    max_dict = make_max_dict('parent-group,parent-group-CGAC_SYS')
    max_dict['cas:serviceResponse']['cas:authenticationSuccess'][
        'cas:attributes']['maxAttribute:MAX-ID'] = 'S_id'
    monkeypatch.setattr(account_handler, 'get_max_dict',
                        Mock(return_value=max_dict))

    mock_resp = Mock()
    mock_resp.return_value = mock_response(url='ticket=12345')
    monkeypatch.setattr('requests.get', mock_resp)

    # If it gets to this point, that means the user was in all the right groups aka successful login
    monkeypatch.setattr(
        ah, 'create_session_and_response',
        Mock(return_value=JsonResponse.create(
            StatusCode.OK, {"message": "Login successful"})))
    json_response = ah.max_login(Mock())

    assert "Login successful" == json.loads(
        json_response.get_data().decode("utf-8"))['message']

    max_dict = make_max_dict('')
    monkeypatch.setattr(account_handler, 'get_max_dict',
                        Mock(return_value=max_dict))

    # If it gets to this point, that means the user was in all the right groups aka successful login
    monkeypatch.setattr(
        ah, 'create_session_and_response',
        Mock(return_value=JsonResponse.create(
            StatusCode.OK, {"message": "Login successful"})))
    json_response = ah.max_login(Mock())

    assert "Login successful" == json.loads(
        json_response.get_data().decode("utf-8"))['message']
def test_max_login_success_normal_login(monkeypatch):
    ah = account_handler.AccountHandler(Mock())

    mock_dict = Mock()
    mock_dict.return_value.exists.return_value = False
    mock_dict.return_value.safeDictionary.side_effect = {
        'ticket': '',
        'service': ''
    }
    monkeypatch.setattr(account_handler, 'RequestDictionary', mock_dict)

    max_dict = {'cas:serviceResponse': {}}
    monkeypatch.setattr(account_handler, 'get_max_dict',
                        Mock(return_value=max_dict))
    config = {'parent_group': 'parent-group'}
    monkeypatch.setattr(account_handler, 'CONFIG_BROKER', config)
    max_dict = make_max_dict('parent-group,parent-group-CGAC_SYS')
    monkeypatch.setattr(account_handler, 'get_max_dict',
                        Mock(return_value=max_dict))

    # If it gets to this point, that means the user was in all the right groups aka successful login
    monkeypatch.setattr(
        ah, 'create_session_and_response',
        Mock(return_value=JsonResponse.create(
            StatusCode.OK, {"message": "Login successful"})))
    json_response = ah.max_login(Mock())

    assert "Login successful" == json.loads(
        json_response.get_data().decode("utf-8"))['message']

    max_dict = make_max_dict('')
    monkeypatch.setattr(account_handler, 'get_max_dict',
                        Mock(return_value=max_dict))

    # If it gets to this point, that means the user was in all the right groups aka successful login
    monkeypatch.setattr(
        ah, 'create_session_and_response',
        Mock(return_value=JsonResponse.create(
            StatusCode.OK, {"message": "Login successful"})))
    json_response = ah.max_login(Mock())

    assert "Login successful" == json.loads(
        json_response.get_data().decode("utf-8"))['message']
def test_max_login_failure_normal_login(monkeypatch):
    ah = account_handler.AccountHandler(Mock())
    config = {'parent_group': 'parent-group'}
    monkeypatch.setattr(account_handler, 'CONFIG_BROKER', config)

    mock_dict = Mock()
    mock_dict.return_value.exists.return_value = False
    mock_dict.return_value.safeDictionary.side_effect = {
        'ticket': '',
        'service': ''
    }
    monkeypatch.setattr(account_handler, 'RequestDictionary', mock_dict)

    max_dict = {'cas:serviceResponse': {}}
    monkeypatch.setattr(account_handler, 'get_max_dict',
                        Mock(return_value=max_dict))
    json_response = ah.max_login(Mock())
    error_message = "You have failed to login successfully with MAX"

    # Did not get a successful response from MAX
    assert error_message == json.loads(
        json_response.get_data().decode("utf-8"))['message']