'Test session ownership methods'

def testSessionOwnership(self):

    db = current.db
    auth = current.auth
    s3db = current.s3db
    ptable = s3db.pr_person
    otable = s3db.org_organisation
    assertTrue = self.assertTrue
    assertFalse = self.assertFalse
    auth.s3_impersonate(None)
    auth.s3_clear_session_ownership()
    auth.s3_make_session_owner(ptable, 1)
    assertFalse(auth.s3_session_owns(ptable, None))
    assertFalse(auth.s3_session_owns(ptable, 2))
    assertTrue(auth.s3_session_owns(ptable, 1))
    auth.s3_impersonate('normaluser@example.com')
    assertFalse(auth.s3_session_owns(ptable, 1))
    auth.s3_impersonate(None)
    auth.s3_make_session_owner(ptable, 1)
    auth.s3_make_session_owner(ptable, 2)
    assertTrue(auth.s3_session_owns(ptable, 1))
    assertTrue(auth.s3_session_owns(ptable, 2))
    auth.s3_clear_session_ownership(ptable, 1)
    assertFalse(auth.s3_session_owns(ptable, 1))
    assertTrue(auth.s3_session_owns(ptable, 2))
    auth.s3_make_session_owner(ptable, 1)
    auth.s3_make_session_owner(ptable, 2)
    auth.s3_make_session_owner(otable, 1)
    auth.s3_make_session_owner(otable, 2)
    assertTrue(auth.s3_session_owns(ptable, 1))
    assertTrue(auth.s3_session_owns(ptable, 2))
    assertTrue(auth.s3_session_owns(otable, 1))
    assertTrue(auth.s3_session_owns(otable, 2))
    auth.s3_clear_session_ownership(ptable)
    assertFalse(auth.s3_session_owns(ptable, 1))
    assertFalse(auth.s3_session_owns(ptable, 2))
    assertTrue(auth.s3_session_owns(otable, 1))
    assertTrue(auth.s3_session_owns(otable, 2))
    auth.s3_make_session_owner(ptable, 1)
    auth.s3_make_session_owner(ptable, 2)
    auth.s3_make_session_owner(otable, 1)
    auth.s3_make_session_owner(otable, 2)
    assertTrue(auth.s3_session_owns(ptable, 1))
    assertTrue(auth.s3_session_owns(ptable, 2))
    assertTrue(auth.s3_session_owns(otable, 1))
    assertTrue(auth.s3_session_owns(otable, 2))
    auth.s3_clear_session_ownership()
    assertFalse(auth.s3_session_owns(ptable, 1))
    assertFalse(auth.s3_session_owns(ptable, 2))
    assertFalse(auth.s3_session_owns(otable, 1))
    assertFalse(auth.s3_session_owns(otable, 2))
