def testDoMunkiAuth(self):
    level = 123
    cookie_str = 'foo=bar'
    token = 'cookie value for auth.AUTH_TOKEN_COOKIE'
    uuid = 'session uuid'
    mock_valobj = self.mox.CreateMockAnything()
    mock_valobj.value = token
    mock_session = self.mox.CreateMockAnything()
    mock_session.uuid = 'session uuid'

    mock_environ = self.mox.CreateMockAnything()
    mock_cookie = self.mox.CreateMockAnything()
    mock_auth1 = self.mox.CreateMockAnything()

    self.stubs.Set(gaeserver.os, 'environ', mock_environ)
    self.mox.StubOutWithMock(gaeserver.Cookie, 'SimpleCookie', True)
    self.mox.StubOutWithMock(gaeserver, 'AuthSimianServer', True)

    # 0: fake_noauth=True, nothing to mock

    # test 1: missing cookie
    mock_environ.get('HTTP_COOKIE', None).AndReturn(None)

    # test 2: cookie is malformed
    mock_environ.get('HTTP_COOKIE', None).AndReturn(cookie_str)
    gaeserver.Cookie.SimpleCookie().AndReturn(mock_cookie)
    mock_cookie.load(cookie_str).AndRaise(TypeError)

    # test 3: cookie exists, but isn't ours
    mock_environ.get('HTTP_COOKIE', None).AndReturn(cookie_str)
    gaeserver.Cookie.SimpleCookie().AndReturn(mock_cookie)
    mock_cookie.load(cookie_str).AndRaise(gaeserver.Cookie.CookieError)

    # test 4: cookie exists, is ours, but token isn't authenticated
    mock_environ.get('HTTP_COOKIE', None).AndReturn(cookie_str)
    gaeserver.Cookie.SimpleCookie().AndReturn(mock_cookie)
    mock_cookie.load(cookie_str).AndReturn(None)
    mock_cookie.__contains__(
        gaeserver.auth.AUTH_TOKEN_COOKIE).AndReturn(False)

    # test 5: GetSessionIfAuthOK() returns false, bad token
    mock_environ.get('HTTP_COOKIE', None).AndReturn(cookie_str)
    gaeserver.Cookie.SimpleCookie().AndReturn(mock_cookie)
    mock_cookie.load(cookie_str).AndReturn(None)
    mock_cookie.__contains__(
        gaeserver.auth.AUTH_TOKEN_COOKIE).AndReturn(True)
    mock_cookie.__getitem__(
        gaeserver.auth.AUTH_TOKEN_COOKIE).AndReturn(mock_valobj)
    gaeserver.AuthSimianServer().AndReturn(mock_auth1)
    mock_cookie.__getitem__(
        gaeserver.auth.AUTH_TOKEN_COOKIE).AndReturn(mock_valobj)
    mock_auth1.GetSessionIfAuthOK(token, gaeserver.LEVEL_BASE).AndRaise(
        gaeserver.base.AuthSessionError)

    # 6: test all success!
    mock_environ.get('HTTP_COOKIE', None).AndReturn(cookie_str)
    gaeserver.Cookie.SimpleCookie().AndReturn(mock_cookie)
    mock_cookie.load(cookie_str).AndReturn(None)
    mock_cookie.__contains__(
        gaeserver.auth.AUTH_TOKEN_COOKIE).AndReturn(True)
    mock_cookie.__getitem__(
        gaeserver.auth.AUTH_TOKEN_COOKIE).AndReturn(mock_valobj)
    gaeserver.AuthSimianServer().AndReturn(mock_auth1)
    mock_cookie.__getitem__(
        gaeserver.auth.AUTH_TOKEN_COOKIE).AndReturn(mock_valobj)
    mock_auth1.GetSessionIfAuthOK(token, level).AndReturn(mock_session)

    self.mox.ReplayAll()
    self.assertRaises(
        gaeserver.NotAuthenticated,
        gaeserver.DoMunkiAuth, fake_noauth=True)  # 0
    self.assertRaises(gaeserver.NotAuthenticated, gaeserver.DoMunkiAuth)  # 1
    self.assertRaises(gaeserver.NotAuthenticated, gaeserver.DoMunkiAuth)  # 2
    self.assertRaises(gaeserver.NotAuthenticated, gaeserver.DoMunkiAuth)  # 3
    self.assertRaises(gaeserver.NotAuthenticated, gaeserver.DoMunkiAuth)  # 4
    self.assertRaises(gaeserver.NotAuthenticated, gaeserver.DoMunkiAuth)  # 5
    session = gaeserver.DoMunkiAuth(require_level=level)  # 6
    self.assertEqual(uuid, session.uuid)  # 6
    self.mox.VerifyAll()
Test DoMunkiAuth().
dataset/ETHPy150Open google/simian/src/tests/simian/auth/gaeserver_test.py/GaeserverModuleTest.testDoMunkiAuth