def test_usersession_revoke(self): """Test to revoke on UserSession instance""" crusoe = self.fixtures.crusoe yet_another_usersession = models.UserSession( user=crusoe, ipaddr='192.168.1.1', user_agent= 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36', accessed_at=utcnow(), ) yet_another_usersession.revoke() result = models.UserSession.get(yet_another_usersession.buid) assert result.revoked_at is not None
def test_usersession_active_sessions(self): "Test for verifying UserSession's active_sessions" piglet = self.fixtures.piglet piglet_session = models.UserSession( user=piglet, ipaddr='192.168.1.3', buid=buid(), user_agent= 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36', accessed_at=utcnow(), ) assert isinstance(piglet.active_user_sessions.all(), list) assert piglet.active_user_sessions.all() == [piglet_session]
def test_usersession_has_sudo(self): """Test to set sudo and test if UserSession instance has_sudo """ crusoe = self.fixtures.crusoe another_user_session = models.UserSession( user=crusoe, ipaddr='192.168.1.1', user_agent= 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36', accessed_at=utcnow(), ) another_user_session.set_sudo() db.session.add(another_user_session) db.session.commit() assert another_user_session.has_sudo is True
def test_usersession_get(self): """Test for verifying UserSession's get method""" oakley = self.fixtures.oakley oakley_buid = buid() oakley_session = models.UserSession( user=oakley, ipaddr='192.168.1.2', buid=oakley_buid, user_agent= 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36', accessed_at=utcnow(), ) result = oakley_session.get(buid=oakley_buid) assert isinstance(result, models.UserSession) assert result.user_id == oakley.id
def test_client_authtoken_for(self): """ Test for retrieving authtoken for this user and client (only confidential clients) """ # scenario 1: for a client that has confidential=True auth_client = self.fixtures.auth_client crusoe = self.fixtures.crusoe result = auth_client.authtoken_for(crusoe) client_token = models.AuthToken(auth_client=auth_client, user=crusoe, scope='id', validity=0) result = auth_client.authtoken_for(user=crusoe) self.assertEqual(client_token, result) self.assertIsInstance(result, models.AuthToken) assert result.user == crusoe # scenario 2: for a client that has confidential=False varys = models.User(username='******', fullname='Lord Varys') house_lannisters = models.AuthClient( title='House of Lannisters', confidential=False, user=varys, website='houseoflannisters.westeros', ) varys_session = models.UserSession( user=varys, ipaddr='192.168.1.99', user_agent= 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36', accessed_at=utcnow(), ) lannisters_auth_token = models.AuthToken( auth_client=house_lannisters, user=varys, scope='throne', validity=0, user_session=varys_session, ) db.session.add_all( [varys, house_lannisters, lannisters_auth_token, varys_session]) db.session.commit() result = house_lannisters.authtoken_for(varys, user_session=varys_session) self.assertIsInstance(result, models.AuthToken) assert "Lord Varys" == result.user.fullname
def test_authtoken_user(self): """ Test for checking AuthToken's user property """ crusoe = self.fixtures.crusoe auth_client = self.fixtures.auth_client user_session = models.UserSession(buid=buid(), user=crusoe) auth_token_with_user_session = models.AuthToken( user=crusoe, user_session=user_session ) self.assertIsInstance( auth_token_with_user_session.user_session.user, models.User ) self.assertEqual(auth_token_with_user_session.user_session.user, crusoe) auth_token_without_user_session = models.AuthToken( auth_client=auth_client, user=crusoe ) self.assertIsInstance(auth_token_without_user_session._user, models.User) self.assertEqual(auth_token_without_user_session._user, crusoe)
def test_usersession_authenticate(self): """Test to verify authenticate method on UserSession""" chandler = models.User(username='******', fullname='Chandler Bing') chandler_session = models.UserSession( user=chandler, ipaddr='192.168.1.4', user_agent= 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36', accessed_at=utcnow(), ) db.session.add(chandler) db.session.add(chandler_session) db.session.commit() result = models.UserSession.authenticate(chandler_session.buid) assert isinstance(result, models.UserSession) assert result == chandler_session # Now manipulate the session to make it invalid # 1. More than a year since last access, so it's expired chandler_session.accessed_at = utcnow() - timedelta(days=1000) db.session.commit() # By default, expired sessions raise an exception with pytest.raises(models.UserSessionExpired): models.UserSession.authenticate(chandler_session.buid) # However, silent mode simply returns None assert (models.UserSession.authenticate(chandler_session.buid, silent=True) is None) # 2. Revoked session (taking priority over expiry) chandler_session.accessed_at = utcnow() chandler_session.revoked_at = utcnow() db.session.commit() with pytest.raises(models.UserSessionRevoked): models.UserSession.authenticate(chandler_session.buid) # Again, silent mode simply returns None assert (models.UserSession.authenticate(chandler_session.buid, silent=True) is None)
def test_usersession_init(self): """Test to verify the creation of UserSession instance""" result = models.UserSession() assert isinstance(result, models.UserSession)