File size: 1,764 Bytes
5e56bcf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import time
from upif.core.licensing import LicenseManager
from mock_gumroad import run_mock_server

# 1. Start Mock Server
print("Starting Mock Gumroad Server...")
server = run_mock_server(port=8000)
time.sleep(1)

# 2. Patch URL to point to localhost
LicenseManager.PRODUCT_PERMALINK = "test-product"
original_url = "https://api.gumroad.com/v2/licenses/verify"

# We must mock the request call inside LicenseManager instance or just valid URL
# Since we can't easily patch the class method URL variable locally without modifying code,
# we will rely on requests mocking or just override the instance method if possible.
# Actually, the implementation uses a hardcoded URL. Let's make it configurable for testing or patch 'requests.post'.

import requests
original_post = requests.post

def mock_post(url, data, timeout):
    if "api.gumroad.com" in url:
        # Redirect to our mock
        return original_post("http://localhost:8000/verify", data=data, timeout=timeout)
    return original_post(url, data, timeout)

requests.post = mock_post

# 3. Test Activation
lm = LicenseManager()
print("\n--- Test 1: Invalid Key ---")
success = lm.activate("INVALID-KEY")
print(f"Activation Result (Expected False): {success}")
assert not success
assert lm.get_tier() == "BASELINE"

print("\n--- Test 2: Valid Key ---")
success = lm.activate("TEST-PRO-KEY")
print(f"Activation Result (Expected True): {success}")
assert success
assert lm.get_tier() == "PRO"

# 4. Test Offline Persistence
print("\n--- Test 3: Offline Persistence ---")
lm2 = LicenseManager() # New instance
is_valid = lm2.validate_offline()
print(f"Offline Validation (Expected True): {is_valid}")
assert is_valid
assert lm2.get_tier() == "PRO"

print("\n--- SUCCESS: Licensing System Verified! ---")