-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrud-test.py
51 lines (41 loc) · 1.85 KB
/
crud-test.py
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
import requests
# Base URL API
BASE_URL = "http://127.0.0.1:3000/diaries"
# Header dengan token JWT
HEADERS = {
"Authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJMQjRBRklYMnZ3bEsxZVBEeFBpQSIsImVtYWlsIjoidGVzdEBleGFtcGxlLmNvbSIsImV4cCI6MTczMzg3MDU0OCwiaWF0IjoxNzMzODY2OTQ4fQ.6ci4mf3Od-Xwgl3fnWKFTEnYfCrYHrQ93woz3Gqb5Ns",
"Content-Type": "application/json"
}
def test_create_diary():
data = {
"userId": "LB4AFIX2vwlK1ePDxPiA", # Ganti dengan userId yang sesuai
"date": "2024-12-10",
"content": "Hari ini saya sangat bahagia karena lulus ujian!"
}
response = requests.post(BASE_URL, json=data, headers=HEADERS)
print("Create Diary Response:", response.status_code, response.json())
return response.json().get("diaryId")
def test_get_diaries(user_id):
params = {"userId": user_id}
response = requests.get(BASE_URL, params=params, headers=HEADERS)
print("Get Diaries Response:", response.status_code, response.json())
def test_update_diary(diary_id):
data = {
"date": "2024-12-11",
"content": "Hari ini saya merasa sangat bersemangat untuk belajar lebih banyak."
}
response = requests.put(f"{BASE_URL}/{diary_id}", json=data, headers=HEADERS)
print("Update Diary Response:", response.status_code, response.json())
def test_delete_diary(diary_id):
response = requests.delete(f"{BASE_URL}/{diary_id}", headers=HEADERS)
print("Delete Diary Response:", response.status_code, response.json())
if __name__ == "__main__":
# Step 1: Create a new diary
diary_id = test_create_diary()
if diary_id:
# Step 2: Fetch all diaries for the user
test_get_diaries("LB4AFIX2vwlK1ePDxPiA")
# Step 3: Update the created diary
test_update_diary(diary_id)
# Step 4: Delete the created diary
test_delete_diary(diary_id)