File size: 3,315 Bytes
317dbf5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cbd85dd
 
 
 
317dbf5
 
 
 
 
 
 
 
 
 
 
 
 
 
18ca959
 
 
 
 
317dbf5
 
 
 
 
 
 
18ca959
 
 
 
 
 
 
 
 
 
 
 
 
 
 
317dbf5
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import unittest
import requests
import time
import os

API_URL = os.environ.get("API_URL", "https://igor53627-shitposter-api.hf.space")

class TestShitposterAPI(unittest.TestCase):
    def test_e2e_encryption_flow(self):
        print(f"\nTesting against: {API_URL}")
        
        # 0. Wait for service (simple retry loop)
        retries = 5
        while retries > 0:
            try:
                requests.get(f"{API_URL}/docs", timeout=5)
                break
            except:
                print("Waiting for API to wake up...")
                time.sleep(5)
                retries -= 1
        
        # 1. Generate Alice
        print("[1] Generating Alice...")
        resp = requests.get(f"{API_URL}/keygen")
        if resp.status_code == 503:
            self.skipTest("API Service is unavailable (still building?)")
            
        self.assertEqual(resp.status_code, 200)
        alice = resp.json()
        self.assertTrue("private_key_b64" in alice)
        self.assertTrue("public_signal" in alice)

        # 2. Generate Bob
        print("[2] Generating Bob...")
        resp = requests.get(f"{API_URL}/keygen")
        self.assertEqual(resp.status_code, 200)
        bob = resp.json()

        # 3. Alice Encrypts for Bob
        message = "The eagle flies at midnight."
        print(f"[3] Encrypting: '{message}'")
        
        payload = {
            "message": message,
            "sender_private_key_b64": alice["private_key_b64"],
            "recipient_public_signal": bob["public_signal"],
            "stealth": True
        }
        resp = requests.post(f"{API_URL}/encrypt", json=payload)
        
        if resp.status_code != 200:
            print(f"!!! Encrypt Error: {resp.text}")
            
        self.assertEqual(resp.status_code, 200)
        data = resp.json()
        self.assertTrue("shitpost" in data)
        ciphertext = data["shitpost"]
        print(f"    Shitpost: {ciphertext[:50]}...")

        # 4. Bob Decrypts
        print("[4] Decrypting...")
        payload = {
            "ciphertext_shitpost": ciphertext,
            "recipient_private_key_b64": bob["private_key_b64"],
            "sender_public_signal": alice["public_signal"]
        }
        resp = requests.post(f"{API_URL}/decrypt", json=payload)
        
        if resp.status_code != 200:
            print(f"!!! Decrypt Error: {resp.text}")
            print(f"!!! Ciphertext was: {ciphertext}")
            
        self.assertEqual(resp.status_code, 200)
        data = resp.json()
        self.assertEqual(data["message"], message)
        
        print(f"    Decrypted: {data['message']}")
        print("[SUCCESS] API E2E Test Passed.")

    def test_channel_generation(self):
        print("\n[5] Testing Channel Key Generation...")
        resp = requests.post(f"{API_URL}/channel/generate")
        self.assertEqual(resp.status_code, 200)
        data = resp.json()
        
        self.assertTrue("key_b64" in data)
        self.assertTrue("key_words" in data)
        
        # Verify word count
        words = data["key_words"].split()
        self.assertEqual(len(words), 32)
        print(f"    Generated Channel Key: {data['key_words'][:30]}...")
        print("[SUCCESS] Channel Gen Test Passed.")

if __name__ == '__main__':
    unittest.main()