Example #1
0
 def process_request(self, request):
     user = getattr(request, 'user', None)
     if user is not None and user.is_authenticated():
         user_api = vumi_api_for_user(request.user)
         request.user_api = user_api
         SessionManager.set_user_account_key(
             request.session, user_api.user_account_key)
Example #2
0
 def process_request(self, request):
     user = getattr(request, 'user', None)
     if user is not None and user.is_authenticated():
         user_api = vumi_api_for_user(request.user)
         request.user_api = user_api
         SessionManager.set_user_account_key(request.session,
                                             user_api.user_account_key)
Example #3
0
 def test_get_user_account_key(self):
     session = {
         GO_USER_ACCOUNT_KEY: u"user-1",
     }
     self.assertEqual(
         SessionManager.get_user_account_key(session),
         u"user-1")
Example #4
0
 def test_authenticated_access(self):
     request = self.factory.get('/accounts/login/')
     request.user = self.user_helper.get_django_user()
     request.session = {}
     self.mw.process_request(request)
     self.assertTrue(isinstance(request.user_api, VumiUserApi))
     self.assertEqual(SessionManager.get_user_account_key(request.session),
                      self.user_helper.account_key)
Example #5
0
 def test_authenticated_access(self):
     request = self.factory.get('/accounts/login/')
     request.user = self.user_helper.get_django_user()
     request.session = {}
     self.mw.process_request(request)
     self.assertTrue(isinstance(request.user_api, VumiUserApi))
     self.assertEqual(
         SessionManager.get_user_account_key(request.session),
         self.user_helper.account_key)
Example #6
0
    def __init__(self, manager, redis, sender=None, metric_publisher=None):
        # local import to avoid circular import since
        # go.api.go_api needs to access VumiApi
        from go.api.go_api.session_manager import SessionManager

        self.manager = manager
        self.redis = redis

        self.tpm = TagpoolManager(self.redis.sub_manager('tagpool_store'))
        self.mdb = MessageStore(self.manager,
                                self.redis.sub_manager('message_store'))
        self.account_store = AccountStore(self.manager)
        self.token_manager = TokenManager(
            self.redis.sub_manager('token_manager'))
        self.session_manager = SessionManager(
            self.redis.sub_manager('session_manager'))
        self.mapi = sender
        self.metric_publisher = metric_publisher
Example #7
0
class TestGoUserSessionAccessChecker(VumiTestCase):
    @inlineCallbacks
    def setUp(self):
        self.persistence_helper = self.add_helper(
            PersistenceHelper(is_sync=False))
        self.redis = yield self.persistence_helper.get_redis_manager()
        self.sm = SessionManager(self.redis)

    @inlineCallbacks
    def test_request_avatar_id(self):
        checker = GoUserSessionAccessChecker(self.sm)
        session = {}
        self.sm.set_user_account_key(session, u"user-1")
        yield self.sm.save_session(u"session-1", session, 10)
        creds = UsernamePassword(u"session_id", u"session-1")
        user = yield checker.requestAvatarId(creds)
        self.assertEqual(user, u"user-1")

    @inlineCallbacks
    def test_request_avatar_id_bad_password(self):
        checker = GoUserSessionAccessChecker(self.sm)
        creds = UsernamePassword(u"session_id", u"session-unknown")
        errored = False
        try:
            yield checker.requestAvatarId(creds)
        except error.UnauthorizedLogin:
            errored = True
        self.assertTrue(errored)

    @inlineCallbacks
    def test_request_avatar_id_bad_username(self):
        checker = GoUserSessionAccessChecker(self.sm)
        session = {}
        self.sm.set_user_account_key(session, u"user-1")
        yield self.sm.save_session(u"session-1", session, 10)
        creds = UsernamePassword(u"session_id_BAD", u"session-1")
        try:
            yield checker.requestAvatarId(creds)
        except error.UnauthorizedLogin:
            errored = True
        self.assertTrue(errored)
Example #8
0
 def setUp(self):
     self.persistence_helper = self.add_helper(
         PersistenceHelper(is_sync=False))
     self.redis = yield self.persistence_helper.get_redis_manager()
     self.sm = SessionManager(self.redis)
Example #9
0
 def test_set_user_account_key(self):
     session = {}
     SessionManager.set_user_account_key(session, u"user-1")
     self.assertEqual(session, {
         GO_USER_ACCOUNT_KEY: u"user-1",
     })
Example #10
0
 def test_get_user_account_key_for_none_session(self):
     self.assertEqual(
         SessionManager.get_user_account_key(None),
         None)
Example #11
0
 def test_get_user_account_key_missing(self):
     self.assertEqual(
         SessionManager.get_user_account_key({}),
         None)
Example #12
0
class TestSessionManager(VumiTestCase):
    @inlineCallbacks
    def setUp(self):
        self.persistence_helper = self.add_helper(PersistenceHelper())
        self.redis = yield self.persistence_helper.get_redis_manager()
        self.sm = SessionManager(self.redis)

    @inlineCallbacks
    def mk_session(self, session_id, session):
        key = "sessions.%s" % (session_id,)
        session_json = json.dumps(session)
        yield self.redis.set(key, session_json)

    def test_get_user_account_key(self):
        session = {
            GO_USER_ACCOUNT_KEY: u"user-1",
        }
        self.assertEqual(
            SessionManager.get_user_account_key(session),
            u"user-1")

    def test_get_user_account_key_missing(self):
        self.assertEqual(
            SessionManager.get_user_account_key({}),
            None)

    def test_get_user_account_key_for_none_session(self):
        self.assertEqual(
            SessionManager.get_user_account_key(None),
            None)

    def test_set_user_account_key(self):
        session = {}
        SessionManager.set_user_account_key(session, u"user-1")
        self.assertEqual(session, {
            GO_USER_ACCOUNT_KEY: u"user-1",
        })

    @inlineCallbacks
    def test_exists(self):
        self.assertEqual((yield self.sm.exists("unknown-session")), False)
        yield self.mk_session("session-1", {})
        self.assertEqual((yield self.sm.exists("session-1")), True)

    @inlineCallbacks
    def test_session_ttl(self):
        yield self.sm.save_session(u"session-1", {}, 10)
        self.assertTrue((yield self.sm.session_ttl(u"session-1")) <= 10)
        self.assertEqual((yield self.sm.session_ttl(u"session-unknown")), None)

    @inlineCallbacks
    def test_get_session(self):
        session = {
            "foo": {"thing": 1},
            "bar": "baz",
        }
        yield self.mk_session("session-1", session)
        self.assertEqual(
            (yield self.sm.get_session("session-1")),
            session)

    @inlineCallbacks
    def test_get_session_missing(self):
        self.assertEqual(
            (yield self.sm.get_session("session-1")),
            None)

    @inlineCallbacks
    def test_create_session_success(self):
        session = {"test": 1}
        created = (yield self.sm.create_session(
            u"session-1", session, 10))
        self.assertEqual(created, True)
        self.assertEqual(
            (yield self.redis.get("sessions.session-1")),
            json.dumps(session))

    @inlineCallbacks
    def test_create_session_fails(self):
        session = {"test": 1}
        yield self.mk_session(u"session-1", session)
        created = (yield self.sm.create_session(
            u"session-1", {"test": 2}, 10))
        self.assertEqual(created, False)
        # test session hasn't changed
        self.assertEqual(
            (yield self.redis.get("sessions.session-1")),
            json.dumps(session))

    @inlineCallbacks
    def test_save_session_new(self):
        session = {"test": 1}
        yield self.sm.save_session(
            u"session-1", session, 10)
        self.assertEqual(
            (yield self.redis.get("sessions.session-1")),
            json.dumps(session))

    @inlineCallbacks
    def test_save_session_existing(self):
        session = {"test": 1}
        yield self.mk_session(u"session-1", session)
        session = {"test": 2}
        yield self.sm.save_session(
            u"session-1", session, 10)
        self.assertEqual(
            (yield self.redis.get("sessions.session-1")),
            json.dumps(session))

    @inlineCallbacks
    def test_delete_session(self):
        yield self.mk_session(u"session-1", {})
        deleted = yield self.sm.delete_session(u"session-1")
        self.assertEqual(deleted, True)

    @inlineCallbacks
    def test_delete_session_missing(self):
        deleted = yield self.sm.delete_session(u"session-1")
        self.assertEqual(deleted, False)