summaryrefslogtreecommitdiff
path: root/tests/test_sqlite_storage.py
diff options
context:
space:
mode:
authorJohn Turner <jturner.usa@gmail.com>2025-09-23 21:11:01 -0400
committerJohn Turner <jturner.usa@gmail.com>2025-09-23 21:48:01 -0400
commit83040f948f6e8264fb427b5d9cf338f46a906fe1 (patch)
treed0efab55a60a10c638df2e5b4194a060c9a85c38 /tests/test_sqlite_storage.py
parent2aa14b931f1a4fa7f465537b5310217b84615203 (diff)
downloadpypaste-83040f948f6e8264fb427b5d9cf338f46a906fe1.tar.gz
change keys to 78 bit random tokens and humanize the result
Instead of picking random words via a random choice function, we generate a 13 * key_length bit random int as the key. We humanize the key by splitting it up into 13 bit segments and using the 13 bits as an index into the word list. This allows us to store the keys in binary form which is faster and uses less space.
Diffstat (limited to 'tests/test_sqlite_storage.py')
-rwxr-xr-xtests/test_sqlite_storage.py26
1 files changed, 13 insertions, 13 deletions
diff --git a/tests/test_sqlite_storage.py b/tests/test_sqlite_storage.py
index 384158c..6a93561 100755
--- a/tests/test_sqlite_storage.py
+++ b/tests/test_sqlite_storage.py
@@ -6,7 +6,7 @@ import tempfile
import aiosqlite
import string
import random
-from pypaste.server import Paste
+from pypaste.server import Paste, Key, keygen
from pypaste.server.sqlite import Sqlite
from datetime import datetime
from pathlib import Path
@@ -24,11 +24,11 @@ def generate_key() -> str:
async def test_exists_but_not_in_our_table(storage: Sqlite) -> None:
- key = generate_key()
+ key = keygen(6)
await storage.connection.execute(
- "insert into pastes values(?, ?, ?, ?)",
- (key, datetime.now().isoformat(), None, bytes()),
+ "insert into pastes values(?, ?, ?, ?, ?)",
+ (key.data, key.length, datetime.now().isoformat(), None, bytes()),
)
assert not await storage.exists(key)
@@ -36,20 +36,20 @@ async def test_exists_but_not_in_our_table(storage: Sqlite) -> None:
async def test_exists(storage: Sqlite) -> None:
dt = datetime.now()
- key = generate_key()
+ key = keygen(6)
- await storage.insert(Paste(key, dt, "test", "hello world"))
+ await storage.insert(Paste(dt, "test", "hello world"), key)
assert await storage.exists(key)
- assert not await storage.exists(generate_key())
+ assert not await storage.exists(keygen(6))
async def test_delete(storage: Sqlite) -> None:
dt = datetime.now()
- key = generate_key()
+ key = keygen(6)
- await storage.insert(Paste(key, dt, "test", "hello world"))
+ await storage.insert(Paste(dt, "test", "hello world"), key)
assert await storage.exists(key)
@@ -60,14 +60,13 @@ async def test_delete(storage: Sqlite) -> None:
async def test_insert_retrieve(storage: Sqlite) -> None:
dt = datetime.now()
- key = generate_key()
+ key = keygen(6)
- await storage.insert(Paste(key, dt, "test", "hello world"))
+ await storage.insert(Paste(dt, "test", "hello world"), key)
paste = await storage.retrieve(key)
assert paste is not None
- assert paste.key == key
assert paste.dt == dt
assert paste.syntax == "test"
assert paste.text == "hello world"
@@ -81,7 +80,8 @@ async def main() -> int:
await connection.execute(
(
"create table pastes("
- "key text,"
+ "key blob,"
+ "key_length int,"
"datetime text,"
"size int,"
"syntax text"