1+ import pytest
2+ import requests
3+ from utils .test_runner import start_server , stop_server
4+ import jsonschema
5+ from utils .logger import log_response
6+ from utils .assertion import assert_equal
7+
8+
9+ class TestApiGetListThread :
10+
11+ @pytest .fixture (autouse = True )
12+ def setup_and_teardown (self ):
13+ # Setup
14+ success = start_server ()
15+ if not success :
16+ raise Exception ("Failed to start server" )
17+
18+ yield
19+
20+ # Teardown
21+ stop_server ()
22+
23+ def test_api_get_list_thread_successfully (self ):
24+ title = "New Thread"
25+
26+ data = {
27+ "metadata" : {
28+ "title" : title
29+ }
30+ }
31+
32+ thread_url = f"http://localhost:3928/v1/threads"
33+ response = requests .post (
34+ thread_url , json = data
35+ )
36+ json_data = response .json ()
37+ log_response (json_data , "test_api_get_list_thread_successfully" )
38+ assert_equal (response .status_code ,200 )
39+
40+ list_thread_response = requests .get (thread_url )
41+ json_data_list_thread = list_thread_response .json ()
42+ log_response (json_data_list_thread , "test_api_get_list_thread_successfully" )
43+ assert_equal (list_thread_response .status_code ,200 )
44+
45+ schema = {
46+ "type" : "object" ,
47+ "properties" : {
48+ "object" : {
49+ "type" : "string" ,
50+ "description" : "Type of the list response, always 'list'"
51+ },
52+ "data" : {
53+ "type" : "array" ,
54+ "description" : "Array of thread objects" ,
55+ "items" : {
56+ "type" : "object" ,
57+ "properties" : {
58+ "created_at" : {
59+ "type" : "integer" ,
60+ "description" : "Unix timestamp of when the thread was created"
61+ },
62+ "id" : {
63+ "type" : "string" ,
64+ "description" : "Unique identifier for the thread"
65+ },
66+ "metadata" : {
67+ "type" : "object" ,
68+ "properties" : {
69+ "title" : {
70+ "type" : "string" ,
71+ "description" : "Title of the thread"
72+ },
73+ "lastMessage" : {
74+ "type" : "string" ,
75+ "description" : "Content of the last message in the thread"
76+ }
77+ },
78+ "description" : "Metadata associated with the thread"
79+ },
80+ "object" : {
81+ "type" : "string" ,
82+ "description" : "Type of object, always 'thread'"
83+ }
84+ },
85+ "required" : [
86+ "created_at" ,
87+ "id" ,
88+ "object"
89+ ]
90+ }
91+ }
92+ },
93+ "required" : [
94+ "object" ,
95+ "data"
96+ ]
97+ }
98+
99+ # Validate response schema
100+ jsonschema .validate (instance = json_data_list_thread , schema = schema )
101+ assert_equal (json_data_list_thread ["data" ][0 ]["metadata" ]['title' ], title )
0 commit comments