def fake_eq_session(): eq_session = EQSession( eq_session_id="sessionid", user_id="someuser", session_data="somedata", expires_at=NOW + timedelta(minutes=1), ) return eq_session
def test_eq_session(self): new_model = self._test_model( EQSession( eq_session_id="sessionid", user_id="someuser", session_data="somedata", expires_at=NOW, )) self.assertGreaterEqual(new_model.created_at, NOW) self.assertGreaterEqual(new_model.updated_at, NOW) self.assertGreaterEqual(new_model.expires_at, NOW)
def test_eq_session(): model = EQSession( eq_session_id="sessionid", user_id="someuser", session_data="somedata", expires_at=NOW, ) new_model = create_model(model) assert new_model.__dict__ == model.__dict__ assert new_model.created_at >= NOW assert new_model.updated_at >= NOW assert new_model.expires_at >= NOW
def test_get_redis_expiry_when_expiry_set(self): # Given eq_session = EQSession( eq_session_id="sessionid", user_id="someuser", session_data="somedata", expires_at=EXPIRES_AT, ) # When self.redis.put(eq_session) # Then expires_in = self.mock_client.ttl(eq_session.eq_session_id) assert expires_in > 0
def create(self, eq_session_id, user_id, session_data, expires_at): """ Create a new eq_session and associate it with the user_id specified """ self.eq_session_id = eq_session_id self.user_id = user_id self.session_data = session_data self._eq_session = EQSession( eq_session_id=self.eq_session_id, user_id=self.user_id, expires_at=expires_at, ) return self
def test_delete_session(self): # Given eq_session = EQSession( eq_session_id="sessionid", user_id="someuser", session_data="somedata", expires_at=EXPIRES_AT, ) self.redis.put(eq_session) session = self.redis.get(EQSession, "sessionid") self.assertEqual(session.eq_session_id, eq_session.eq_session_id) # When self.redis.delete(eq_session) # Then self.assertIsNone(self.redis.get(EQSession, "sessionid"))
def test_put_session(self): # given eq_session = EQSession( eq_session_id="sessionid", user_id="someuser", session_data="somedata", expires_at=EXPIRES_AT, ) stored_data = self.redis.get(EQSession, eq_session.eq_session_id) self.assertIsNone(stored_data) # when self.redis.put(eq_session) # Then stored_data = self.redis.get(EQSession, eq_session.eq_session_id) self.assertIsNotNone(stored_data)
def test_redis_does_not_store_key_field_in_value(self): # Given eq_session = EQSession( eq_session_id="sessionid", user_id="someuser", session_data="somedata", expires_at=EXPIRES_AT, ) stored_data = self.redis.get(EQSession, eq_session.eq_session_id) self.assertIsNone(stored_data) self.redis.put(eq_session) # When stored_data = self.mock_client.get(eq_session.eq_session_id) storage_model = StorageModel(model_type=EQSession) assert storage_model.key_field not in json_loads(stored_data)
def test_get_redis_expiry_when_expiry_not_set(self): # Given eq_session = EQSession( eq_session_id="sessionid", user_id="someuser", session_data="somedata", expires_at=EXPIRES_AT, ) # When with mock.patch( "app.storage.storage.StorageModel.expiry_field", new_callable=mock.PropertyMock, ) as mock_expiry_field: mock_expiry_field.return_value = None self.redis.put(eq_session) # Then expires_in = self.mock_client.ttl(eq_session.eq_session_id) assert expires_in == -1
def _save_session(app_session, session_id, user_id, data, legacy=False): raw_data = json_dumps(vars(data)) protected_header = {"alg": "dir", "enc": "A256GCM", "kid": "1,1"} if legacy: plaintext = base64url_encode(raw_data) else: plaintext = raw_data jwe_token = jwe.JWE(plaintext=plaintext, protected=protected_header, recipient=app_session.key) session_model = EQSession( eq_session_id=session_id, user_id=user_id, session_data=jwe_token.serialize(compact=True), expires_at=app_session.expires_at, ) current_app.eq["storage"].put(session_model)
def create( self, eq_session_id: str, user_id: str, session_data: SessionData, expires_at: datetime, ) -> SessionStore: """ Create a new eq_session and associate it with the user_id specified """ self.eq_session_id = eq_session_id self.user_id = user_id self.session_data = session_data self._eq_session = EQSession( eq_session_id=self.eq_session_id, user_id=self.user_id, expires_at=expires_at, session_data=None, ) return self
def test_get_session(self): # Given eq_session = EQSession( eq_session_id="sessionid", user_id="someuser", session_data="somedata", expires_at=EXPIRES_AT, ) stored_data = self.redis.get(EQSession, eq_session.eq_session_id) self.assertIsNone(stored_data) self.redis.put(eq_session) # When stored_data = self.redis.get(EQSession, eq_session.eq_session_id) # Then for k, v in eq_session.__dict__.items(): parsed_value = getattr(stored_data, k) if isinstance(v, datetime): self.assertGreaterEqual(v, parsed_value) else: self.assertEqual(v, parsed_value)
def test_put_with_index(self, mock_entity): model = EQSession("session-id", "user-id", datetime.now(tz=timezone.utc), "session-data") self.ds.put(model) self.assertNotIn("expires_at", mock_entity.call_args.kwargs["exclude_from_indexes"])