File size: 4,063 Bytes
25f22bf
 
 
 
 
 
 
 
 
 
 
0e3cb04
25f22bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 pytest
import json
from app import create_app
from unittest.mock import patch, MagicMock

def test_generate_post_with_unicode():
    """Test that generate post endpoint handles Unicode characters correctly."""
    app = create_app()
    client = app.test_client()
    
    # Mock the content service to return content with emojis
    with patch('backend.services.content_service.ContentService.generate_post_content') as mock_generate:
        # Test content with emojis
        test_content = "πŸš€ Check out this amazing new feature! πŸŽ‰ #innovation"
        mock_generate.return_value = test_content
        
        # Mock JWT identity
        with app.test_request_context():
            from flask_jwt_extended import create_access_token
            with patch('flask_jwt_extended.get_jwt_identity') as mock_identity:
                mock_identity.return_value = "test-user-id"
                
                # Get access token
                access_token = create_access_token(identity="test-user-id")
                
                # Make request to generate post
                response = client.post(
                    '/api/posts/generate',
                    headers={'Authorization': f'Bearer {access_token}'},
                    content_type='application/json'
                )
                
                # Verify response
                assert response.status_code == 200
                data = json.loads(response.data)
                
                # Verify the response contains the Unicode content
                assert 'success' in data
                assert data['success'] is True
                assert 'content' in data
                assert data['content'] == test_content
                
                # Verify no encoding errors occurred
                assert not response.data.decode('utf-8').encode('utf-8').decode('latin-1', errors='ignore') != response.data.decode('utf-8')

def test_get_posts_with_unicode_content():
    """Test that get posts endpoint handles Unicode content correctly."""
    app = create_app()
    client = app.test_client()
    
    # Mock Supabase response with Unicode content
    mock_post_data = [
        {
            'id': 'test-post-1',
            'Text_content': 'πŸš€ Amazing post with emoji! ✨',
            'is_published': False,
            'created_at': '2025-01-01T00:00:00Z',
            'Social_network': {
                'id_utilisateur': 'test-user-id'
            }
        }
    ]
    
    with app.test_request_context():
        from flask_jwt_extended import create_access_token
        with patch('flask_jwt_extended.get_jwt_identity') as mock_identity:
            mock_identity.return_value = "test-user-id"
            
            # Mock Supabase client
            with patch('app.supabase') as mock_supabase:
                mock_response = MagicMock()
                mock_response.data = mock_post_data
                mock_supabase.table.return_value.select.return_value.execute.return_value = mock_response
                
                access_token = create_access_token(identity="test-user-id")
                
                # Make request to get posts
                response = client.get(
                    '/api/posts',
                    headers={'Authorization': f'Bearer {access_token}'},
                    content_type='application/json'
                )
                
                # Verify response
                assert response.status_code == 200
                data = json.loads(response.data)
                
                # Verify the response contains Unicode content
                assert 'success' in data
                assert data['success'] is True
                assert 'posts' in data
                assert len(data['posts']) == 1
                assert data['posts'][0]['Text_content'] == 'πŸš€ Amazing post with emoji! ✨'

if __name__ == '__main__':
    test_generate_post_with_unicode()
    test_get_posts_with_unicode_content()
    print("All Unicode integration tests passed!")