๐Ÿ” Code Extractor

function test_upload_endpoint

Maturity: 36

A test function that validates the reMarkable Cloud API file upload endpoint by attempting to upload a test JSON file and verifying the GET endpoint functionality.

File:
/tf/active/vicechatdev/e-ink-llm/cloudtest/test_upload_endpoint.py
Lines:
21 - 96
Complexity:
moderate

Purpose

This function serves as a diagnostic tool to test the reMarkable Cloud sync API endpoints. It authenticates with the reMarkable service, attempts to upload a small test file to the /sync/v3/files endpoint with proper headers and CRC32C checksum, and then validates that the GET endpoint is working correctly. It provides detailed console output for debugging API interactions, including response status codes, headers, and error messages.

Source Code

def test_upload_endpoint():
    print("๐Ÿงช Testing File Upload Endpoint")
    print("=" * 40)
    
    # Authenticate
    auth = RemarkableAuth()
    user_token = auth.authenticate()
    session = auth.get_authenticated_session()
    
    if not session or not user_token:
        print("โŒ Authentication failed")
        return
    
    # Test with a simple small file
    test_content = '{"test": "data"}'
    test_crc = calculate_crc32c(test_content)
    
    print(f"๐Ÿ“„ Test content: {test_content}")
    print(f"๐Ÿ”ข CRC32C: {test_crc}")
    print(f"๐Ÿ”‘ Token length: {len(user_token)}")
    
    headers = {
        'Authorization': f'Bearer {user_token}',
        'User-Agent': 'reMarkable-desktop-win/3.11.1.1951',
        'rm-filename': 'test.json',
        'rm-crc32c': test_crc,
        'Content-Type': 'application/octet-stream'
    }
    
    print(f"๐Ÿ“ค Headers:")
    for key, value in headers.items():
        if key == 'Authorization':
            print(f"   {key}: Bearer {value[7:20]}...")
        else:
            print(f"   {key}: {value}")
    
    try:
        print(f"\n๐ŸŒ Testing PUT to: https://eu.tectonic.remarkable.com/sync/v3/files")
        response = session.put(
            'https://eu.tectonic.remarkable.com/sync/v3/files',
            headers=headers,
            data=test_content
        )
        
        print(f"๐Ÿ“ˆ Response status: {response.status_code}")
        print(f"๐Ÿ“ Response headers:")
        for key, value in response.headers.items():
            print(f"   {key}: {value}")
        print(f"๐Ÿ“„ Response body: {response.text}")
        
        if response.status_code == 201:
            print("โœ… Upload successful!")
        elif response.status_code == 404:
            print("โŒ 404 Not Found - endpoint might be wrong")
        elif response.status_code == 401:
            print("โŒ 401 Unauthorized - token issue")
        elif response.status_code == 403:
            print("โŒ 403 Forbidden - permission issue")
        else:
            print(f"โŒ Unexpected status: {response.status_code}")
            
    except Exception as e:
        print(f"โŒ Request failed: {e}")
    
    # Also test if we can read existing files
    print(f"\n๐Ÿ” Testing GET endpoint...")
    try:
        get_response = session.get('https://eu.tectonic.remarkable.com/sync/v3/files/root.docSchema')
        print(f"๐Ÿ“ˆ GET Response status: {get_response.status_code}")
        if get_response.status_code == 200:
            print(f"๐Ÿ“„ GET Response size: {len(get_response.text)} bytes")
            print("โœ… GET endpoint works fine")
        else:
            print(f"โŒ GET also failing: {get_response.status_code}")
    except Exception as e:
        print(f"โŒ GET failed: {e}")

Return Value

This function does not return any value (implicitly returns None). It outputs diagnostic information to the console via print statements, including authentication status, request/response details, and success/failure indicators with emoji markers.

Dependencies

  • auth
  • json
  • base64
  • binascii
  • requests

Required Imports

from auth import RemarkableAuth
import json
import base64
import binascii

Usage Example

# Ensure auth.py is available with RemarkableAuth class
# Ensure calculate_crc32c function is defined

def calculate_crc32c(content):
    # Implementation of CRC32C calculation
    import binascii
    return format(binascii.crc32(content.encode()) & 0xffffffff, '08x')

# Run the test
test_upload_endpoint()

# Expected output:
# ๐Ÿงช Testing File Upload Endpoint
# ========================================
# ๐Ÿ“„ Test content: {"test": "data"}
# ๐Ÿ”ข CRC32C: <checksum>
# ... (detailed diagnostic output)

Best Practices

  • This is a test/diagnostic function and should not be used in production code
  • Ensure the calculate_crc32c function is properly implemented before calling this function
  • The function prints sensitive information (partial tokens) - use only in secure development environments
  • The hardcoded EU region endpoint may not work for all users - consider making it configurable
  • No error handling for missing calculate_crc32c function - ensure it exists before execution
  • The function does not clean up test files created on the reMarkable Cloud
  • Consider adding timeout parameters to HTTP requests for production use
  • The test file name 'test.json' may conflict with existing files - use unique names in production

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function main_v15 80.8% similar

    A test function that uploads a PDF document to reMarkable cloud, syncs the local replica, and validates the upload with detailed logging and metrics.

    From: /tf/active/vicechatdev/e-ink-llm/cloudtest/test_raw_upload.py
  • function main_v6 79.4% similar

    Integration test function that validates the fixed upload implementation for reMarkable cloud sync by creating a test PDF document, uploading it with corrected metadata patterns, and verifying its successful appearance in the reMarkable ecosystem.

    From: /tf/active/vicechatdev/e-ink-llm/cloudtest/test_fixed_upload.py
  • function test_remarkable_auth 77.7% similar

    Asynchronous function that tests authentication and API connectivity with the reMarkable Cloud service, verifying credentials and basic API access.

    From: /tf/active/vicechatdev/e-ink-llm/test_mixed_mode.py
  • function test_quick_upload 75.9% similar

    A test function that performs a quick PDF upload to a reMarkable device without performing a full synchronization, used for testing the upload functionality in isolation.

    From: /tf/active/vicechatdev/e-ink-llm/cloudtest/quick_test.py
  • function main_v100 74.6% similar

    Tests uploading a PDF document to a specific folder ('Myfolder') on a reMarkable device and verifies the upload by syncing and checking folder contents.

    From: /tf/active/vicechatdev/e-ink-llm/cloudtest/test_folder_upload.py
โ† Back to Browse