"""Test login to an ICAT server.
"""

from __future__ import print_function
import pytest
import icat
import icat.config
from conftest import getConfig, tmpSessionId

# Try out three different users: root, useroffice, and acord.  Normal
# users like acord might use a different authentication plugin then
# system users as root and useroffice.  We want to try out both cases.
@pytest.mark.parametrize("user", ["root", "useroffice", "acord"])
def test_login(user):
    """Login to the ICAT server.
    """

    client, conf = getConfig(confSection=user)
    sessionId = client.login(conf.auth, conf.credentials)
    assert sessionId
    assert sessionId == client.sessionId
    username = client.getUserName()
    assert username == "%s/%s" % (conf.auth, user)
    print("\nLogged in as %s to %s." % (user, conf.url))
    client.logout()
    assert client.sessionId is None

    # Verify that the logout was effective, e.g. that the sessionId is
    # invalidated.
    with tmpSessionId(client, sessionId):
        with pytest.raises(icat.exception.ICATSessionError):
            username = client.getUserName()

@@ -69,16 +69,143 @@
 unction%0A
+try:%0A    # Python 3%0A    from urllib.parse import urlparse%0Aexcept ImportError:%0A    # Python 2%0A    from urlparse import urlparse%0A
 import p
@@ -1211,28 +1211,2006 @@
 name = client.getUserName()%0A
+%0Adef test_login_icat_shorturl():%0A    %22%22%22Test connecting to ICAT using a generic URL.  Ref. #63.%0A    %22%22%22%0A    # Connect normally using the configured URL first.  If that URL%0A    # has a path component that is equal to the default path, shorten%0A    # the URL stripping the path and try again.  It should connect to%0A    # the same ICAT.  To test the latter, we check that a session id%0A    # obtained from one client is valid at the other one.%0A    client, conf = getConfig()%0A    sessionId = client.login(conf.auth, conf.credentials)%0A    assert sessionId%0A    assert sessionId == client.sessionId%0A    username = client.getUserName()%0A    o = urlparse(conf.url)%0A    if o%5B2:6%5D != (%22/ICATService/ICAT%22, %22%22, %22wsdl%22, %22%22):%0A        pytest.skip(%22not a canonical path in URL%22)%0A    url = %22%25s://%25s%22 %25 (o.scheme, o.netloc)%0A    gen_client = icat.Client(url, **client.kwargs)%0A    gen_client.autoLogout = False%0A    gen_client.sessionId = sessionId%0A    assert gen_client.getUserName() == username%0A%0Adef test_login_ids_shorturl():%0A    %22%22%22Test connecting to IDS using a generic URL.  Ref. #63.%0A    %22%22%22%0A    # Connect normally using the configured URL first.  If that URL%0A    # has a path component that is equal to the default path, shorten%0A    # the URL stripping the path and try again.  It should connect to%0A    # the same IDS.  There is no real test for the latter, but at%0A    # least we check that getIcatUrl() returns the same URL for both%0A    # clients.%0A    client, conf = getConfig()%0A    if not client.ids:%0A        pytest.skip(%22no IDS%22)%0A    try:%0A        icatURL = client.ids.getIcatUrl()%0A    except icat.VersionMethodError:%0A        pytest.skip(%22IDS is too old for this test%22)%0A    o = urlparse(conf.idsurl)%0A    if o%5B2:6%5D != (%22/ids%22, %22%22, %22%22, %22%22):%0A        pytest.skip(%22not a canonical path in URL%22)%0A    url = %22%25s://%25s%22 %25 (o.scheme, o.netloc)%0A    kwargs = dict(client.kwargs)%0A    kwargs%5B'idsurl'%5D = url%0A    gen_client = icat.Client(conf.url, **kwargs)%0A    assert gen_client.ids.getIcatUrl() == icatURL%0A

Add tests for #63.