예제 #1
0
    def test_unsuccessful_authentication(self):
        # Use REST proxy for testing
        rest = _RestProxyForTest()
        auth = Auth(rest)

        # Authenticate in order to fill in email/logged_in so that next test
        # can verify that these are cleared.
        rest.expect_get('/me', 200, {'email': '*****@*****.**', 'loggedIn': True})
        auth.authenticate()

        # An unsuccessful authentication should clear token and other values.
        # An example of the few ways that the server might reject a user. Others look
        # like this with different messages.
        server_error_on_bad_token = """
            {
              "error": {
                "errors": [
                  {
                    "domain": "global",
                    "reason": "backendError",
                    "message": "org.apache.shiro.authc.IncorrectCredentialsException"
                  }
                ],
                "code": 503,
                "message": "org.apache.shiro.authc.IncorrectCredentialsException"
              }
            }
            """
        rest.expect_get('/me', 503, json.loads(server_error_on_bad_token))
        auth.authenticate()
        self.assertEqual(auth.get_user(), '')
        self.assertEqual(auth.get_logged_in(), False)
예제 #2
0
    def test_successful_authentication(self):
        # Use REST proxy for testing
        rest = _RestProxyForTest()
        auth = Auth(rest)

        # Before authenticating, auth should reflect not logged in.
        self.assertEqual(auth.get_user(), '')
        self.assertEqual(auth.get_logged_in(), False)

        # A successful authentication should store token and set user to returned value.
        rest.expect_get('/me', 200, {'email': '*****@*****.**', 'loggedIn': True})
        auth.authenticate()
        self.assertEqual(auth.get_user(), '*****@*****.**')
        self.assertEqual(auth.get_logged_in(), True)
예제 #3
0
    def test_authentication_empty_token(self):
        # Use REST proxy for testing
        rest = _RestProxyForTest()
        auth = Auth(rest)

        # Authenticate in order to fill in email/logged_in so that next test
        # can verify that these are cleared.
        rest.expect_get('/me', 200, {'email': '*****@*****.**', 'loggedIn': True})
        auth.authenticate()

        # Authentication with an empty token should be no problem and result in an empty
        # auth object.
        rest.expect_get('/me', 200, {"loggedIn": False})
        auth.authenticate()
        self.assertEqual(auth.get_user(), '')
        self.assertEqual(auth.get_logged_in(), False)
예제 #4
0
    def test_authentication_server_error(self):
        # Use REST proxy for testing
        rest = _RestProxyForTest()
        auth = Auth(rest)

        # Authenticate in order to fill in email/logged_in/token so that next test
        # can verify that these are not cleared.
        rest.expect_get('/me', 200, {'email': '*****@*****.**', 'loggedIn': True})
        auth.authenticate()

        # Authentication should throw on a non-200 response and leave auth contents
        # unchanged.
        rest.expect_get('/me', 404, {})
        with self.assertRaises(RuntimeError):
            auth.authenticate()
        self.assertEqual(auth.get_user(), '*****@*****.**')
        self.assertEqual(auth.get_logged_in(), True)
예제 #5
0
from adam import Auth
from adam import Batch
from adam import Projects
from adam import RestRequests
from adam import AuthorizingRestProxy
import time
import os

# Authorize user TODO: move this somewhere else
# This should really be done under the hood and not exposed to the client
url = "https://pro-equinox-162418.appspot.com/_ah/api/adam/v1"
rest = RestRequests(url)
auth = Auth(rest)
tokenFile = os.getcwd() + '/token.txt'
# Opening with "a+" instead of "r" creates the file if it doesn't exist.
with open(tokenFile, "a+") as f:
    f.seek(0)
    token = f.readline().replace('\n', '')

try:
    if not auth.authorize(token):
        if auth.initial_authorization():
            with open(tokenFile, "w") as f:
                f.write(auth.get_token())
except RuntimeError as e:
    print('Encountered server error while attempting to authorize: ' + str(e))

if auth.get_token() == "":
    print('Could not authorize user.')
else:
    print('Welcome, ' + auth.get_user())